Define byte-cursor output aliasing with cursor state
Current master follow-up (2026-11-12)
This update records the remaining behavior on the audited master. The historical report and earlier delivered work are retained below; current code paths are Rust rather than the retired Python helpers.
Problem
The fixed-width cursor paths were updated, but LEB128 read/peek wrappers still accept outputs aliasing cursor state. The selected reject-before-mutation policy is already decided; this is a remaining implementation gap.
Reproduction and evidence
Audited canonical master 69a67c6e0fb700f37ee6114f5a66d37ee13771a5 (0.0.1-alpha-dev). Matching checkout-built compiler/std were used for compiler probes on Linux amd64 with Rust 1.89.0 and LLVM 21.1.8. Artifact inspection, mocked/projection checks and foreign-target object emission are identified below and are not native execution on those operating systems.
import("std::bytes::cursor")::{ByteReader, bytes_reader};
import("std::bytes::leb128")::{bytes_reader_peek_uleb128_u64, bytes_reader_read_uleb128_u64, bytes_reader_peek_sleb128_i64, bytes_reader_read_sleb128_i64};
fun main() -> i32 {
var data: array<u8, 16>;
data[0] = 42;
var r: ByteReader = bytes_reader(&data[0], 16);
var status: i32 = bytes_reader_peek_uleb128_u64(&r, &r.position as ptr<u64>);
println("peek unsigned status={} position={}", status, r.position);
r.position = 0;
status = bytes_reader_read_uleb128_u64(&r, &r.position as ptr<u64>);
println("read unsigned status={} position={}", status, r.position);
r.position = 0;
status = bytes_reader_peek_sleb128_i64(&r, &r.position);
println("peek signed status={} position={}", status, r.position);
r.position = 0;
status = bytes_reader_read_sleb128_i64(&r, &r.position);
println("read signed status={} position={}", status, r.position);
return 0;
}
target/debug/wavec --std-root "$PWD/std" build repro.wave -O0 --run --out-dir out-O0
target/debug/wavec --std-root "$PWD/std" build repro.wave -O2 --run --out-dir out-O2
O0/O2: both unsigned/signed peek return status 0 with reader.position=42. Both reads return status 0 with position=43 even though len=16. These calls use actual 16-byte backing storage and output=&reader.position.
Scope
Apply the existing output/state/backing-storage non-overlap policy to every signed/unsigned LEB read and peek. Snapshot validation must not hide aliases of the original reader.
Starting points: std/bytes/leb128.wave:12, std/bytes/leb128.wave:156.
Completion criteria
- [ ] Reject these overlaps with BYTES_ERROR_INVALID before mutation.
- [ ] Preserve output, original cursor and backing bytes on rejection.
- [ ] Retain ordinary and accepted nonminimal LEB decoding.
Related: #674, #718, #763. Related broad trackers or completed implementations are context, not claims that their entire scope is unfinished.
Original report (historical)
Motivation
Passing &reader.position as the u64 output of a valid eight-byte read returns success and leaves position=28 for len=8 when the decoded value is 20. O0/O2 agree. The API does not state whether output may overlap cursor state, and cannot simultaneously preserve two meanings for the same bytes.
Starting point: std/bytes/read.wave:40.
Validation
Audited canonical Wave 540728c2eb6cc12f5c69c3fb3fdb96e07ba059a4 (0.0.1-alpha-dev), whose tree matches the local checkout. Validation: Executed valid-memory output/state alias fixture at native O0/O2. Executed compiler probes use the matching checkout-built compiler and std on Linux amd64 (Rust 1.89.0 / LLVM 21.1.8). Source/header or mocked checks are not native execution on another OS.
Scope
Choose explicit non-aliasing or checked-overlap rules for cursor outputs/state/backing storage. Do not promise arbitrary alias support or call this a Rust memory-safety defect.
Related: #674, #763. These links do not claim those broader or completed scopes are unfinished.
Design discussion required
This issue records a proposal or an unresolved contract; it is not approval to choose new semantics. Please mention @LunaStev to discuss the design before starting implementation.
Completion criteria
- [ ] The position/len/data alias cases have documented permitted or rejected behavior.
- [ ] Normal outputs and failure rollback keep their existing results.
- [ ] Read and peek variants apply the same approved rule without duplicating one cause per integer width.