Skip to content

Commit fa089ef

Browse files
authored
[orc][mach-o] Unlock the JITDylib state mutex during +load (llvm#105333)
Similar to what was already done for static initializers, we need to unlock the state mutext when calling out to libobjc to run +load methods in case they cause us to reenter the runtime, which was previously deadlocking. No test for now, because we don't have any code paths in llvm-jitlink itself that could lead to this deadlock. If we interpose calls to dlopen to go back to the JIT in the future then calling dlopen from a +load is the easiest way to reproduce this. rdar://133430490
1 parent 960a210 commit fa089ef

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

compiler-rt/lib/orc/macho_platform.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ class MachOPlatformRuntimeState {
367367
static Error registerEHFrames(span<const char> EHFrameSection);
368368
static Error deregisterEHFrames(span<const char> EHFrameSection);
369369

370-
static Error registerObjCRegistrationObjects(JITDylibState &JDS);
370+
static Error
371+
registerObjCRegistrationObjects(std::unique_lock<std::mutex> &JDStatesLock,
372+
JITDylibState &JDS);
371373
static Error runModInits(std::unique_lock<std::mutex> &JDStatesLock,
372374
JITDylibState &JDS);
373375

@@ -1059,7 +1061,7 @@ Error MachOPlatformRuntimeState::deregisterEHFrames(
10591061
}
10601062

10611063
Error MachOPlatformRuntimeState::registerObjCRegistrationObjects(
1062-
JITDylibState &JDS) {
1064+
std::unique_lock<std::mutex> &JDStatesLock, JITDylibState &JDS) {
10631065
ORC_RT_DEBUG(printdbg("Registering Objective-C / Swift metadata.\n"));
10641066

10651067
std::vector<char *> RegObjBases;
@@ -1074,13 +1076,17 @@ Error MachOPlatformRuntimeState::registerObjCRegistrationObjects(
10741076
"Could not register Objective-C / Swift metadata: _objc_map_images / "
10751077
"_objc_load_image not found");
10761078

1079+
// Release the lock while calling out to libobjc in case +load methods cause
1080+
// reentering the orc runtime.
1081+
JDStatesLock.unlock();
10771082
std::vector<char *> Paths;
10781083
Paths.resize(RegObjBases.size());
10791084
_objc_map_images(RegObjBases.size(), Paths.data(),
10801085
reinterpret_cast<mach_header **>(RegObjBases.data()));
10811086

10821087
for (void *RegObjBase : RegObjBases)
10831088
_objc_load_image(nullptr, reinterpret_cast<mach_header *>(RegObjBase));
1089+
JDStatesLock.lock();
10841090

10851091
return Error::success();
10861092
}
@@ -1218,7 +1224,7 @@ Error MachOPlatformRuntimeState::dlopenInitialize(
12181224
}
12191225

12201226
// Initialize this JITDylib.
1221-
if (auto Err = registerObjCRegistrationObjects(JDS))
1227+
if (auto Err = registerObjCRegistrationObjects(JDStatesLock, JDS))
12221228
return Err;
12231229
if (auto Err = runModInits(JDStatesLock, JDS))
12241230
return Err;

0 commit comments

Comments
 (0)