Skip to content

Commit

Permalink
vm: don't abort process when stack space runs out
Browse files Browse the repository at this point in the history
Make less assumptions about what objects will be available when
vm context creation or error message printing fail because V8
runs out of JS stack space.

Ref: nodejs#6899
PR-URL: nodejs#6907
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
addaleax authored and Fishrock123 committed May 30, 2016
1 parent 7a29c6f commit 4afbcf5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,8 @@ void AppendExceptionLine(Environment* env,
// sourceline to 78 characters, and we end up not providing very much
// useful debugging info to the user if we remove 62 characters.

int start = message->GetStartColumn(env->context()).FromJust();
int end = message->GetEndColumn(env->context()).FromJust();
int start = message->GetStartColumn(env->context()).FromMaybe(0);
int end = message->GetEndColumn(env->context()).FromMaybe(0);

char arrow[1024];
int max_off = sizeof(arrow) - 2;
Expand Down
14 changes: 10 additions & 4 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ class ContextifyContext {

Local<Context> ctx = Context::New(env->isolate(), nullptr, object_template);

CHECK(!ctx.IsEmpty());
if (ctx.IsEmpty()) {
env->ThrowError("Could not instantiate context");
return Local<Context>();
}

ctx->SetSecurityToken(env->context()->GetSecurityToken());

// We need to tie the lifetime of the sandbox object with the lifetime of
Expand Down Expand Up @@ -632,9 +636,11 @@ class ContextifyScript : public BaseObject {
env->arrow_message_private_symbol());

Local<Value> arrow;
if (!(maybe_value.ToLocal(&arrow) &&
arrow->IsString() &&
stack->IsString())) {
if (!(maybe_value.ToLocal(&arrow) && arrow->IsString())) {
return;
}

if (stack.IsEmpty() || !stack->IsString()) {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-vm-low-stack-space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
require('../common');
const assert = require('assert');
const vm = require('vm');

function a() {
try {
return a();
} catch (e) {
// Throw an exception as near to the recursion-based RangeError as possible.
return vm.runInThisContext('() => 42')();
}
}

assert.strictEqual(a(), 42);

function b() {
try {
return b();
} catch (e) {
// This writes a lot of noise to stderr, but it still works.
return vm.runInNewContext('() => 42')();
}
}

assert.strictEqual(b(), 42);

0 comments on commit 4afbcf5

Please sign in to comment.