Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>.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).
13 changes: 13 additions & 0 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5267,6 +5267,19 @@ struct ArraySpliceOpLowering : public TsLlvmPattern<mlir_ts::ArraySpliceOp>
rewriter.create<mlir_ts::DialectCastOp>(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<LLVM::ConstantOp>(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, 1));
rewriter.create<LLVM::StoreOp>(loc, one, blockPtr);
}
}

// Keep all arithmetic in the already-LLVM-converted domain (llvmIndexType), matching
Expand Down
6 changes: 6 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions tslang/test/tester/tests/00rc_splice_result_owned.ts
Original file line number Diff line number Diff line change
@@ -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<T>.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<T> {
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<Item>();
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.");
}
Loading