-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
src: fix segfault handling/RegisterSignalHandler #27775
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
97ea9f0
src: do not use posix feature macro in node.h
addaleax 543b838
src: reset SIGSEGV handler before crashing
addaleax cdd38f4
src: forbid reset_handler for SIGSEGV handling
addaleax 5dd1210
test: add addon tests for `RegisterSignalHandler()`
addaleax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
|
|
||
| // This is a sibling test to test/addons/register-signal-handler/ | ||
|
|
||
| process.env.ALLOW_CRASHES = true; | ||
| require('../addons/register-signal-handler/test'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #ifndef _WIN32 | ||
| #include <node.h> | ||
| #include <v8.h> | ||
| #include <uv.h> | ||
| #include <assert.h> | ||
| #include <unistd.h> | ||
|
|
||
| using v8::Boolean; | ||
| using v8::FunctionCallbackInfo; | ||
| using v8::Int32; | ||
| using v8::Value; | ||
|
|
||
| void Handler(int signo, siginfo_t* siginfo, void* ucontext) { | ||
| char signo_char = signo; | ||
| int written; | ||
| do { | ||
| written = write(1, &signo_char, 1); // write() is signal-safe. | ||
| } while (written == -1 && errno == EINTR); | ||
| assert(written == 1); | ||
| } | ||
|
|
||
| void RegisterSignalHandler(const FunctionCallbackInfo<Value>& args) { | ||
| assert(args[0]->IsInt32()); | ||
| assert(args[1]->IsBoolean()); | ||
|
|
||
| int32_t signo = args[0].As<Int32>()->Value(); | ||
| bool reset_handler = args[1].As<Boolean>()->Value(); | ||
|
|
||
| node::RegisterSignalHandler(signo, Handler, reset_handler); | ||
| } | ||
|
|
||
| NODE_MODULE_INIT() { | ||
| NODE_SET_METHOD(exports, "registerSignalHandler", RegisterSignalHandler); | ||
| } | ||
|
|
||
| #endif // _WIN32 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| 'targets': [ | ||
| { | ||
| 'target_name': 'binding', | ||
| 'sources': [ 'binding.cc' ], | ||
| 'includes': ['../common.gypi'], | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| 'use strict'; | ||
| const common = require('../../common'); | ||
| if (common.isWindows) | ||
| common.skip('No RegisterSignalHandler() on Windows'); | ||
|
|
||
| const assert = require('assert'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const { signals } = require('os').constants; | ||
| const { spawnSync } = require('child_process'); | ||
|
|
||
| const bindingPath = path.resolve( | ||
| __dirname, 'build', common.buildType, 'binding.node'); | ||
|
|
||
| if (!fs.existsSync(bindingPath)) | ||
| common.skip('binding not built yet'); | ||
|
|
||
| const binding = require(bindingPath); | ||
|
|
||
| if (process.argv[2] === 'child') { | ||
| const signo = +process.argv[3]; | ||
| const reset = process.argv[4] === 'reset'; | ||
| const count = +process.argv[5]; | ||
|
|
||
| binding.registerSignalHandler(signo, reset); | ||
| for (let i = 0; i < count; i++) | ||
| process.kill(process.pid, signo); | ||
| return; | ||
| } | ||
|
|
||
| for (const raiseSignal of [ 'SIGABRT', 'SIGSEGV' ]) { | ||
| const signo = signals[raiseSignal]; | ||
| for (const { reset, count, stderr, code, signal } of [ | ||
| { reset: true, count: 1, stderr: [signo], code: 0, signal: null }, | ||
| { reset: true, count: 2, stderr: [signo], code: null, signal: raiseSignal }, | ||
| { reset: false, count: 1, stderr: [signo], code: 0, signal: null }, | ||
| { reset: false, count: 2, stderr: [signo, signo], code: 0, signal: null } | ||
| ]) { | ||
| // We do not want to generate core files when running this test as an | ||
| // addon test. We require this file as an abort test as well, though, | ||
| // with ALLOW_CRASHES set. | ||
| if (signal !== null && !process.env.ALLOW_CRASHES) | ||
| continue; | ||
| // reset_handler does not work with SIGSEGV. | ||
| if (reset && signo === signals.SIGSEGV) | ||
| continue; | ||
|
|
||
| const args = [__filename, 'child', signo, reset ? 'reset' : '', count]; | ||
| console.log(`Running: node ${args.join(' ')}`); | ||
| const result = spawnSync( | ||
| process.execPath, args, { stdio: ['inherit', 'pipe', 'inherit'] }); | ||
| assert.strictEqual(result.status, code); | ||
| assert.strictEqual(result.signal, signal); | ||
| assert.deepStrictEqual([...result.stdout], stderr); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.