-
Notifications
You must be signed in to change notification settings - Fork 10.5k
DNM: stdlib: Add backward deployment versions for the dynamic-replacement runtime functions - version 2 #25360
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===--- DynamicReplaceable.h - dynamic replaceable support -----*- C++ -*-===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Swift runtime support for dynamic replaceable functions. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_RUNTIME_DYNAMIC_REPLACEABLE_H | ||
#define SWIFT_RUNTIME_DYNAMIC_REPLACEABLE_H | ||
|
||
#include "swift/Runtime/Config.h" | ||
|
||
namespace swift { | ||
|
||
/// Loads the replacement function pointer from \p ReplFnPtr and returns the | ||
/// replacement function if it should be called. | ||
/// Returns null if the original function (which is passed in \p CurrFn) should | ||
/// be called. | ||
SWIFT_RUNTIME_EXPORT | ||
char *swift_getFunctionReplacement(char **ReplFnPtr, char *CurrFn); | ||
|
||
/// Returns the original function of a replaced function, which is loaded from | ||
/// \p OrigFnPtr. | ||
/// This function is called from a replacement function to call the original | ||
/// function. | ||
SWIFT_RUNTIME_EXPORT | ||
char *swift_getOrigOfReplaceable(char **OrigFnPtr); | ||
|
||
} // end namespace swift | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//===--- 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/Exclusivity.h" | ||
#include "swift/Runtime/Metadata.h" | ||
#include "../runtime/ThreadLocalStorage.h" | ||
|
||
using namespace swift; | ||
|
||
// Mirror the SwiftTLSContext, which is defined in the runtime. | ||
// This is a hack: the layout must match with SwiftTLSContext defined in | ||
// Exclusivity.cpp. Fortunately it contains only a single pointer (the head of | ||
// the access list). | ||
class SwiftTLSContext { | ||
void *AccessHead; | ||
|
||
public: | ||
void *getHead() const { return AccessHead; } | ||
|
||
void setExtraBit() { | ||
AccessHead = (void *)((intptr_t)AccessHead | 1); | ||
} | ||
|
||
void clearExtraBit() { | ||
AccessHead = (void *)((intptr_t)AccessHead & ~1); | ||
} | ||
|
||
bool testExtraBit() const { | ||
return (bool)((intptr_t)AccessHead & 1); | ||
} | ||
}; | ||
|
||
__attribute__((visibility("hidden"), weak)) | ||
extern "C" char *swift_getFunctionReplacement(char **ReplFnPtr, char *CurrFn) { | ||
char *ReplFn = *ReplFnPtr; | ||
char *RawReplFn = ReplFn; | ||
|
||
if (RawReplFn == CurrFn) | ||
return nullptr; | ||
|
||
void *tlsStorage = SWIFT_THREAD_GETSPECIFIC(SWIFT_RUNTIME_TLS_KEY); | ||
if (tlsStorage) { | ||
SwiftTLSContext *ctx = static_cast<SwiftTLSContext*>(tlsStorage); | ||
|
||
if (ctx->testExtraBit()) { | ||
ctx->clearExtraBit(); | ||
return nullptr; | ||
} | ||
} | ||
return ReplFn; | ||
} | ||
|
||
__attribute__((visibility("hidden"), weak)) | ||
extern "C" char *swift_getOrigOfReplaceable(char **OrigFnPtr) { | ||
void *tlsStorage = SWIFT_THREAD_GETSPECIFIC(SWIFT_RUNTIME_TLS_KEY); | ||
if (!tlsStorage) { | ||
ValueBuffer dummyAccess; | ||
int dummyValue = 0; | ||
swift_beginAccess(&dummyValue, &dummyAccess, ExclusivityFlags::Read, nullptr); | ||
swift_endAccess(&dummyAccess); | ||
tlsStorage = SWIFT_THREAD_GETSPECIFIC(SWIFT_RUNTIME_TLS_KEY); | ||
if (!tlsStorage) | ||
abort(); | ||
} | ||
SwiftTLSContext *ctx = static_cast<SwiftTLSContext*>(tlsStorage); | ||
void *pr = ctx->getHead(); | ||
ctx->setExtraBit(); | ||
char *OrigFn = *OrigFnPtr; | ||
return OrigFn; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm really wary about baking this into apps. These are pretty simple functions, which means there isn't much potential for breaking changes, but I'm paranoid that something would come up eventually.
Can we find a way to make these call the versions from
libswiftCore.dylib
if they exist? ARCLite accomplishes this using a terrifying technique that involves patching entries in the lazy pointer section at runtime. We could potentially do that, although it is scary.Another option might be to use
dlsym
in these functions to try to find one inlibswiftCore
, and then call through to it if it exists. That would add a bit more overhead but it wouldn't be too horrible.We could cut down on that overhead by having the static library include global variable function pointers that point to these functions, similar to how
swift_retain
etc. work. When code is compiled for older targets, have it call through the function pointers. The function pointers can point to something that decides whether to use the version fromlibswiftCore
or the version here, then writes the appropriate function pointer back to the global variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Andy pointed out that the current implementation would bake in the ABI of how we do things.
Jordan and I talked yesterday and he proposed marking the functions in the libswiftCore.dylib weak. Something like
The compatibility archive can then check whether the weak symbols is bound and use it if it is otherwise fall back to a compatibility version. This way you always get the current implementation and only fall back if there is not implementation. The fallback does not become ABI this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, this is like my
dlsym
idea except much better.