Skip to content

Commit 81480b5

Browse files
authored
Merge pull request #71891 from mikeash/libprespecialize-rtld-noload
[Runtime] Harden SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH.
2 parents 558ed7b + 20d6040 commit 81480b5

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

stdlib/public/runtime/EnvironmentVariables.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ VARIABLE(SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED, bool, true,
9292
"Enable use of prespecializations library.")
9393

9494
VARIABLE(SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH, string, "",
95-
"A path to a prespecializations library to use at runtime.")
95+
"A path to a prespecializations library to use at runtime. In order to"
96+
"be used, this library must be loaded into the process by other means"
97+
"(such as DYLD_INSERT_LIBRARIES) before Swift tries to use it.")
9698

9799
VARIABLE(SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_LOGGING, bool, false,
98100
"Enable debug logging of prespecializations library use.")

stdlib/public/runtime/LibPrespecialized.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ static const LibPrespecializedData<InProcess> *findLibPrespecialized() {
3535
#if USE_DLOPEN
3636
auto path = runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH();
3737
if (path && path[0]) {
38-
void *handle = dlopen(path, RTLD_LAZY);
38+
// Use RTLD_NOLOAD to avoid actually loading the library. We just want to
39+
// find it if it has already been loaded by other means, such as
40+
// DYLD_INSERT_LIBRARIES.
41+
void *handle = dlopen(path, RTLD_LAZY | RTLD_NOLOAD);
3942
if (!handle) {
4043
swift::warning(0, "Failed to load prespecializations library: %s\n",
4144
dlerror());
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public struct GenericStruct<T, U, V> {
2+
var t: T
3+
var u: U
4+
var v: V
5+
var str: String
6+
}
7+
8+
public struct GenericField<T, U> {
9+
var field: GenericStruct<T, U, Double>
10+
var int: Int
11+
}
12+
13+
public struct Box<T> {
14+
var field: T
15+
}
16+
17+
public struct Box3<T, U, V> {
18+
var field1: T
19+
var field2: U
20+
var field3: V
21+
}
22+
23+
// The protocol conformance puts a symbol into __DATA_CONST which the builder
24+
// can use as the base symbol for references to other data.
25+
public protocol PublicProto {}
26+
extension Box3: PublicProto {}

test/ExternalGenericMetadataBuilder/VerifyExternalMetadata.swift

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -I %swift-lib-dir -I %swift_src_root/lib/ExternalGenericMetadataBuilder -enable-experimental-feature Extern %s -o %t/VerifyExternalMetadata
2+
//
3+
// RUN: %host-build-swift %S/Inputs/testMetadataLibrary.swift -emit-library -emit-module -o %t/libtestMetadataLibrary.dylib
4+
// RUN: %target-codesign %t/libtestMetadataLibrary.dylib
5+
//
6+
// RUN: %target-build-swift -Xfrontend -disable-availability-checking -I %swift-lib-dir -I %swift_src_root/lib/ExternalGenericMetadataBuilder -I %t -L %t -ltestMetadataLibrary -enable-experimental-feature Extern %s -o %t/VerifyExternalMetadata
37
// RUN: %target-codesign %t/VerifyExternalMetadata
48
//
59
// RUN: %host-build-swift -Xfrontend -disable-availability-checking -I %swift-lib-dir -I %swift_src_root/lib/ExternalGenericMetadataBuilder -L%swift-lib-dir -lswiftGenericMetadataBuilder -Xlinker -rpath -Xlinker %swift-lib-dir -enable-experimental-feature Extern %S/Inputs/buildMetadataJSON.swift -o %t/buildMetadataJSON
@@ -11,12 +15,12 @@
1115
// RUN: %target-run %t/VerifyExternalMetadata getJSON > %t/names.json
1216
// RUN: %target-run %t/buildMetadataJSON %target-arch %t/VerifyExternalMetadata %stdlib_dir/libswiftCore.dylib < %t/names.json > %t/libswiftPrespecialized.json
1317
// RUN: %target-run %t/json2c %t/libswiftPrespecialized.json > %t/libswiftPrespecialized.c
14-
// RUN: %clang -isysroot %sdk -target %target-triple -bundle %t/libswiftPrespecialized.c -L%stdlib_dir -lswiftCore -bundle_loader %t/VerifyExternalMetadata -o %t/libswiftPrespecialized.bundle
18+
// RUN: %clang -isysroot %sdk -target %target-triple -dynamiclib %t/libswiftPrespecialized.c -L%stdlib_dir -lswiftCore -o %t/libswiftPrespecialized.dylib
1519
//
1620
// Set a custom library path because we need to ensure we don't load an arm64e
1721
// dylib into an arm64 test, since the prespecialized metadata depends on the
1822
// exact contents of the library.
19-
// RUN: env SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_LOGGING=y SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH=%t/libswiftPrespecialized.bundle %target-run env DYLD_LIBRARY_PATH=%stdlib_dir/%target-arch %t/VerifyExternalMetadata
23+
// RUN: env SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED_LOGGING=y DYLD_INSERT_LIBRARIES=%t/libswiftPrespecialized.dylib SWIFT_DEBUG_LIB_PRESPECIALIZED_PATH=%t/libswiftPrespecialized.dylib %target-run env DYLD_LIBRARY_PATH=%stdlib_dir/%target-arch %t/VerifyExternalMetadata
2024

2125
// REQUIRES: executable_test
2226
// REQUIRES: OS=macosx && CPU=arm64
@@ -25,32 +29,7 @@ import ExternalGenericMetadataBuilder
2529
import Foundation
2630
import StdlibUnittest
2731

28-
public struct GenericStruct<T, U, V> {
29-
var t: T
30-
var u: U
31-
var v: V
32-
var str: String
33-
}
34-
35-
public struct GenericField<T, U> {
36-
var field: GenericStruct<T, U, Double>
37-
var int: Int
38-
}
39-
40-
public struct Box<T> {
41-
var field: T
42-
}
43-
44-
public struct Box3<T, U, V> {
45-
var field1: T
46-
var field2: U
47-
var field3: V
48-
}
49-
50-
// The protocol conformance puts a symbol into __DATA_CONST which the builder
51-
// can use as the base symbol for references to other data.
52-
public protocol PublicProto {}
53-
extension Box3: PublicProto {}
32+
import testMetadataLibrary
5433

5534
let args = CommandLine.arguments
5635

@@ -60,8 +39,8 @@ if args.count > 1 && args[1] == "getJSON" {
6039
GenericField<GenericField<Int8, Int16>,
6140
Array<GenericStruct<Double, String, Float>>>.self,
6241
Array<Array<Array<Array<Array<Array<Array<Double>>>>>>>.self,
63-
Box<Int>.self,
64-
Box<String>.self,
42+
testMetadataLibrary.Box<Int>.self,
43+
testMetadataLibrary.Box<String>.self,
6544
Box3<Int, Int, Int>.self,
6645
]
6746
let typeNames = types.map { _mangledTypeName($0)! }

0 commit comments

Comments
 (0)