Skip to content

[Runtime] Disable prespecialized metadata if we have overridden images. #72873

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
merged 1 commit into from
Apr 9, 2024
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
1 change: 1 addition & 0 deletions include/swift/Runtime/LibPrespecialized.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct LibPrespecializedData {
const LibPrespecializedData<InProcess> *getLibPrespecializedData();
Metadata *getLibPrespecializedMetadata(const TypeContextDescriptor *description,
const void *const *arguments);
void libPrespecializedImageLoaded();

} // namespace swift

Expand Down
19 changes: 18 additions & 1 deletion stdlib/public/runtime/LibPrespecialized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/Metadata.h"

#include <atomic>

#if SWIFT_STDLIB_HAS_DLADDR && __has_include(<dlfcn.h>)
#include <dlfcn.h>
#define USE_DLOPEN 1
Expand All @@ -27,6 +29,8 @@

using namespace swift;

static std::atomic<bool> disablePrespecializedMetadata = false;

static const LibPrespecializedData<InProcess> *findLibPrespecialized() {
if (!runtime::environment::SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED())
return nullptr;
Expand Down Expand Up @@ -91,10 +95,23 @@ static bool hasNonTypeGenericArguments(const TypeContextDescriptor *description)

static bool disableForValidation = false;

void
swift::libPrespecializedImageLoaded() {
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
// A newly loaded image might have caused us to load images that are
// overriding images in the shared cache. If we do that, turn off
// prespecialized metadata.
if (dyld_shared_cache_some_image_overridden())
disablePrespecializedMetadata.store(true, std::memory_order_release);
#endif
}

Metadata *
swift::getLibPrespecializedMetadata(const TypeContextDescriptor *description,
const void *const *arguments) {
if (disableForValidation)
if (SWIFT_UNLIKELY(
disableForValidation
|| disablePrespecializedMetadata.load(std::memory_order_acquire)))
return nullptr;

auto *data = getLibPrespecializedData();
Expand Down
3 changes: 3 additions & 0 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/EnvironmentVariables.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/LibPrespecialized.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Strings.h"
#include "swift/Threading/Mutex.h"
Expand Down Expand Up @@ -347,6 +348,8 @@ void swift::addImageTypeMetadataRecordBlockCallbackUnsafe(
assert(recordsSize % sizeof(TypeMetadataRecord) == 0
&& "weird-sized type metadata section?!");

libPrespecializedImageLoaded();

// If we have a section, enqueue the type metadata for lookup.
auto recordBytes = reinterpret_cast<const char *>(records);
auto recordsBegin
Expand Down