src: reset SIGSEGV handler before crashing
Without this, we would re-enter the signal handler immediately after re-raising the signal, leading to an infinite loop. PR-URL: https://github.com/nodejs/node/pull/27775 Refs: https://github.com/nodejs/node/pull/27246 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
e6b3ec3d3c
commit
e256204776
@ -487,6 +487,12 @@ void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) {
|
||||
if (prev != nullptr) {
|
||||
prev(signo, info, ucontext);
|
||||
} else {
|
||||
// Reset to the default signal handler, i.e. cause a hard crash.
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
sa.sa_handler = SIG_DFL;
|
||||
CHECK_EQ(sigaction(signo, &sa, nullptr), 0);
|
||||
|
||||
ResetStdio();
|
||||
raise(signo);
|
||||
}
|
||||
|
@ -63,6 +63,12 @@ static void Abort(const FunctionCallbackInfo<Value>& args) {
|
||||
Abort();
|
||||
}
|
||||
|
||||
// For internal testing only, not exposed to userland.
|
||||
static void CauseSegfault(const FunctionCallbackInfo<Value>& args) {
|
||||
// This should crash hard all platforms.
|
||||
*static_cast<void**>(nullptr) = nullptr;
|
||||
}
|
||||
|
||||
static void Chdir(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
CHECK(env->owns_process_state());
|
||||
@ -405,6 +411,7 @@ static void InitializeProcessMethods(Local<Object> target,
|
||||
env->SetMethod(target, "_debugProcess", DebugProcess);
|
||||
env->SetMethod(target, "_debugEnd", DebugEnd);
|
||||
env->SetMethod(target, "abort", Abort);
|
||||
env->SetMethod(target, "causeSegfault", CauseSegfault);
|
||||
env->SetMethod(target, "chdir", Chdir);
|
||||
}
|
||||
|
||||
|
23
test/abort/test-signal-handler.js
Normal file
23
test/abort/test-signal-handler.js
Normal file
@ -0,0 +1,23 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
if (common.isWindows)
|
||||
common.skip('No signals on Window');
|
||||
|
||||
const assert = require('assert');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
// Test that a hard crash does not cause an endless loop.
|
||||
|
||||
if (process.argv[2] === 'child') {
|
||||
const { internalBinding } = require('internal/test/binding');
|
||||
const { causeSegfault } = internalBinding('process_methods');
|
||||
|
||||
causeSegfault();
|
||||
} else {
|
||||
const child = spawnSync(process.execPath,
|
||||
['--expose-internals', __filename, 'child'],
|
||||
{ stdio: 'inherit' });
|
||||
// FreeBSD and macOS use SIGILL for the kind of crash we're causing here.
|
||||
assert(child.signal === 'SIGSEGV' || child.signal === 'SIGILL',
|
||||
`child.signal = ${child.signal}`);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user