|
| 1 | +//===-- Implementation of internal environment utilities ------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "environ_internal.h" |
| 10 | +#include "config/app.h" |
| 11 | +#include "src/__support/CPP/string_view.h" |
| 12 | +#include "src/__support/macros/config.h" |
| 13 | +#include "src/string/memcpy.h" |
| 14 | + |
| 15 | +// We use extern "C" declarations for malloc/free/realloc instead of including |
| 16 | +// src/stdlib/malloc.h, src/stdlib/free.h, and src/stdlib/realloc.h. This allows |
| 17 | +// the implementation to work with different allocator implementations, |
| 18 | +// particularly in integration tests which provide a simple bump allocator. The |
| 19 | +// extern "C" linkage ensures we use whatever allocator is linked with the test |
| 20 | +// or application. |
| 21 | +extern "C" void *malloc(size_t); |
| 22 | +extern "C" void free(void *); |
| 23 | +extern "C" void *realloc(void *, size_t); |
| 24 | + |
| 25 | +namespace LIBC_NAMESPACE_DECL { |
| 26 | +namespace internal { |
| 27 | + |
| 28 | +// Minimum initial capacity for the environment array when first allocated. |
| 29 | +// This avoids frequent reallocations for small environments. |
| 30 | +constexpr size_t MIN_ENVIRON_CAPACITY = 32; |
| 31 | + |
| 32 | +// Growth factor for environment array capacity when expanding. |
| 33 | +// When capacity is exceeded, new_capacity = old_capacity * |
| 34 | +// ENVIRON_GROWTH_FACTOR. |
| 35 | +constexpr size_t ENVIRON_GROWTH_FACTOR = 2; |
| 36 | + |
| 37 | +// Global state for environment management |
| 38 | +Mutex environ_mutex(false, false, false, false); |
| 39 | +char **environ_storage = nullptr; |
| 40 | +EnvStringOwnership *environ_ownership = nullptr; |
| 41 | +size_t environ_capacity = 0; |
| 42 | +size_t environ_size = 0; |
| 43 | +bool environ_is_ours = false; |
| 44 | + |
| 45 | +char **get_environ_array() { |
| 46 | + if (environ_is_ours) |
| 47 | + return environ_storage; |
| 48 | + return reinterpret_cast<char **>(LIBC_NAMESPACE::app.env_ptr); |
| 49 | +} |
| 50 | + |
| 51 | +void init_environ() { |
| 52 | + // Count entries in the startup environ |
| 53 | + char **env_ptr = reinterpret_cast<char **>(LIBC_NAMESPACE::app.env_ptr); |
| 54 | + if (!env_ptr) |
| 55 | + return; |
| 56 | + |
| 57 | + size_t count = 0; |
| 58 | + for (char **env = env_ptr; *env != nullptr; env++) |
| 59 | + count++; |
| 60 | + |
| 61 | + environ_size = count; |
| 62 | +} |
| 63 | + |
| 64 | +int find_env_var(cpp::string_view name) { |
| 65 | + char **env_array = get_environ_array(); |
| 66 | + if (!env_array) |
| 67 | + return -1; |
| 68 | + |
| 69 | + for (size_t i = 0; i < environ_size; i++) { |
| 70 | + cpp::string_view current(env_array[i]); |
| 71 | + if (!current.starts_with(name)) |
| 72 | + continue; |
| 73 | + |
| 74 | + // Check that name is followed by '=' |
| 75 | + if (current.size() > name.size() && current[name.size()] == '=') |
| 76 | + return static_cast<int>(i); |
| 77 | + } |
| 78 | + |
| 79 | + return -1; |
| 80 | +} |
| 81 | + |
| 82 | +bool ensure_capacity(size_t needed) { |
| 83 | + // IMPORTANT: This function assumes environ_mutex is already held by the |
| 84 | + // caller. Do not add locking here as it would cause deadlock. |
| 85 | + |
| 86 | + // If we're still using the startup environ, we need to copy it |
| 87 | + if (!environ_is_ours) { |
| 88 | + char **old_env = reinterpret_cast<char **>(LIBC_NAMESPACE::app.env_ptr); |
| 89 | + |
| 90 | + // Allocate new array with room to grow |
| 91 | + size_t new_capacity = needed < MIN_ENVIRON_CAPACITY |
| 92 | + ? MIN_ENVIRON_CAPACITY |
| 93 | + : needed * ENVIRON_GROWTH_FACTOR; |
| 94 | + char **new_storage = |
| 95 | + reinterpret_cast<char **>(malloc(sizeof(char *) * (new_capacity + 1))); |
| 96 | + if (!new_storage) |
| 97 | + return false; |
| 98 | + |
| 99 | + // Allocate ownership tracking array |
| 100 | + EnvStringOwnership *new_ownership = reinterpret_cast<EnvStringOwnership *>( |
| 101 | + malloc(sizeof(EnvStringOwnership) * (new_capacity + 1))); |
| 102 | + if (!new_ownership) { |
| 103 | + free(new_storage); |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + // Copy existing pointers (we don't own the strings yet, so just copy |
| 108 | + // pointers) |
| 109 | + if (old_env) { |
| 110 | + for (size_t i = 0; i < environ_size; i++) { |
| 111 | + new_storage[i] = old_env[i]; |
| 112 | + // Initialize ownership: startup strings are not owned by us |
| 113 | + new_ownership[i] = EnvStringOwnership(); |
| 114 | + } |
| 115 | + } |
| 116 | + new_storage[environ_size] = nullptr; |
| 117 | + |
| 118 | + environ_storage = new_storage; |
| 119 | + environ_ownership = new_ownership; |
| 120 | + environ_capacity = new_capacity; |
| 121 | + environ_is_ours = true; |
| 122 | + |
| 123 | + // Update app.env_ptr to point to our storage |
| 124 | + LIBC_NAMESPACE::app.env_ptr = |
| 125 | + reinterpret_cast<uintptr_t *>(environ_storage); |
| 126 | + |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + // We already own environ, check if we need to grow it |
| 131 | + if (needed <= environ_capacity) |
| 132 | + return true; |
| 133 | + |
| 134 | + // Grow capacity by the growth factor |
| 135 | + size_t new_capacity = needed * ENVIRON_GROWTH_FACTOR; |
| 136 | + |
| 137 | + // Use realloc to grow the arrays |
| 138 | + char **new_storage = reinterpret_cast<char **>( |
| 139 | + realloc(environ_storage, sizeof(char *) * (new_capacity + 1))); |
| 140 | + if (!new_storage) |
| 141 | + return false; |
| 142 | + |
| 143 | + EnvStringOwnership *new_ownership = |
| 144 | + reinterpret_cast<EnvStringOwnership *>(realloc( |
| 145 | + environ_ownership, sizeof(EnvStringOwnership) * (new_capacity + 1))); |
| 146 | + if (!new_ownership) { |
| 147 | + // If ownership realloc fails, we still have the old storage in new_storage |
| 148 | + // which was successfully reallocated. We need to restore or handle this. |
| 149 | + // For safety, we'll keep the successfully reallocated storage. |
| 150 | + environ_storage = new_storage; |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + environ_storage = new_storage; |
| 155 | + environ_ownership = new_ownership; |
| 156 | + environ_capacity = new_capacity; |
| 157 | + |
| 158 | + // Update app.env_ptr to point to our new storage |
| 159 | + LIBC_NAMESPACE::app.env_ptr = reinterpret_cast<uintptr_t *>(environ_storage); |
| 160 | + |
| 161 | + return true; |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace internal |
| 165 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments