Skip to content

[orc][mach-o] Unlock the JITDylib state mutex during +load #105333

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
Aug 23, 2024
Merged
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
12 changes: 9 additions & 3 deletions compiler-rt/lib/orc/macho_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ class MachOPlatformRuntimeState {
static Error registerEHFrames(span<const char> EHFrameSection);
static Error deregisterEHFrames(span<const char> EHFrameSection);

static Error registerObjCRegistrationObjects(JITDylibState &JDS);
static Error
registerObjCRegistrationObjects(std::unique_lock<std::mutex> &JDStatesLock,
JITDylibState &JDS);
static Error runModInits(std::unique_lock<std::mutex> &JDStatesLock,
JITDylibState &JDS);

Expand Down Expand Up @@ -1059,7 +1061,7 @@ Error MachOPlatformRuntimeState::deregisterEHFrames(
}

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

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

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

for (void *RegObjBase : RegObjBases)
_objc_load_image(nullptr, reinterpret_cast<mach_header *>(RegObjBase));
JDStatesLock.lock();

return Error::success();
}
Expand Down Expand Up @@ -1218,7 +1224,7 @@ Error MachOPlatformRuntimeState::dlopenInitialize(
}

// Initialize this JITDylib.
if (auto Err = registerObjCRegistrationObjects(JDS))
if (auto Err = registerObjCRegistrationObjects(JDStatesLock, JDS))
return Err;
if (auto Err = runModInits(JDStatesLock, JDS))
return Err;
Expand Down
Loading