From b76ab809a9fca2a7fbcb66a1cd13ed6297db2c2f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 2 Dec 2026 12:38:10 +0000 Subject: [PATCH] Array literal of a runtime array and a constant one read past the constant; own: s32 to s64 union element cast in place; rc: delete of a value box frees it - `[["a" + i, "b"], ["c", "d"]]`: `["c", "d"]` is a const_array, which widens to the string[] the other element is, so the element types matched and no cast was made: the constant's data was stored as the array, and reading it read past the global (every model). An element whose type differs from the element type, other than a literal, is now cast. - -mm=own: `[["a", 301349860000], ["c", 0]]` is an array of (string | s64)[] and (string | s32)[]; the element of the s32 one was cast through `__cast`, whose result borrows, and own refused to keep it (`for (const [k, v] of data)`). A member that widens into a number member of the target (s32 -> s64, an integer -> a wider float) is cast in place by the tag, as `string | s32` -> `string | number` is. - -mm=rc: `delete i` of `new (TypeOf<1>)()` (a value_ref) released nothing - the type has no release routine - and the block leaked. It is freed (no one counts it). ASan corpus 124 -> 130 of 154 clean; full ctest (WSL ASan) 10455/10455. Co-Authored-By: Claude Opus 5.5 --- .../LowerToLLVM/OwnershipRoutineLogic.h | 26 ++++++++++++++++++ tslang/lib/TypeScript/LowerToLLVM.cpp | 9 ++++++- tslang/lib/TypeScript/MLIRGenCast.cpp | 27 +++++++++++++++++-- tslang/lib/TypeScript/MLIRGenImpl.h | 6 ++++- tslang/test/tester/CMakeLists.txt | 5 ++++ tslang/test/tester/sanitize/leaks_more.ts | 16 +++++++++++ .../tests/00array_of_arrays_const_element.ts | 24 +++++++++++++++++ 7 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 tslang/test/tester/tests/00array_of_arrays_const_element.ts diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index 0537e500d..c315f51fd 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -413,6 +413,32 @@ class OwnershipRoutineLogic emitIfLastReference(boxValue, [&]() { emitFreeBlock(boxValue); }); } + // `delete` of a box `new` made of a value type (`new (TypeOf<1>)()`, a value_ref): a block with a header + // but no release routine of its own, so a release of it did nothing and the block leaked. Nothing counts + // references to it (its count stays 0, as made), so `delete` frees it outright, as without counting: + // what it holds is released and the block freed. A null box is skipped. + void emitReleaseValueRef(mlir_ts::ValueRefType valueRefType, mlir::Value payloadPtr) + { + TypeHelper th(rewriter); + auto loc = op->getLoc(); + + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto *freeBlock = rewriter.createBlock(continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + auto null = rewriter.create(loc, th.getPtrType()); + auto notNull = rewriter.create(loc, LLVM::ICmpPredicate::ne, payloadPtr, null); + rewriter.create(loc, notNull, freeBlock, continuationBlock); + + rewriter.setInsertionPointToEnd(freeBlock); + releaseSlot(valueRefType.getElementType(), payloadPtr); + emitFreeBlock(payloadPtr); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToStart(continuationBlock); + } + // What destroying a block of `storageType` at `payloadPtr` releases, without freeing the block: // a promoted object's fields when its frame is done with it (ts.ReleaseContents). Each field // given back is nulled, so that the block never holds a reference it no longer owns: a second diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index f5cdf4c27..8167d25b9 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -5584,7 +5584,14 @@ struct DeleteOpLowering : public TsLlvmPattern if (tsLlvmContext->compileOptions.tracksOwnership()) { OwnershipRoutineLogic orl(deleteOp, rewriter, getTypeConverter(), tsLlvmContext->compileOptions); - orl.emitReleaseValue(deleteOp.getReference().getType(), transformed.getReference()); + if (auto valueRefType = dyn_cast(deleteOp.getReference().getType())) + { + orl.emitReleaseValueRef(valueRefType, transformed.getReference()); + } + else + { + orl.emitReleaseValue(deleteOp.getReference().getType(), transformed.getReference()); + } rewriter.eraseOp(deleteOp); return mlir::success(); diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 0a981f990..b7568fc42 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -6564,7 +6564,31 @@ namespace mlirgen // number)[]`): the call of `__cast` hands back what it is given, a borrow of it, which own refused to // store ("a value that borrows an element cannot be stored"); cast in place, a string read out of the // union is copied where it is kept, as any string read is + // (...or a wider number of its own: `string | s32` to `string | s64`, the element of `["c", 0]` read + // out of `[["a", 301349860000], ["c", 0]]`, an array of `(string | s32)[] | (string | s64)[]`) auto targetUnion = dyn_cast(type); + auto numberWidensInto = [](mlir::Type member, mlir::Type targetMember) { + if (isa(targetMember)) + { + return isa(member); + } + + auto memberInt = dyn_cast(member); + if (auto targetInt = dyn_cast(targetMember)) + { + return memberInt && memberInt.getSignedness() == targetInt.getSignedness() && + memberInt.getWidth() <= targetInt.getWidth(); + } + + if (auto targetFloat = dyn_cast(targetMember)) + { + return (memberInt && memberInt.getWidth() < targetFloat.getWidth()) || + (isa(member) && + mlir::cast(member).getWidth() <= targetFloat.getWidth()); + } + + return false; + }; auto widensIntoTarget = compileOptions.memoryModel == MemoryModelOwn && targetUnion && llvm::all_of(unionType.getTypes(), [&](mlir::Type member) { if (auto literalType = dyn_cast(member)) @@ -6572,9 +6596,8 @@ namespace mlirgen member = literalType.getElementType(); } - auto isNumber = isa(member); return llvm::any_of(targetUnion.getTypes(), [&](mlir::Type targetMember) { - return targetMember == member || (isNumber && isa(targetMember)); + return targetMember == member || numberWidensInto(member, targetMember); }); }); if ((hasUnsupportedType || widensIntoTarget) && isUnionToldApartByTag(location, unionType)) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 98252164b..70514e428 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -18032,7 +18032,11 @@ class MLIRGenImpl for (auto val : values) { auto arrayValue = val.value; - if (arrayInfo.applyCast) + // also an element whose own type only widens to the element type: `["c", "d"]` beside `["a" + i, "b"]` + // is a const_array, which widens to the string[] the other is (so the types matched), but + // stored as it is it was the constant's data, not an array: reading it read past the global + if (arrayInfo.applyCast || (arrayValue.getType() != arrayInfo.arrayElementType && + !isa(arrayValue.getType()))) { CAST(arrayValue, location, arrayInfo.arrayElementType, val.value, genContext) } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 810e57eae..d4be7251a 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -3055,6 +3055,11 @@ foreach(failed_field_mm gc own) PASS_REGULAR_EXPRESSION "a computed property name must be a constant" FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") endforeach() +# an array literal of a runtime array and a constant one; arrays of string | s64 and string | s32 taken apart +foreach(aoa_mm gc rc none own) + tslang_add_test(NAME test-jit-00-array-of-arrays-const-element-${aoa_mm} COMMAND test-runner -jit -mm=${aoa_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_of_arrays_const_element.ts") + tslang_add_test(NAME test-compile-00-array-of-arrays-const-element-${aoa_mm} COMMAND test-runner -mm=${aoa_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_of_arrays_const_element.ts") +endforeach() # TSLANG_ISSUES 31, 33: a const Shared in several closures; a callback's result stored in a field foreach(issues_mm gc rc none own) tslang_add_test(NAME test-jit-00-tslang-issues-31-33-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issues_31_33.ts") diff --git a/tslang/test/tester/sanitize/leaks_more.ts b/tslang/test/tester/sanitize/leaks_more.ts index e799659f0..3f034128e 100644 --- a/tslang/test/tester/sanitize/leaks_more.ts +++ b/tslang/test/tester/sanitize/leaks_more.ts @@ -188,6 +188,21 @@ function keptLentResults(i: number) { return ys.length + u.length + h.s.length + ms.length; } +// an array literal of a runtime array and an all-literal one (`[["a" + i, "b"], ["c", "d"]]`): the literal one was +// stored as the constant's data, not as an array, and read past it; elements of `[k, 301349860000]` and `[k, 0]`, +// arrays of `string | s64` and `string | s32`, taken apart in a loop (own refused the widening cast's result) +type Num = TypeOf<1>; +function arraysOfArrays(i: number) { + const data = [["a" + i, "b"], ["c", "d"]]; + let n = 0; + for (const [k, v] of data) n += k.length + v.length; + const mixed = [["x" + i, 301349860000], ["y", 2]]; + for (const [k, v] of mixed) n += k.length; + let boxed = new Num(); + delete boxed; + return n; +} + function main() { for (let i = 0; i < 4; i++) { assert(literalMethod(i) == i + 1, "literal with a method"); @@ -221,6 +236,7 @@ function main() { assert(destructuredLiteral(i) == i + 6, "array literal taken apart"); assert(unionSpread(i) == 8, "union spread"); assert(keptLentResults(i) == 10, "kept lent results"); + assert(arraysOfArrays(i) == 6 + 2 * ("" + i).length, "arrays of arrays"); } print("done."); diff --git a/tslang/test/tester/tests/00array_of_arrays_const_element.ts b/tslang/test/tester/tests/00array_of_arrays_const_element.ts new file mode 100644 index 000000000..6ffe4a012 --- /dev/null +++ b/tslang/test/tester/tests/00array_of_arrays_const_element.ts @@ -0,0 +1,24 @@ +// An array literal of a runtime array and an all-literal one: `["c", "d"]` beside `["a" + i, "b"]` is a constant +// array, which widens to the string[] the other is. It was stored as the constant's data, not as an array, and +// reading it read past the constant. And `[["a", 301349860000], ["c", 0]]`, an array of (string | s64)[] and +// (string | s32)[], taken apart: -mm=own refused the cast of the narrower element. +function main() { + for (let i = 0; i < 3; i++) { + const data = [["a" + i, "b"], ["c", "d"]]; + let s = ""; + for (const [k, v] of data) s += k + v + ","; + for (const row of data) s += row.length + ","; + const [x, y] = data[1]; + s += x + y; + print(s); + assert(s == "a" + i + "b,cd,2,2,cd"); + } + + const mixed = [["a" + 1, 301349860000], ["c", 7]]; + let t = ""; + for (const [k, v] of mixed) t += k + ":" + v + ","; + print(t); + assert(t == "a1:301349860000,c:7,"); + + print("done."); +}