Skip to content

Commit

Permalink
Avoid concurrency hazard in signal handler registration
Browse files Browse the repository at this point in the history
Several static functions from the signal API can be invoked
simultaneously; RemoveFileOnSignal for instance can be called indirectly
by multiple parallel loadModule() invocations, which might lead to
the assertion:

Assertion failed: (NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) && "Out of space for signal handlers!"),
  function RegisterHandler, file /llvm/lib/Support/Unix/Signals.inc, line 105.

RemoveFileOnSignal calls RegisterHandlers(), which isn't currently
mutex protected, leading to the behavior above. This potentially affect
a few other users of RegisterHandlers() too.

rdar://problem/30381224

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298871 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
bcardosolopes authored and MarijnS95 committed Jun 14, 2023
1 parent c8c6347 commit 9fc8942
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/Support/Unix/Signals.inc
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ static void RegisterHandler(int Signal) {
}

static void RegisterHandlers() {
// We need to dereference the signals mutex during handler registration so
// that we force its construction. This is to prevent the first use being
// during handling an actual signal because you can't safely call new in a
// signal handler.
*SignalsMutex;

sys::SmartScopedLock<true> Guard(*SignalsMutex);

// If the handlers are already registered, we're done.
if (NumRegisteredSignals != 0) return;

Expand Down Expand Up @@ -164,7 +160,7 @@ static void RemoveFilesToRemove() {
// super-user permissions.
if (!S_ISREG(buf.st_mode))
continue;

// Otherwise, remove the file. We ignore any errors here as there is nothing
// else we can do.
unlink(path);
Expand Down

0 comments on commit 9fc8942

Please sign in to comment.