From fda82cc823cba1d0eb127bea37763ff99b20a889 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 21:49:58 +0000 Subject: [PATCH] -mm=own: a move in a scope released on the unwind leg empties the moved-from slot Under own, `const b = a` (or a value moved out of an await's slot) in the body of a TryOp was "moved here on some paths only": the scope's release in the cleanup landing pad is reached from before the move and after it, and no release can go on an unwind edge (spec 30's edge releases need a branch). On Itanium every function body with a typed `catch` is wrapped in such a TryOp, so on Linux this hit any move at the top level of such a function - DefaultLib's promise.ts (`const awaited = await Promise.all([...])` beside a `try/catch`) failed the v0.0-pre-alpha114 release under own on every ubuntu runner. An explicit `try` body failed on Windows too. The slot of a hoisted owned local starts null (VariableOpLowering) and a release of null does nothing, so the move now stores an empty value into the slot right after it, and the handler's release stays: it frees the value only on the paths that unwind before the move. Only when the read, the taker and rc's retain are in one non-handler block with no call between them, and the taker is not a call (one that may throw having taken the value or not). Test: 00own_move_in_unwound_scope.ts, run under every model, plus an --emit=llvm check for the Windows and Linux triples on any host. Co-Authored-By: Claude Opus 5.5 --- .../OwnershipInferenceEdgeReleases.cpp | 52 +++++++ .../TypeScript/OwnershipInferenceMoves.cpp | 24 +++ .../lib/TypeScript/OwnershipInferencePass.cpp | 1 + .../lib/TypeScript/OwnershipInferencePass.h | 23 ++- tslang/test/tester/CMakeLists.txt | 16 ++ .../tests/00own_move_in_unwound_scope.ts | 142 ++++++++++++++++++ 6 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 tslang/test/tester/tests/00own_move_in_unwound_scope.ts diff --git a/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp b/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp index 678e7f158..835d739be 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceEdgeReleases.cpp @@ -512,6 +512,58 @@ namespace own_inference } edgeReleases.clear(); + + for (auto &clear : slotClears) + { + mlir::OpBuilder builder(clear.after->getContext()); + builder.setInsertionPointAfter(clear.after); + auto loc = clear.after->getLoc(); + auto elementType = mlir::cast(clear.slot.getType()).getElementType(); + auto empty = builder.create(loc, elementType); + builder.create(loc, empty, clear.slot); + } + + slotClears.clear(); + } + + mlir::Operation *OwnershipInferencePass::clearsOnUnwind(mlir_ts::VariableOp varOp, mlir_ts::LoadOp load, + mlir::Operation *acquire, mlir::Operation *taker, + mlir::Operation *release, + const llvm::SmallPtrSet &handler) + { + // a hoisted owned local: no initializer, so VariableOpLowering starts it as null + auto &body = getFunction().getFunctionBody(); + if (!varOp || !varOp->hasAttr(OWNED_LOCAL_ATTR_NAME) || varOp.getInitializer() || + varOp.getCaptured().value_or(false) || varOp->getParentRegion() != &body) + { + return nullptr; + } + + if (!handler.contains(release->getBlock())) + { + return nullptr; + } + + // the read, the taker and rc's retain in one block of the body, with nothing between that + // could unwind with the value read but not yet taken: a call that takes it may throw having + // taken it or not + auto *block = load->getBlock(); + if (block->getParent() != &body || taker->getBlock() != block || acquire->getBlock() != block || + handler.contains(block) || !load->isBeforeInBlock(taker) || isCall(taker)) + { + return nullptr; + } + + auto *last = taker->isBeforeInBlock(acquire) ? acquire : taker; + for (auto *op = load->getNextNode(); op && op != last->getNextNode(); op = op->getNextNode()) + { + if (isCall(op) || op->hasTrait()) + { + return nullptr; + } + } + + return last; } llvm::SmallVector OwnershipInferencePass::slotAssignments(mlir::Value slot) diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index 255b398ba..beaaf6533 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -1376,6 +1376,8 @@ namespace own_inference auto *acquire = mlir::isa(retain) ? taker : retain; llvm::SmallVector moved; + mlir::Operation *clearAfter = nullptr; + std::optional> handler; for (auto *use : slot.getUsers()) { if (!mlir::isa(use) || !reachableAfter(acquire, use, kills)) @@ -1400,6 +1402,23 @@ namespace own_inference if (someMoved) { + // ...the scope's release on the unwind leg, reached from before the move and after + // it: the slot is emptied at the move, and the release stays for the paths that + // did not move it + if (placeReleases) + { + if (!handler) + { + handler = handlerBlocks(); + } + + if (auto *after = clearsOnUnwind(varOp, load, acquire, taker, use, *handler)) + { + clearAfter = after; + continue; + } + } + // ...but the paths that did not move it can release it before they join the // others (spec 30) if (placeReleases && placeOnUnmovedEdges(varOp, acquire, retain, taker, assignments, toErase)) @@ -1414,6 +1433,11 @@ namespace own_inference moved.push_back(use); } + if (clearAfter) + { + slotClears.push_back({slot, clearAfter}); + } + toErase.insert(moved.begin(), moved.end()); return true; } diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index f5389d21f..485676272 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -375,6 +375,7 @@ namespace own_inference auto f = getFunction(); drops.clear(); edgeReleases.clear(); + slotClears.clear(); derivedCache.clear(); closures.clear(); literalBoxes.clear(); diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 56f8afafc..dcfbf6a08 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -1223,9 +1223,30 @@ class OwnershipInferencePass const llvm::SetVector &releasedInto); // Puts in the releases placeOnUnmovedEdges decided: at the end of a block that goes only to the - // join, else in a block of its own on the edge. + // join, else in a block of its own on the edge - and the clears clearsOnUnwind decided. void placeEdgeReleases(); + // A slot emptied right after the move out of it, put in after the analysis (clearsOnUnwind). + struct SlotClear + { + mlir::Value slot; + mlir::Operation *after; + }; + + llvm::SmallVector slotClears; + + // The move out of `varOp` at `taker` (rc's retain at `acquire`) reaches `release`, in an exception + // handler, on some paths only: the scope's release on the unwind leg of a block wrapped in a TryOp + // - an explicit `try`, or on Itanium any block with a typed `catch` - is reached from before the + // move and after it. No release can go on an unwind edge, but the slot of a hoisted owned local + // starts null and a release of null does nothing, so emptying the slot right after the move + // leaves the handler's release with the value only on the paths that did not move it. The op to + // put the clear after, or null when the slot is not such a local or the move could unwind + // part-way (a call taking the value, or one between the read and the taker). + mlir::Operation *clearsOnUnwind(mlir_ts::VariableOp varOp, mlir_ts::LoadOp load, mlir::Operation *acquire, + mlir::Operation *taker, mlir::Operation *release, + const llvm::SmallPtrSet &handler); + // The stores of the assignments to an owning local: rc releases what the slot holds right // before it stores the new value (an assignment, and a `var` declaration met again). static llvm::SmallVector slotAssignments(mlir::Value slot); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 8ec5f967d..94007fedb 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -925,6 +925,8 @@ tslang_add_test(NAME test-compile-own-00-generic-literal-inference COMMAND test- tslang_add_test(NAME test-compile-own-00-new-alias-and-ctor-overloads COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") tslang_add_test(NAME test-compile-00-own-throw-param-await-borrow COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-compile-own-00-own-throw-param-await-borrow COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-compile-00-own-move-in-unwound-scope COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_move_in_unwound_scope.ts") +tslang_add_test(NAME test-compile-own-00-own-move-in-unwound-scope COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_move_in_unwound_scope.ts") tslang_add_test(NAME test-compile-load-store-decorators COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-compile-reference-null-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-compile-reference-index-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -1578,6 +1580,19 @@ tslang_add_test(NAME test-jit-own-00-generic-literal-inference COMMAND test-runn tslang_add_test(NAME test-jit-own-00-new-alias-and-ctor-overloads COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") tslang_add_test(NAME test-jit-00-own-throw-param-await-borrow COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-jit-own-00-own-throw-param-await-borrow COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-jit-00-own-move-in-unwound-scope COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_move_in_unwound_scope.ts") +tslang_add_test(NAME test-jit-own-00-own-move-in-unwound-scope COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_move_in_unwound_scope.ts") +# On Itanium a function body with a typed `catch` is wrapped in a TryOp for its cleanup, so a move at +# its top level meets the unwind leg's release there too: checked on every host, for both triples +foreach(own_unwind_triple x86_64-pc-windows-msvc x86_64-unknown-linux-gnu) + add_test(NAME test-compile-own-move-in-unwound-scope-${own_unwind_triple} + COMMAND $ --emit=llvm -mm=own -mtriple=${own_unwind_triple} + "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_move_in_unwound_scope.ts" -o -) + set_tests_properties(test-compile-own-move-in-unwound-scope-${own_unwind_triple} + PROPERTIES PASS_REGULAR_EXPRESSION "define [^ +]*@main" + FAIL_REGULAR_EXPRESSION "error:|Stack dump|Assertion failed") +endforeach() tslang_add_test(NAME test-jit-load-store-decorators COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-jit-reference-null-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-jit-reference-index-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -2173,6 +2188,7 @@ set(TSLANG_CORPUS 00generic_literal_inference.ts 00new_alias_and_ctor_overloads.ts 00own_throw_param_await_borrow.ts + 00own_move_in_unwound_scope.ts 00any_truthiness_objects.ts 00add_promotes_both_operands.ts 00conditional_owned_result.ts diff --git a/tslang/test/tester/tests/00own_move_in_unwound_scope.ts b/tslang/test/tester/tests/00own_move_in_unwound_scope.ts new file mode 100644 index 000000000..692814dee --- /dev/null +++ b/tslang/test/tester/tests/00own_move_in_unwound_scope.ts @@ -0,0 +1,142 @@ +// -mm=own: a local moved to another inside a block whose scope is also released on the unwind leg - +// a `try` body, or on Itanium (Linux) a function body that holds a typed `catch` - was "moved here on +// some paths only": the unwind leg's release is reached from before the move and after it. The +// moved-from slot is emptied at the move, so that release frees the value only if the move has not +// run. Each case runs many times so a double free or a use of a freed block shows. + +class E { + constructor(public m: string) {} +} + +function make(n: int): int[] { + return [n, n + 1]; +} + +function check(a: int[], fail: boolean): int { + if (fail) { + throw new E("check"); + } + + return a.length; +} + +async function twice(n: int): Promise { + return n * 2; +} + +async function pair(n: int): Promise { + return [n, n * 2]; +} + +// Promise.all's shape: the values, already settled +function all(values: int[]): int[] { + const settled: int[] = []; + for (const value of values) { + settled.push(value); + } + + return settled; +} + +// the move in a `try` body; the throw before it, or after it, unwinds through the body's scope +function inTry(failBefore: boolean, failAfter: boolean): int { + let n = 0; + try { + const a = make(1); + check(a, failBefore); + const b = a; + n = check(b, failAfter); + } catch (e: E) { + n = -1; + } + + return n; +} + +// the move and a typed catch in one function body: on Itanium the body is wrapped for its cleanup +function besideCatch(fail: boolean): int { + const first = make(1); + const a = make(2); + const b = a; + let caught = false; + try { + const none: int[] = []; + check(none, fail); + } catch (e: E) { + caught = true; + } + + return first.length + b.length + (caught ? 10 : 0); +} + +// an awaited value moved out of the await's slot, beside a typed catch +function awaitBesideCatch(fail: boolean): int { + const first = make(1); + const awaited = await pair(3); + let caught = false; + try { + check(first, fail); + } catch (e: E) { + caught = true; + } + + return awaited[0] + awaited[1] + (caught ? 100 : 0); +} + +// the shape from DefaultLib's promise test: an array literal of async calls, awaited +function awaitAllBesideCatch(fail: boolean): int { + const both = [twice(2), twice(5)]; + const awaited = await all([twice(3), twice(4)]); + let caught = false; + try { + const none: int[] = []; + check(none, fail); + } catch (e: E) { + caught = true; + } + + return both[0] + awaited[0] + awaited[1] + (caught ? 100 : 0); +} + +// the unwind leaves the function: its own scope's release on the way out, after the move +function moveThenThrow(): int { + const a = make(4); + const b = a; + let caught = false; + try { + check(b, false); + } catch (e: E) { + caught = true; + } + + check(b, true); + return caught ? 1 : 0; +} + +function main() { + for (let i = 0; i < 1000; i++) { + assert(inTry(false, false) == 2, "in try, no throw"); + assert(inTry(true, false) == -1, "in try, thrown before the move"); + assert(inTry(false, true) == -1, "in try, thrown after the move"); + + assert(besideCatch(false) == 4, "beside a catch"); + assert(besideCatch(true) == 14, "beside a catch, caught"); + + assert(awaitBesideCatch(false) == 9, "an await beside a catch"); + assert(awaitBesideCatch(true) == 109, "an await beside a catch, caught"); + + assert(awaitAllBesideCatch(false) == 18, "an awaited array of async calls beside a catch"); + assert(awaitAllBesideCatch(true) == 118, "an awaited array of async calls beside a catch, caught"); + + let caught = ""; + try { + moveThenThrow(); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "check", "moved, then thrown out of the function"); + } + + print("done."); +}