From 7ddfad7a2a4f67611d23aa3bb9be9f0006cfd78a Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Wed, 6 Dec 2023 15:33:51 +0100 Subject: [PATCH] Use the same reserved signals as CRuby and add specs for it --- doc/user/compatibility.md | 14 +++++++------- spec/ruby/core/signal/trap_spec.rb | 8 ++++++++ src/main/ruby/truffleruby/core/signal.rb | 5 ++--- test/mri/excludes/TestSignal.rb | 1 - 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/doc/user/compatibility.md b/doc/user/compatibility.md index 28862f705c34..8f4e87633d17 100644 --- a/doc/user/compatibility.md +++ b/doc/user/compatibility.md @@ -141,15 +141,15 @@ Error message strings will sometimes differ from MRI, as these are not generally ### Signals -The set of signals that TruffleRuby can handle is different from MRI. -When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does, as well as a few that MRI does not. -The only signals that can't be trapped are `KILL`, `STOP`, and `VTALRM`. +First, `KILL` and `STOP` can never be trapped, per POSIX (`man 2 signal`). +Some signals are reserved on CRuby, and they are also reserved on TruffleRuby, because trapping those would cause all sorts of problems: `SEGV`, `BUS`, `ILL`, `FPE` and `VTALRM`. + +When using the native configuration, TruffleRuby allows trapping all the same signals that MRI does. Consequently, any signal handling code that runs on MRI can run on TruffleRuby without modification in the native configuration. -However, when run on the JVM, TruffleRuby is unable to trap `USR1` or `QUIT`, as these signals are reserved by the JVM. -In such a case `trap(:USR1) {}` will raise an `ArgumentError`. -Any code that relies on being able to trap those signals will need to fall back to another available signal. -Additionally, `FPE`, `ILL`, `KILL`, `SEGV`, `STOP`, and `VTALRM` cannot be trapped, but these signals are also unavailable on MRI. +However, when run on the JVM, TruffleRuby is unable to trap `QUIT`, as this signal is reserved by the JVM. +In such a case `trap(:QUIT) {}` will raise an `ArgumentError`. +Any code that relies on being able to trap this signal will need to fall back to another available signal. When TruffleRuby is run as part of a polyglot application, any signals that are handled by another language become unavailable for TruffleRuby to trap. diff --git a/spec/ruby/core/signal/trap_spec.rb b/spec/ruby/core/signal/trap_spec.rb index b3186cda92b2..e238da3ca296 100644 --- a/spec/ruby/core/signal/trap_spec.rb +++ b/spec/ruby/core/signal/trap_spec.rb @@ -264,6 +264,14 @@ end end + %w[SEGV BUS ILL FPE VTALRM].each do |signal| + it "raises ArgumentError for SIG#{signal} which is reserved by Ruby" do + -> { + Signal.trap(signal, -> {}) + }.should raise_error(ArgumentError, "can't trap reserved signal: SIG#{signal}") + end + end + it "allows to register a handler for all known signals, except reserved signals for which it raises ArgumentError" do out = ruby_exe(fixture(__FILE__, "trap_all.rb"), args: "2>&1") out.should == "OK\n" diff --git a/src/main/ruby/truffleruby/core/signal.rb b/src/main/ruby/truffleruby/core/signal.rb index a4df7de9dcfe..89fc880290a8 100644 --- a/src/main/ruby/truffleruby/core/signal.rb +++ b/src/main/ruby/truffleruby/core/signal.rb @@ -84,9 +84,8 @@ def self.trap(signal, handler = nil, &block) signame = self.signame(number) - if signame == 'VTALRM' - # Used internally to unblock native calls, like MRI - raise ArgumentError, "can't trap reserved signal: SIGVTALRM" + if %w[SEGV BUS ILL FPE VTALRM].include?(signame) + raise ArgumentError, "can't trap reserved signal: SIG#{signame}" end handler ||= block diff --git a/test/mri/excludes/TestSignal.rb b/test/mri/excludes/TestSignal.rb index e7ec10609fb5..5e54c53beb2c 100644 --- a/test/mri/excludes/TestSignal.rb +++ b/test/mri/excludes/TestSignal.rb @@ -9,4 +9,3 @@ exclude :test_signal2, "needs investigation" exclude :test_trap, "needs investigation" exclude :test_trap_system_default, "needs investigation" -exclude :test_reserved_signal, "needs investigation"