From e04fa702c3e019463cde4c42576a0d5b671c1f85 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 20 May 2016 22:55:37 +0200 Subject: [PATCH] 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 --- src/node_contextify.cc | 6 +++++- test/parallel/test-vm-low-stack-space.js | 26 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-vm-low-stack-space.js diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 8235dcd49e093c..d50b4b568b5da5 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -205,7 +205,11 @@ class ContextifyContext { Local ctx = Context::New(env->isolate(), nullptr, object_template); - CHECK(!ctx.IsEmpty()); + if (ctx.IsEmpty()) { + env->ThrowError("Could not instantiate context"); + return Local(); + } + ctx->SetSecurityToken(env->context()->GetSecurityToken()); // We need to tie the lifetime of the sandbox object with the lifetime of diff --git a/test/parallel/test-vm-low-stack-space.js b/test/parallel/test-vm-low-stack-space.js new file mode 100644 index 00000000000000..7c1313d47c1ad9 --- /dev/null +++ b/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);