From ea0113e6efee661b886f575a0d56b860862fab49 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 2 Oct 2026 00:40:33 +0100 Subject: [PATCH] rc: a store through an interface hands the count over, as a store into the field does Under -mm=rc, `i.o = x` (an interface's field) neither retained x nor released what the field held: isOwningSlot did not know an InterfaceSymbolRefOp's reference. The replaced value leaked (a loop doing it grew ~100 bytes an iteration), and the stored one kept no reference of its own, so a `new` stored that way was freed at the end of the storing function while the field still pointed at it - read back later, it gave whatever was allocated over it. A field reached through an interface belongs to the object behind it - a class instance releases it, a literal's box retained it when boxed - so isOwningSlot now answers yes for it (isOwnedInterfaceFieldSlot). 00owned_interfaces.ts gains both shapes (class instance, boxed literal); they allocate over freed blocks with fillers kept alive, as churn()'s freed-at-once fillers reused one block and missed it. Both fail without the fix. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 20 +++++++- .../test/tester/tests/00owned_interfaces.ts | 51 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 05cba451b..e32637ddc 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -1091,7 +1091,25 @@ class MLIRGenImpl { return isOwnedLocalSlot(reference) || isCapturedVariableCell(reference) || isCapturedCellSlot(reference) || isOwnedGlobalSlot(location, reference) || - isOwnedFieldSlot(location, reference) || isOwnedElementSlot(location, reference); + isOwnedFieldSlot(location, reference) || isOwnedElementSlot(location, reference) || + isOwnedInterfaceFieldSlot(location, reference); + } + + // A field reached through an interface: it belongs to the object behind the interface, which releases + // it - a class instance through its release routine, an object literal through the block it was boxed + // into, which retained its fields (castTupleToInterface). So `i.o = x` hands the count over as `obj.o = x` + // does. It did not: the old value was never released, and the new one kept no reference of its own, so + // its block was freed at the end of the producer's block while the field still pointed at it. + bool isOwnedInterfaceFieldSlot(mlir::Location location, mlir::Value reference) + { + auto interfaceSymbolRefOp = reference.getDefiningOp(); + if (!interfaceSymbolRefOp) + { + return false; + } + + auto refType = dyn_cast(reference.getType()); + return refType && mth.ownsHeapMemory(location, refType.getElementType()); } // One `using` declaration's `[Symbol.dispose]()`, at one scope exit. diff --git a/tslang/test/tester/tests/00owned_interfaces.ts b/tslang/test/tester/tests/00owned_interfaces.ts index f7250ef23..196fe39eb 100644 --- a/tslang/test/tester/tests/00owned_interfaces.ts +++ b/tslang/test/tester/tests/00owned_interfaces.ts @@ -156,6 +156,55 @@ function interfacesInALoop() { return total + last.v.x; } +// A store through an interface hands the count over as a store into the field itself does: the +// object behind the interface owns the field. It did not - the replaced value was never released, +// and the stored one kept no reference of its own, so it was freed at the end of the storing +// function while the field still pointed at it. +class Box { + v: Vec; + + constructor(v: Vec) { + this.v = v; + } +} + +// churn() frees each filler at once, so it can keep reusing one block; keeping them makes the +// allocations spread over the blocks just freed, which is what finds this one +let fillers: Vec[] = []; + +function churnKeeping() { + for (let i = 0; i < 256; i++) { + fillers.push(new Vec(999)); + } +} + +function storeThroughInterface(): Box { + let box = new Box(new Vec(1)); + let h: Holder = box; + h.v = new Vec(31); + return box; +} + +function storedThroughInterfaceSurvives() { + let box = storeThroughInterface(); + churnKeeping(); + + return box.v.x; +} + +function storeIntoBoxedLiteral(): Holder { + let h: Holder = { v: new Vec(1) }; + h.v = new Vec(37); + return h; +} + +function storedIntoBoxedLiteralSurvives() { + let h = storeIntoBoxedLiteral(); + churnKeeping(); + + return h.v.x; +} + function main() { assert(returnedThroughInterface() == 7, "a literal boxed as an interface survives its maker"); assert(argumentNeverBound() == 11, "an interface argument nothing bound survives the call"); @@ -165,6 +214,8 @@ function main() { assert(classThroughInterface() == 46, "a class behind an interface keeps its own owner"); assert(nullInterfaceIsInert() == 29, "a null interface releases nothing"); assert(interfacesInALoop() == 44, "a loop's interface temporaries do not disturb what it carries out"); + assert(storedThroughInterfaceSurvives() == 31, "a value stored through an interface is owned by the object"); + assert(storedIntoBoxedLiteralSurvives() == 37, "a value stored through an interface into a boxed literal is owned by it"); print("done."); }