Skip to content

Commit c2c1bfb

Browse files
authored
Add SWIFT_STDLIB_HAS_ENVIRON to remove usage of getenv/environ from stdlib (#39599)
1 parent 653642d commit c2c1bfb

File tree

13 files changed

+60
-4
lines changed

13 files changed

+60
-4
lines changed

stdlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ option(SWIFT_STDLIB_HAS_STDIN
111111
"Build stdlib assuming the platform supports stdin and getline API."
112112
TRUE)
113113

114+
option(SWIFT_STDLIB_HAS_ENVIRON
115+
"Build stdlib assuming the platform supports environment variables."
116+
TRUE)
117+
114118
option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
115119
"Build the standard libraries assuming that they will be used in an environment with only a single thread."
116120
FALSE)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ function(_add_target_variant_c_compile_flags)
343343
list(APPEND result "-DSWIFT_STDLIB_HAS_STDIN")
344344
endif()
345345

346+
if(SWIFT_STDLIB_HAS_ENVIRON)
347+
list(APPEND result "-DSWIFT_STDLIB_HAS_ENVIRON")
348+
endif()
349+
346350
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
347351
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
348352
endif()

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ function(_add_target_variant_swift_compile_flags
293293
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
294294
endif()
295295

296+
if(SWIFT_STDLIB_HAS_ENVIRON)
297+
list(APPEND result "-D" "SWIFT_STDLIB_HAS_ENVIRON")
298+
list(APPEND result "-Xcc" "-DSWIFT_STDLIB_HAS_ENVIRON")
299+
endif()
300+
296301
set("${result_var_name}" "${result}" PARENT_SCOPE)
297302
endfunction()
298303

stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
set(swift_private_libc_extras_flags)
2+
if(SWIFT_STDLIB_HAS_ENVIRON)
3+
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
4+
endif()
5+
16
add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
27
# This file should be listed the first. Module name is inferred from the
38
# filename.
@@ -8,7 +13,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
813
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
914

1015
SWIFT_MODULE_DEPENDS SwiftPrivate
11-
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
16+
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} ${swift_private_libc_extras_flags}
1217
SWIFT_MODULE_DEPENDS_OSX Darwin
1318
SWIFT_MODULE_DEPENDS_IOS Darwin
1419
SWIFT_MODULE_DEPENDS_TVOS Darwin

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ func posixPipe() -> (readFD: CInt, writeFD: CInt) {
213213
return (fds[0], fds[1])
214214
}
215215

216+
#if !SWIFT_STDLIB_HAS_ENVIRON
217+
@_silgen_name("_NSGetEnviron")
218+
func _NSGetEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>>
219+
220+
var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
221+
#if os(macOS)
222+
return _NSGetEnviron().pointee
223+
#elseif os(Windows)
224+
return __p_environ().pointee
225+
#elseif os(Linux)
226+
return __environ
227+
#else
228+
#error("unsupported platform")
229+
#endif
230+
}
231+
#endif
232+
216233
/// Start the same executable as a child process, redirecting its stdout and
217234
/// stderr.
218235
public func spawnChild(_ args: [String])

stdlib/public/Platform/Platform.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ public func sem_open(
427427
//===----------------------------------------------------------------------===//
428428

429429
// Some platforms don't have `extern char** environ` imported from C.
430+
#if SWIFT_STDLIB_HAS_ENVIRON
430431
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(OpenBSD) || os(PS4)
431432
public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
432433
return _swift_stdlib_getEnviron()
@@ -436,3 +437,4 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
436437
return __environ
437438
}
438439
#endif
440+
#endif // SWIFT_STDLIB_HAS_ENVIRON

stdlib/public/SwiftShims/LibcOverlayShims.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static inline int _swift_stdlib_fcntlPtr(int fd, int cmd, void* ptr) {
5252
#endif
5353

5454
// Environment
55+
#if SWIFT_STDLIB_HAS_ENVIRON
5556
#if defined(__FreeBSD__) || defined(__OpenBSD__)
5657
static inline char * _Nullable * _Null_unspecified _swift_stdlib_getEnviron() {
5758
extern char **environ;
@@ -63,6 +64,7 @@ static inline char * _Nullable *_swift_stdlib_getEnviron() {
6364
return *_NSGetEnviron();
6465
}
6566
#endif
67+
#endif // SWIFT_STDLIB_HAS_ENVIRON
6668

6769
// System error numbers <errno.h>
6870
static inline int _swift_stdlib_getErrno() {

stdlib/public/runtime/EnvironmentVariables.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static constexpr bool hasSwiftPrefix(const char *str) {
3535
static_assert(hasSwiftPrefix(#name), "Names must start with SWIFT");
3636
#include "EnvironmentVariables.def"
3737

38+
#if SWIFT_STDLIB_HAS_ENVIRON
39+
3840
// Value parsers. Add new functions named parse_<type> to accommodate more
3941
// debug variable types.
4042
static bool parse_bool(const char *name, const char *value, bool defaultValue) {
@@ -108,6 +110,8 @@ void printHelp(const char *extra) {
108110
swift::warning(RuntimeErrorFlagNone, "SWIFT_DEBUG_HELP=YES - Print this help.");
109111
}
110112

113+
#endif // SWIFT_STDLIB_HAS_ENVIRON
114+
111115
} // end anonymous namespace
112116

113117
// Define backing variables.
@@ -118,7 +122,7 @@ void printHelp(const char *extra) {
118122
// Initialization code.
119123
OnceToken_t swift::runtime::environment::initializeToken;
120124

121-
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__)
125+
#if SWIFT_STDLIB_HAS_ENVIRON && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__))
122126
extern "C" char **environ;
123127
#define ENVIRON environ
124128
#elif defined(_WIN32)
@@ -184,7 +188,7 @@ void swift::runtime::environment::initialize(void *context) {
184188
if (SWIFT_DEBUG_HELP_variable)
185189
printHelp(nullptr);
186190
}
187-
#else
191+
#elif SWIFT_STDLIB_HAS_ENVIRON
188192
void swift::runtime::environment::initialize(void *context) {
189193
// Emit a getenv call for each variable. This is less efficient but works
190194
// everywhere.
@@ -197,6 +201,10 @@ void swift::runtime::environment::initialize(void *context) {
197201
printHelp("Using getenv to read variables. Unknown SWIFT_DEBUG_ variables "
198202
"will not be flagged.");
199203
}
204+
#else
205+
void swift::runtime::environment::initialize(void *context) {
206+
(void)context;
207+
}
200208
#endif
201209

202210
SWIFT_RUNTIME_EXPORT

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,7 @@ swift-stdlib-supports-backtrace-reporting=0
24682468
swift-stdlib-has-darwin-libmalloc=0
24692469
swift-stdlib-has-asl=0
24702470
swift-stdlib-has-stdin=0
2471+
swift-stdlib-has-environ=0
24712472
swift-runtime-static-image-inspection=1
24722473
swift-stdlib-single-threaded-runtime=1
24732474
swift-stdlib-os-versioning=0

utils/build-script-impl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ KNOWN_SETTINGS=(
215215
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
216216
swift-stdlib-has-asl "" "whether the stdlib can use the asl_log API, defaults to true on Darwin, false otherwise"
217217
swift-stdlib-has-stdin "1" "whether to build stdlib assuming the platform supports stdin and getline API"
218+
swift-stdlib-has-environ "1" "whether to build stdlib assuming the platform supports environment variables"
218219
swift-stdlib-lto "" "enable LLVM LTO on the stdlib, valid values are empty string (no LTO), 'full' and 'thin'"
219220
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
220221
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
@@ -1991,6 +1992,7 @@ for host in "${ALL_HOSTS[@]}"; do
19911992
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
19921993
-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}")
19931994
-DSWIFT_STDLIB_HAS_STDIN:BOOL=$(true_false "${SWIFT_STDLIB_HAS_STDIN}")
1995+
-DSWIFT_STDLIB_HAS_ENVIRON:BOOL=$(true_false "${SWIFT_STDLIB_HAS_ENVIRON}")
19941996
-DSWIFT_STDLIB_ENABLE_LTO:STRING="${SWIFT_STDLIB_LTO}"
19951997
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
19961998
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"

utils/check_freestanding_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
common_expected_dependencies = [
5252
"___bzero", "___divti3", "___error", "___stderrp", "___stdoutp",
5353
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf",
54-
"_calloc", "_close", "_environ", "_flockfile", "_floorl", "_fprintf",
54+
"_calloc", "_close", "_flockfile", "_floorl", "_fprintf",
5555
"_fputc", "_fputs", "_free", "_funlockfile", "_fwrite", "_malloc",
5656
"_malloc_size", "_memchr", "_memcmp", "_memcpy", "_memmove", "_memset",
5757
"_posix_memalign", "_putc", "_read", "_realloc", "_snprintf", "_strchr",

validation-test/stdlib/Hashing.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// RUN: %target-run-stdlib-swift
22
// REQUIRES: executable_test
33

4+
// Freestanding doesn't support environment variables, and this test depends on SWIFT_DETERMINISTIC_HASHING=1
5+
// UNSUPPORTED: freestanding
6+
47
import Swift
58
import SwiftPrivate
69
import StdlibUnittest

validation-test/stdlib/HashingRandomization.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
// REQUIRES: executable_test
1212

13+
// Freestanding doesn't support environment variables, and this test depends on SWIFT_DETERMINISTIC_HASHING=1
14+
// UNSUPPORTED: freestanding
15+
1316
// This check verifies that the hash seed is randomly generated on every
1417
// execution of a Swift program unless the SWIFT_DETERMINISTIC_HASHING
1518
// environment variable is set.

0 commit comments

Comments
 (0)