From 81577b50aa185c6ac29b4e057a343e2ab0c4afd3 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 26 Nov 2026 21:05:25 +0000 Subject: [PATCH] -mm=rc: the array splice gives back is born owned; it was freed while being returned (TSLANG 206) The release run of 2026-11-26 failed every time on array_class_splice.ts (-mm=rc, debug JIT, Ubuntu 22.04/24.04) with no output. Cause: mlirGenArraySplice marks splice's result owned, so its receiver takes the reference over instead of retaining, but ArraySpliceOpLowering made the header with ArrayLayout::make, whose block is born with no reference. `const removed = this.data.splice(...); return removed;` (the default library's Array.splice) retained it to 1 for the return and released the local to 0: the caller got a freed array, later allocations wrote over it and mimalloc's free list, and the next splice crashed in mi_malloc. Under rc the header is now born with that reference, as an owned `new C()`'s is (NewOpLowering). It was not the CPU: CI starts every pass with a cold JIT cache (a warm one passed), and on Windows the freed block happened to survive. Checked in docker ubuntu:22.04/26.04 with the release run's package and default library: unfixed 3/3 crashes and 7 invalid accesses under valgrind, fixed 0 and 0. Test 00rc_splice_result_owned.ts (gc, rc, none): pre-alpha134 crashes on it on Windows. Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 13 +++++ tslang/lib/TypeScript/LowerToLLVM.cpp | 13 +++++ tslang/test/tester/CMakeLists.txt | 6 ++ .../tester/tests/00rc_splice_result_owned.ts | 56 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tslang/test/tester/tests/00rc_splice_result_owned.ts diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 17d786ef3..faea9f2bf 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -3695,3 +3695,16 @@ Not yet: - The debug JIT crashes in LLVM translation on any program (local debug build, 11-26); release is fine. Tests `00tslang_issue_205.ts` and `00tslang_issue_205_this.ts` (gc, rc, none: under -mm=own the returned closure over a parameter is refused by design). + +206. The array `splice` gives back, under -mm=rc, was freed while it was being returned (release run of 2026-11-26, +array_class_splice.ts crashing in the debug JIT on Ubuntu 22.04/24.04 with no output). Fixed: +- `const removed = this.data.splice(start, count); ...; return removed;` (the default library's Array.splice): the + result is marked owned (mlirGenArraySplice), so `removed` takes its reference over and does not retain, but the + lowering made the header with `ArrayLayout::make`, whose block is born with no reference. The return retained it to + 1, the local's release took it back to 0 and freed it, and the caller read a freed array: the next allocations wrote + over it and mimalloc's free list (a crash in `mi_malloc` at a later `splice`). It has been so since allocations are + born with no reference; on Windows and in a warm JIT cache the freed block happened to survive. Under rc the header + is now born with the reference, as an owned `new C()`'s is. + +Tests `00rc_splice_result_owned.ts` (gc, rc, none; under own the test's re-insertion of the removed elements is +refused). diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index ae5e64c4b..5b0cf77d2 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -5267,6 +5267,19 @@ struct ArraySpliceOpLowering : public TsLlvmPattern rewriter.create(loc, indexType, removedCopyBytes)); removedArray = layout.make(arrayType, removedData, decSizeAsLLVMType); + + // The result arrives owned (mlirGenArraySplice marks it, as a `new` is): its receiver takes + // the reference over and does not retain, and one nothing receives is released (ยง9.30). Under + // rc the header block has to carry that reference, as an owned `new C()`'s does + // (NewOpLowering); born with none, `const removed = a.splice(i, 1); return removed;` was + // retained to 1 by the return and freed by the local's release on the way out, and the caller + // read a freed array (array_class_splice.ts, -mm=rc --di: heap corruption on Linux) + if (tsLlvmContext->compileOptions.isRefCounted() && spliceOp->hasAttr(OWNED_RESULT_ATTR_NAME)) + { + auto blockPtr = ch.getBlockPtrFromPayloadPtr(loc, removedArray, llvmIndexType); + auto one = rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, 1)); + rewriter.create(loc, one, blockPtr); + } } // Keep all arithmetic in the already-LLVM-converted domain (llvmIndexType), matching diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 6ebbe7031..5481902e9 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2544,6 +2544,12 @@ foreach(issues_mm gc rc none) tslang_add_test(NAME test-compile-${issue_test}-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issue_test}.ts") endforeach() endforeach() +# the array `splice` gives back, kept in a local and returned: under rc its header is born owned (it was freed on +# the way out) +foreach(splice_mm gc rc none) + tslang_add_test(NAME test-jit-00rc_splice_result_owned-${splice_mm} COMMAND test-runner -jit -mm=${splice_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_splice_result_owned.ts") + tslang_add_test(NAME test-compile-00rc_splice_result_owned-${splice_mm} COMMAND test-runner -mm=${splice_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00rc_splice_result_owned.ts") +endforeach() # TSLANG_ISSUES 176: not under rc (a closure in a parameter's default value is given back before it is used) nor own # (a loop variable the body assigns and a closure takes is refused) foreach(issues_mm gc none) diff --git a/tslang/test/tester/tests/00rc_splice_result_owned.ts b/tslang/test/tester/tests/00rc_splice_result_owned.ts new file mode 100644 index 000000000..1e40fbab8 --- /dev/null +++ b/tslang/test/tester/tests/00rc_splice_result_owned.ts @@ -0,0 +1,56 @@ +// -mm=rc: the array `splice` gives back arrives owned - its receiver takes the reference over and does not +// retain - but its header block was born with no reference at all. A method that kept the result in a local and +// returned it (`const removed = this.data.splice(...); return removed;`, the default library's Array.splice) +// retained it to 1 for the return and released the local to 0 on the way out: the caller got a freed array, and +// the next allocations wrote over it (array_class_splice.ts crashed with heap corruption under the debug JIT on +// Linux, and this one crashes on Windows). +class Item { + constructor(public name: string) {} +} + +class Arr { + data: T[] = []; + + take(start: int, count: int): T[] { + const removed = this.data.splice(start, count); + for (let i = 0; i < 1; i++) {} + return removed; + } + + takeIf(start: int, count: int, early: boolean): T[] { + const removed = this.data.splice(start, count); + if (early) { + return removed; + } + + return removed; + } +} + +function main() { + const a = new Arr(); + for (let i = 0; i < 6; i++) a.data.push(new Item("i" + i)); + + let ok = 0; + for (let n = 0; n < 1000; n++) { + const removed = n % 2 == 0 ? a.take(1, 2) : a.takeIf(1, 2, n % 4 == 1); + // the same size of block as the array's header, made right after: a freed header is handed out again + const other: Item[][] = []; + for (let k = 0; k < 8; k++) other.push([new Item("z" + k)]); + if (removed.length == 2 && removed[0].name.length == 2 && removed[1].name.length == 2) ok++; + a.data.splice(1, 0, removed[0], removed[1]); + } + + assert(ok == 1000, "every result read back as it was taken"); + assert(a.data.length == 6); + + // a result nobody keeps is given back where it is made + for (let n = 0; n < 100; n++) { + a.data.splice(0, 1); + a.data.push(new Item("p" + n)); + } + + assert(a.data.length == 6); + + print("done."); +}