Skip to content

[Support] Re-raise external signals #125854

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
Feb 8, 2025

Conversation

guy-david
Copy link
Contributor

Otherwise, the handler "swallows" the signal and the process continues to execute. While this use case is peculiar, ignoring these signals entirely seems more odd.

@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-llvm-support

Author: Guy David (guy-david)

Changes

Otherwise, the handler "swallows" the signal and the process continues to execute. While this use case is peculiar, ignoring these signals entirely seems more odd.


Full diff: https://github.com/llvm/llvm-project/pull/125854.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Unix/Signals.inc (+11-6)
diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 2e7b467a14bbe2..388fff7d5c9dcf 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -80,7 +80,8 @@
 
 using namespace llvm;
 
-static void SignalHandler(int Sig);     // defined below.
+static void SignalHandler(int Sig, siginfo_t *Info,
+                          void *Context); // defined below.
 static void InfoSignalHandler(int Sig); // defined below.
 
 using SignalHandlerFunctionType = void (*)();
@@ -313,8 +314,8 @@ static void RegisterHandlers() { // Not signal-safe.
 
     switch (Kind) {
     case SignalKind::IsKill:
-      NewHandler.sa_handler = SignalHandler;
-      NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
+      NewHandler.sa_sigaction = SignalHandler;
+      NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK | SA_SIGINFO;
       break;
     case SignalKind::IsInfo:
       NewHandler.sa_handler = InfoSignalHandler;
@@ -370,7 +371,7 @@ void sys::CleanupOnSignal(uintptr_t Context) {
 }
 
 // The signal handler that runs.
-static void SignalHandler(int Sig) {
+static void SignalHandler(int Sig, siginfo_t *Info, void *Context) {
   // Restore the signal behavior to default, so that the program actually
   // crashes when we return and the signal reissues.  This also ensures that if
   // we crash in our signal handler that the program will terminate immediately
@@ -401,6 +402,11 @@ static void SignalHandler(int Sig) {
     }
   }
 
+  // Signal sent from another process, do not assume that continuing the
+  // execution would re-raise it.
+  if (Info->si_pid != getpid())
+    raise(Sig);
+
   // Otherwise if it is a fault (like SEGV) run any handler.
   llvm::sys::RunSignalHandlers();
 
@@ -468,8 +474,7 @@ void llvm::sys::AddSignalHandler(sys::SignalHandlerCallback FnPtr,
 
 #if ENABLE_BACKTRACES && defined(HAVE_BACKTRACE) &&                            \
     (defined(__linux__) || defined(__FreeBSD__) ||                             \
-     defined(__FreeBSD_kernel__) || defined(__NetBSD__) ||                     \
-     defined(__OpenBSD__) || defined(__DragonFly__))
+     defined(__FreeBSD_kernel__) || defined(__NetBSD__))
 struct DlIteratePhdrData {
   void **StackTrace;
   int depth;

@aganea
Copy link
Member

aganea commented Feb 5, 2025

I am maybe not the best one to review this. Perhaps @dwblaikie or @MaskRay can help finding another reviewer?

@guy-david guy-david force-pushed the users/guy-david/reraise-external-signals branch from f84d18a to 83f5235 Compare February 5, 2025 13:59
@guy-david guy-david requested review from MaskRay and dwblaikie and removed request for brad0 February 5, 2025 13:59
@guy-david guy-david force-pushed the users/guy-david/reraise-external-signals branch from 83f5235 to a737a57 Compare February 5, 2025 15:01
@dwblaikie
Copy link
Collaborator

Yeah, not sure, sorry - bit outside my wheelhouse. I wonder if @d0k knows about this or knows someone who does...

@MaskRay
Copy link
Member

MaskRay commented Feb 7, 2025

Could you state your motivation behind this change?

Makes sense. I don't know why SIGQUIT in on the Kill list instead of Int list. Nevertheless, Kill/Int are handled similarly. If an external process raises SIGSEGV/SIGQUIT, the default action is termination. With the handler installed, the process should also terminate (by reraising the signal) after removing temporary files and invoking some handlers (for example, LLVM may dump Stack dump:)

It's strange that this

if (Info->si_pid != getpid())
    raise(Sig);

pattern isn't that common.

(@cracauer for https://www.cons.org/cracauer/sigint.html :) This post probably could mention signals other than SIGINT/SIGQUIT )

(@emaste)

Otherwise, the handler "swallows" the signal and the process continues
to execute. While this use case is peculiar, ignoring these signals
entirely seems more odd.
@guy-david guy-david force-pushed the users/guy-david/reraise-external-signals branch from 3162a7f to c70f573 Compare February 8, 2025 10:09
@guy-david
Copy link
Contributor Author

Thanks!
I was looking at the symbolizer to understand why some symbols are not printed nicely during a crash. I ran llc and signaled a SIGSEGV using kill, which to my surprise didn't make it stop.

@guy-david guy-david merged commit ef23ba7 into main Feb 8, 2025
8 checks passed
@guy-david guy-david deleted the users/guy-david/reraise-external-signals branch February 8, 2025 13:16
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Otherwise, the handler "swallows" the signal and the process continues
to execute. While this use case is peculiar, ignoring these signals
entirely seems more odd.
fhahn pushed a commit to swiftlang/llvm-project that referenced this pull request Feb 15, 2025
Otherwise, the handler "swallows" the signal and the process continues
to execute. While this use case is peculiar, ignoring these signals
entirely seems more odd.
fhahn added a commit to swiftlang/llvm-project that referenced this pull request Feb 15, 2025
[cherry-pick] [Support] Re-raise external signals (llvm#125854)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants