diff --git a/README.md b/README.md index 64c871c63..35cc67b80 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ par with it on array loops — see [Performance](docs/performance.md). | WebAssembly | [docs/wasm.md](docs/wasm.md) | | Memory models (`gc`, `rc`, `none`) | [docs/memory-models.md](docs/memory-models.md) | | The `-mm=own` single-owner model | [docs/own-memory-model.md](docs/own-memory-model.md) | +| How ready `-mm=own` is | [docs/own-readiness.md](docs/own-readiness.md) | | Benchmarks against Rust and C++ | [docs/performance.md](docs/performance.md) | | C and C++ bindings with `tsbindgen` | [docs/tsbindgen.md](docs/tsbindgen.md) | | Calling C++ classes (`@cpp`) | [docs/cpp-classes.md](docs/cpp-classes.md) | diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index 908485fde..025fb7345 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -843,6 +843,7 @@ a reference gives it back on every path out of a function. ## Current limits `-mm=own` is still in development, and some valid TypeScript does not compile under it yet. +[own-readiness.md](own-readiness.md) says how far it is, with measurements. - The default library has an `-mm=own` build, and arrays, strings and their methods work. `Map` and `Set` work with number, boolean and string keys and values (`new Map()`, @@ -890,6 +891,15 @@ a reference gives it back on every path out of a function. (`fill`) is copied each time. - A parameter with a default (`constructor(private items: T[] = [])`) is kept like any other parameter, whether the caller passes it or not. +- A linked list whose nodes own their `next` works: a local that steps down it (`for (let c = + this.head; c; c = c.next)`) borrows the nodes, a new node goes in at the head (`n.next = this.head; + this.head = n`) or at the tail (`c.next = n`), and `this.head = this.head.next` or `c.next = + c.next.next` takes a node out. The node taken out is freed right there, so read what you need of + it first: `const h = this.head; const v = h.v; this.head = h.next; return v;`, not `return h.v` + after the assignment. Not accepted yet: a node made with the old head as an argument (`this.head = + new Node(v, this.head)`), a walk that assigns a parameter (`n = n.next`), and an insert that + recurses on a parameter and gives it back (`t.left = insert(t.left, v)`); write the last as a + method that recurses on `this`. - `Shared` cycles are never freed. - There are no lifetime annotations: a borrow that the compiler cannot follow is an error, even when the program would be correct. The fix is usually a move, a fresh value or `Shared`. diff --git a/docs/own-readiness.md b/docs/own-readiness.md new file mode 100644 index 000000000..006c88e48 --- /dev/null +++ b/docs/own-readiness.md @@ -0,0 +1,181 @@ +# `-mm=own`: readiness + +Where the single-owner memory model stands, measured on `main` at `363b13ce`, 2026-11-15, with the +Release compiler on Windows x64. The linked-list rows of [Shapes that matter](#shapes-that-matter) +were rejected in that measurement and are fixed by the change that adds this page; the table shows +them as they are with it. The model itself is described in +[own-memory-model.md](own-memory-model.md); the tiers and their exit criteria are in the +[production-readiness plan](production-readiness-plan.md). + +## Verdict + +**`-mm=own` is not ready for production. It is usable for new code written for it, and it is a +preview for porting existing TypeScript.** + +- Everything its design set out to build is built, and the tracker has no open defect against it. +- Nothing unsound was found in this round of measurement. +- It still rejects about one in five programs the other models accept, and it has no leak-free + way to write a back-reference. +- Memory-safety bugs were still being found in real code in the three days before this was written. +- None of the three tier 3 exit criteria in the plan is met. + +It stays in tier 3 ("experimental") in the plan's terms. + +## Scorecard + +| Area | Status | What it rests on | +| --- | --- | --- | +| Design | Complete | Spec phases 0-8 and the later designs (parameter decorators, facts from object files, immortal keepers, generic containers) are merged. One item is deferred: `WeakRef` under own. | +| Tracker | Clean | No open GitHub issue. The own items of `TSLANG_ISSUES.md` (31-33, 39-42, 47-85) are fixed, with one remnant: whole-program facts (item 48). | +| Soundness | No failure found, not proven | See [Soundness](#soundness). | +| Coverage of TypeScript | Partial | 707 of 882 corpus files (80%) compile. See [Coverage](#coverage). | +| Default library | Nearly complete | Its `-mm=own` build passes its tests; 2 of 230 are skipped as refused by design (`array_flatMap_shared`, `map_groupBy`). | +| Separate compilation | Works, with a cost | `--own-facts` and shared libraries carry facts. A call of a method of an exported class that is not `@final` still ends every borrow. | +| Platforms | In the release matrix | Windows, Linux, wasm32/wasm64 and Android run the own legs in CI. Only Windows was run for this document. | +| Tests | Broad | 560 files in `tslang/test/tester/own` (359 of them expected errors), and the general corpus runs under own in CI. | +| Real code | One program | native_ts_graphics compiles and passes 525/525 under own, after a 55-file source change. See [Real code](#real-code). | +| Tier 3 exit criteria | 0 of 3 | See [Exit criteria](#exit-criteria). | + +## Soundness + +The claim of the model is that an accepted program never frees a block twice, never reads a freed +one and never leaks. What was checked: + +- **Differential fuzzing.** `tslang/test/fuzz/mm_fuzz.py`, 400 seeds from 915000: 738 programs + checked, 353 accepted by own, each built ahead of time and compared with `-mm=gc`. No mismatch, + no crash, under own or rc. +- **Churn loops.** Four shapes that older notes or the guide called refused, and that compile today + (a parameter a generator assigns, a value merged from `?:` stored into a field and then used, a + new object passed through a function that returns its argument, a reassigned `Shared` + parameter): one to three million iterations each, ahead of time. Output equal to gc's, and peak + memory flat at 5.7 MB. +- **The corpus.** No file made the compiler crash under own. + +This is weak evidence, for three reasons. Half of what the fuzzer generates is rejected, so about +350 programs ran. It was Windows only, and the Windows heap hides double frees that glibc reports. +And compiling one real codebase turned up, between 2026-11-13 and 2026-11-15, a read of freed +memory (item 50), a double free (item 83), a fix that leaked 2.7 GB and was backed out (item 80), +and a compiler crash in about one run in 500 (#867). All are fixed. The rate says more are there. + +## Coverage + +Every file of `tslang/test/tester/tests` was compiled with `--no-default-lib --opt --opt_level=3` +under gc and under own. Of 915 files, 882 compile under gc; of those, 707 compile under own and 175 +are rejected. The corpus holds own's own regression tests, which raises the figure, and a few +multi-file tests compiled alone without their facts, which lowers it. + +The rejections, by message: + +| Count | Message | Kind | +| --- | --- | --- | +| 23 | takes a second reference; cannot prove a move or a borrow here yet | Not yet | +| 19 | borrows a field but is used after it may be released or overwritten | Rule, often conservative | +| 19 | used after its value was moved | Rule | +| 15 | borrows a field and cannot be stored, returned or captured | Rule | +| 9 | an object literal that holds a value it owns is moved only into a local or returned | Not yet | +| 8 | a parameter, kept here, but the callers cannot be made to give it up | Needs `@keeps` | +| 7 | merged from several branches (`?:`, `&&`, `\|\|`, `??`, a loop) | Not yet | +| 7 + 6 | borrows an element (stored, or used after a release) | Rule | +| 6 + 4 | borrows the result of a call | Rule | +| 5 | captured by a closure that escapes, and still used | By design | +| 5 | a global's value lent to a call that may release it | Conservative | +| 42 | 22 other messages, one to five files each | Mixed | + +About 50 of the 175 say in the message that the compiler cannot prove something yet. The rest are +the model's rules; some of those are the single-owner design working as intended, and some are the +analysis being more careful than the program needs. + +### Shapes that matter + +Probed one by one under the JIT: + +| Shape | Result | +| --- | --- | +| Walking a list: `for (let c = this.head; c; c = c.next)` | Compiles and runs | +| List `push`: `n.next = this.head; this.head = n` | Compiles and runs | +| Appending at the tail, cutting the tail: `c.next = n`, `c.next = null` after the walk | Compiles and runs | +| List `pop`: `this.head = this.head.next`; unlinking: `c.next = c.next.next` | Compiles and runs | +| Using the popped node afterwards: `this.head = h.next; return h.v` | Rejected: the node is freed by the assignment; read it first | +| Push through a constructor: `this.head = new Node(v, this.head)` | Rejected | +| Walking by assigning a parameter: `n = n.next` | Rejected | +| Tree insert by recursion on a parameter: `t.l = ins(t.l, v)` | Rejected | +| Tree insert and sum as methods recursing on `this` | Compiles and runs | +| Parent link: `k.parent = this` | Rejected; the answer is `Shared`, whose cycles leak | +| `new WeakRef(a)` | Rejected: built on the collector | +| `new Map()`, `filter` over class instances | Rejected by design; `Shared` | +| Swap through a local: `let t = a[i]; a[i] = a[j]; a[j] = t` | Rejected by design; `[a[i], a[j]] = [a[j], a[i]]` | +| `x ??= new B()` on a parameter | Rejected ("merged from several branches") | +| A closure that assigns a parameter it captures | Rejected | +| `m.forEach((v, k) => { total += v; })` on a `Map` or a `Set` | Compiles and runs | +| `m.forEach((v, k) => { names.push(k); })` | Rejected, with no source position in the error | +| try/catch/finally, unions, `any`, async functions (11 corpus files) | Compile; async was not run for this document | + +## Real code + +native_ts_graphics (the `build-own` of powder_sandbox) is the one outside codebase compiled under +own. The sweep that got it there produced compiler items 50-85 in three days, and the source needed +a 55-file change (+432 / -351): getters that return an object of the caller's own instead of a lent +one, statements reordered so a value is used before it is given away, global rows copied where +kept, `@final` on classes, and swaps in place of a sort through a temporary. With those it passes +its 525 tests under own, as under gc and rc. + +That is the realistic cost of a port today: the program works, and a noticeable share of it is +rewritten to say who owns what. + +## Performance + +Covered in [performance.md](performance.md) and [memory-models.md](memory-models.md). In short: the +ray tracer under own runs within about 1.4x of the Rust version with `@value` vectors, memory stays +at the working set, and there is no collector or count at run time. Performance is not what holds +the model back. + +## Exit criteria + +From the plan's Phase 5 row for `-mm=own`: + +| Criterion | Met | Notes | +| --- | --- | --- | +| Close the open items: optional accessor fields, capturing callbacks | No | An optional interface field filled by an accessor is a clean error under every model, not supported. Capturing callbacks over `Map` and `Set` work for numbers and fail when the callback pushes a key into a captured array. | +| A written specification of the ownership rules | No | The rules exist as a design log of about 5,000 lines (`tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md`) and as the user guide. Neither is a specification someone could implement or check against. | +| Error messages reviewed by someone new to the model | No | Not done. Without `--di` most messages say `'this value'` in place of the variable's name. | + +The tier also repeats the Phase 2 gate for its own scope: no open crash or miscompile, an +assertions-enabled CI leg, and a soak run. The first holds today; the other two do not exist for own. + +## What is left, in order + +1. **A non-owning back-reference.** `WeakRef` under own, or a declared borrowing field. Without + it a tree with parent links either does not compile or leaks. +2. **The rest of linked structures.** A list now works; a node built from the old head, a walk + that assigns a parameter and the recursive tree insert do not. These are what a newcomer writes + first. +3. **The "not yet" messages.** Second references the compiler cannot classify, values merged from + branches, object literals that hold owned values. +4. **Capturing callbacks over `Map` and `Set`**, and a source position for the error they give. +5. **Whole-program facts**, so a class nobody extends does not need `@final` to keep its callers' + borrows. +6. **The exit criteria**: a rules specification, an outside review of the messages, variable names + in messages without `--di`. +7. **Soundness evidence at scale**: fuzzing on Linux with a perturbing allocator, an AddressSanitizer + run of compiled own programs, a second and third outside codebase. + +## What it is ready for + +| Use | Ready | +| --- | --- | +| New code written for own: trees of owners, values, arrays, strings, `Shared` where shared | Yes, as a preview | +| Numeric and allocation-heavy kernels where a collector or counting costs too much | Yes, as a preview | +| Porting an existing TypeScript program unchanged | No | +| Programs built on graphs with back-references | No | +| A release that promises memory safety to outside users | No | + +## How this was measured + +- Compiler: `main` at `363b13ce`, Release, Windows 11 x64. Default library built the same day. +- Corpus sweep: each file of `tslang/test/tester/tests` with `--emit=llvm --no-default-lib --opt + --opt_level=3`, under `-mm=gc` and `-mm=own`, compile only. +- Fuzzer: `python mm_fuzz.py --jobs 4 run --count 400 --seed 915000`. +- Probes and churn loops: small files, JIT for accept or reject, ahead-of-time builds for output and + peak working set against gc. +- Not run: the full ctest suite, the default library's tests, anything on Linux, wasm or Android. + Those lines of the scorecard come from the CI configuration and the repository's records. diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index de8418ed7..7ec882617 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -5795,3 +5795,58 @@ function does not own" (`own_err_delete_param`). A local read after its `delete` Tests: `own_param_assigned` (own, rc, gc), `own_delete` (own, rc), and the seven corpus files only these two stopped, under own (`test-*-own-flipped-*`). + +## 82. A linked list: a local that walks down, a node taken out, 2026-11-16 + +A singly linked list whose nodes own their `next` was refused in every method a newcomer writes first: +the walk, `push`, the append at the tail and `pop`. Four rules, all in the inference pass. + +**A `let` given only borrows owns nothing (`chainOf`).** A read through such a `let` (TSLANG 56) had +the `let`'s slot for its root, and the slot's assignments and release for the ends of what was read: +`c = c.next` in a loop ended its own borrow. The chain now goes through the `let` to the roots of every +value it is given (its initializer and each assignment), with `anyPlace` set: what is read through `c` +lasts as long as what `c` borrowed, and any release that could hold it ends it. The declaration is a +kill for the uses of an assigned borrow (`collectBorrowerUses`), as an assignment is: a path from a +drop back to the declaration does not carry the old value. Measured on a function with 8,000 reads +through one such `let`: 63-69 s before, 7.4-7.8 s now (the slot's ends were found again for each read). + +**A field of what was read, overwritten, frees nothing above it (`checkPlaceRead`, `frees`).** `if (c) +c.next = null` and `c.next = n` after a walk release a field of the borrowed node, not the node: an +owner is never under what it owns. A release of a field or an element is no drop for a read when its +object is the value read, or is read out of it through owning places (an element, or a class field +that is no back-reference and no borrowing field; not through a handle). Through a `let` the read is +assigned to, the same holds when the `let` only walks down from there: every assignment that can +follow gives it a value read out of what it holds, and its declaration cannot run again. A `let` that +may be given the head again is refused (`own_err_list_walk_up`), as is a use of what was read out of +the field before (`own_err_list_lookahead_cut`). + +**The refill may store the object the taker is a field of (`refilledFieldTakeOf`).** `n.next = +this.head; this.head = n;` is TSLANG 49's move of a field into another place with the source refilled +right after; rc's retain of the refill's value stopped the match unless that value was `null`. The +retain of the value the refill stores is let through. + +**A value taken out of the object its place holds (`splicedFieldTakeOf`).** `this.head = +this.head.next`, `prev.next = prev.next.next`, also through a `const` that names the object. The store's +release of the place frees the object, and with it what its field holds. The read is a take: the field +is given up right before the release, and the value is the place's. The match is local: the read, rc's +retain, the place's release and the store in one block with only reads between, the object a read of +the same field of the same object as the store's place, both owning class fields that hold an object +or null. That the place still holds the object there is the borrow check of the object's own read; +the store is no use of that borrow (`splicedTakers`, filled where a function's state is reset), and a +use of the object after it is refused (`own_err_list_pop_used_after`): read what is needed first. + +The object may be one an append-only global keeps too (spec 25, the keepers), which the release does +not free. Emptying its field would then show: gc keeps `KEEP[0].next`. So the field is given up by a +new op, `ts.TakeOutOfField(object, field)`: a mortal object's field is emptied, an immortal object +keeps it and the value it holds is made immortal, since the kept object and the place both hold it +from then on (`OwnershipRoutineLogic::emitTakeOutOfField`; `00linked_list_kept`). + +Still refused: the popped node used after the assignment, a node made with the old head as an argument +(`this.head = new Node(v, this.head)`: a call between the read and the refill), a walk that assigns a +parameter (`n = n.next`: an assigned parameter is kept, section 81), and an insert that recurses on a +parameter and gives it back (`t.left = insert(t.left, v)`). + +Tests: `00linked_list` and `00linked_list_kept` (own, rc, gc, none; also `-noopt` under own), and +`own_err_list_walk_dropped`, `_walk_call_drops`, `_lookahead_cut`, `_walk_up`, `_pop_used_after`, +`_next_kept_twice`, `_refill_from_param`. The corpus, the mm fuzzer (1,000 seeds) and native_ts_graphics' +own build are unchanged by it: no file newly refused. diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index 559a5302b..14dfde483 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -562,6 +562,35 @@ class OwnershipRoutineLogic }); } + // ts.TakeOutOfField: the pointer the field at `fieldPtr` holds goes to another owner right before + // the object at `objectPtr` (never null: the field was read through it) is released. A mortal + // object is freed by that release, so its field is emptied. An immortal one keeps the field, + // and what the field holds, which two places hold from then on, is made immortal. + void emitTakeOutOfField(mlir::Value objectPtr, mlir::Value fieldPtr) + { + TypeHelper th(rewriter); + auto loc = op->getLoc(); + + auto isMortal = emitIsMortal(objectPtr); + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto *mortalBlock = rewriter.createBlock(continuationBlock); + auto *immortalBlock = rewriter.createBlock(continuationBlock); + + rewriter.setInsertionPointToEnd(mortalBlock); + rewriter.create(loc, rewriter.create(loc, th.getPtrType()), fieldPtr); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(immortalBlock); + auto held = rewriter.create(loc, th.getPtrType(), fieldPtr); + emitMarkImmortalIfMortal(held); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + rewriter.create(loc, isMortal, mortalBlock, immortalBlock); + rewriter.setInsertionPointToStart(continuationBlock); + } + // === Sharing between threads === // // `Shared.share(v)` (ts.MarkShared) sets the shared bit (Defines.h, beside HEAP_BLOCK_IMMORTAL) diff --git a/tslang/include/TypeScript/TypeScriptOps.td b/tslang/include/TypeScript/TypeScriptOps.td index 978049a2b..088d0bc60 100644 --- a/tslang/include/TypeScript/TypeScriptOps.td +++ b/tslang/include/TypeScript/TypeScriptOps.td @@ -695,6 +695,22 @@ def TypeScript_MarkImmortalCellOp : TypeScript_Op<"MarkImmortalCell"> { let arguments = (ins TypeScript_Ref:$slot); } +def TypeScript_TakeOutOfFieldOp : TypeScript_Op<"TakeOutOfField"> { + let summary = "hand a field's value over before its object is released"; + let description = [{ + -mm=own: `this.head = this.head.next`. The place that holds `object` is about to be given the + value `field` (a field of `object`) holds, and its release of `object` comes right after this. + A mortal `object` is freed by that release, so its field is emptied here and the release frees + nothing of the value. An immortal `object` (one an append-only global keeps, MakeImmortal) + is not freed: it keeps its field, and the value, which two places hold from then on, is made + immortal too. + + Only OwnershipInferencePass makes it; under the other models it is never there. + }]; + + let arguments = (ins AnyType:$object, TypeScript_Ref:$field); +} + def TypeScript_SizeOfOp : TypeScript_Op<"SizeOf", [Pure]> { let summary = "size of type"; let description = [{ diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 98c98be38..11580603a 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -927,6 +927,34 @@ class MarkImmortalCellOpLowering : public TsLlvmPattern +{ + public: + using TsLlvmPattern::TsLlvmPattern; + + LogicalResult matchAndRewrite(mlir_ts::TakeOutOfFieldOp op, Adaptor transformed, + ConversionPatternRewriter &rewriter) const final + { + if (tsLlvmContext->compileOptions.memoryModel == MemoryModelOwn) + { + auto fieldType = cast(op.getField().getType()).getElementType(); + if (!isa(getTypeConverter()->convertType(fieldType))) + { + return op.emitError("-mm=own cannot take a value of this type out of a field"); + } + + OwnershipRoutineLogic orl(op, rewriter, getTypeConverter(), tsLlvmContext->compileOptions); + orl.emitTakeOutOfField(transformed.getObject(), transformed.getField()); + } + + rewriter.eraseOp(op); + return mlir::success(); + } +}; + class SizeOfOpLowering : public TsLlvmPattern { public: @@ -9957,7 +9985,7 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() PointerOffsetRefOpLowering, LogicalBinaryOpLowering, NullOpLowering, NewOpLowering, CreateTupleOpLowering, DeconstructTupleOpLowering, CreateArrayOpLowering, NewEmptyArrayOpLowering, NewArrayOpLowering, ArrayPushOpLowering, ArrayPopOpLowering, ArrayUnshiftOpLowering, ArrayShiftOpLowering, ArraySpliceOpLowering, ArrayViewOpLowering, DeleteOpLowering, - ParseFloatOpLowering, ParseIntOpLowering, IsNaNOpLowering, PrintOpLowering, ConvertFOpLowering, StoreOpLowering, SwapOpLowering, SizeOfOpLowering, TypeDescriptorOpLowering, RetainOpLowering, ReleaseOpLowering, RetainSlotOpLowering, ReleaseSlotOpLowering, RetainCellOpLowering, ReleaseCellOpLowering, ReleaseContentsOpLowering, MakeImmortalOpLowering, MarkImmortalCellOpLowering, + ParseFloatOpLowering, ParseIntOpLowering, IsNaNOpLowering, PrintOpLowering, ConvertFOpLowering, StoreOpLowering, SwapOpLowering, SizeOfOpLowering, TypeDescriptorOpLowering, RetainOpLowering, ReleaseOpLowering, RetainSlotOpLowering, ReleaseSlotOpLowering, RetainCellOpLowering, ReleaseCellOpLowering, ReleaseContentsOpLowering, MakeImmortalOpLowering, MarkImmortalCellOpLowering, TakeOutOfFieldOpLowering, InsertPropertyOpLowering, LengthOfOpLowering, SetLengthOfOpLowering, StringLengthOpLowering, StringResizeOpLowering, StringConcatOpLowering, StringCopyOpLowering, SharedNewOpLowering, SharedValueRefOpLowering, SharedCountOpLowering, MarkSharedOpLowering, StringCompareOpLowering, AnyCompareOpLowering, CharToStringOpLowering, UndefOpLowering, CopyStructOpLowering, MemoryCopyOpLowering, MemoryMoveOpLowering, diff --git a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp index fb5d640a6..d43182e87 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp @@ -329,6 +329,9 @@ namespace own_inference auto onlyBorrows = borrowsOnly(borrower); if (onlyBorrows) { + // its declaration gives it its initializer again, as an assignment gives it a value: what + // it held before, in an earlier round of a loop around the declaration, it holds no more + kills.push_back(borrower); for (auto *user : borrower.getResult().getUsers()) { if (isAssignmentOf(user, borrower)) @@ -658,6 +661,14 @@ namespace own_inference continue; } + // the store of a value taken out of the object its place holds (`this.head = + // this.head.next`): the value is the place's from then on, which checkPlaceRead decides + // for the read of it. No use of the object, which the store's release frees. + if (splicedTakers.contains(user)) + { + continue; + } + uses.push_back({user, pending.kills, isCall(user) && isCalleeOperand(use)}); // a move out of a record's field, its retain and its taker, which checkPlaceRead // decides (spec 48): a use of the borrow until it is taken diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index c5ff09db6..b6952a808 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -412,6 +412,16 @@ namespace own_inference captureRetains.clear(); callCopies.clear(); returnedParams.clear(); + // the stores that take a value out of the object their place holds, for every walk of this + // function to see (never another function's: its operations are gone, their addresses reused) + splicedTakers.clear(); + f->walk([&](mlir_ts::LoadOp loadOp) { + FieldTake take; + if (splicedFieldTakeOf(loadOp, take)) + { + splicedTakers.insert(take.taker); + } + }); returnsBorrowOf = resultBorrows(f); returnsRecordOf = isRecordResult(f) ? resultBorrowsAll(f) : llvm::SmallVector(); records.clear(); diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index fa52f4b02..ef4550a77 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -260,6 +260,9 @@ class OwnershipInferencePass // A move out of a field of an owning record local, being checked (spec 48): its retain and its // taker, which walkBorrowed takes for uses of the borrow and not for keeps llvm::SmallPtrSet takenOps; + // The stores of splicedFieldTakeOf's takes: each stores a value that is its place's own from then + // on, so it is no use of the object the value was read out of, which the store's release frees + llvm::SmallPtrSet splicedTakers; // each closure over a cell, by its `ts.CreateBoundFunction` llvm::DenseMap closureOf; // each generator's capture box, which escapes with its state object (classifyStateBoxes) @@ -900,6 +903,12 @@ class OwnershipInferencePass // refill's release, which would free what was read, hands it over to the taker. `refill` is that // release; nothing but reads and stores of locals runs between the read and the taker. bool refilledFieldTakeOf(mlir::Operation *read, mlir::Operation *kept, FieldTake &take, mlir::Operation *&refill); + // ...or is `read` a read of a field of the object a place holds, stored into that very place: + // `this.head = this.head.next`, `prev.next = prev.next.next`, also through a `const` that names the + // object. The store's release of the place frees the object, and with it what the field holds: + // the field is emptied right before the release, and its value is the place's. `take.release` is + // that release. + bool splicedFieldTakeOf(mlir::Operation *read, FieldTake &take); // Can `call` be given a copy of `value`, a string read out of a place, in its place (callCopies)? static bool copiesInto(mlir::Operation *call, mlir::Value value); // Does `store` put `value`, a string read out of a place, into another place with no retain of diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 93b656960..24aa098c9 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -421,13 +421,32 @@ namespace own_inference continue; } - if (base.getDefiningOp()) + // A `let` given only borrows (TSLANG 56) owns nothing: its assignments and its release + // free nothing, so what is read through it lasts as long as what the `let` borrowed - + // each value it is given, somewhere under those values' own roots. `c = c.next` in a + // loop over a list is the case: the nodes are the list's, not `c`'s. + auto slotValue = base.getDefiningOp() ? base : mlir::Value(); + if (auto slotLoad = slotValue ? mlir_ts::LoadOp() : slotLoadOf(base)) { - chain.roots.push_back({base, Chain::Slot}); + slotValue = slotLoad.getReference(); } - else if (auto slotLoad = slotLoadOf(base)) + + auto borrowingLet = slotValue ? slotValue.getDefiningOp() : mlir_ts::VariableOp(); + if (borrowingLet && borrowsOnly(borrowingLet)) + { + chain.anyPlace = true; + bases.push_back(rootOf(borrowingLet.getInitializer())); + for (auto *user : slotValue.getUsers()) + { + if (isAssignmentOf(user, borrowingLet)) + { + bases.push_back(rootOf(mlir::cast(user).getValue())); + } + } + } + else if (slotValue) { - chain.roots.push_back({slotLoad.getReference(), Chain::Slot}); + chain.roots.push_back({slotValue, Chain::Slot}); } else { @@ -870,6 +889,9 @@ namespace own_inference llvm::SmallVector bookkeeping; mlir_ts::VariableOp letKeeps; mlir::Operation *letKept = nullptr; + // the local that borrows the read, and its declaration or the assignment that gives it the read + mlir_ts::VariableOp assignedLet; + mlir::Operation *assignment = nullptr; // A result that borrows arrives with rc's temporary release, or rc's consuming `let`, and // in a function returning a borrow of the same parameter it may go out with rc's retain. auto walk = [&]() { @@ -878,6 +900,8 @@ namespace own_inference bookkeeping.clear(); letKeeps = {}; letKept = nullptr; + assignedLet = {}; + assignment = nullptr; Keeping keeping{&bookkeeping, returnsBorrowOf >= 0 && borrowedParam(result) == returnsBorrowOf}; return walkBorrowed(result, read, uses, [&](mlir::Operation *user, llvm::ArrayRef kills) { auto borrower = borrowerOf(user, /*consumed=*/true); @@ -904,6 +928,9 @@ namespace own_inference borrower = assigned; } + assignedLet = borrower; + assignment = user; + name = varName(borrower); if (auto *keeps = collectBorrowerUses(borrower, kills, uses, bookkeeping, user); keeps && !letKept) { @@ -917,6 +944,44 @@ namespace own_inference auto *kept = walk(); + // A value taken out of a field of the object a place holds, by the store that gives the place + // that value (`this.head = this.head.next`): the store's release frees the object, so the + // field is emptied right before it and the value is the place's. Nothing else may keep the + // value, or use it after the store through what was read here. + if (FieldTake splice; !countedHandle && splicedFieldTakeOf(read, splice)) + { + takenOps.insert(splice.retain); + kept = walk(); + takenOps.clear(); + auto *after = kept ? kept : letKept; + for (auto &use : uses) + { + if (!after && use.op != splice.retain && reachableAfter(splice.taker, use.op, use.kills)) + { + after = use.op; + } + } + + if (after) + { + reportPlaceBorrowEscapes(after, name, place); + return false; + } + + if (!dryRun) + { + // (an object an append-only global keeps too is not freed: it keeps its field, and the + // value is made immortal - ts.TakeOutOfField) + mlir::OpBuilder builder(splice.release); + auto object = splice.field.getDefiningOp().getObjectRef(); + builder.create(splice.release->getLoc(), object, splice.field); + } + + bookkeeping.push_back(splice.retain); + toErase.insert(bookkeeping.begin(), bookkeeping.end()); + return true; + } + // A value taken out of a field of an owning record local by one move, the field read again // by nothing before the local ends (spec 48): the `value` of the record `next()` returns, in // a `for...of` or a spread that keeps what a generator yields @@ -1083,6 +1148,101 @@ namespace own_inference return false; } + // `c = c.next` walks down: the `let` holds the value read from here on, and every value it is + // given later is read out of the one it holds, so it is that value or one under it. A field + // or an element of what the `let` holds, overwritten (`c.next = n` at the end of a list), is + // then under the value read too, and frees nothing above it: an owner is never under what it + // owns. + // a place whose value is its object's own: an element of an array, or a field of a class + // object that is no back-reference and borrows nothing. What is read out of one is under + // the object, never above it + auto owningPlace = [&](mlir::Operation *placeOp) { + if (mlir::isa_and_nonnull(placeOp)) + { + return true; + } + + auto propertyRefOp = mlir::dyn_cast_or_null(placeOp); + return propertyRefOp && mlir::isa(propertyRefOp->getOperand(0).getType()) && + !isBackReferenceField(propertyRefOp.getResult()) && !isBorrowingField(propertyRefOp.getResult()); + }; + // is `value` the value read (or, with `underLet`, what the local holds), or read out of it + // through such places + auto readUnder = [&](mlir::Value value, bool underLet) { + for (auto root = rootOf(value); root;) + { + if (!underLet && root == result) + { + return true; + } + + auto loadOp = root.getDefiningOp(); + if (!loadOp || isHandle(root)) + { + return false; + } + + if (underLet && loadOp.getReference() == assignedLet.getResult()) + { + return true; + } + + auto *placeOp = loadOp.getReference().getDefiningOp(); + if (!owningPlace(placeOp)) + { + return false; + } + + root = rootOf(placeOp->getOperand(0)); + } + + return false; + }; + auto onlyWalksDown = [&]() { + // declared anew after it is given the read (a declaration in a loop): given its + // initializer again + auto *after = assignedLet && assignment == assignedLet.getOperation() ? assignment->getNextNode() : assignment; + if (!after || reachableAfter(after, assignedLet, llvm::ArrayRef())) + { + return false; + } + + return llvm::all_of(assignedLet.getResult().getUsers(), [&](mlir::Operation *user) { + return !isAssignmentOf(user, assignedLet) || + !(user == assignment || reachableAfter(assignment, user, llvm::ArrayRef())) || + readUnder(mlir::cast(user).getValue(), true); + }); + }; + auto walksDown = -1; + auto frees = [&](mlir::Operation *drop) { + auto releaseSlotOp = mlir::dyn_cast(drop); + auto *placeOp = releaseSlotOp ? releaseSlotOp.getSlot().getDefiningOp() : nullptr; + if (!mlir::isa_and_nonnull(placeOp)) + { + return true; + } + + // a field or an element of the value read itself (`if (c) c.next = null`), or of a value + // under it (`const d = c.next; if (d) d.next = null`) + auto object = placeOp->getOperand(0); + if (readUnder(object, false)) + { + return false; + } + + if (!assignedLet || !readUnder(object, true)) + { + return true; + } + + if (walksDown < 0) + { + walksDown = onlyWalksDown() ? 1 : 0; + } + + return walksDown == 0; + }; + for (auto *drop : drops) { if (countedStoreReleases.contains(drop)) @@ -1090,7 +1250,7 @@ namespace own_inference continue; } - if (dropsChain(drop, chain, read) && outlives(drop)) + if (frees(drop) && dropsChain(drop, chain, read) && outlives(drop)) { return false; } @@ -1485,6 +1645,17 @@ namespace own_inference if (!refillRelease && retained) { auto field = place.getDefiningOp(); + auto sameFieldAs = [&](mlir::Value slot) { + auto other = slot.getDefiningOp(); + return other && sameField(other, field); + }; + // a retain followed by the release of the field and the store of the retained value into it + auto refillsWith = [](mlir::Operation *retain, llvm::function_ref isField) { + auto releaseSlotOp = mlir::dyn_cast_or_null(retain->getNextNode()); + auto storeOp = releaseSlotOp ? mlir::dyn_cast_or_null(releaseSlotOp->getNextNode()) : mlir_ts::StoreOp(); + return storeOp && isField(releaseSlotOp.getSlot()) && storeOp.getReference() == releaseSlotOp.getSlot() && + storeOp.getValue() == retain->getOperand(0); + }; for (auto *op = field ? taker->getNextNode() : nullptr; op; op = op->getNextNode()) { if (auto releaseSlotOp = mlir::dyn_cast(op)) @@ -1500,8 +1671,10 @@ namespace own_inference break; } - // rc's retain of the refill's value, when that holds no block (`null`) - auto retainsNothing = mlir::isa(op) && holdsNoBlock(op->getOperand(0)); + // rc's retain of the refill's value: one that holds no block (`null`), or the value the + // refill stores (`n.next = this.head; this.head = n;`), which is checked where it is stored + auto retainsNothing = mlir::isa(op) && + (holdsNoBlock(op->getOperand(0)) || refillsWith(op, sameFieldAs)); // ...and that value, `null` cast to the field's union auto castsNothing = mlir::isa(op) && holdsNoBlock(op->getResult(0)); if (!mlir::isa(read); + auto field = loadOp ? loadOp.getReference().getDefiningOp() : mlir_ts::PropertyRefOp(); + if (!field || !mlir::isa(field.getObjectRef().getType()) || isBackReferenceField(field.getResult()) || + isBorrowingField(field.getResult()) || isCopyableValueAt(read, loadOp.getType()) || isHandle(loadOp.getResult())) + { + return false; + } + + // ...that holds a class object or null: one pointer, which ts.TakeOutOfField empties + auto holdsObjectOrNull = mlir::isa(loadOp.getType()); + if (auto unionType = mlir::dyn_cast(loadOp.getType())) + { + auto types = unionType.getTypes(); + holdsObjectOrNull = llvm::all_of(types, [](mlir::Type type) { return mlir::isa(type); }) && + llvm::count_if(types, [](mlir::Type type) { return mlir::isa(type); }) == 1; + } + + if (!holdsObjectOrNull) + { + return false; + } + + // rc's retain of the read, in its block, and the store it is for + mlir_ts::RetainOp retainOp; + for (auto *user : loadOp->getUsers()) + { + if (auto found = mlir::dyn_cast(user); found && found->getBlock() == read->getBlock()) + { + if (retainOp) + { + return false; + } + + retainOp = found; + } + } + + auto storeOp = mlir::dyn_cast_or_null(retainOp ? takerOf(retainOp, loadOp.getResult()) : nullptr); + if (!storeOp || storeOp.getValue() != loadOp.getResult() || storeOp->getBlock() != read->getBlock()) + { + return false; + } + + // the store's place, released right before it: a field of a class object that owns what it + // holds, so that the release frees the object read from + auto place = storeOp.getReference().getDefiningOp(); + auto release = mlir::dyn_cast_or_null(storeOp->getPrevNode()); + if (!place || !release || release.getSlot() != storeOp.getReference() || + !mlir::isa(place.getObjectRef().getType()) || isBackReferenceField(place.getResult()) || + isBorrowingField(place.getResult())) + { + return false; + } + + // the object read from is what that place holds: a read of the same field of the same object + // (the borrow check of that read sees to it that the place still holds it here) + auto held = rootOf(field.getObjectRef()).getDefiningOp(); + auto heldPlace = held ? held.getReference().getDefiningOp() : mlir_ts::PropertyRefOp(); + auto sameObjectHere = [&](mlir::Value object, mlir::Value other) { + if (sameObject(object, other)) + { + return true; + } + + // two reads of one local in a block, with no assignment of it between + auto first = object.getDefiningOp(); + auto second = other.getDefiningOp(); + if (!first || !second || first.getReference() != second.getReference() || + !first.getReference().getDefiningOp() || first->getBlock() != second->getBlock()) + { + return false; + } + + if (!first->isBeforeInBlock(second)) + { + std::swap(first, second); + } + + for (auto *op = first->getNextNode(); op && op != second.getOperation(); op = op->getNextNode()) + { + if (auto assigned = mlir::dyn_cast(op); assigned && assigned.getReference() == first.getReference()) + { + return false; + } + + if (op->getNumRegions() > 0 || isCall(op)) + { + return false; + } + } + + return true; + }; + if (!heldPlace || isHandle(held.getResult()) || heldPlace.getPosition() != place.getPosition() || + heldPlace.getType() != place.getType() || !sameObjectHere(rootOf(heldPlace.getObjectRef()), rootOf(place.getObjectRef()))) + { + return false; + } + + // from the read to the store: reads, rc's retain and the release, nothing that runs other code + for (auto *op = read->getNextNode(); op && op != storeOp.getOperation(); op = op->getNextNode()) + { + if (op != release.getOperation() && + !mlir::isa(op) && !isView(op)) + { + return false; + } + } + + take.retain = retainOp; + take.taker = storeOp; + take.field = field.getResult(); + take.release = release; + return true; + } + bool OwnershipInferencePass::namedFieldTakeOf(mlir::Operation *read, mlir::Operation *retain, mlir_ts::PropertyRefOp field, FieldTake &take) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 96b1e648e..a9f771503 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1946,6 +1946,14 @@ foreach(issues_mm gc rc none own) tslang_add_test(NAME test-compile-${issues_test}-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${issues_test}.ts") endforeach() endforeach() +# A singly linked list: walked by a local that steps down it, pushed on, appended to, cut, popped and unlinked; +# and a node popped off a list that an append-only global keeps too +foreach(issues_mm gc rc none own) + foreach(list_test 00linked_list 00linked_list_kept) + tslang_add_test(NAME test-jit-${list_test}-${issues_mm} COMMAND test-runner -jit -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${list_test}.ts") + tslang_add_test(NAME test-compile-${list_test}-${issues_mm} COMMAND test-runner -mm=${issues_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${list_test}.ts") + endforeach() +endforeach() # TSLANG_ISSUES 37, 38: a program declares its own Symbol, as a function or as a class foreach(issues_mm gc rc none own) foreach(symbol_test 00tslang_issue_37 00tslang_issue_38) @@ -5515,6 +5523,13 @@ set(own_error_cases "own_err_fill_other_field|:20:11: error: '[^']*' borrows the result of 'Dev.getQ' but is used here after it may be released or overwritten" "own_err_fill_after_fill_call|:19:20: error: '[^']*' borrows the result of 'Dev.fill' but is used here after it may be released or overwritten" "own_err_field_moved_call_between|:12:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" + "own_err_list_walk_dropped|:16:17: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_list_walk_call_drops|:16:17: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_list_lookahead_cut|:17:20: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_list_walk_up|:21:24: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_list_pop_used_after|:14:16: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_list_next_kept_twice|:14:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" + "own_err_list_refill_from_param|:13:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" "own_err_override_lends|:13:5: error: method 'read' returns a borrow .* but the method of 'Base' it overrides does not" "own_err_moved_field_overwritten|:15:25: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_moved_param_field_dropped|:13:15: error: '[^']*' borrows a field but is used here after it may be released or overwritten" @@ -6668,7 +6683,7 @@ endforeach() tslang_add_test(NAME test-compile-debug-shared-export-import-class-implements-interface-abstract COMMAND test-runner -shared -noopt "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_abstract.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_abstract.ts") tslang_add_test(NAME test-jit-debug-shared-export-import-class-implements-interface-abstract COMMAND test-runner -jit -shared -noopt "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_abstract.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_abstract.ts") foreach(debug_own_test own/own_interface_getter_read own/own_immortal_keepers tests/00nullable_interface_to_interface - stack-promotion/to_any) + stack-promotion/to_any tests/00linked_list tests/00linked_list_kept) get_filename_component(debug_own_name ${debug_own_test} NAME) tslang_add_test(NAME test-jit-debug-own-${debug_own_name} COMMAND test-runner -jit -mm=own -noopt "${PROJECT_SOURCE_DIR}/test/tester/${debug_own_test}.ts") tslang_add_test(NAME test-compile-debug-own-${debug_own_name} COMMAND test-runner -mm=own -noopt "${PROJECT_SOURCE_DIR}/test/tester/${debug_own_test}.ts") diff --git a/tslang/test/tester/own/own_err_list_lookahead_cut.ts b/tslang/test/tester/own/own_err_list_lookahead_cut.ts new file mode 100644 index 000000000..78f464860 --- /dev/null +++ b/tslang/test/tester/own/own_err_list_lookahead_cut.ts @@ -0,0 +1,21 @@ +// A field of the node a walking local holds may be overwritten (the node itself stays) - but not +// while something read out of that field is still used: `k` names the node `c.next = null` frees. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + f() { + let c = this.head; + if (!c) return -1; + c = c.next; + if (!c) return -1; + const k = c.next; + c.next = null; + return k ? k.v : -2; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/own/own_err_list_next_kept_twice.ts b/tslang/test/tester/own/own_err_list_next_kept_twice.ts new file mode 100644 index 000000000..5cfd3474e --- /dev/null +++ b/tslang/test/tester/own/own_err_list_next_kept_twice.ts @@ -0,0 +1,20 @@ +// The node after the head goes to the list where the head is replaced by it; it cannot go to +// another place too. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + other: Node | null = null; + f() { + const h = this.head; + if (!h) return -1; + this.other = h.next; + this.head = h.next; + return 1; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/own/own_err_list_pop_used_after.ts b/tslang/test/tester/own/own_err_list_pop_used_after.ts new file mode 100644 index 000000000..8e78b6689 --- /dev/null +++ b/tslang/test/tester/own/own_err_list_pop_used_after.ts @@ -0,0 +1,18 @@ +// `this.head = h.next` frees the node `h` names and gives the list its `next`: `h` may not be used +// after it. Read what is needed of the node first (`const v = h.v`). +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + f(): number { + const h = this.head; + if (!h) return -1; + this.head = h.next; + return h.v; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/own/own_err_list_refill_from_param.ts b/tslang/test/tester/own/own_err_list_refill_from_param.ts new file mode 100644 index 000000000..c13b02a50 --- /dev/null +++ b/tslang/test/tester/own/own_err_list_refill_from_param.ts @@ -0,0 +1,18 @@ +// `n.next = this.head; this.head = n.next;` moves the head into `n` and back when `n` is a node this +// function made (it is freed with nothing in it). A node of the caller's lives on: the list cannot +// take back what the node keeps. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + f(n: Node) { + n.next = this.head; + this.head = n.next; + return 1; + } +} + +function main() { const l = new List(); l.head = new Node(1); const n = new Node(2); print(l.f(n), n.v); } diff --git a/tslang/test/tester/own/own_err_list_walk_call_drops.ts b/tslang/test/tester/own/own_err_list_walk_call_drops.ts new file mode 100644 index 000000000..ad595e2d4 --- /dev/null +++ b/tslang/test/tester/own/own_err_list_walk_call_drops.ts @@ -0,0 +1,23 @@ +// A call that may free the list, made while a local walks it: the local's node may be gone after it. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + clear() { this.head = null; } + f() { + let s = 0.0; + let c = this.head; + while (c) { + s += c.v; + this.clear(); + c = c.next; + } + + return s; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/own/own_err_list_walk_dropped.ts b/tslang/test/tester/own/own_err_list_walk_dropped.ts new file mode 100644 index 000000000..e6d9eb552 --- /dev/null +++ b/tslang/test/tester/own/own_err_list_walk_dropped.ts @@ -0,0 +1,23 @@ +// A local that walks a list borrows its nodes: the list given up inside the loop frees them, and +// the next step would read a freed node. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + f() { + let s = 0.0; + let c = this.head; + while (c) { + s += c.v; + this.head = null; + c = c.next; + } + + return s; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/own/own_err_list_walk_up.ts b/tslang/test/tester/own/own_err_list_walk_up.ts new file mode 100644 index 000000000..4b152452f --- /dev/null +++ b/tslang/test/tester/own/own_err_list_walk_up.ts @@ -0,0 +1,28 @@ +// A walking local that may be given the head again does not only step down the list: a field of +// what it holds, overwritten, may free a node an earlier step reached (`m`). +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + f() { + let c = this.head; + let k = -1; + while (c) { + const m = c.next; + c = c.next; + if (c && c.v == 3) { + c = this.head; + } + + if (c) c.next = null; + if (m) k = m.v; + } + + return k; + } +} + +function main() { const l = new List(); l.head = new Node(1); print(l.f()); } diff --git a/tslang/test/tester/tests/00linked_list.ts b/tslang/test/tester/tests/00linked_list.ts new file mode 100644 index 000000000..bb8f2bb95 --- /dev/null +++ b/tslang/test/tester/tests/00linked_list.ts @@ -0,0 +1,173 @@ +// A singly linked list whose nodes own their `next`: walked by a local that steps down it, pushed +// on at the head, appended at the tail, cut, popped and unlinked. Under -mm=own the walking local +// borrows the nodes, and a node taken out of the list is freed where its place is given its `next`. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +class List { + head: Node | null = null; + + push(v: number) { + const n = new Node(v); + n.next = this.head; + this.head = n; + } + + append(v: number) { + const n = new Node(v); + let c = this.head; + if (!c) { + this.head = n; + return; + } + + while (c.next) { + c = c.next; + } + + c.next = n; + } + + sum() { + let s = 0.0; + for (let c = this.head; c; c = c.next) s += c.v; + return s; + } + + count() { + let n = 0; + let c = this.head; + while (c) { + n++; + c = c.next; + } + + return n; + } + + find(v: number) { + let c = this.head; + while (c) { + if (c.v == v) return true; + c = c.next; + } + + return false; + } + + // drops every node after the first that holds `v` + truncate(v: number) { + let c = this.head; + while (c && c.v != v) { + c = c.next; + } + + if (c) c.next = null; + } + + pop(): number { + const h = this.head; + if (!h) return -1; + const v = h.v; + this.head = h.next; + return v; + } + + shift(): number { + if (!this.head) return -1; + const v = this.head.v; + this.head = this.head.next; + return v; + } + + // unlinks the node after the first that holds `v` + removeAfter(v: number) { + let c = this.head; + while (c) { + if (c.v == v && c.next) { + c.next = c.next.next; + return true; + } + + c = c.next; + } + + return false; + } + + // a new node given the list, which takes it back out of the node at once: the node is freed + // with nothing in it + roundTrip() { + const n = new Node(3); + n.next = this.head; + this.head = n.next; + } + + clear() { + let n = 0; + while (this.head) { + this.head = this.head.next; + n++; + } + + return n; + } +} + +function total(l: List) { + let s = 0.0; + let c = l.head; + while (c) { + s += c.v; + c = c.next; + } + + return s; +} + +function cut(n: Node) { + if (n.next) { + n.next = n.next.next; + } +} + +function main() { + let checks = 0.0; + for (let i = 0; i < 2000; i++) { + const l = new List(); + for (let j = 1; j <= 5; j++) l.push(j); // 5 4 3 2 1 + l.append(10); + l.append(20); // 5 4 3 2 1 10 20 + assert(l.sum() == 45 && total(l) == 45 && l.count() == 7, "walk"); + assert(l.find(10) && !l.find(7), "find"); + + l.truncate(10); // 5 4 3 2 1 10 + assert(l.sum() == 25 && l.count() == 6, "truncate"); + + assert(l.pop() == 5 && l.shift() == 4, "pop"); // 3 2 1 10 + assert(l.removeAfter(2) && !l.removeAfter(10), "remove"); // 3 2 10 + assert(l.sum() == 15 && l.count() == 3, "after remove"); + + if (l.head) cut(l.head); // 3 10 + assert(l.sum() == 13, "cut"); + + // a local list, walked where it is made + let local = 0.0; + for (let c = l.head; c; c = c.next) local += c.v; + assert(local == 13, "local walk"); + + // nodes of the same size made anew: a freed node read by mistake would show here + const fresh = new Node(1000); + checks += l.sum() + fresh.v; + + l.roundTrip(); + assert(l.sum() == 13 && l.count() == 2, "round trip"); + + assert(l.clear() == 2 && l.count() == 0 && l.pop() == -1, "clear"); + } + + assert(checks == 2026000, "checks"); + print("done."); +} diff --git a/tslang/test/tester/tests/00linked_list_kept.ts b/tslang/test/tester/tests/00linked_list_kept.ts new file mode 100644 index 000000000..9abd35a5d --- /dev/null +++ b/tslang/test/tester/tests/00linked_list_kept.ts @@ -0,0 +1,66 @@ +// A node an append-only global keeps as well as a list: popping it off the list leaves the node as +// it is, its `next` included. Under -mm=own the node is immortal, so the list's release frees +// nothing, and the node after it, which the kept node and the list both hold then, is made immortal. +class Node { + next: Node | null = null; + constructor(public v: number) {} +} + +let KEEP: Node[] = []; + +function keep(n: Node) { + KEEP.push(n); +} + +class List { + head: Node | null = null; + + shift() { + if (this.head) this.head = this.head.next; + } + + sum() { + let s = 0.0; + for (let c = this.head; c; c = c.next) s += c.v; + return s; + } +} + +function build(l: List, i: number) { + const a = new Node(i); + const b = new Node(i + 1); + b.next = new Node(i + 2); + a.next = b; + keep(a); + l.head = KEEP[KEEP.length - 1]; +} + +function main() { + let checks = 0.0; + for (let i = 0; i < 500; i++) { + const l = new List(); + build(l, i); + assert(l.sum() == 3 * i + 3, "built"); + + l.shift(); // the kept node leaves the list, and keeps its `next` + const kept = KEEP[i]; + assert(kept.v == i && kept.next != null && kept.next.v == i + 1, "kept"); + assert(l.sum() == 2 * i + 3, "shifted"); + + l.shift(); // a node the kept node still holds + assert(kept.next != null && kept.next.next != null && kept.next.next.v == i + 2, "kept chain"); + assert(l.sum() == i + 2, "shifted twice"); + + const fresh = new Node(1000); + checks += fresh.v + l.sum(); + } + + let all = 0.0; + for (const n of KEEP) { + for (let c: Node | null = n; c; c = c.next) all += c.v; + } + + assert(all == 3 * 124750 + 1500, "all"); + assert(checks == 500000 + 124750 + 1000, "checks"); + print("done."); +}