Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 35 additions & 1 deletion signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,41 @@ check_stack_overflow(int sig, const void *addr)
# else
# define CHECK_STACK_OVERFLOW_() check_stack_overflow(sig, FAULT_ADDRESS)
# endif
# define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS

/* Only the si_code values whose siginfo_t carries a sender pid; SI_TIMER and
* SI_SIGIO overlay si_tid/si_band at si_pid's offset. */
static bool
is_user_generated(const siginfo_t *info)
{
int code = info->si_code;
if (code == SI_USER) return true;
# ifdef SI_QUEUE
if (code == SI_QUEUE) return true;
# endif
# ifdef SI_TKILL
if (code == SI_TKILL) return true;
# endif
return code == 0;
}

static const char *
signal_sender_message(const siginfo_t *info)
{
/* check_reserved_signal_ lets only the first fatal signal get this far. */
static char buf[64];

if (info && is_user_generated(info)) {
rb_pid_t pid = info->si_pid;
if (pid == getpid()) return "";
/* si_pid is 0 when the sender is not visible in our PID namespace. */
if (pid == 0) return " (sent by another process)";
snprintf(buf, sizeof(buf), " (sent by pid %"PRI_PIDT_PREFIX"d)", pid);
return buf;
}
return "";
}

# define MESSAGE_FAULT_ADDRESS " at %p%s", FAULT_ADDRESS, signal_sender_message(info)
# define SIGNAL_FROM_USER_P() ((info)->si_code == SI_USER)
# define CHECK_STACK_OVERFLOW() (SIGNAL_FROM_USER_P() ? (void)0 : CHECK_STACK_OVERFLOW_())
# endif
Expand Down
29 changes: 29 additions & 0 deletions test/ruby/test_rubyoptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,35 @@ def test_crash_report_pipe_script
end
end

# The sender pid is only reported for a signal another process sent us.
def abrt_crash_report(code)
Dir.mktmpdir("ruby_crash_report") do |dir|
IO.popen([{"RUBY_CRASH_REPORT" => "abrt.log", "RUBY_ON_BUG" => nil},
EnvUtil.rubybin, "--disable-gems", "-e", "STDOUT.sync = true; puts; #{code}"],
chdir: dir, err: File::NULL, rlimit_core: 0) do |child|
child.gets
yield child if block_given?
end
break File.read(File.join(dir, "abrt.log"))
end
end

def test_crash_report_sender_pid
omit "needs siginfo" unless (macos? || linux?)

report = abrt_crash_report("sleep") {|child| Process.kill(:ABRT, child.pid)}
assert_include(report, "[BUG] Aborted")
assert_include(report, "(sent by pid #{Process.pid})")
end

def test_crash_report_no_sender_pid_when_self_inflicted
omit "needs siginfo" unless (macos? || linux?)

report = abrt_crash_report("Process.kill(:ABRT, $$)")
assert_include(report, "[BUG] Aborted")
assert_not_include(report, "sent by pid")
end

def test_DATA
Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
t.puts "puts DATA.read.inspect"
Expand Down