Browse Source

vm: don't abort process when stack space runs out

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: https://github.com/nodejs/node/issues/6899
PR-URL: https://github.com/nodejs/node/pull/6907
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
v7.x
Anna Henningsen 9 years ago
parent
commit
4bd410bbbe
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 4
      src/node.cc
  2. 14
      src/node_contextify.cc
  3. 26
      test/parallel/test-vm-low-stack-space.js

4
src/node.cc

@ -1521,8 +1521,8 @@ void AppendExceptionLine(Environment* env,
// sourceline to 78 characters, and we end up not providing very much // sourceline to 78 characters, and we end up not providing very much
// useful debugging info to the user if we remove 62 characters. // useful debugging info to the user if we remove 62 characters.
int start = message->GetStartColumn(env->context()).FromJust(); int start = message->GetStartColumn(env->context()).FromMaybe(0);
int end = message->GetEndColumn(env->context()).FromJust(); int end = message->GetEndColumn(env->context()).FromMaybe(0);
char arrow[1024]; char arrow[1024];
int max_off = sizeof(arrow) - 2; int max_off = sizeof(arrow) - 2;

14
src/node_contextify.cc

@ -205,7 +205,11 @@ class ContextifyContext {
Local<Context> ctx = Context::New(env->isolate(), nullptr, object_template); 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()); ctx->SetSecurityToken(env->context()->GetSecurityToken());
// We need to tie the lifetime of the sandbox object with the lifetime of // We need to tie the lifetime of the sandbox object with the lifetime of
@ -632,9 +636,11 @@ class ContextifyScript : public BaseObject {
env->arrow_message_private_symbol()); env->arrow_message_private_symbol());
Local<Value> arrow; Local<Value> arrow;
if (!(maybe_value.ToLocal(&arrow) && if (!(maybe_value.ToLocal(&arrow) && arrow->IsString())) {
arrow->IsString() && return;
stack->IsString())) { }
if (stack.IsEmpty() || !stack->IsString()) {
return; return;
} }

26
test/parallel/test-vm-low-stack-space.js

@ -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);
Loading…
Cancel
Save