Skip to content

Commit

Permalink
report: disable js stack when no context is entered
Browse files Browse the repository at this point in the history
There are no guarantees that the JS stack can be generated when no
context is entered.

PR-URL: #48495
Fixes: nodejs/node-v8#250
Refs: https://chromium-review.googlesource.com/c/v8/v8/+/4582948
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
legendecas authored and RafaelGSS committed Jul 3, 2023
1 parent 52de27b commit bdcc037
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static void PrintJavaScriptErrorStack(JSONWriter* writer,
Isolate* isolate,
Local<Value> error,
const char* trigger);
static void PrintEmptyJavaScriptStack(JSONWriter* writer);
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
const char* trigger);
Expand Down Expand Up @@ -184,6 +185,10 @@ static void WriteNodeReport(Isolate* isolate,

// Report V8 Heap and Garbage Collector information
PrintGCStatistics(&writer, isolate);
} else {
writer.json_objectstart("javascriptStack");
PrintEmptyJavaScriptStack(&writer);
writer.json_objectend(); // the end of 'javascriptStack'
}

// Report native stack backtrace
Expand Down Expand Up @@ -452,8 +457,9 @@ static void PrintEmptyJavaScriptStack(JSONWriter* writer) {
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
const char* trigger) {
// Can not capture the stacktrace when the isolate is in a OOM state.
if (!strcmp(trigger, "OOMError")) {
// Can not capture the stacktrace when the isolate is in a OOM state or no
// context is entered.
if (!strcmp(trigger, "OOMError") || !isolate->InContext()) {
PrintEmptyJavaScriptStack(writer);
return;
}
Expand Down
12 changes: 6 additions & 6 deletions test/addons/report-api/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const tmpdir = require('../../common/tmpdir');
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
const addon = require(binding);

function myAddonMain(method, { hasIsolate, hasEnv }) {
function myAddonMain(method, { hasContext, hasEnv }) {
tmpdir.refresh();
process.report.directory = tmpdir.path;

Expand All @@ -27,10 +27,10 @@ function myAddonMain(method, { hasIsolate, hasEnv }) {
const content = require(report);

// Check that the javascript stack is present.
if (hasIsolate) {
if (hasContext) {
assert.strictEqual(content.javascriptStack.stack.findIndex((frame) => frame.match('myAddonMain')), 0);
} else {
assert.strictEqual(content.javascriptStack, undefined);
assert.strictEqual(content.javascriptStack.message, 'No stack.');
}

if (hasEnv) {
Expand All @@ -45,9 +45,9 @@ const methods = [
['triggerReportNoIsolate', false, false],
['triggerReportEnv', true, true],
['triggerReportNoEnv', false, false],
['triggerReportNoContext', true, false],
['triggerReportNoContext', false, false],
['triggerReportNewContext', true, false],
];
for (const [method, hasIsolate, hasEnv] of methods) {
myAddonMain(method, { hasIsolate, hasEnv });
for (const [method, hasContext, hasEnv] of methods) {
myAddonMain(method, { hasContext, hasEnv });
}
8 changes: 4 additions & 4 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ function validateContent(report, fields = []) {

function _validateContent(report, fields = []) {
const isWindows = process.platform === 'win32';
const isJavaScriptThreadReport = report.javascriptStack != null;
const isJavaScriptThreadReport = report.javascriptHeap != null;

// Verify that all sections are present as own properties of the report.
const sections = ['header', 'nativeStack', 'libuv', 'environmentVariables',
'sharedObjects', 'resourceUsage', 'workers'];
const sections = ['header', 'nativeStack', 'javascriptStack', 'libuv',
'environmentVariables', 'sharedObjects', 'resourceUsage', 'workers'];
if (!isWindows)
sections.push('userLimits');

if (report.uvthreadResourceUsage)
sections.push('uvthreadResourceUsage');

if (isJavaScriptThreadReport)
sections.push('javascriptStack', 'javascriptHeap');
sections.push('javascriptHeap');

checkForUnknownFields(report, sections);
sections.forEach((section) => {
Expand Down

0 comments on commit bdcc037

Please sign in to comment.