Skip to content

Commit 1573ffb

Browse files
committed
[Freestanding] Remove uses of stat() and dlsym().
We shouldn't be using stat() or dlsym() in the freestanding runtime. rdar://111214571 rdar://106555012
1 parent d9aed16 commit 1573ffb

File tree

8 files changed

+45
-7
lines changed

8 files changed

+45
-7
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,11 @@ option(SWIFT_THREADING_PACKAGE
585585
Valid package names are 'pthreads', 'darwin', 'linux', 'win32', 'c11', 'none'
586586
or the empty string for the SDK default.")
587587

588+
option(SWIFT_THREADING_HAS_DLSYM
589+
"Enable the use of the dlsym() function. This gets used to provide TSan
590+
support on some platforms."
591+
TRUE)
592+
588593
#
589594
# End of user-configurable options.
590595
#

cmake/modules/AddSwiftUnittests.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,19 @@ function(add_swift_unittest test_dirname)
4747
endif()
4848

4949
# some headers switch their inline implementations based on
50-
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and
50+
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY, SWIFT_STDLIB_HAS_DLSYM and
5151
# SWIFT_THREADING_PACKAGE definitions
5252
if(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5353
target_compile_definitions("${test_dirname}" PRIVATE
5454
SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5555
endif()
56+
if(SWIFT_STDLIB_HAS_DLSYM)
57+
target_compile_definitions("${test_dirname}" PRIVATE
58+
"SWIFT_STDLIB_HAS_DLSYM=1")
59+
else()
60+
target_compile_definitions("${test_dirname}" PRIVATE
61+
"SWIFT_STDLIB_HAS_DLSYM=0")
62+
endif()
5663

5764
string(TOUPPER "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_THREADING_PACKAGE}" _threading_package)
5865
target_compile_definitions("${test_dirname}" PRIVATE

include/swift/Threading/ThreadSanitizer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
namespace swift {
2626

27-
#if defined(_WIN32) || defined(__wasi__) || !__has_include(<dlfcn.h>)
27+
#if SWIFT_THREADING_NONE \
28+
|| defined(_WIN32) || defined(__wasi__) \
29+
|| !__has_include(<dlfcn.h>) \
30+
|| (defined(SWIFT_STDLIB_HAS_DLSYM) && !SWIFT_STDLIB_HAS_DLSYM)
2831

2932
#define SWIFT_THREADING_TSAN_SUPPORT 0
3033

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

3740
} // namespace tsan
41+
3842
#else
3943

4044
#define SWIFT_THREADING_TSAN_SUPPORT 1

lib/Threading/ThreadSanitizer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include "swift/shims/Visibility.h"
2525

26+
#include <dlfcn.h>
27+
2628
#include <cstdio>
2729

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

35-
#if __has_include(<dlfcn.h>)
36-
#include <dlfcn.h>
37-
3837
// The TSan library code will call this function when it starts up
3938
extern "C" SWIFT_ATTRIBUTE_FOR_EXPORTS
4039
void __tsan_on_initialize() {
@@ -53,7 +52,6 @@ void __tsan_on_initialize() {
5352
next_init();
5453
}
5554
}
56-
#endif // __has_include(<dlfcn.h>)
5755

5856
} // namespace threading_impl
5957
} // namespace swift

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ function(_add_target_variant_c_compile_flags)
323323
list(APPEND result "-DSWIFT_STDLIB_HAS_DLADDR")
324324
endif()
325325

326+
if(SWIFT_STDLIB_HAS_DLSYM)
327+
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=1")
328+
else()
329+
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=0")
330+
endif()
331+
332+
if(SWIFT_STDLIB_HAS_FILESYSTEM)
333+
list(APPEND result "-DSWIFT_STDLIB_HAS_FILESYSTEM")
334+
endif()
335+
326336
if(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION)
327337
list(APPEND result "-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION")
328338
endif()

stdlib/cmake/modules/StdlibOptions.cmake

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,15 @@ option(SWIFT_STDLIB_BUILD_PRIVATE
135135
TRUE)
136136

137137
option(SWIFT_STDLIB_HAS_DLADDR
138-
"Build stdlib assuming the runtime environment runtime environment provides dladdr API."
138+
"Build stdlib assuming the runtime environment provides the dladdr API."
139+
TRUE)
140+
141+
option(SWIFT_STDLIB_HAS_DLSYM
142+
"Build stdlib assuming the runtime environment provides the dlsym API."
143+
TRUE)
144+
145+
option(SWIFT_STDLIB_HAS_FILESYSTEM
146+
"Build stdlib assuming the runtime environment has a filesystem."
139147
TRUE)
140148

141149
option(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION

utils/build-presets.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,8 @@ swift-enable-reflection=0
25502550
swift-stdlib-reflection-metadata=debugger-only
25512551
swift-stdlib-stable-abi=0
25522552
swift-stdlib-has-dladdr=0
2553+
swift-stdlib-has-dlsym=0
2554+
swift-stdlib-has-filesystem=0
25532555
swift-stdlib-supports-backtrace-reporting=0
25542556
swift-stdlib-has-darwin-libmalloc=0
25552557
swift-stdlib-has-asl=0

utils/build-script-impl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ KNOWN_SETTINGS=(
206206
swift-enable-reflection "1" "whether to support reflection and mirrors"
207207
swift-stdlib-reflection-metadata "enabled" "whether to build stdlib with runtime metadata (valid options are 'enabled', 'disabled' and 'debugger-only')"
208208
swift-stdlib-has-dladdr "1" "whether to build stdlib assuming the runtime environment provides dladdr API"
209+
swift-stdlib-has-dlsym "1" "whether to build stdlib assuming the runtime environment provides the dlsym API"
210+
swift-stdlib-has-filesystem "1" "whether to build stdlib assuming the runtime environment provides a filesystem"
209211
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"
210212
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
211213
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'"
@@ -1940,6 +1942,8 @@ for host in "${ALL_HOSTS[@]}"; do
19401942
-DSWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY:BOOL=$(true_false "${SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY}")
19411943
-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
19421944
-DSWIFT_STDLIB_HAS_DLADDR:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLADDR}")
1945+
-DSWIFT_STDLIB_HAS_DLSYM:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLSYM}")
1946+
-DSWIFT_STDLIB_HAS_FILESYSTEM:BOOL=$(true_false "${SWIFT_STDLIB_HAS_FILESYSTEM}")
19431947
-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION:BOOL=$(true_false "${SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION}")
19441948
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
19451949
-DSWIFT_STDLIB_HAS_COMMANDLINE:BOOL=$(true_false "${SWIFT_STDLIB_HAS_COMMANDLINE}")

0 commit comments

Comments
 (0)