Skip to content

-mm=own: fix #869's walk-cut use-after-free; ctor-built nodes, recursive tree insert and parameter walks compile - #870

Merged
ASDAlexander77 merged 2 commits into
mainfrom
own-linked-structures-2
Nov 16, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
own-linked-structures-2

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fix first: #869 let a walk read freed memory

Since #869, -mm=own accepted a walk that cuts the list ahead of itself, and the program crashed at run time:

let n = l.head;
while (n) { const nx = n.next; n.next = null; n = nx; }   // n.next = null frees the node nx names
let c = l.head;
while (c) { c.next = (c = c.next, null); }                // the place is the old node's, the store frees the new one

The compiler before #869 refused both. #869's rule ("a field of what a walking let holds, overwritten, frees nothing above the value read") held only once the let had been given that value. The let read the place hangs from must now come after the assignment, on every path, and no path from it may reach the release through the read again. No release tag contains #869.

Linked structures that now compile

Shape Before Now
this.head = new Node(v, this.head) refused compiles: the field is given up to the call (ts.TakeOutOfField), the result refills it
t.left = insert(t.left, v) (recursion on a field, given back) refused compiles, same rule
while (n) { k++; n = n.next; } on a parameter refused (parameter kept) compiles: a parameter that only walks down borrows
  • The field is given to a call only if the call never throws. That means a new signature fact, __own_no_throw: no throw, no async, and only calls to functions of this module that also never throw. Without it, a constructor that throws and is caught by a caller would leave the list empty where gc and rc keep it.
  • The call's other arguments must hold no heap block, or be the object new made. this.wrap(this.head) and f(t.l, t.l) stay errors.
  • An alias through a global cannot see the difference. What a global reaches is immortal, so TakeOutOfField leaves its field as it is.
  • The parameter rule applies only when every assignment reads out of what the local holds. It also requires the local to have no other use than reads and counts. A parameter given a new object on some path is still kept.

Spec section 83. The user guide and docs/own-readiness.md are updated, including the #869 hole in the soundness section.

Tests

  • 00linked_list_build, run under own, rc, gc and none, and also with -noopt under own.
  • The own_err_list_* cases:
    • own_err_list_unlink_walk and own_err_list_cut_in_step: the crash shapes. Both crash at run time on main.
    • own_err_list_push_ctor_throws, own_err_list_push_through_this, own_err_list_cut_through_alias.
  • own_err_tree_field_given_twice and own_err_param_walk_mixed.

Validation

  • Windows: ctest 7514/7514.
  • Linux: the own and list tests pass under glibc MALLOC_PERTURB_=165 with tcache off: 2234/2234.
  • Corpus: each file was compiled under own by main and by this branch. No file is newly refused. 00for_condition_narrowing newly compiles and prints what gc prints. On main, the two crash shapes compile and crash at run time.
  • mm fuzzer: 500 seeds, 936 programs, 455 accepted by own, 0 failures (run on the first commit; the second only refuses more).
  • native_ts_graphics, own build: the same 6 failing units before and after (first commit).
  • Default library, own build: 219/225 in each of the four configurations, with 6 skipped by design.
  • Memory: 20M iterations of the new shapes, built ahead of time, print done.. Private memory stays flat at 1.1 MB under own (10 s), against 2.9 MB under gc (38 s).

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits November 16, 2026 09:53
…lk that assigns its parameter; a walk cut ahead of itself is refused again

Three linked-structure shapes #869 left refused now compile, and a hole #869
opened is closed:

- A field given to a call that keeps it, refilled from the call's result
  (`this.head = new Node(v, this.head)`, `t.left = insert(t.left, v)`): the
  field is given up before the call (ts.TakeOutOfField) and the refill's
  release stays. Only for a call that never throws, whose other arguments hold
  no heap block or are the object `new` made.
- `__own_no_throw`: the signature pass marks calls whose callees (and all they
  call) have no throw. Without it a throwing constructor caught by a caller
  would empty the list where gc and rc keep it.
- A parameter (or a `let` given one) whose every assignment reads out of what
  it holds (`n = n.next`) borrows the caller's value instead of being kept.
- Fixed: `const nx = n.next; n.next = null; n = nx` compiled since #869 and
  read freed memory at run time. A walking `let` frees nothing above the value
  read only where it is read after being given that value.

Spec 83; the user guide and the readiness page are updated.

Tests: 00linked_list_build (every model, and -noopt under own), and
own_err_list_unlink_walk, _cut_through_alias, _push_ctor_throws,
_push_through_this, own_err_tree_field_given_twice, own_err_param_walk_mixed.
ctest 7513/7513 on Windows; own and list tests on Linux under
MALLOC_PERTURB_ with tcache off 2233/2233; corpus: no file newly refused
(00for_condition_narrowing newly compiles, output as gc's); 500 fuzz seeds
(936 programs) no failure; native_ts_graphics' own build unchanged; a 20M
iteration churn of the new shapes stays flat in memory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ays kept; the in-step cut pinned

Review follow-ups to the previous commit:

- __own_no_throw also counts ops of the async dialect as throwing (an await
  rethrows what the awaited body threw).
- walksDownOnly accepts only reads, rc's counts and the debugger's record
  besides its walk-down stores: a swap or a given-away address keeps the
  parameter kept.
- own_err_list_cut_in_step pins the ordering half of the walk-cut fix:
  `c.next = (c = c.next, null)` crashes at run time on main.
  own_err_list_cut_through_alias was refused before #869 too.
- The user guide and the readiness page say the call must be one of the
  same module.

ctest 7514/7514 on Windows; Linux own and list tests 2234/2234 under
MALLOC_PERTURB_ with tcache off; corpus unchanged but the two crash shapes;
the default library's own build 219/225 in all four configurations (6
skipped by design).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit ae8697f into main Nov 16, 2026
7 of 8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-linked-structures-2 branch November 16, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant