Skip to content

Commit b4b7baa

Browse files
authored
Merge pull request #72873 from al45tair/eng/PR-125727356-6.0
[Runtime] Disable prespecialized metadata if we have overridden images.
2 parents b42f4a7 + be3eef8 commit b4b7baa

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

include/swift/Runtime/LibPrespecialized.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct LibPrespecializedData {
4848
const LibPrespecializedData<InProcess> *getLibPrespecializedData();
4949
Metadata *getLibPrespecializedMetadata(const TypeContextDescriptor *description,
5050
const void *const *arguments);
51+
void libPrespecializedImageLoaded();
5152

5253
} // namespace swift
5354

stdlib/public/runtime/LibPrespecialized.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "swift/Runtime/EnvironmentVariables.h"
1818
#include "swift/Runtime/Metadata.h"
1919

20+
#include <atomic>
21+
2022
#if SWIFT_STDLIB_HAS_DLADDR && __has_include(<dlfcn.h>)
2123
#include <dlfcn.h>
2224
#define USE_DLOPEN 1
@@ -28,6 +30,8 @@
2830

2931
using namespace swift;
3032

33+
static std::atomic<bool> disablePrespecializedMetadata = false;
34+
3135
static const LibPrespecializedData<InProcess> *findLibPrespecialized() {
3236
if (!runtime::environment::SWIFT_DEBUG_ENABLE_LIB_PRESPECIALIZED())
3337
return nullptr;
@@ -157,10 +161,23 @@ isPotentialPrespecializedPointer(LibPrespecializedState &prespecialized,
157161

158162
static bool disableForValidation = false;
159163

164+
void
165+
swift::libPrespecializedImageLoaded() {
166+
#if DYLD_GET_SWIFT_PRESPECIALIZED_DATA_DEFINED
167+
// A newly loaded image might have caused us to load images that are
168+
// overriding images in the shared cache. If we do that, turn off
169+
// prespecialized metadata.
170+
if (dyld_shared_cache_some_image_overridden())
171+
disablePrespecializedMetadata.store(true, std::memory_order_release);
172+
#endif
173+
}
174+
160175
Metadata *
161176
swift::getLibPrespecializedMetadata(const TypeContextDescriptor *description,
162177
const void *const *arguments) {
163-
if (disableForValidation)
178+
if (SWIFT_UNLIKELY(
179+
disableForValidation
180+
|| disablePrespecializedMetadata.load(std::memory_order_acquire)))
164181
return nullptr;
165182

166183
auto &prespecialized = LibPrespecialized.get();

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/Runtime/Debug.h"
3030
#include "swift/Runtime/EnvironmentVariables.h"
3131
#include "swift/Runtime/HeapObject.h"
32+
#include "swift/Runtime/LibPrespecialized.h"
3233
#include "swift/Runtime/Metadata.h"
3334
#include "swift/Strings.h"
3435
#include "swift/Threading/Mutex.h"
@@ -347,6 +348,8 @@ void swift::addImageTypeMetadataRecordBlockCallbackUnsafe(
347348
assert(recordsSize % sizeof(TypeMetadataRecord) == 0
348349
&& "weird-sized type metadata section?!");
349350

351+
libPrespecializedImageLoaded();
352+
350353
// If we have a section, enqueue the type metadata for lookup.
351354
auto recordBytes = reinterpret_cast<const char *>(records);
352355
auto recordsBegin

0 commit comments

Comments
 (0)