Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
43 changes: 18 additions & 25 deletions src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5456,16 +5456,13 @@
// The iter callback can run user code (a nested value's inspect.custom, Proxy
// traps) that adds properties to this object, rehashing the PropertyTable
// mid-walk (use-after-free). Same for getters resolved through
// getIfPropertyExists. Collect the entries with no side effects first, then
// do the getter calls and callbacks on the snapshot.
struct SnapshottedProperty {
Identifier key;
unsigned attributes;
};
WTF::Vector<SnapshottedProperty, 16> snapshot;
// Parallel to `snapshot`; keeps the collected values visible to GC. Empty
// slots mark prototype properties that are fetched after the walk.
MarkedArgumentBuffer snapshotValues;
// getIfPropertyExists. Collect the keys with no side effects first, then
// resolve the values and run the callbacks on the snapshot. Values are
// looked up by name on the live object so a property the callback
// overwrites reports its current value and a deleted one is re-resolved
// through the prototype chain (the shadowed inherited value if there is
// one, otherwise omitted), like the slow path below.
Comment thread
robobun marked this conversation as resolved.
Resolved
WTF::Vector<Identifier, 16> snapshot;

structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool {
if ((entry.attributes() & (PropertyAttribute::Function)) == 0 && (entry.attributes() & (PropertyAttribute::Builtin)) != 0) {
Expand All @@ -5486,27 +5483,23 @@
}
visitedProperties.append(Identifier::fromUid(vm, prop));

JSC::JSValue propertyValue = JSValue();
if (objectToUse == object) {
propertyValue = objectToUse->getDirect(entry.offset());
if (!propertyValue)
return true;
}

snapshot.append({ Identifier::fromUid(vm, prop), entry.attributes() });
snapshotValues.appendWithCrashOnOverflow(propertyValue);
snapshot.append(Identifier::fromUid(vm, prop));
return true;
});

for (size_t i = 0; i < snapshot.size(); i++) {
const auto& snapshotted = snapshot[i];
auto* prop = snapshotted.key.impl();
for (const Identifier& property : snapshot) {
auto* prop = property.impl();
ZigString key = toZigString(prop);

JSC::JSValue propertyValue = snapshotValues.at(i);
if (!propertyValue || propertyValue.isGetterSetter() && !((snapshotted.attributes & PropertyAttribute::Accessor) != 0)) {
JSC::JSValue propertyValue = JSValue();
unsigned attributes = 0;
if (objectToUse == object) {
propertyValue = objectToUse->getDirect(vm, property, attributes);
}

if (!propertyValue || propertyValue.isGetterSetter() && !((attributes & PropertyAttribute::Accessor) != 0)) {
propertyValue = objectToUse->getIfPropertyExists(globalObject, prop);
}

Check warning on line 5502 in src/jsc/bindings/bindings.cpp

View check run for this annotation

Claude / Claude Code Review

Fast-path fallback invokes prototype accessor when shadowing own key is deleted mid-format; slow path shows [Getter]

When the deleted own key is shadowing a **prototype accessor** (rather than a data property), the fast-path fallback at `getIfPropertyExists` performs a full `[[Get]]` and *invokes* the getter — printing its return value and firing side effects — while the slow path checks `slot.isAccessor()` and prints `[Getter]` without calling it. Consider routing the fallback through `getPropertySlot` + `isAccessor()` like the slow path, and adding an accessor-prototype variant to the new "agrees across form
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved

// Ignore exceptions due to getters.
CLEAR_IF_EXCEPTION(scope);
Expand All @@ -5517,7 +5510,7 @@
anyHits = true;
JSC::EnsureStillAliveScope ensureStillAliveScope(propertyValue);

bool isPrivate = prop->isSymbol() && snapshotted.key.isPrivateName();
bool isPrivate = prop->isSymbol() && property.isPrivateName();

if (isPrivate && !JSC::Options::showPrivateScriptsInStackTraces())
continue;
Expand Down
90 changes: 88 additions & 2 deletions test/js/bun/util/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,48 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => {
const s = Bun.inspect(p);
console.log("custom delete:", s.includes("z: 1"));
}
{
// Mutating a sibling that has not been formatted yet: only the key
// list is snapshotted, values resolve live. An overwrite shows the
// new value, a delete is omitted, and an accessor redefinition shows
// [Getter] (matches Node and the slow path).
const p = makeParent();
let fired = 0;
p.a = { [custom]() { if (!fired++) p.z = 777; return "a"; } };
p.z = 1;
console.log("overwrite later:", Bun.inspect(p).includes("z: 777"));
}
{
const p = makeParent();
let fired = 0;
p.a = { [custom]() { if (!fired++) delete p.z; return "a"; } };
p.z = 1;
console.log("delete later:", !Bun.inspect(p).includes("z:"));
}
{
const p = makeParent();
let fired = 0;
p.a = { [custom]() { if (!fired++) { delete p.z; p.z = 999; } return "a"; } };
p.z = 1;
console.log("delete+readd later:", Bun.inspect(p).includes("z: 999"));
}
{
const p = makeParent();
let fired = 0;
p.a = { [custom]() { if (!fired++) Object.defineProperty(p, "z", { get() { return 5; }, enumerable: true, configurable: true }); return "a"; } };
p.z = 1;
console.log("redefine later:", Bun.inspect(p).includes("z: [Getter]"));
}
{
// Deleting an own key that shadows an inherited one: the value is
// re-resolved through the prototype chain.
const p = Object.create({ z: "inherited" });
for (let i = 0; i < 8; i++) p["k" + i] = i;
let fired = 0;
p.a = { [custom]() { if (!fired++) delete p.z; return "a"; } };
p.z = 1;
console.log("shadowed delete:", Bun.inspect(p).includes('z: "inherited"'));
}
{
// A getter on a built-in subclass (Map.size) is another way the
// formatter runs user code for a nested value.
Expand All @@ -866,7 +908,8 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => {
}
{
// Allocation churn + GC inside the hook, with object-valued siblings
// formatted afterwards: catches a snapshot that is invisible to GC.
// formatted afterwards: the snapshotted keys and the values resolved
// after the walk must survive the collection.
const p = makeParent();
let fired = 0;
p.a = { [custom]() {
Expand Down Expand Up @@ -904,7 +947,7 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => {
"custom add: true",
// console.log dump of the mutated parent: properties added by the
// inspect.custom hook mid-format are not shown (the walk snapshots
// the properties up front, like Node).
// the key list up front, like Node).
"{",
" k0: 0,",
" k1: 1,",
Expand All @@ -918,6 +961,11 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => {
" z: 1,",
"}",
"custom delete: true",
"overwrite later: true",
"delete later: true",
"delete+readd later: true",
"redefine later: true",
"shadowed delete: true",
"map size getter: true true",
"prototype walk: true true",
"gc churn: true",
Expand All @@ -928,3 +976,41 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => {
expect(exitCode).toBe(0);
});
});

// Mutating a not-yet-formatted sibling from an inspect.custom hook must
// produce the same output on every formatter path: the fast structure walk, the
// slow path (forced by a getter on the parent), and the sorted/ordered walk.
describe("mid-format sibling mutation agrees across formatter paths", () => {
const custom = Symbol.for("nodejs.util.inspect.custom");

function make(mutate, { slow = false, proto = null } = {}) {
const p = proto ? Object.create(proto) : {};
for (let i = 0; i < 8; i++) p["k" + i] = i;
if (slow) Object.defineProperty(p, "g", { get() { return 0; }, enumerable: true, configurable: true });
let fired = 0;
p.a = { [custom]() { if (!fired++) mutate(p); return "a"; } };
p.z = 1;
return p;
}

const zLine = s => s.match(/z: [^,}\n]*/)?.[0] ?? "omitted";

it.each([
["overwrite", p => void (p.z = 777), null, "z: 777"],
["delete", p => void delete p.z, null, "omitted"],
["delete and re-add", p => void (delete p.z, (p.z = 999)), null, "z: 999"],
[
"redefine as getter",
p => void Object.defineProperty(p, "z", { get: () => 5, enumerable: true, configurable: true }),
null,
"z: [Getter]",
],
["delete a shadowing key", p => void delete p.z, { z: "inherited" }, 'z: "inherited"'],
])("%s", (_name, mutate, proto, expected) => {
expect({
fast: zLine(Bun.inspect(make(mutate, { proto }))),
slow: zLine(Bun.inspect(make(mutate, { proto, slow: true }))),
sorted: zLine(Bun.inspect(make(mutate, { proto }), { sorted: true })),
}).toEqual({ fast: expected, slow: expected, sorted: expected });
});
});
Loading