Skip to content

Add SWIFT_STDLIB_HAS_ENVIRON to remove usage of getenv/environ from stdlib #39599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ option(SWIFT_STDLIB_HAS_STDIN
"Build stdlib assuming the platform supports stdin and getline API."
TRUE)

option(SWIFT_STDLIB_HAS_ENVIRON
"Build stdlib assuming the platform supports environment variables."
TRUE)

option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
"Build the standard libraries assuming that they will be used in an environment with only a single thread."
FALSE)
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_STDLIB_HAS_STDIN")
endif()

if(SWIFT_STDLIB_HAS_ENVIRON)
list(APPEND result "-DSWIFT_STDLIB_HAS_ENVIRON")
endif()

if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
endif()
Expand Down
5 changes: 5 additions & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ function(_add_target_variant_swift_compile_flags
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
endif()

if(SWIFT_STDLIB_HAS_ENVIRON)
list(APPEND result "-D" "SWIFT_STDLIB_HAS_ENVIRON")
list(APPEND result "-Xcc" "-DSWIFT_STDLIB_HAS_ENVIRON")
endif()

set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()

Expand Down
7 changes: 6 additions & 1 deletion stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
set(swift_private_libc_extras_flags)
if(SWIFT_STDLIB_HAS_ENVIRON)
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
endif()

add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
# This file should be listed the first. Module name is inferred from the
# filename.
Expand All @@ -8,7 +13,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"

SWIFT_MODULE_DEPENDS SwiftPrivate
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} ${swift_private_libc_extras_flags}
SWIFT_MODULE_DEPENDS_OSX Darwin
SWIFT_MODULE_DEPENDS_IOS Darwin
SWIFT_MODULE_DEPENDS_TVOS Darwin
Expand Down
17 changes: 17 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ func posixPipe() -> (readFD: CInt, writeFD: CInt) {
return (fds[0], fds[1])
}

#if !SWIFT_STDLIB_HAS_ENVIRON
@_silgen_name("_NSGetEnviron")
func _NSGetEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>>

var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
#if os(macOS)
return _NSGetEnviron().pointee
#elseif os(Windows)
return __p_environ().pointee
#elseif os(Linux)
return __environ
#else
#error("unsupported platform")
#endif
}
#endif

/// Start the same executable as a child process, redirecting its stdout and
/// stderr.
public func spawnChild(_ args: [String])
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/Platform/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ public func sem_open(
//===----------------------------------------------------------------------===//

// Some platforms don't have `extern char** environ` imported from C.
#if SWIFT_STDLIB_HAS_ENVIRON
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(OpenBSD) || os(PS4)
public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
return _swift_stdlib_getEnviron()
Expand All @@ -436,3 +437,4 @@ public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
return __environ
}
#endif
#endif // SWIFT_STDLIB_HAS_ENVIRON
2 changes: 2 additions & 0 deletions stdlib/public/SwiftShims/LibcOverlayShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static inline int _swift_stdlib_fcntlPtr(int fd, int cmd, void* ptr) {
#endif

// Environment
#if SWIFT_STDLIB_HAS_ENVIRON
#if defined(__FreeBSD__) || defined(__OpenBSD__)
static inline char * _Nullable * _Null_unspecified _swift_stdlib_getEnviron() {
extern char **environ;
Expand All @@ -63,6 +64,7 @@ static inline char * _Nullable *_swift_stdlib_getEnviron() {
return *_NSGetEnviron();
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I'm not sure why Windows is missing here...

#endif // SWIFT_STDLIB_HAS_ENVIRON

// System error numbers <errno.h>
static inline int _swift_stdlib_getErrno() {
Expand Down
12 changes: 10 additions & 2 deletions stdlib/public/runtime/EnvironmentVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static constexpr bool hasSwiftPrefix(const char *str) {
static_assert(hasSwiftPrefix(#name), "Names must start with SWIFT");
#include "EnvironmentVariables.def"

#if SWIFT_STDLIB_HAS_ENVIRON

// Value parsers. Add new functions named parse_<type> to accommodate more
// debug variable types.
static bool parse_bool(const char *name, const char *value, bool defaultValue) {
Expand Down Expand Up @@ -108,6 +110,8 @@ void printHelp(const char *extra) {
swift::warning(RuntimeErrorFlagNone, "SWIFT_DEBUG_HELP=YES - Print this help.");
}

#endif // SWIFT_STDLIB_HAS_ENVIRON

} // end anonymous namespace

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

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__)
#if SWIFT_STDLIB_HAS_ENVIRON && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__))
extern "C" char **environ;
#define ENVIRON environ
#elif defined(_WIN32)
Expand Down Expand Up @@ -184,7 +188,7 @@ void swift::runtime::environment::initialize(void *context) {
if (SWIFT_DEBUG_HELP_variable)
printHelp(nullptr);
}
#else
#elif SWIFT_STDLIB_HAS_ENVIRON
void swift::runtime::environment::initialize(void *context) {
// Emit a getenv call for each variable. This is less efficient but works
// everywhere.
Expand All @@ -197,6 +201,10 @@ void swift::runtime::environment::initialize(void *context) {
printHelp("Using getenv to read variables. Unknown SWIFT_DEBUG_ variables "
"will not be flagged.");
}
#else
void swift::runtime::environment::initialize(void *context) {
(void)context;
}
#endif

SWIFT_RUNTIME_EXPORT
Expand Down
1 change: 1 addition & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,7 @@ swift-stdlib-has-dladdr=0
swift-stdlib-supports-backtrace-reporting=0
swift-stdlib-has-darwin-libmalloc=0
swift-stdlib-has-stdin=0
swift-stdlib-has-environ=0
swift-runtime-static-image-inspection=1
swift-stdlib-single-threaded-runtime=1
swift-stdlib-os-versioning=0
Expand Down
2 changes: 2 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ KNOWN_SETTINGS=(
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
swift-stdlib-has-stdin "1" "whether to build stdlib assuming the platform supports stdin and getline API"
swift-stdlib-has-environ "1" "whether to build stdlib assuming the platform supports environment variables"
swift-stdlib-lto "" "enable LLVM LTO on the stdlib, valid values are empty string (no LTO), 'full' and 'thin'"
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
Expand Down Expand Up @@ -1990,6 +1991,7 @@ for host in "${ALL_HOSTS[@]}"; do
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}")
-DSWIFT_STDLIB_HAS_STDIN:BOOL=$(true_false "${SWIFT_STDLIB_HAS_STDIN}")
-DSWIFT_STDLIB_HAS_ENVIRON:BOOL=$(true_false "${SWIFT_STDLIB_HAS_ENVIRON}")
-DSWIFT_STDLIB_ENABLE_LTO:STRING="${SWIFT_STDLIB_LTO}"
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"
Expand Down
2 changes: 1 addition & 1 deletion utils/check_freestanding_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
common_expected_dependencies = [
"___bzero", "___divti3", "___error", "___stderrp", "___stdoutp",
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf",
"_calloc", "_close", "_environ", "_flockfile", "_floorl", "_fprintf",
"_calloc", "_close", "_flockfile", "_floorl", "_fprintf",
"_fputc", "_fputs", "_free", "_funlockfile", "_fwrite", "_malloc",
"_malloc_size", "_memchr", "_memcmp", "_memcpy", "_memmove", "_memset",
"_posix_memalign", "_putc", "_read", "_realloc", "_snprintf", "_strchr",
Expand Down
3 changes: 3 additions & 0 deletions validation-test/stdlib/Hashing.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test

// Freestanding doesn't support environment variables, and this test depends on SWIFT_DETERMINISTIC_HASHING=1
// UNSUPPORTED: freestanding

import Swift
import SwiftPrivate
import StdlibUnittest
Expand Down
3 changes: 3 additions & 0 deletions validation-test/stdlib/HashingRandomization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

// REQUIRES: executable_test

// Freestanding doesn't support environment variables, and this test depends on SWIFT_DETERMINISTIC_HASHING=1
// UNSUPPORTED: freestanding

// This check verifies that the hash seed is randomly generated on every
// execution of a Swift program unless the SWIFT_DETERMINISTIC_HASHING
// environment variable is set.
Expand Down