From 3f6d4273b5b63fdf354c2d27b9b47a029e4ceeb1 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 17:30:43 +0000 Subject: [PATCH 1/2] -mm=own: two fuzzer finds - keeper literal elements, parameters given a local's value A round of mm_fuzz.py (400 programs, seeds 70000-70399) found 10 own failures and no rc ones. Two causes: - A keeper (an append-only array global) makes what is pushed onto it immortal, and reads of its elements own nothing. `const GA: N[] = [mk(5)]` was taken for one, but the elements its literal is made with were never made immortal, so a local given `GA[0]` freed it when its block ended (9 of the 10). An array literal of anything but constants makes no keeper now. - `p = n` with `p` a parameter stored `n`'s value in a slot that owns nothing, with no retain for own to judge, so `n = mk()` then freed what `p` read. Own reported an assigned parameter only while explaining a retain; a store of a value that owns a block into a parameter's slot is reported now, unless the value is another parameter's, which the caller holds for the whole call. Co-Authored-By: Claude Opus 5.5 --- .../lib/TypeScript/OwnershipInferencePass.cpp | 47 +++++++++++++++++ .../TypeScript/OwnershipSignatureKeepers.cpp | 15 ++++-- tslang/test/tester/CMakeLists.txt | 3 +- .../own/own_err_param_assigned_local.ts | 22 ++++++++ .../tester/own/own_keeper_literal_elements.ts | 51 +++++++++++++++++++ 5 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 tslang/test/tester/own/own_err_param_assigned_local.ts create mode 100644 tslang/test/tester/own/own_keeper_literal_elements.ts diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index a4ccb6f77..83e203ab1 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -160,6 +160,32 @@ namespace own_inference recordStoreUses.clear(); } + // A store of a value that owns a block into a parameter's own slot: the function's argument, which no local + // that owns what it holds took over (MLIRGen's mlirGenFunctionOwnAssignedParams makes one only of a string or a + // Shared parameter). Another parameter's value may go there: the caller holds both for the whole call. + static bool assignsParameter(mlir_ts::StoreOp storeOp) + { + auto isParameterSlot = [](mlir_ts::VariableOp slot) { + auto argument = slot ? mlir::dyn_cast_or_null(slot.getInitializer()) : nullptr; + return slot && !isOwningVariable(slot) && argument && argument.getOwner()->isEntryBlock() && + mlir::isa(argument.getOwner()->getParentOp()); + }; + + if (!isParameterSlot(storeOp.getReference().getDefiningOp()) || !ownsHeap(storeOp.getValue())) + { + return false; + } + + auto value = storeOp.getValue(); + while (auto castOp = value.getDefiningOp()) + { + value = castOp.getIn(); + } + + auto loadOp = value.getDefiningOp(); + return !loadOp || !isParameterSlot(loadOp.getReference().getDefiningOp()); + } + void OwnershipInferencePass::collectOwnershipOps(FunctionOps &ops, llvm::SetVector &toErase) { auto f = getFunction(); @@ -207,6 +233,27 @@ namespace own_inference signalPassFailure(); } } + else if (auto storeOp = mlir::dyn_cast(op); storeOp && assignsParameter(storeOp)) + { + // A parameter's slot given another value: the slot owns nothing, so the value is neither + // taken from its owner nor retained, and the owner frees it - `p = n; n = mk()` left `p` + // reading freed memory. Reported where a retain for it would have been, but there is none. + if (reporting()) + { + // a parameter's name is known only from its debug location (--di) + auto name = varName(storeOp.getReference().getDefiningOp()); + if (name == "this value") + { + op->emitError("a parameter cannot be assigned under -mm=own yet"); + } + else + { + op->emitError("'") << name << "' is a parameter and cannot be assigned under -mm=own yet"; + } + + signalPassFailure(); + } + } else if (mlir::isa(op)) { if (reporting()) diff --git a/tslang/lib/TypeScript/OwnershipSignatureKeepers.cpp b/tslang/lib/TypeScript/OwnershipSignatureKeepers.cpp index 90821a709..d532d5ece 100644 --- a/tslang/lib/TypeScript/OwnershipSignatureKeepers.cpp +++ b/tslang/lib/TypeScript/OwnershipSignatureKeepers.cpp @@ -22,8 +22,10 @@ namespace own_signature return value; } - // Does a global's initializer region give it a new array - `[]`, an array literal - and nothing - // else? + // Does a global's initializer region give it a new array - `[]`, an array literal of constants - and + // nothing else? Only what is pushed onto a keeper is made immortal (decideKeptForGood): an element its + // literal is made with - `[mk(5)]` - is not, so a read of it is no keeper's read, and a local given + // one and released frees it from the array. static bool regionYieldsNewArray(mlir_ts::GlobalOp globalOp) { if (!globalOp.getInitializerRegion().hasOneBlock()) @@ -38,7 +40,14 @@ namespace own_signature } auto *def = castRoot(resultOp->getOperand(0)).getDefiningOp(); - return mlir::isa_and_nonnull(def); + if (auto createArrayOp = mlir::dyn_cast_or_null(def)) + { + return llvm::all_of(createArrayOp->getOperands(), [](mlir::Value element) { + return !!castRoot(element).getDefiningOp(); + }); + } + + return mlir::isa_and_nonnull(def); } void OwnershipSignaturePass::findKeepers(mlir::ModuleOp module) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 574e5964b..4ea98703b 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -4076,7 +4076,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_borrowing_record own_borrowing_record_arg own_reduce own_fresh_call_result own_ternary_outer_loop own_destructure_elements own_tuple_record_values own_value_record own_value_loop own_value_temporaries - own_call_field_drops own_immortal_keepers + own_call_field_drops own_immortal_keepers own_keeper_literal_elements own_generator_method_this own_string_copy_call own_param_kept_some_paths own_method_callbacks own_loop_read_again own_length_drops_elements own_tuple_param_kept own_for_of_borrowed_iterator own_generator_keeps own_generator_keeps_bounded @@ -4670,6 +4670,7 @@ set(own_error_cases "own_err_global_read_callee_assigns|borrows 'G' but is used here after 'G' is released or overwritten" "own_err_global_read_callee_assigns_transitive|borrows 'G' but is used here after 'G' is released or overwritten" "own_err_argument_assigned|is lent to 'g', but is given a new value before the call takes it" + "own_err_param_assigned_local|:15:5: error: a parameter cannot be assigned under -mm=own yet" "own_err_argument_assigned_conditional|is lent to 'g', but is given a new value before the call takes it" "own_err_argument_assigned_closure|is lent to 'g', but is given a new value before the call takes it" "own_err_global_method_transitive|:19:11: error: a value read out of the global 'G' is lent to a call that may release or overwrite it" diff --git a/tslang/test/tester/own/own_err_param_assigned_local.ts b/tslang/test/tester/own/own_err_param_assigned_local.ts new file mode 100644 index 000000000..536b1beff --- /dev/null +++ b/tslang/test/tester/own/own_err_param_assigned_local.ts @@ -0,0 +1,22 @@ +// -mm=own rejects: `p = n` puts `n`'s object in a parameter's slot, which owns nothing, so the +// object is neither taken from `n` nor counted again - and `n = mk(9)` frees it while `p` still +// reads it. Own accepted this (it reported an assigned parameter only where a retain stood for the +// store, and a store into a parameter has none), and `p.x` read freed memory (mm_fuzz seed 70377). +class N { + constructor(public x: number) {} +} + +function mk(i: number): N { + return new N(i); +} + +function f(p: N): number { + let n: N = mk(2); + p = n; + n = mk(9); + return p.x; +} + +function main() { + print(f(mk(5))); +} diff --git a/tslang/test/tester/own/own_keeper_literal_elements.ts b/tslang/test/tester/own/own_keeper_literal_elements.ts new file mode 100644 index 000000000..19b39963c --- /dev/null +++ b/tslang/test/tester/own/own_keeper_literal_elements.ts @@ -0,0 +1,51 @@ +// -mm=own: a keeper - an append-only array global - makes what is pushed onto it immortal, so a read +// of its elements owns nothing and any release skips it. The elements its own literal is made with +// were not made immortal: `const GA: N[] = [mk(5)]` was taken for a keeper all the same, and a local +// given `GA[0]` freed it when the local's block ended (mm_fuzz seeds 70040-70386: 9 of 10 own +// failures of one round). An array literal of anything but constants makes no keeper now, and its +// elements are borrowed like any array's: storing one in an array or a global is an error under own. +// Each value is read after churn() has reused anything freed too early. +class N { + items: number[] = [5]; + constructor(public x: number) {} +} + +function mk(i: number): N { + return new N(i); +} + +function churn() { + const hold: N[] = []; + for (let i = 0; i < 64; i++) { + hold.push(new N(777)); + } +} + +interface IX { + x: number; +} + +const GA: N[] = [mk(5)]; + +function main() { + for (let i = 0; i < 3; i++) { + let n = GA[0]; + } + + churn(); + assert(GA[0].x == 5 && GA[0].items[0] == 5, "a local in a loop leaves the element"); + + { + let n = GA[0]; + let ix: IX = GA[0]; + } + + churn(); + assert(GA[0].x == 5, "a local and an interface in a block leave the element"); + + GA.push(mk(6)); + churn(); + assert(GA[1].x == 6, "a pushed element"); + + print("done."); +} From 31404d5e2f4a5dfab5ed73cbf5367e8ab8357973 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Fri, 30 Oct 2026 17:47:16 +0000 Subject: [PATCH 2/2] -mm=rc: an argument read through a parameter's object is held across a call that writes a global A second fuzz round (600 programs, seeds 71000-71599, on main with #751 and this branch) found no own failures in 1101 checked programs, and one rc crash: `g(p.n)` where `p` was a global's object (`f(HG)`) and `g` did `HG.n = mk(5)`. The argument was borrowed with no count, so the store freed it while `g` still read it, or after `g` gave it back. An argument read out of a global itself was already held across such a call (#647). An argument read out of a field or an element of an object a parameter names is now held too, when the callee may give any global a new value. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/OwnedReturnConsumptionPass.cpp | 51 ++++++++++ tslang/test/tester/CMakeLists.txt | 3 + .../tests/00call_arg_param_field_global.ts | 95 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 tslang/test/tester/tests/00call_arg_param_field_global.ts diff --git a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp index ac59ac58d..e649eaf56 100644 --- a/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp +++ b/tslang/lib/TypeScript/OwnedReturnConsumptionPass.cpp @@ -470,6 +470,48 @@ class OwnedReturnConsumptionPass } } + auto mayAssignAnyGlobal = [&](mlir::Operation *op) { + auto callee = calleeOf(op); + if (callee.empty() || assignsAny.contains(callee)) + { + return true; + } + + auto it = assigns.find(callee); + return it != assigns.end() && !it->getValue().empty(); + }; + + // Is this a read out of a field or an element of an object a parameter names (`p.n`, `p.a[0].v`)? The + // parameter may be a global's object (`f(HG)`), so a call that gives a global's field a new value may + // free what was read. + auto readsParameterPlace = [](mlir::Value value) { + auto inPlace = false; + for (auto depth = 0; depth < 16 && value; depth++) + { + while (auto castOp = value.getDefiningOp()) + { + value = castOp.getIn(); + } + + auto loadOp = value.getDefiningOp(); + auto *place = loadOp ? loadOp.getReference().getDefiningOp() : nullptr; + if (mlir::isa_and_nonnull(place)) + { + return inPlace; + } + + if (!mlir::isa_and_nonnull(place)) + { + return false; + } + + inPlace = true; + value = place->getOperand(0); + } + + return false; + }; + auto mayAssign = [&](mlir::Operation *op, mlir::StringRef global) { auto callee = calleeOf(op); if (callee.empty() || assignsAny.contains(callee)) @@ -646,6 +688,15 @@ class OwnedReturnConsumptionPass continue; } + // Out of a field of an object a parameter names, to a call that may give a global's field a new + // value: the object may be that global's (`f(HG)` then `g(p.n)`, `g` setting `HG.n`), and the + // store freed the block the argument read. Under rc the caller holds it (fuzz seed 71210). + if (compileOptions.isRefCounted() && readsParameterPlace(argument) && mayAssignAnyGlobal(op)) + { + held.push_back({op, argument}); + continue; + } + auto global = globalOf(argument, false); if (global.empty()) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 4ea98703b..502377225 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -614,6 +614,7 @@ tslang_add_test(NAME test-compile-00-class-accessor-implements-field COMMAND tes tslang_add_test(NAME test-compile-00-object-literal-contextual-lambda COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-compile-own-00-object-literal-contextual-lambda COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-compile-00-interface-field-getter-owned COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") +tslang_add_test(NAME test-compile-00-call-arg-param-field-global COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00call_arg_param_field_global.ts") tslang_add_test(NAME test-compile-00-class-accessor-untyped-setter COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts") tslang_add_test(NAME test-compile-00-class-super COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts") tslang_add_test(NAME test-compile-00-class-implicit-constructor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts") @@ -1253,6 +1254,7 @@ tslang_add_test(NAME test-jit-00-class-accessor-implements-field COMMAND test-ru tslang_add_test(NAME test-jit-00-object-literal-contextual-lambda COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-jit-own-00-object-literal-contextual-lambda COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_literal_contextual_lambda.ts") tslang_add_test(NAME test-jit-00-interface-field-getter-owned COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_field_getter_owned.ts") +tslang_add_test(NAME test-jit-00-call-arg-param-field-global COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00call_arg_param_field_global.ts") tslang_add_test(NAME test-jit-00-class-accessor-untyped-setter COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_accessor_untyped_setter.ts") tslang_add_test(NAME test-jit-00-class-super COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_super.ts") tslang_add_test(NAME test-jit-00-class-implicit-constructor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_implicit_constructor.ts") @@ -2267,6 +2269,7 @@ set(TSLANG_CORPUS 00class_accessor_implements_field.ts 00object_literal_contextual_lambda.ts 00interface_field_getter_owned.ts + 00call_arg_param_field_global.ts 00class_accessor_untyped_setter.ts 00class_accessor.ts 00class_accessor2.ts diff --git a/tslang/test/tester/tests/00call_arg_param_field_global.ts b/tslang/test/tester/tests/00call_arg_param_field_global.ts new file mode 100644 index 000000000..c3e72e52c --- /dev/null +++ b/tslang/test/tester/tests/00call_arg_param_field_global.ts @@ -0,0 +1,95 @@ +// An argument read out of a field of an object a parameter names - `g(p.n)` - is borrowed by the +// callee, and the caller took no count for it. When the parameter is a global's object (`f(HG)`) and +// the callee gives that global's field a new value (`HG.n = mk(5)`), the store freed the block the +// argument read, under rc: `g` read freed memory, and a result that was the argument was freed too +// (mm_fuzz seed 71210). An argument read out of a global itself was already held across such a call +// (#647); now one read through a parameter's object is held as well, when the callee may give a +// global a new value. Each value is read after churn() has reused anything freed too early. +class V { + constructor(public x: number) {} +} + +class N { + items: number[] = [1]; + constructor(public v: V) {} +} + +class H { + n: N = new N(new V(3)); +} + +function mk(i: number): N { + return new N(new V(i)); +} + +function churn() { + const hold: N[] = []; + for (let i = 0; i < 64; i++) { + hold.push(new N(new V(777))); + } +} + +const HG = new H(); + +// the callee reads its parameter after giving the global's field a new value +function readsAfter(a: N): number { + HG.n = mk(5); + churn(); + return a.v.x; +} + +function callReadsAfter(p: H): number { + return readsAfter(p.n); +} + +// the callee gives back the argument it was given +function givesBack(a: N): N { + HG.n = mk(6); + return a; +} + +function callGivesBack(p: H): number { + const r = givesBack(p.n); + churn(); + return r.v.x; +} + +// ...into the very field it was read from +function storesBack(p: H): number { + p.n = givesBack(p.n); + churn(); + return p.n.v.x; +} + +// through an element +class A { + ns: N[] = [mk(8)]; +} + +const AG = new A(); + +function setsElement(a: N): number { + AG.ns[0] = mk(9); + churn(); + return a.v.x; +} + +function callSetsElement(p: A): number { + return setsElement(p.ns[0]); +} + +function main() { + assert(callReadsAfter(HG) == 3, "the callee reads the argument after the store"); + assert(HG.n.v.x == 5, "the store took"); + + HG.n = mk(4); + assert(callGivesBack(HG) == 4, "the callee gives the argument back"); + + HG.n = mk(7); + assert(storesBack(HG) == 7, "the argument stored back into its field"); + + assert(callSetsElement(AG) == 8, "an element read through a parameter"); + assert(AG.ns[0].v.x == 9, "the element store took"); + + print("done."); +}