Skip to content

[Freestanding] Remove uses of stat() and dlsym(). #66883

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
Jun 23, 2023
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ option(SWIFT_THREADING_PACKAGE
Valid package names are 'pthreads', 'darwin', 'linux', 'win32', 'c11', 'none'
or the empty string for the SDK default.")

option(SWIFT_THREADING_HAS_DLSYM
"Enable the use of the dlsym() function. This gets used to provide TSan
support on some platforms."
TRUE)

option(SWIFT_ENABLE_MACCATALYST
"Build the Standard Library and overlays with MacCatalyst support"
FALSE)
Expand Down
9 changes: 8 additions & 1 deletion cmake/modules/AddSwiftUnittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ function(add_swift_unittest test_dirname)
endif()

# some headers switch their inline implementations based on
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY, SWIFT_STDLIB_HAS_DLSYM and
# SWIFT_THREADING_PACKAGE definitions
if(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
target_compile_definitions("${test_dirname}" PRIVATE
SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
endif()
if(SWIFT_STDLIB_HAS_DLSYM)
target_compile_definitions("${test_dirname}" PRIVATE
"SWIFT_STDLIB_HAS_DLSYM=1")
else()
target_compile_definitions("${test_dirname}" PRIVATE
"SWIFT_STDLIB_HAS_DLSYM=0")
endif()

string(TOUPPER "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_THREADING_PACKAGE}" _threading_package)
target_compile_definitions("${test_dirname}" PRIVATE
Expand Down
6 changes: 5 additions & 1 deletion include/swift/Threading/ThreadSanitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

namespace swift {

#if defined(_WIN32) || defined(__wasi__) || !__has_include(<dlfcn.h>)
#if SWIFT_THREADING_NONE \
|| defined(_WIN32) || defined(__wasi__) \
|| !__has_include(<dlfcn.h>) \
|| (defined(SWIFT_STDLIB_HAS_DLSYM) && !SWIFT_STDLIB_HAS_DLSYM)

#define SWIFT_THREADING_TSAN_SUPPORT 0

Expand All @@ -35,6 +38,7 @@ template <typename T> T *acquire(T *ptr) { return ptr; }
template <typename T> T *release(T *ptr) { return ptr; }

} // namespace tsan

#else

#define SWIFT_THREADING_TSAN_SUPPORT 1
Expand Down
6 changes: 2 additions & 4 deletions lib/Threading/ThreadSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "swift/shims/Visibility.h"

#include <dlfcn.h>

#include <cstdio>

namespace swift {
Expand All @@ -32,9 +34,6 @@ SWIFT_THREADING_EXPORT bool _swift_tsan_enabled = false;
SWIFT_THREADING_EXPORT void (*_swift_tsan_acquire)(const void *) = nullptr;
SWIFT_THREADING_EXPORT void (*_swift_tsan_release)(const void *) = nullptr;

#if __has_include(<dlfcn.h>)
#include <dlfcn.h>

// The TSan library code will call this function when it starts up
extern "C" SWIFT_ATTRIBUTE_FOR_EXPORTS
void __tsan_on_initialize() {
Expand All @@ -53,7 +52,6 @@ void __tsan_on_initialize() {
next_init();
}
}
#endif // __has_include(<dlfcn.h>)

} // namespace threading_impl
} // namespace swift
Expand Down
10 changes: 10 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_STDLIB_HAS_DLADDR")
endif()

if(SWIFT_STDLIB_HAS_DLSYM)
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=1")
else()
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=0")
endif()

if(SWIFT_STDLIB_HAS_FILESYSTEM)
list(APPEND result "-DSWIFT_STDLIB_HAS_FILESYSTEM")
endif()

if(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION)
list(APPEND result "-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION")
endif()
Expand Down
10 changes: 9 additions & 1 deletion stdlib/cmake/modules/StdlibOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ option(SWIFT_STDLIB_BUILD_PRIVATE
TRUE)

option(SWIFT_STDLIB_HAS_DLADDR
"Build stdlib assuming the runtime environment runtime environment provides dladdr API."
"Build stdlib assuming the runtime environment provides the dladdr API."
TRUE)

option(SWIFT_STDLIB_HAS_DLSYM
"Build stdlib assuming the runtime environment provides the dlsym API."
TRUE)

option(SWIFT_STDLIB_HAS_FILESYSTEM
"Build stdlib assuming the runtime environment has a filesystem."
TRUE)

option(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION
Expand Down
24 changes: 24 additions & 0 deletions stdlib/public/runtime/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@
#include <cstdlib>
#include <cstring>

#if !SWIFT_STDLIB_HAS_FILESYSTEM

SWIFT_RUNTIME_EXPORT
const char *
swift_getRuntimeLibraryPath() {
return nullptr;
}

SWIFT_RUNTIME_EXPORT
const char *
swift_getRootPath() {
return nullptr;
}

SWIFT_RUNTIME_EXPORT
char *
swift_copyAuxiliaryExecutablePath(const char *name) {
return nullptr;
}

#else // SWIFT_STDLIB_HAS_FILESYSTEM

namespace {

swift::once_t runtimePathToken;
Expand Down Expand Up @@ -578,3 +600,5 @@ bool _swift_exists(const char *path)
}

}

#endif // SWIFT_STDLIB_HAS_FILESYSTEM
2 changes: 2 additions & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,8 @@ swift-enable-reflection=0
swift-stdlib-reflection-metadata=debugger-only
swift-stdlib-stable-abi=0
swift-stdlib-has-dladdr=0
swift-stdlib-has-dlsym=0
swift-stdlib-has-filesystem=0
swift-stdlib-supports-backtrace-reporting=0
swift-stdlib-has-darwin-libmalloc=0
swift-stdlib-has-asl=0
Expand Down
4 changes: 4 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ KNOWN_SETTINGS=(
swift-enable-reflection "1" "whether to support reflection and mirrors"
swift-stdlib-reflection-metadata "enabled" "whether to build stdlib with runtime metadata (valid options are 'enabled', 'disabled' and 'debugger-only')"
swift-stdlib-has-dladdr "1" "whether to build stdlib assuming the runtime environment provides dladdr API"
swift-stdlib-has-dlsym "1" "whether to build stdlib assuming the runtime environment provides the dlsym API"
swift-stdlib-has-filesystem "1" "whether to build stdlib assuming the runtime environment provides a filesystem"
swift-stdlib-supports-backtrace-reporting "" "whether to build stdlib assuming the runtime environment provides the backtrace(3) API, if not set defaults to true on all platforms except for Cygwin, Haiku and wasm"
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
swift-threading-package "" "override the threading package for the host build; this is either a single package or a semicolon-separated list of sdk:package pairs. Valid packages are empty string (no override), 'pthreads', 'darwin', 'linux', 'win32', 'c11', 'none'"
Expand Down Expand Up @@ -1831,6 +1833,8 @@ for host in "${ALL_HOSTS[@]}"; do
-DSWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY:BOOL=$(true_false "${SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY}")
-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
-DSWIFT_STDLIB_HAS_DLADDR:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLADDR}")
-DSWIFT_STDLIB_HAS_DLSYM:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLSYM}")
-DSWIFT_STDLIB_HAS_FILESYSTEM:BOOL=$(true_false "${SWIFT_STDLIB_HAS_FILESYSTEM}")
-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION:BOOL=$(true_false "${SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION}")
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
-DSWIFT_STDLIB_HAS_COMMANDLINE:BOOL=$(true_false "${SWIFT_STDLIB_HAS_COMMANDLINE}")
Expand Down
1 change: 0 additions & 1 deletion utils/check_freestanding_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"_posix_memalign", "_putc", "_read", "_realloc", "_snprintf", "_strchr",
"_strcmp", "_strdup", "_strlen", "_strncmp", "_strtod", "_strtof",
"_strtol", "_strtold", "_vprintf", "_vsnprintf", "_write",
"_stat", "_stat$INODE64",
] + cxx_dependencies + math_dependencies
vendor_apple_specific_dependencies = [
"___stack_chk_fail", "___stack_chk_guard",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def build(self, host_target):
self.cmake_options.define(
'SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_DLADDR:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_DLSYM:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_ENVIRON:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_FILESYSTEM:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_LOCALE:BOOL', 'FALSE')
self.cmake_options.define('SWIFT_STDLIB_HAS_STDIN:BOOL', 'FALSE')
self.cmake_options.define(
Expand Down