From b8881508cde6de4d2fd96a56b881f3fdd82a6d12 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 8 Aug 2026 02:13:27 +0000 Subject: [PATCH 1/6] inspect: resolve property values live after the mutation-safe key snapshot Follow-up to #37169, which fixed the use-after-free by snapshotting both keys and values before running formatter callbacks. Freezing the values diverged from Node, from previous Bun releases, and from the slow path in the same function: a hook that overwrote a later-ordered sibling printed the stale value, and a deleted property was printed anyway. Keep the key-list snapshot (that is what the use-after-free fix requires) but resolve each value by name on the live structure in the post-walk loop, the same way the prototype path already did. Deleted properties are omitted, overwritten ones report the current value, and accessor redefinitions show [Getter], matching Node and the slow path. This also removes the MarkedArgumentBuffer value buffer, which is no longer needed. --- src/jsc/bindings/bindings.cpp | 44 ++++++++++++++------------------ test/js/bun/util/inspect.test.js | 41 +++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/jsc/bindings/bindings.cpp b/src/jsc/bindings/bindings.cpp index 59fd92da7d05..4ccecf9b3381 100644 --- a/src/jsc/bindings/bindings.cpp +++ b/src/jsc/bindings/bindings.cpp @@ -5456,16 +5456,12 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: // 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 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 structure so a property the callback + // deletes is omitted and one it overwrites reports the current value, + // like the slow path below and Node. + WTF::Vector snapshot; structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { if ((entry.attributes() & (PropertyAttribute::Function)) == 0 && (entry.attributes() & (PropertyAttribute::Builtin)) != 0) { @@ -5486,25 +5482,23 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: } 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) + continue; + } + + if (!propertyValue || propertyValue.isGetterSetter() && !((attributes & PropertyAttribute::Accessor) != 0)) { propertyValue = objectToUse->getIfPropertyExists(globalObject, prop); } @@ -5517,7 +5511,7 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: 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; diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 5c91f3dbe05f..4e473661d5f7 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -843,6 +843,38 @@ 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]")); + } { // A getter on a built-in subclass (Map.size) is another way the // formatter runs user code for a nested value. @@ -866,7 +898,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]() { @@ -904,7 +937,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,", @@ -918,6 +951,10 @@ 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", "map size getter: true true", "prototype walk: true true", "gc churn: true", From 0b6d0ed92187db96ff612d2f94a3a85262b2d028 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 8 Aug 2026 03:02:30 +0000 Subject: [PATCH 2/6] inspect: resolve a deleted shadowing key through the prototype chain A getDirect miss for a key deleted mid-format skipped the property entirely, but the slow path resolves it through the prototype chain and prints the inherited value it was shadowing. Fall through to getIfPropertyExists instead: truly-gone keys are still omitted, shadowed ones match the slow path and previous releases. Also pin the overwrite/delete/re-add/redefine/shadow behavior across the fast, slow, and sorted formatter paths with a test that runs on every build. --- src/jsc/bindings/bindings.cpp | 9 +++--- test/js/bun/util/inspect.test.js | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/jsc/bindings/bindings.cpp b/src/jsc/bindings/bindings.cpp index 4ccecf9b3381..b5d5a466e675 100644 --- a/src/jsc/bindings/bindings.cpp +++ b/src/jsc/bindings/bindings.cpp @@ -5458,9 +5458,10 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: // mid-walk (use-after-free). Same for getters resolved through // 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 structure so a property the callback - // deletes is omitted and one it overwrites reports the current value, - // like the slow path below and Node. + // 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. WTF::Vector snapshot; structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { @@ -5494,8 +5495,6 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: unsigned attributes = 0; if (objectToUse == object) { propertyValue = objectToUse->getDirect(vm, property, attributes); - if (!propertyValue) - continue; } if (!propertyValue || propertyValue.isGetterSetter() && !((attributes & PropertyAttribute::Accessor) != 0)) { diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 4e473661d5f7..172aacfe8063 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -875,6 +875,16 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => { 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. @@ -955,6 +965,7 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => { "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", @@ -965,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 }); + }); +}); From 19a466d2a540619dfc7294fa535b0919df546a34 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 8 Aug 2026 03:04:18 +0000 Subject: [PATCH 3/6] Tighten the snapshot comment --- src/jsc/bindings/bindings.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/jsc/bindings/bindings.cpp b/src/jsc/bindings/bindings.cpp index b5d5a466e675..a8ba81f1f620 100644 --- a/src/jsc/bindings/bindings.cpp +++ b/src/jsc/bindings/bindings.cpp @@ -5453,15 +5453,11 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: bool anyHits = false; JSC::JSObject* objectToUse = prototypeObject.getObject(); - // 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 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. + // The iter callback and getIfPropertyExists can run user code that mutates + // this object, rehashing the PropertyTable mid-walk (use-after-free). + // Snapshot the keys with no side effects, then resolve values by name on + // the live object and invoke the callbacks; mutation between callbacks + // behaves like the slow path below. WTF::Vector snapshot; structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { From d481497c7624d5c6bb10509aedbb1bfb8eac3739 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 8 Aug 2026 03:05:48 +0000 Subject: [PATCH 4/6] Shorten the snapshot comment to one sentence --- src/jsc/bindings/bindings.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/jsc/bindings/bindings.cpp b/src/jsc/bindings/bindings.cpp index a8ba81f1f620..18ba2c690e2d 100644 --- a/src/jsc/bindings/bindings.cpp +++ b/src/jsc/bindings/bindings.cpp @@ -5453,11 +5453,8 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: bool anyHits = false; JSC::JSObject* objectToUse = prototypeObject.getObject(); - // The iter callback and getIfPropertyExists can run user code that mutates - // this object, rehashing the PropertyTable mid-walk (use-after-free). - // Snapshot the keys with no side effects, then resolve values by name on - // the live object and invoke the callbacks; mutation between callbacks - // behaves like the slow path below. + // The iter callback runs user code that can rehash this PropertyTable mid-walk + // (use-after-free), so collect the keys first and resolve values by name after. WTF::Vector snapshot; structure->forEachProperty(vm, [&](const PropertyTableEntry& entry) -> bool { From ec6e9516dcc193efbb3c7ae3bf4388d47c5c0820 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 8 Aug 2026 03:08:09 +0000 Subject: [PATCH 5/6] [autofix.ci] apply automated fixes --- test/js/bun/util/inspect.test.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 172aacfe8063..3b7075905b26 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -986,9 +986,21 @@ describe("mid-format sibling mutation agrees across formatter paths", () => { 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 }); + 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.a = { + [custom]() { + if (!fired++) mutate(p); + return "a"; + }, + }; p.z = 1; return p; } From bdd3758de51a831c2eb0b8e1e898c12462e0e6e5 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 8 Aug 2026 03:15:08 +0000 Subject: [PATCH 6/6] inspect: preserve inherited accessors when re-resolving a deleted key The deleted-key fallback used getIfPropertyExists, which invokes an inherited getter and prints its result; the slow path keeps the accessor and prints [Getter]. Resolve through a PropertySlot with the slow path's accessor handling instead, so the getter is not called and all paths agree. --- src/jsc/bindings/bindings.cpp | 15 ++++++++++++++- test/js/bun/util/inspect.test.js | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/jsc/bindings/bindings.cpp b/src/jsc/bindings/bindings.cpp index 18ba2c690e2d..cb6f65384040 100644 --- a/src/jsc/bindings/bindings.cpp +++ b/src/jsc/bindings/bindings.cpp @@ -5490,7 +5490,20 @@ static void JSC__JSValue__forEachPropertyImpl(JSC::EncodedJSValue JSValue0, JSC: propertyValue = objectToUse->getDirect(vm, property, attributes); } - if (!propertyValue || propertyValue.isGetterSetter() && !((attributes & PropertyAttribute::Accessor) != 0)) { + if (!propertyValue) { + // Deleted mid-format, or a prototype-walk property: resolve through + // the prototype chain preserving accessors, like the slow path. + PropertySlot slot(objectToUse, PropertySlot::InternalMethodType::Get); + bool found = objectToUse->getPropertySlot(globalObject, property, slot); + CLEAR_IF_EXCEPTION(scope); + if (found) { + if (slot.isAccessor()) { + propertyValue = slot.isCacheableGetter() ? slot.getPureResult() : slot.getterSetter(); + } else { + propertyValue = slot.getValue(globalObject, property); + } + } + } else if (propertyValue.isGetterSetter() && !((attributes & PropertyAttribute::Accessor) != 0)) { propertyValue = objectToUse->getIfPropertyExists(globalObject, prop); } diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 3b7075905b26..bc00014cc129 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -885,6 +885,16 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => { p.z = 1; console.log("shadowed delete:", Bun.inspect(p).includes('z: "inherited"')); } + { + // Deleting an own key that shadows an inherited getter must show the + // accessor, not invoke it. + const p = Object.create({ get z() { throw new Error("must not invoke"); } }); + for (let i = 0; i < 8; i++) p["k" + i] = i; + let fired = 0; + p.a = { [custom]() { if (!fired++) delete p.z; return "a"; } }; + Object.defineProperty(p, "z", { value: 1, enumerable: true, configurable: true, writable: true }); + console.log("shadowed getter delete:", Bun.inspect(p).includes("z: [Getter]")); + } { // A getter on a built-in subclass (Map.size) is another way the // formatter runs user code for a nested value. @@ -966,6 +976,7 @@ describe.skipIf(!isASAN)("object mutated while being formatted", () => { "delete+readd later: true", "redefine later: true", "shadowed delete: true", + "shadowed getter delete: true", "map size getter: true true", "prototype walk: true true", "gc churn: true", @@ -1001,7 +1012,8 @@ describe("mid-format sibling mutation agrees across formatter paths", () => { return "a"; }, }; - p.z = 1; + // defineProperty, not `p.z = 1`: a getter-only inherited z would reject the assignment. + Object.defineProperty(p, "z", { value: 1, enumerable: true, configurable: true, writable: true }); return p; } @@ -1018,6 +1030,16 @@ describe("mid-format sibling mutation agrees across formatter paths", () => { "z: [Getter]", ], ["delete a shadowing key", p => void delete p.z, { z: "inherited" }, 'z: "inherited"'], + [ + "delete a key shadowing an inherited getter", + p => void delete p.z, + { + get z() { + throw new Error("inspect must not invoke the inherited getter"); + }, + }, + "z: [Getter]", + ], ])("%s", (_name, mutate, proto, expected) => { expect({ fast: zLine(Bun.inspect(make(mutate, { proto }))),