Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/jsc/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3757,6 +3757,7 @@ pub fn takeHeapSnapshot(
) callconv(jsc.conv) void {
// TODO: this does an extra JSONStringify and we don't need it to!
var snapshot: [1]JSValue = .{globalThis.generateHeapSnapshot()};
if (snapshot[0] == .zero or globalThis.hasException()) return;
ConsoleObject.messageWithTypeAndLevel(undefined, MessageType.Log, MessageLevel.Debug, globalThis, &snapshot, 1);
}
pub fn timeStamp(
Expand Down
5 changes: 2 additions & 3 deletions src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3931,9 +3931,8 @@ JSC::EncodedJSValue JSC__JSGlobalObject__generateHeapSnapshot(JSC::JSGlobalObjec
snapshotBuilder.buildSnapshot();

WTF::String jsonString = snapshotBuilder.json();
JSC::EncodedJSValue result = JSC::JSValue::encode(JSONParse(globalObject, jsonString));
scope.releaseAssertNoException();
return result;
RETURN_IF_EXCEPTION(scope, {});
RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSONParse(globalObject, jsonString)));
}

JSC::VM* JSC__JSGlobalObject__vm(JSC::JSGlobalObject* arg0) { return &arg0->vm(); };
Expand Down
8 changes: 3 additions & 5 deletions src/jsc/modules/BunJSCModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,7 @@ JSC_DEFINE_HOST_FUNCTION(functionSamplingProfilerStackTraces,
createError(globalObject, "Sampling profiler was never started"_s)));

WTF::String jsonString = vm.samplingProfiler()->stackTracesAsJSON()->toJSONString();
JSC::EncodedJSValue result = JSC::JSValue::encode(JSONParse(globalObject, jsonString));
scope.releaseAssertNoException();
return result;
RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSONParse(globalObject, jsonString)));
}

JSC_DECLARE_HOST_FUNCTION(functionGetRandomSeed);
Expand Down Expand Up @@ -797,9 +795,9 @@ JSC_DEFINE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging,

jsonString = snapshotBuilder.json();
}
scope.releaseAssertNoException();
RETURN_IF_EXCEPTION(scope, {});

return JSValue::encode(JSONParse(globalObject, WTF::move(jsonString)));
RELEASE_AND_RETURN(scope, JSValue::encode(JSONParse(globalObject, WTF::move(jsonString))));
}

JSC_DEFINE_HOST_FUNCTION(functionSerialize,
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/api/html_rewriter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,15 @@ fn HandlerCallback(
if (fail) {
this.global.bunVM().unhandledRejection(this.global, promise.result(this.global.vm()), promise.asValue());
}
if (scope.exception()) |exc| {
const exc_value = JSValue.fromCell(exc);
if (this.global.bunVM().unhandled_pending_rejection_to_capture) |err_ptr| {
err_ptr.* = exc_value;
exc_value.protect();
}
scope.clearException();
return true;
}
return fail;
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/js/bun/util/take-heap-snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";

test("console.takeHeapSnapshot does not crash with exotic objects on the heap", async () => {
const code = `
const vm = require("vm");
// Put a variety of objects on the heap that exercise class-name resolution
// during the snapshot's JSON serialization.
globalThis.sr = new ShadowRealm();
globalThis.p = new Proxy({}, {
getOwnPropertyDescriptor() { ArrayBuffer(); },
get() { ArrayBuffer(); },
});
globalThis.child = Object.create(globalThis.p);
globalThis.ctx = vm.createContext({});
vm.runInContext("globalThis.x = {}", globalThis.ctx);
console.takeHeapSnapshot();
console.error("ok");
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", code],
env: bunEnv,
stdout: "ignore",
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toContain("ok");
expect(exitCode).toBe(0);
});
Loading