Skip to content

fix(hy-check): track nested address-of borrows in composites and enforce Rule O12 - #312

Merged
limitlessprojects merged 6 commits into
limitlessprojects:mainfrom
1932819331hy-debug:fix/pointer-borrow-composites-soundness
Oct 17, 2026
Merged

limitlessprojects merged 6 commits into
limitlessprojects:mainfrom
1932819331hy-debug:fix/pointer-borrow-composites-soundness

Conversation

@1932819331hy-debug

@1932819331hy-debug 1932819331hy-debug commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a soundness hole where explicit address-of borrows embedded in composite literals were not fully propagated through the borrow checker.

Before the fix, moving a composite containing references to local storage could fail to freeze the borrowed local, permitting subsequent access that can race with another task.

The patch also extends borrow-shape validation for nested pointer/borrow containers in struct fields.

Root Cause

Two related gaps were involved:

  1. collect_ref_roots did not account for explicit address-of expressions.

  2. Local/assignment borrow tracking only recognized simple top-level address-of expressions, so explicit borrows embedded in composite literals such as slices, maps, and structs were not recorded correctly.

Nested container shapes containing pointers or borrow handles were also not recursively recognized by field-borrow validation.

Current Implementation

At current HEAD 6fce9bb4:

  • collect_addr_of_roots follows only syntax that actually preserves an explicit borrow into the resulting value:

    • explicit address-of expressions;
    • address-of unary form;
    • Try;
    • Move;
    • nested struct literals;
    • nested map literals;
    • nested slice literals.
  • It deliberately does NOT recursively descend through ordinary:

    • function calls or their argument lists;
    • binary expressions;
    • index expressions;
    • field expressions.

This restriction prevents false positives such as:

count := count_items(&n)
task worker(move n)
fmt.println(count)

where a scalar result must not become an alias of n.

  • record_local_borrow_aliases and record_assign_borrow_aliases are shape-gated before creating borrow aliases.

  • field_borrow_kind recursively recognizes borrow-bearing shapes through slices, arrays, and maps for O12 / field validation.

Soundness Coverage

The regression suite covers:

  • pointer slices;
  • maps containing pointers;
  • nested composite literals;
  • multiple borrowed roots;
  • deep nested pointer containers;
  • parameter-derived borrows;
  • const mutation through borrowed composites;
  • borrow-bearing struct fields;
  • moves with no illegal post-move access;
  • scalar and boolean function-call results that receive borrowed arguments but return non-borrowed values;
  • scalar expressions built from those calls.

The final adversarial validation additionally verified:

  1. pointer extraction from a borrowed container:
mp := map[int]*node{1: &n}
p := mp[1]
task worker(move n, done)
p.val = 42

is rejected with HYB-E3003;

  1. slice/projection propagation:
s := []*node{&n}
t := s[:]
task worker(move t, done)
n.val = 42

is rejected with HYB-E3003;

  1. scalar-return, boolean-return, and scalar copy-out controls remain accepted.

Current dedicated suite:

25 passed, 0 failed

No production-code changes were required after the final adversarial gate.

Verification

Current HEAD has passed:

  • dedicated pointer_borrow_composites regression suite;
  • cargo test -p hy-check;
  • full Cargo workspace tests;
  • Python tool tests;
  • git diff --check.

The final adversarial gate found no remaining false negative in the tested container-extraction or projection paths and no regression requiring an additional production patch.

…12 container restrictions

- Fix soundness hole where taking the address of a local (&x) inside a slice, map, or struct literal dropped borrow aliases, bypassing A1b move freeze (HYB-E3003), T22 const immutability (HYB-E3006), and O5.1 non-owned move restrictions (HYB-E3004).
- Add Expr::AddrOf and Expr::Unary(Addr) handling in collect_ref_roots.
- Introduce collect_addr_of_roots to recursively extract address-of identifiers from composite literals, slice expressions, and call arguments.
- Propagate share edges and alias bindings for composite address-of expressions in loans.rs.
- Enforce Rule O12 on nested pointer and borrow-handle container types (e.g. []*T, map[K]*T) in struct fields (HYB-E3103).
- Add comprehensive integration test suite pointer_borrow_composites.rs with 10 tests.
@1932819331hy-debug

1932819331hy-debug commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Update — superseded by current HEAD 6fce9bb4:
The traversal described below was subsequently narrowed after adversarial testing exposed false-positive propagation through ordinary expressions and call arguments. Current collect_addr_of_roots intentionally tracks explicit borrows only through address-of, Try/Move, and composite literals. The PR description now documents the final behavior.


Multi-Root Borrow and Deep Nesting Hardening Update

To ensure complete soundness when tracking address-of borrow roots across complex expressions:

  1. Recursive Traversal Completeness:

    • Extended collect_addr_of_roots in compiler/crates/hy-check/src/model.rs to recursively inspect:
      • Expr::Move: Unwraps operand to detect move-wrapped composite literals.
      • Expr::Binary: Recursively collects from both lhs and rhs.
      • Expr::Index: Recursively collects from both base container and index expressions.
      • Expr::Call: Recursively collects from callee and all argument expressions.
  2. Added 7 Rigorous Regression and Control Tests (compiler/crates/hy-check/tests/pointer_borrow_composites.rs):

    • multiple_borrow_roots_in_slice_freezes_first_root: When a composite slice []*node{&n1, &n2} is moved to another task, mutating the first root n1 post-move triggers HYB-E3003.
    • multiple_borrow_roots_in_slice_freezes_second_root: Mutating the second root n2 post-move triggers HYB-E3003.
    • deeply_nested_slice_of_slices_freezes_all_roots: Mutating n1 inside [][]*node{ []*node{&n1}, []*node{&n2} } post-move triggers HYB-E3003.
    • deeply_nested_slice_of_slices_freezes_second_root: Mutating n2 in the secondary slice post-move triggers HYB-E3003.
    • deeply_nested_map_of_pointer_slices_freezes_root: Mutating n inside map[string][]*node{"row": []*node{&n}} post-move triggers HYB-E3003.
    • positive_control_multiple_borrow_roots_unaccessed_post_move: Moving multi-root slices without post-move access is cleanly accepted (Verdict::Accept, 0 diagnostics).
    • positive_control_deeply_nested_composites_unaccessed_post_move: Moving deeply nested multi-root composites without post-move access is cleanly accepted (Verdict::Accept, 0 diagnostics).
  3. Local Test Verification:

    • cargo test --test pointer_borrow_composites: 17/17 passed (10 existing + 7 new).
    • cargo test -p hy-check: all passed.
    • cargo test --workspace: all passed.
    • python3 -m unittest discover -s tools -v: 104/104 passed.
    • git diff --check: 0 whitespace issues.

… results and restrict composite borrow roots
@limitlessprojects

Copy link
Copy Markdown
Owner

Wave-2 triage: queued — next tier. Nested address-of borrows in composites + Rule O12 enforcement; new rule needs a spec cross-check before landing.

@1932819331hy-debug

Copy link
Copy Markdown
Contributor Author

Pushed an empty commit (2174e7e4) to retrigger CI without changing the code. The new upstream run is currently action_required and has no jobs yet: https://github.com/limitlessprojects/Go-Hybrid/actions/runs/39007859413 . Could a maintainer approve this workflow run when convenient? This is separate from the queued spec cross-check.

@limitlessprojects

Copy link
Copy Markdown
Owner

Triage (agent session-15/2-a, validated against main @ c53978b): VALIDATED — READY TO LAND (with the maintainer's pending O12 spec cross-check).

Hard repros (both accepted with 0 diagnostics on main, both rejected with HYB-E3003 on this PR's binary): (1) n := node{v: 1}; s := []*node{&n}; task worker(move s, done); n.v = 2 — address-of embedded in a slice composite now freezes the referenced local; (2) same shape through map[string]*node{"k": &n}. Control (&node{...} of a composite literal with no named local) stays accepted on both, so the propagation is not a blanket false-positive on &.

Battery: cargo test --workspace 622 passed / 0 failed (595 baseline + 27 new across the two added test files), corpus 52/52. Merges clean into current main. The diff's 21 deleted lines in loans.rs are within the recently-landed code but my 50-file repro sweep shows no behavior change on any pre-existing shape — only the new nested-address-of shapes newly reject.

Per the earlier wave-2 note: Rule O12 enforcement is new policy, so the spec cross-check (GUARANTEES/SPEC borrow section wording) is the one open item before merge. Thank you — the negative_regression_* tests encode exactly the hole I could reproduce on main.

# Conflicts:
#	compiler/crates/hy-check/src/loans.rs
@limitlessprojects
limitlessprojects merged commit 8f0a152 into limitlessprojects:main Oct 17, 2026
2 checks passed
limitlessprojects added a commit that referenced this pull request Oct 17, 2026
…sable slice

Merge pr-319 (Taurahh, fixes #318): freeze the array behind an addressable slice

`binding_may_share` is false for a primitive array ([N]int copies its
elements), but a slice of that array views its storage: s := a[:],
s := b.data[:], row := g[i][:], append(a[0:1], x). The define-alias
decision now also links the share edge when
expr_slices_addressable_array(value, root) holds, so a later `move s`
freezes the array `a` too (E3003).

Integration: main's O9 handle-continue + moved_non_owned_origin +
(#312's landed) addr_roots loop kept; the PR's disjunct grafted into the
share_edge condition. Corpus fixture renumbered 46 -> 53 (main took 46-49
for #185's rejects); parse_all 53; VERDICTS row renumbered.

Evidence (main @ 8f0a152 vs merged tree): `var a [4]int; s := a[:];
task worker(move s, done); a[1] = 2` - 0-diagnostic bypass on main ->
REJECT HYB-E3003 (write through a live alias of a moved value); copy
control `b := a` accepts both. Suites: cargo 665/0, corpus 53/53. CI
green on 09c621d. Fixes #318.
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.

2 participants