Skip to content
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

Fix 6899: console.* not working when running out of stack space #6907

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
this.id = id;
this.exports = {};
this.loaded = false;
this.loading = false;
}

NativeModule._source = process.binding('natives');
Expand All @@ -368,7 +369,7 @@
}

var cached = NativeModule.getCached(id);
if (cached) {
if (cached && (cached.loaded || cached.loading)) {
return cached.exports;
}

Expand Down Expand Up @@ -432,14 +433,20 @@
var source = NativeModule.getSource(this.id);
source = NativeModule.wrap(source);

var fn = runInThisContext(source, {
filename: this.filename,
lineOffset: 0,
displayErrors: true
});
fn(this.exports, NativeModule.require, this, this.filename);
this.loading = true;

try {
var fn = runInThisContext(source, {
filename: this.filename,
lineOffset: 0,
displayErrors: true
});
fn(this.exports, NativeModule.require, this, this.filename);

this.loaded = true;
this.loaded = true;
} finally {
this.loading = false;
}
};

NativeModule.prototype.cache = function() {
Expand Down
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
22 changes: 22 additions & 0 deletions test/message/console_low_stack_space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
// copy console accessor because requiring ../common touches it
const consoleDescriptor = Object.getOwnPropertyDescriptor(global, 'console');
delete global.console;
global.console = {};

require('../common');

function a() {
try {
return a();
} catch (e) {
const console = consoleDescriptor.get();
if (console.log) {
console.log('Hello, World!');
} else {
throw e;
}
}
}

a();
1 change: 1 addition & 0 deletions test/message/console_low_stack_space.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
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);