Skip to content

Commit ecdf75a

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 12f01ce commit ecdf75a

File tree

11 files changed

+73
-8
lines changed

11 files changed

+73
-8
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ option(SWIFT_THREADING_PACKAGE
646646
Valid package names are 'pthreads', 'darwin', 'linux', 'win32', 'c11', 'none'
647647
or the empty string for the SDK default.")
648648

649+
option(SWIFT_THREADING_HAS_DLSYM
650+
"Enable the use of the dlsym() function. This gets used to provide TSan
651+
support on some platforms."
652+
TRUE)
653+
649654
option(SWIFT_ENABLE_MACCATALYST
650655
"Build the Standard Library and overlays with MacCatalyst support"
651656
FALSE)

cmake/modules/AddSwiftUnittests.cmake

+8-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,19 @@ function(add_swift_unittest test_dirname)
4949
endif()
5050

5151
# some headers switch their inline implementations based on
52-
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and
52+
# SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY, SWIFT_STDLIB_HAS_DLSYM and
5353
# SWIFT_THREADING_PACKAGE definitions
5454
if(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5555
target_compile_definitions("${test_dirname}" PRIVATE
5656
SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
5757
endif()
58+
if(SWIFT_STDLIB_HAS_DLSYM)
59+
target_compile_definitions("${test_dirname}" PRIVATE
60+
"SWIFT_STDLIB_HAS_DLSYM=1")
61+
else()
62+
target_compile_definitions("${test_dirname}" PRIVATE
63+
"SWIFT_STDLIB_HAS_DLSYM=0")
64+
endif()
5865

5966
string(TOUPPER "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_THREADING_PACKAGE}" _threading_package)
6067
target_compile_definitions("${test_dirname}" PRIVATE

include/swift/Threading/ThreadSanitizer.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
#ifndef SWIFT_THREADING_THREAD_SANITIZER_H
2121
#define SWIFT_THREADING_THREAD_SANITIZER_H
2222

23+
#include "Impl.h"
24+
2325
#include "swift/shims/Visibility.h"
2426

2527
namespace swift {
2628

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

2934
#define SWIFT_THREADING_TSAN_SUPPORT 0
3035

@@ -35,6 +40,7 @@ template <typename T> T *acquire(T *ptr) { return ptr; }
3540
template <typename T> T *release(T *ptr) { return ptr; }
3641

3742
} // namespace tsan
43+
3844
#else
3945

4046
#define SWIFT_THREADING_TSAN_SUPPORT 1

lib/Threading/ThreadSanitizer.cpp

+2-4
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

+10
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ function(_add_target_variant_c_compile_flags)
352352
list(APPEND result "-DSWIFT_STDLIB_HAS_DLADDR")
353353
endif()
354354

355+
if(SWIFT_STDLIB_HAS_DLSYM)
356+
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=1")
357+
else()
358+
list(APPEND result "-DSWIFT_STDLIB_HAS_DLSYM=0")
359+
endif()
360+
361+
if(SWIFT_STDLIB_HAS_FILESYSTEM)
362+
list(APPEND result "-DSWIFT_STDLIB_HAS_FILESYSTEM")
363+
endif()
364+
355365
if(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION)
356366
list(APPEND result "-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION")
357367
endif()

stdlib/cmake/modules/StdlibOptions.cmake

+9-1
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

stdlib/public/runtime/Paths.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@
5353
#include <cstdlib>
5454
#include <cstring>
5555

56+
#if !SWIFT_STDLIB_HAS_FILESYSTEM
57+
58+
SWIFT_RUNTIME_EXPORT
59+
const char *
60+
swift_getRuntimeLibraryPath() {
61+
return nullptr;
62+
}
63+
64+
SWIFT_RUNTIME_EXPORT
65+
const char *
66+
swift_getRootPath() {
67+
return nullptr;
68+
}
69+
70+
SWIFT_RUNTIME_EXPORT
71+
char *
72+
swift_copyAuxiliaryExecutablePath(const char *name) {
73+
return nullptr;
74+
}
75+
76+
#else // SWIFT_STDLIB_HAS_FILESYSTEM
77+
5678
namespace {
5779

5880
swift::once_t runtimePathToken;
@@ -578,3 +600,5 @@ bool _swift_exists(const char *path)
578600
}
579601

580602
}
603+
604+
#endif // SWIFT_STDLIB_HAS_FILESYSTEM

utils/build-presets.ini

+2
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,8 @@ swift-enable-reflection=0
26182618
swift-stdlib-reflection-metadata=debugger-only
26192619
swift-stdlib-stable-abi=0
26202620
swift-stdlib-has-dladdr=0
2621+
swift-stdlib-has-dlsym=0
2622+
swift-stdlib-has-filesystem=0
26212623
swift-stdlib-supports-backtrace-reporting=0
26222624
swift-stdlib-has-darwin-libmalloc=0
26232625
swift-stdlib-has-asl=0

utils/build-script-impl

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ KNOWN_SETTINGS=(
207207
swift-enable-reflection "1" "whether to support reflection and mirrors"
208208
swift-stdlib-reflection-metadata "enabled" "whether to build stdlib with runtime metadata (valid options are 'enabled', 'disabled' and 'debugger-only')"
209209
swift-stdlib-has-dladdr "1" "whether to build stdlib assuming the runtime environment provides dladdr API"
210+
swift-stdlib-has-dlsym "1" "whether to build stdlib assuming the runtime environment provides the dlsym API"
211+
swift-stdlib-has-filesystem "1" "whether to build stdlib assuming the runtime environment provides a filesystem"
210212
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"
211213
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
212214
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'"
@@ -1831,6 +1833,8 @@ for host in "${ALL_HOSTS[@]}"; do
18311833
-DSWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY:BOOL=$(true_false "${SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY}")
18321834
-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS:BOOL=$(true_false "${SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS}")
18331835
-DSWIFT_STDLIB_HAS_DLADDR:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DLADDR}")
1836+
-DSWIFT_STDLIB_HAS_DLSYM:BOOL=$(true_false "${SWIFT_THREADING_HAS_DLSYM}")
1837+
-DSWIFT_STDLIB_HAS_FILESYSTEM:BOOL=$(true_false "${SWIFT_STDLIB_HAS_FILESYSTEM}")
18341838
-DSWIFT_RUNTIME_STATIC_IMAGE_INSPECTION:BOOL=$(true_false "${SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION}")
18351839
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
18361840
-DSWIFT_STDLIB_HAS_COMMANDLINE:BOOL=$(true_false "${SWIFT_STDLIB_HAS_COMMANDLINE}")

utils/check_freestanding_dependencies.py

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"_posix_memalign", "_putc", "_read", "_realloc", "_snprintf", "_strchr",
5757
"_strcmp", "_strdup", "_strlen", "_strncmp", "_strtod", "_strtof",
5858
"_strtol", "_strtold", "_vprintf", "_vsnprintf", "_write",
59-
"_stat", "_stat$INODE64",
6059
] + cxx_dependencies + math_dependencies
6160
vendor_apple_specific_dependencies = [
6261
"___stack_chk_fail", "___stack_chk_guard",

utils/swift_build_support/swift_build_support/products/minimalstdlib.py

+2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def build(self, host_target):
140140
self.cmake_options.define(
141141
'SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL', 'FALSE')
142142
self.cmake_options.define('SWIFT_STDLIB_HAS_DLADDR:BOOL', 'FALSE')
143+
self.cmake_options.define('SWIFT_STDLIB_HAS_DLSYM:BOOL', 'FALSE')
143144
self.cmake_options.define('SWIFT_STDLIB_HAS_ENVIRON:BOOL', 'FALSE')
145+
self.cmake_options.define('SWIFT_STDLIB_HAS_FILESYSTEM:BOOL', 'FALSE')
144146
self.cmake_options.define('SWIFT_STDLIB_HAS_LOCALE:BOOL', 'FALSE')
145147
self.cmake_options.define('SWIFT_STDLIB_HAS_STDIN:BOOL', 'FALSE')
146148
self.cmake_options.define(

0 commit comments

Comments
 (0)