Skip to content

[5.0] DynamicReplacement: Don't fail when a library is closed and reloaded #22126

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
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/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1677,11 +1677,15 @@ void DynamicReplacementDescriptor::enableReplacement() const {
replacedFunctionKey->root.get());

// Make sure this entry is not already enabled.
// This does not work until we make sure that when a dynamic library is
// unloaded all descriptors are removed.
#if 0
for (auto *curr = chainRoot; curr != nullptr; curr = curr->next) {
if (curr == chainEntry.get()) {
swift::swift_abortDynamicReplacementEnabling();
}
}
#endif

// Unlink the previous entry if we are not chaining.
if (!shouldChain() && chainRoot->next) {
Expand Down
9 changes: 9 additions & 0 deletions test/Interpreter/Inputs/dynamic_replacement_dlclose.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct A {
dynamic var value : Int {
return 1
}
}

public func test() -> Int{
return A().value
}
8 changes: 8 additions & 0 deletions test/Interpreter/Inputs/dynamic_replacement_dlclose2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@_private(sourceFile: "dynamic_replacement_dlclose.swift") import Module1

extension A {
@_dynamicReplacement(for: value)
var repl: Int {
return 2
}
}
70 changes: 70 additions & 0 deletions test/Interpreter/dynamic_replacement_dlclose.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift-dylib(%t/libModule1.%target-dylib-extension) -DMODULE -module-name Module1 -emit-module -emit-module-path %t/Module1.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_dlclose.swift -Xfrontend -enable-private-imports
// RUN: %target-build-swift-dylib(%t/libModule2.%target-dylib-extension) -I%t -L%t -lModule1 -Xlinker -rpath -Xlinker %t -DMODULE2 -module-name Module2 -emit-module -emit-module-path %t/Module2.swiftmodule -swift-version 5 %S/Inputs/dynamic_replacement_dlclose2.swift
// RUN: %target-build-swift -I%t -L%t -lModule1 -DMAIN -o %t/main -Xlinker -rpath -Xlinker %t %s -swift-version 5
// RUN: %target-codesign %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension
// RUN: %target-run %t/main %t/libModule1.%target-dylib-extension %t/libModule2.%target-dylib-extension


import Module1

import StdlibUnittest

#if os(Linux)
import Glibc
#elseif os(Windows)
import MSVCRT
import WinSDK
#else
import Darwin
#endif

var DynamicallyReplaceable = TestSuite("DynamicallyReplaceable")



private func target_library_name(_ name: String) -> String {
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
return "lib\(name).dylib"
#elseif os(Windows)
return "\(name).dll"
#else
return "lib\(name).so"
#endif
}


DynamicallyReplaceable.test("DynamicallyReplaceable") {
var executablePath = CommandLine.arguments[0]
executablePath.removeLast(4)
expectEqual(1, test())
// Now, test with the module containing the replacements.

#if os(Linux)
let h = dlopen(target_library_name("Module2"), RTLD_NOW)
#elseif os(Windows)
let h = LoadLibraryA(target_library_name("Module2"))
#else
let h = dlopen(executablePath+target_library_name("Module2"), RTLD_NOW)
#endif

expectEqual(2, test())

#if os(Linux)
dlclose(h!)
#elseif os(Windows)
#else
dlclose(h)
#endif

#if os(Linux)
_ = dlopen(target_library_name("Module2"), RTLD_NOW)
#elseif os(Windows)
#else
_ = dlopen(executablePath+target_library_name("Module2"), RTLD_NOW)
#endif
expectEqual(2, test())

}

runAllTests()