Skip to content

DNM: stdlib: Add backward deployment versions for the dynamic-replacement runtime functions. #25340

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions stdlib/public/Compatibility50/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set(swift_runtime_linker_flags ${SWIFT_RUNTIME_CORE_LINK_FLAGS})

add_swift_target_library(swiftCompatibility50 TARGET_LIBRARY STATIC INSTALL_WITH_SHARED
ProtocolConformance.cpp
DynamicReplaceable.cpp
Overrides.cpp
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags}
LINK_FLAGS ${swift_runtime_linker_flags}
Expand Down
63 changes: 63 additions & 0 deletions stdlib/public/Compatibility50/DynamicReplaceable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===--- ProtocolConformance.cpp - Swift protocol conformance checking ----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Runtime support for dynamic replaceable functions.
//
// This implementation is intended to be backward-deployed into Swift 5.0
// runtimes.
//
//===----------------------------------------------------------------------===//

#include "Overrides.h"
#include "swift/Runtime/Once.h"
#include "../runtime/ThreadLocalStorage.h"

using namespace swift;

// The thread-local key must be weak so that it is shared between different
// binaries (e.g. shared libraries).
__attribute__((weak)) __swift_thread_key_t _swift_dr_key;
__attribute__((weak)) swift_once_t _swift_dr_Predicate;

static void createSwiftThreadKey(void *) {
int result = SWIFT_THREAD_KEY_CREATE(&_swift_dr_key, nullptr);
if (result != 0)
abort();
}

static __swift_thread_key_t &getTLSKey() {
swift_once(&_swift_dr_Predicate, createSwiftThreadKey, nullptr);
return _swift_dr_key;
}

__attribute__((visibility("hidden"), weak))
extern "C" char *swift_getFunctionReplacement(char **ReplFnPtr, char *CurrFn) {
char *ReplFn = *ReplFnPtr;
char *RawReplFn = ReplFn;

if (RawReplFn == CurrFn)
return nullptr;

__swift_thread_key_t key = getTLSKey();
if ((intptr_t)SWIFT_THREAD_GETSPECIFIC(key) != 0) {
SWIFT_THREAD_SETSPECIFIC(key, (void *)0);
return nullptr;
}
return ReplFn;
}

__attribute__((visibility("hidden"), weak))
extern "C" char *swift_getOrigOfReplaceable(char **OrigFnPtr) {
char *OrigFn = *OrigFnPtr;
SWIFT_THREAD_SETSPECIFIC(getTLSKey(), (void *)-1);
return OrigFn;
}
1 change: 1 addition & 0 deletions validation-test/stdlib/MicroStdlib/Inputs/RuntimeStubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ void swift_allocBox(){}
void swift_getWitnessTable(void) {}
void swift_getObjCClassMetadata(void) {}
void swift_addNewDSOImage(void) {}
void swift_once() {}