Skip to content

fix(handle-map): the handle delta is an unsigned MC and both accumulators restart per handle section - #53

Merged
DrunkOnJava merged 2 commits into
mainfrom
fix/handle-map-address-space
Aug 30, 2026
Merged

DrunkOnJava merged 2 commits into
mainfrom
fix/handle-map-address-space

Conversation

@DrunkOnJava

@DrunkOnJava DrunkOnJava commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

AcDb:Handles was decoded with two wrong rules. Their combination — not the
section assembly — is what #44 measured as a 96,739-byte address-space
shortfall against AcDb:AcDbObjects, and what #43 saw as classes the walker
never reached.

  1. The handle delta is an UNSIGNED modular char; only the offset delta is
    signed.
    Handles grow monotonically inside a handle section, so the encoder
    spends all seven low bits of the terminating byte on magnitude. Reading it as
    a signed MC steals bit 0x40 as a negation flag, so every single-byte
    delta in 0x40..=0x7F decoded small and negative and the multi-byte form
    split its limbs one bit short. Byte offsets genuinely move backward (deleted
    objects' space gets reused), so the offset delta keeps the signed encoding.
  2. Both accumulators restart at zero on every handle section. The first
    (handle, offset) pair of a section is absolute, not a delta from the
    previous section's last entry.

Evidence

The section assembly was never wrong. examples/probe_section_pages.rs
(new) walks AcDb:AcDbObjects page by page on sample_AC1032.dwg:

  • 41 page refs, all present in the page map, start_offset stepping by exactly
    0x7400 (29,696) from 0 to 1,187,840.
  • every page's LZ77 payload produces exactly 29,696 bytes → 1,217,536 total.
  • the description declares 1,192,851, so the last page contributes 5,011 real
    bytes and 24,685 bytes of padding — consistent with that page's compressed
    payload being only 3,317 bytes where every full page is ~29,817.
  • max_decomp_page_size 29,696, compressed 2, encrypted 0, unknown 1,
    every hdr.start_offset equal to its ref start. Nothing is skipped or
    truncated.

The map was wrong. Every record in AcDb:AcDbObjects repeats its own
handle, so the object stream is a free oracle for the map: does the record at
the offset this map produced carry the handle this map produced?
Decoding the
same 2,642 handle bytes four ways:

handle delta per-section restart agree disagree no record
signed no (shipped) 272 457 113
signed yes 445 397 0
unsigned no 656 73 113
unsigned yes 842 0 0

Byte-level witnesses on that file:

  • Bug 1 first bites at entry 272: the map claimed 0x438 at offset 148,422;
    the record there carries 0x4A4. Every later handle ran 108 low.
  • Bug 2: handle section 1 ends at entry 655 (payload bytes 0..2030, consumed
    exactly). Section 2's first pair is hd=2851, od=99522. Carried across the
    boundary that reads as handle 0x1343 at offset 197,439, where no record
    starts. Read as absolute it is handle 0xB23 at offset 99,522 — and the
    record at 99,522 declares size 1,608 and handle 2851. The next pair
    (+1, +1613) lands at 101,135 on a record declaring handle 2852.
  • The 1,289,590 phantom offset was pure accumulated drift. After the fix the
    map's highest offset is 1,192,823, 28 bytes inside the declared section,
    and all six previously out-of-range entries resolve to real records.

No records are hiding outside the map. The 842 walked records cover
1,191,935 of the section's 1,192,851 bytes; 916 bytes are unclaimed and the
longest unclaimed run is 4 bytes (inter-record padding).

Measured results

cargo run --release --example coverage_report -- <19-file corpus>, base
f706937 vs this branch:

Version Files Decoded Skipped Errored Rate
R2004 (AC1018) 3 489 → 489 108 → 108 0 → 0 81.9 % → 81.9 %
R2010 (AC1024) 3 510 → 510 33 → 33 0 → 0 93.9 % → 93.9 %
R2013 (AC1027) 3 363 → 363 33 → 33 0 → 0 91.7 % → 91.7 %
R2018 (AC1032) 1 561 → 712 145 → 91 39 → 39 75.3 % → 84.6 %
Aggregate 19 1923 → 2074 319 → 265 39 → 39 84.3 % → 87.2 %

R2004/R2010/R2013 are unchanged because those samples carry a single handle
section and no delta ≥ 0x40 — neither bug could fire there. That is the
control.

Walker diagnostics on sample_AC1032.dwg: 97 handle-map entries yielded no
record before (91 "no record at offset", 6 "past end of stream"); 0 after.
Handle self-check mismatches: 430 before (measured with this PR's new
diagnostic against the old delta rules), 0 after.

Declared vs walked per class (#43)

examples/probe_class_census.rs (new) against AcDb:Classes
num_objects. Only rows that changed or are still short:

Class Code Declared Walked before Walked after
MATERIAL 505 11 3 11
MULTILEADER 526 15 10 15
ACAD_EVALUATION_GRAPH 532 3 1 3
ACSH_HISTORY_CLASS 545 2 0 2
PDFDEFINITION 542 1 0 1
PDFUNDERLAY 543 1 0 1
SPATIAL_FILTER 544 1 0 1
ACSH_BOX_CLASS 546 1 0 1
ACDBASSOCPERSSUBENTMANAGER 548 1 0 1
ACDBPERSSUBENTMANAGER 549 1 0 1
TABLECONTENT 524 5 1 2
TABLEGEOMETRY 525 5 1 2

Pre-fix the census also over-counted several classes (ACDBDICTIONARYWDFLT
1/2, DICTIONARYVAR 11/12, MESH 2/3, BLOCKLINEARPARAMETER 1/2,
ACDB_BLOCKREPRESENTATION_DATA 2/3) because garbage offsets parsed into
plausible-looking records; those are gone too.

41 of 43 classes now reach their declared instance count. LAYOUT (4) and
ACDBPLACEHOLDER (1) are credited via their fixed type codes 0x52/0x50 —
both were promoted out of the custom-class range, so the drawing registers the
class but writes the instances with the fixed code. TABLECONTENT and
TABLEGEOMETRY each declare 5 and reach 2; with the object stream 99.92 %
covered and zero unclaimed runs longer than 4 bytes, those three instances
apiece are not present as standalone records in this drawing. That is a
class-table bookkeeping question, not a walker gap — follow-up below.

#51 — read_handle rejects a counter above 8

BitCursor::read_handle now returns the new Error::HandleCounterTooLarge
when the counter nibble exceeds 8. The nibble holds 0..=15 but a handle value
is at most a u64, so a wider counter proves the cursor is not on a handle
field; the old code shifted the excess out and produced a plausible handle from
garbage (the counter = 11 in #44). Measured against the pre-fix address
space, this gate alone caught 43 phantom records on sample_AC1032.dwg.
It surfaces as a walker skip, never as a dispatch error, so coverage ratios
stay comparable — coverage_report grows a separate wskip column and a
walker-diagnostic histogram.

The three phantom CUSTOM(727) records from #44 are gone, along with
CUSTOM(517) at 212,392 and CUSTOM(564) at 229,599.

Changes

  • src/handle_map.rs — read_unsigned_mc / write_unsigned_mc; accumulators
    scoped to the handle section on both read and write; the writer opens a new
    section when a caller's handles move backward (unencodable as an unsigned
    delta). Five new byte-built tests, including one that pins 0x56 as +86 and
    not −22, and one that pins the per-section restart.
  • src/bitcursor.rs — the counter gate + two tests (a full 8-byte counter still
    round-trips; a BitWriter-built code 2 / counter 11 is rejected).
  • src/object.rs — ObjectWalkSummary::handle_mismatches records every record
    whose own handle disagrees with the map. Reported, not enforced.
  • src/reader.rs — DwgFile::all_objects_lossy() exposes the walk summary.
  • src/section_map.rs — SectionDescription::unknown surfaced for the probe.
  • tests/canonical_corpus.rs — two new gates across all four fixtures: the
    handle map must resolve to records that agree with it (zero skips, zero
    mismatches, walked count == map length), and the walk must reach every
    declared class instance.
  • src/entities/dispatch.rs — one negative-control test now expects the
    misaligned read to fail at read_handle rather than in the entity body.
  • New probes examples/probe_section_pages.rs and
    examples/probe_class_census.rs; examples/coverage_report.rs gains the
    walker-diagnostic reporting.
  • README / STATUS / CHANGELOG / ARCHITECTURE updated with the measured numbers
    and the corrected AcDb:Handles wire description.

No fixture bytes changed, so tests/canonical_corpus.rs's existing field pins
are untouched: the writer moved in lockstep with the reader and the synthetic
maps are single-section with small deltas.

Spec sections

§2.6 (modular chars), §2.13 (handle references), §4.5 / §4.6 (section info and
the 0x20 data-page header), §21 (object map / AcDb:Handles), §20 (object
stream records).

Source provenance

Clean-room. Derived from the ODA DWG specification v5.4.1 and from measurements
of sample_AC1032.dwg itself using the probes in this repository. No
LibreDWG, ODA, Autodesk, or other third-party DWG source was read or consulted
for this change; CLEANROOM.md needs no new disclosure.

Gate

cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings,
cargo test --release (703 lib + all integration suites), cargo deny check all, RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features, and
cargo +1.85.0 test --no-run all pass.

Follow-up candidates

  • TABLECONTENT / TABLEGEOMETRY each declare 5 instances on
    sample_AC1032.dwg and only 2 exist as standalone records. Decide whether
    num_objects is a live census or a registration-time counter, or whether the
    missing instances are embedded inside the two ACAD_TABLE entities.
  • probe_class_census exits non-zero on any under-walked class; wiring it into
    CI once the two rows above are explained would make the class table a
    standing completeness gate.
  • The 916 unclaimed object-stream bytes (842 runs, max 4 bytes each) are
    presumably record alignment padding; confirming that against the spec would
    turn "99.92 % covered" into "100 % accounted for".
  • 39 dispatch errors remain on sample_AC1032.dwg, now concentrated in
    MULTILEADER (15) and HATCH (8) — both grew because the fix recovered records
    that previously never reached a decoder.

Closes #43
Closes #44
Closes #51

https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C


Note

Medium Risk
Changes core R2004+ object enumeration (HandleMap read/write and the object walker), which affects every downstream decode path; behavior is heavily tested and corpus-gated, but regressions on multi-section handle maps would be high impact.

Overview
Fixes AcDb:Handles decoding so handle deltas use unsigned modular chars (offset deltas stay signed) and handle/offset accumulators reset at each handle section. The writer mirrors the same rules, including splitting sections when handles would move backward.

Adds Error::HandleCounterTooLarge when BitCursor::read_handle sees a counter above 8, ObjectWalkSummary::handle_mismatches, and DwgFile::all_objects_lossy() so walks can be checked against each record’s embedded handle. Corpus integration tests and new probes (probe_section_pages, probe_class_census) gate completeness; coverage_report adds a separate wskip column for walker skips.

On sample_AC1032.dwg, handle-map self-check goes to 842/842, walker skips drop to 0, and measured decode coverage rises (712 decoded, 84.6% file / 87.2% aggregate). Closes #43, #44, #51.

Reviewed by Cursor Bugbot for commit ac5e7e0. Configure here.

…tors restart per handle section

The AcDb:Handles delta run was decoded with two wrong rules, and their
combination is what #44 measured as a 96,739-byte address-space
shortfall against AcDb:AcDbObjects.

1. The handle delta is an UNSIGNED modular char; only the offset delta
   is signed. Reading it as signed steals bit 0x40 of the terminating
   byte as a negation flag, so every single-byte delta in 0x40..=0x7F
   decoded small and negative. On sample_AC1032.dwg the first
   divergence is entry 272, after which every handle ran 108 low.

2. Both accumulators restart at zero on every handle section. The
   first pair of a section is absolute. Carrying the totals across the
   boundary threw entries 656..841 (186 of 842) into a fabricated
   address space and produced the phantom offset 1,289,590.

The section assembly was never wrong: AcDb:AcDbObjects is 41 pages of
0x7400 decompressed bytes truncated to its declared 1,192,851, and the
trailing 24,685 bytes are the last page's zero padding. After the fix
the map's highest offset is 1,192,823.

Every record repeats its own handle, so the object stream verifies the
map against itself. Agreement goes from 272/842 to 842/842, walker
skips from 97 to 0, and the walked records cover 1,191,935 of 1,192,851
section bytes (916 unclaimed, longest run 4).

BitCursor::read_handle now rejects a counter above 8 with the new
Error::HandleCounterTooLarge, surfaced as a walker skip rather than a
decoder error so coverage ratios stay comparable.

Coverage over the local 19-file corpus: R2018 561/145/39/75.3% ->
712/91/39/84.6%; aggregate 1923/319/39/84.3% -> 2074/265/39/87.2%.

Closes #43
Closes #44
Closes #51

Claude-Session: https://claude.ai/code/session_01BaHKf1Rw5aJBGK5y3xmx7C
@cursor

cursor Bot commented Aug 30, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_94fd1163-7878-4592-b7d3-3de47c7602dc)

…s-space

# Conflicts:
#	CHANGELOG.md
#	README.md
#	STATUS.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment