Skip to content

Fix: accept interior copy_to/copy_from ranges within a live allocation - #1557

Merged
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/1537-copy-interior-range
Jul 29, 2026
Merged

Fix: accept interior copy_to/copy_from ranges within a live allocation#1557
ChaoWao merged 1 commit into
hw-native-sys:mainfrom
ChaoZheng109:fix/1537-copy-interior-range

Conversation

@ChaoZheng109

@ChaoZheng109 ChaoZheng109 commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Problem

Issue #1537: the child-pointer provenance guard keyed every allocation by its exact (worker_id, base) and admitted a copy only when the address hit that base. A partial update of a persistent device buffer — copy_to(base + offset) into one page of a resident cache (e.g. the DeepSeek V4 decode KV cache) — was rejected as though the interior address were unknown, because the guard stored no allocation extent to range-check against.

base = orch.malloc(worker_id=0, size=64)
orch.copy_to(worker_id=0, dst=base + 32, src=host_ptr, size=16)
# before: ValueError: device pointer ... is not a live allocation

Fix

Record each allocation's byte extent in provenance and validate copies by containment instead of exact base:

base <= ptr && ptr + nbytes <= base + extent
  • _ChildProvEntry now carries malloc_size and maps each CommDomain allocation id to the extent of the window / buffer recorded at that base; live_extent() returns the widest live role.
  • copy_to / copy_from (Worker L2 path and Orchestrator L3 path) call the new _child_prov_require_live_range, which scans the worker's live allocations and admits any range wholly contained in one. Python ints are unbounded, so the ptr + nbytes bound is exact (no wraparound).
  • CommDomain windows / carved buffers carry their live extent so interior copies into them are validated too.

free() and kind4 dispatch are unchanged — they still require the exact base, since they act on the whole allocation, not an interior slice.

Behavior

Operation Before After
copy_to(base + 32, ..., 16) within a malloc(64) rejected accepted
copy_to(base + 63, ..., 2) (overruns) accepted (guard blind to size) rejected — out of range
same address on another worker rejected rejected
copy after free(base) rejected rejected
free(base + 32) rejected rejected

Tests

Device-free, mocked-orchestrator regression coverage in tests/ut/py/test_worker/test_child_addr_guard.py for all five cases in the issue, at both the L3 orchestrator and L2 worker entry points. tests/ut/py/test_worker/ — 349 passed, 2 skipped (pre-existing).

close #1537

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ec503a6-e1ec-407e-bf8d-bde7f098452a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

copy_to and copy_from now validate byte ranges within live allocations. Provenance tracks malloc and CommDomain extents, while free and dispatch retain exact-base requirements. Documentation and tests cover interior ranges, boundaries, aliases, worker isolation, L2 paths, and release ordering.

Changes

Child-memory provenance guards

Layer / File(s) Summary
Extent-aware provenance model
python/simpler/worker.py
Provenance entries now store malloc sizes and per-domain extents, calculate live ranges, and remove released domain extents.
Copy-path integration and API contract
python/simpler/orchestrator.py, python/simpler/worker.py, docs/user/reference/python-api.md
Malloc records allocation sizes, copy operations validate requested ranges, and API documentation describes valid interior addresses and partial updates.
Provenance and memory-operation regression coverage
tests/ut/py/test_worker/test_child_addr_guard.py
Tests cover contained ranges, boundary and stale-pointer rejection, exact-base free and dispatch behavior, aliases, worker isolation, L2 paths, and release ordering.

Estimated code review effort: 4 (Complex) | ~35 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Orchestrator
  participant ChildProvenance
  participant DeviceMemory
  Caller->>Orchestrator: copy_to or copy_from with pointer and size
  Orchestrator->>ChildProvenance: Validate contained live range
  ChildProvenance-->>Orchestrator: Validation result
  Orchestrator->>DeviceMemory: Execute validated copy
Loading

Possibly related PRs

Poem

A bunny found a buffer wide,
And hopped to bytes tucked deep inside.
“Copy this range!” the guards now say,
“But free the base, the proper way.”
Extents bloom—hip-hop hooray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main behavior change: interior copy_to/copy_from ranges are now accepted within live allocations.
Description check ✅ Passed The description matches the change and issue: it explains the range-based provenance fix and the expected copy/free behavior.
Linked Issues check ✅ Passed The changes satisfy #1537 by adding extent-aware range validation, keeping exact-base free checks, and covering the five requested cases.
Out of Scope Changes check ✅ Passed No clear out-of-scope changes; the docs, runtime code, and tests all support the interior-range provenance fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ChaoZheng109 ChaoZheng109 changed the title Fix: accept interior copy_to/copy_from ranges within a live allocation (#1537) Fix: accept interior copy_to/copy_from ranges within a live allocation Jul 29, 2026
@ChaoZheng109

Copy link
Copy Markdown
Collaborator Author

/gemini review

@ChaoZheng109

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

The child-pointer provenance guard keyed every allocation by its exact
(worker_id, base) and admitted a copy only when the address hit that base.
A partial update of a persistent device buffer — copy_to(base + offset) into
one page of a resident cache — was rejected as though the interior address
were unknown, because the guard stored no allocation extent to range-check
against.

Record each allocation's byte extent in provenance and validate copies by
containment instead of exact base:

  base <= ptr && ptr + nbytes <= base + extent

Changes:

- _ChildProvEntry carries malloc_size and maps each CommDomain allocation id
  to the extent of the window / buffer recorded at that base; live_extent()
  returns the widest live role.
- copy_to / copy_from (Worker L2 path and Orchestrator L3 path) call the new
  _child_prov_require_live_range, which admits any range wholly contained in
  one live allocation on the worker. A copy to the exact base resolves in
  O(1); only an interior address falls back to scanning the worker's live
  allocations. Python ints are unbounded, so the ptr + nbytes bound is exact.
- A CommDomain carves its first buffer at offset 0, so that buffer aliases the
  window base under the same allocation id. Record the max of the prior and
  new extent per allocation id so the smaller buffer never narrows the
  window's copy range.
- free() and kind4 dispatch are unchanged — they still require the exact base,
  since they act on the whole allocation, not an interior slice.
- Document the interior-range behavior in the Python API reference.

Regression coverage in tests/ut/py/test_worker/test_child_addr_guard.py, at
both the L3 orchestrator and L2 worker entry points: interior range accepted,
out-of-range / negative-size / wrong-worker / stale rejected, free still
requires the exact base, and a domain-window chunk is not narrowed by its
aliasing buffer.

Fixes hw-native-sys#1537.
@ChaoZheng109
ChaoZheng109 force-pushed the fix/1537-copy-interior-range branch from 8f33cbb to 3e48520 Compare July 29, 2026 03:03
@ChaoWao
ChaoWao merged commit 68ab1b4 into hw-native-sys:main Jul 29, 2026
18 checks passed
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.

[Bug] provenance guard rejects valid interior ranges in copy_to/copy_from

2 participants