Skip to content

fix(walker): checked string length so wasm32 does not wrap and panic - #835

Merged
DrunkOnJava merged 1 commit into
DrunkOnJava:mainfrom
MadsHolten:fix/walker-wasm32-string-length-overflow
Oct 19, 2026
Merged

DrunkOnJava merged 1 commit into
DrunkOnJava:mainfrom
MadsHolten:fix/walker-wasm32-string-length-overflow

Conversation

@MadsHolten

Copy link
Copy Markdown
Contributor

Summary

decode_instance_field checks a length-prefixed UTF-16 string's bounds with 4 + char_count.saturating_mul(2). On wasm32, usize is 32 bits, so a garbage char_count (any value of 2^31 or more) saturates byte_count to usize::MAX, 4 + byte_count wraps to 3 in a release build, the bounds check passes, and the slice &slice[4..3] panics. The panic aborts the whole export in the browser viewer and any other wasm build. This PR uses checked arithmetic instead, so such a field falls back to InstanceField::Bytes the same way a short slice already does.

Motivation

A 67 MB Revit 2024 project (a private architectural model of a university building, not redistributable) fails in the wasm build with:

rvt-rs wasm panic: panicked at src/walker.rs:688:37:
slice index starts at 4 but ends at 3

fileMetadata and quickSummary succeed on the same file; openRvtBytesWithDiagnostics panics. The native 64-bit build does not panic, because u32 as usize * 2 cannot saturate a 64-bit usize. That is why the native checks do not catch it.

Changes

  • src/walker.rs: compute the end of the string with checked_mul(2) and checked_add(4), and return InstanceField::Bytes when either overflows. The behaviour for every in-range length is unchanged.

Testing

  • tools/check-local.sh passes (run in the rust:latest Docker image, rustc 1.99.1)
  • Verified end to end on the real file above with the wasm build (wasm-pack build rvt-wasm --release --target web). Before: panic. After: openRvtBytesWithDiagnostics completes in about 42 s with 31,912 building elements (29,281 with geometry, 2,626 carried by parts), confidence geometry, and modelToIfcStep writes a 123 MB IFC4 file that IFC-Lite opens and renders. I cannot share the file, and there is no Revit export of it to measure against.
  • N/A: this does not change the IFC exporter's output for any file that decoded before. The witness verdicts are unaffected.
  • N/A: not a performance change.

No test is added: the failure needs a 32-bit usize and a file that is not redistributable. A synthetic fixture would need a string field with a corrupt length in a partition record, which I could not build without reverse-engineering the record framing further.

Audit-honesty checklist

  • README / docs reflect the current state after this PR. It fixes a crash and adds no feature claim.
  • N/A: no limitation resolved or introduced.
  • Commit message describes what changed and why
  • No emojis in code, docs, or commit messages
  • No private paths, personal email addresses, or internal jargon in committed files

Legal and contribution hygiene

  • I agree this work is licensable under Apache-2.0
  • This PR contains no Autodesk proprietary source, decompiled internals, or NDA'd SDK content. It is an arithmetic fix found from the panic message and the existing code. No format knowledge is involved.
  • N/A: no new fixtures

Breaking changes

None.

Related issues

None.

🤖 Generated with Claude Code

decode_instance_field bounded a UTF-16 string field with
4 + char_count.saturating_mul(2). On wasm32 usize is 32 bits, so a garbage
char_count saturates byte_count to usize::MAX and 4 + byte_count wraps to 3
in a release build: the bounds check passes and &slice[4..3] panics,
aborting the whole export in the browser. Native 64-bit builds cannot
saturate there, which is why their checks never saw it.

Use checked_mul / checked_add and fall back to InstanceField::Bytes on
overflow, as a short slice already does. Lengths in range decode as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@DrunkOnJava
DrunkOnJava force-pushed the fix/walker-wasm32-string-length-overflow branch from 97d11a5 to de2a456 Compare October 19, 2026 14:56
@DrunkOnJava

Copy link
Copy Markdown
Owner

Thank you, and for the clear write-up: the wrap is real on any 32-bit target, and checked arithmetic with the Bytes fallback is the right fix.

To land it I re-signed your commit (the branch rules require signed commits; you stay its author), rebased it onto current main and approved the workflow runs. It squash-merges once CI is green. The squash message will be your commit message without the Co-Authored-By trailer, which this repository does not carry. The fix goes in the next release's CHANGELOG under your name.

I am also checking the rest of the parser for the same 32-bit wrap.

@DrunkOnJava
DrunkOnJava merged commit 66cfc71 into DrunkOnJava:main Oct 19, 2026
26 checks passed
@DrunkOnJava DrunkOnJava mentioned this pull request Oct 19, 2026
6 tasks done
DrunkOnJava added a commit that referenced this pull request Oct 19, 2026
## Summary

Prepare v0.6.0. Merging this is not the release: the release is the signed `v0.6.0` tag on the merged commit, after the maintainer's go-ahead. This stays a draft until then.

## Motivation

Since 0.5.1, `main` has:

- **IFC4's own property sets (#822):** the IFC2X3 sets 0.5 took from Revit's exports of the RE1 models are gone or moved to where IFC4 and Revit's own IFC4 export put them (#825, #829, #833), and Measure checks every export's standard property sets against buildingSMART's published IFC4 property set definitions (#826). That is rvt-rs's own check: buildingSMART's Validation Service has not been run on the exports, and quantity sets are not checked.
- **More of what Revit's exporter writes:** parameters named for an IFC4 property set's property (RE-194, #815, #817/#821), each element's `IsExternal` (RE-193, #814), a type's Fire Rating (#830), a door's overall size (#788), Revit 2024 Areas as spaces (#811), port types and electrical flow directions (#832), rooms with no number (#818), pieces' property sets (#819), a room's floor finish in IFC4's set (#820).
- **Older and newer releases, experimentally:** Revit 2015 to 2022 (#742) and 2027 (#740) element records.
- **Fixes:** Export to IFC As on fixtures, stairs, doors, curtain walls and wall sweeps (#796, #804, #812, #813), the latest copy of an element's or type's data (#803, #809), declared ElementIds (#797), diagnostics naming the file, not its folders (#785), the browser viewer's wasm32 string-length panic (#835), and `rvt-inspect`, `rvt-corpus doctor` and the viewer's File Status taking Revit 2015 and 2027 as releases rvt-rs reads (#838).
- **CI and release:** IfcOpenShell 0.9.0.post1 (#795), Rust installed with rustup (#793), a tag publishes nothing until the maintainer approves it (#786, #787), and Measure's office, R2025, paired-probe and per-sample hashing (#790, #799, #805, #806).

## Changes

- **Versions 0.5.1 → 0.6.0** in the nine files that carry them: `rvt`, `rvt-py` and its pin, `rvt-wasm`, the `stream_evidence` pin, `CITATION.cff` (with `date-released`), `.release-please-manifest.json`, `viewer/package.json` and both lockfiles' workspace entries.
- **CHANGELOG:** `[0.6.0] — 2026-10-19`, with an introduction, **Upgrading from 0.5** (the property sets that moved, the public API changes, the releases that now export elements) and the outside contributors credited: puzzbobb (#740, #742, #798), Erry91 (#788) and MadsHolten (#835). The section's headings were already single; its 41 entries are all kept, with #835's and #838's added. Its introduction says the property sets match buildingSMART's published IFC4 definitions by rvt-rs's own check, as does the README's release summary.
- **README:** "Release 0.6.0 and main" names v0.6.0 and summarises what it added. The lines that said `main` reads Revit 2015 to 2022 and 2027 now say experimental (since 0.6.0). The opening and "Not yet" lines name those releases, and so does the capability table. The bullet list of what 0.5.0 added is gone; #832 had put 0.6.0's port types in it. As Codex found, the IFC4 export row, the viewer paragraph and the decoder-work paragraph also said 2022 and earlier export only the scaffold, and the by-input table skipped 2014; they name 2015 to 2022, 2026 and 2027 now.
- **Witnesses:** the IFC header carries the version, so both Core Interior `rvt-rs.json` observations and their verdicts are refreshed from this pull request's CI run.

## Testing

- CI on this pull request, the witness replays included.
- Measure on `main` at 23a9d76, which is everything in this release but #835's wasm32-only fix and #838's CLI and viewer version range, neither of which changes an export: run [39273188597](https://github.com/DrunkOnJava/rvt-rs/actions/runs/39273188597). All 89 exports (the reference models, both BIM4LCA office files, Autodesk's 58 sample projects and the 23 R2025 models) have no finding in Measure's IFC4 property set check (#826, against buildingSMART's published definitions), and IfcOpenShell's schema validation finds nothing on any of the 89; none was too large to validate.
- Witnesses: both Core Interior observations and verdicts are taken from this pull request's CI run 39275014518; only `witness_version` and the verdict hashes differ, and CI's replay on b82c0c3 passes.

## Audit-honesty checklist

- [x] README / docs reflect the current state after this PR
- [x] Commit messages describe what changed and why
- [x] No emojis in code, docs, or commit messages
- [x] No private paths, personal email addresses, or internal jargon in committed files

## Legal and contribution hygiene

- [x] I agree this work is licensable under Apache-2.0
- [x] This PR contains no Autodesk proprietary source, decompiled internals, or NDA'd SDK content

## Breaking changes

See **Upgrading from 0.5** in the CHANGELOG: standard property sets moved to IFC4's, and five public API changes.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Version and documentation-only release prep with no runtime code changes in this PR.
> 
> **Overview**
> **Bumps the workspace from 0.5.1 to 0.6.0** across `Cargo.toml`, `rvt-py` / `rvt-wasm` / `stream_evidence` pins, lockfiles, `viewer/package.json`, `.release-please-manifest.json`, and `CITATION.cff` (release date 2026-10-19).
> 
> **Documents the release:** adds the `[0.6.0]` section to `CHANGELOG.md` (IFC4 property sets, broader Revit export parity, experimental 2015–2022/2027 element export, wasm viewer fix, and **Upgrading from 0.5** breaking notes). **README** now treats v0.6.0 as the current release—updates the support matrix and “Release 0.6.0 and main” to describe shipped capabilities instead of the old 0.5.1 bullet list.
> 
> No application or library source changes in this diff; it packages and narrates work already on `main`.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c8826b82982270fd4f47b8da656f2e3e6021c76b. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
@DrunkOnJava

DrunkOnJava commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

This shipped in v0.6.0, on crates.io, PyPI, the release archives and the container image, and the browser viewer runs it. It also led to #836: a CI job that runs the real-file checks on 32-bit i686 with overflow checks on (#839, in review) has found two more wraps of the same kind. Thank you.

DrunkOnJava added a commit that referenced this pull request Oct 19, 2026
…h overflow checks on, and fix the two wraps they found (#836) (#839)

## Summary

CI runs the real-file, CLI, contract and hardening checks, the fuzz regressions among them, on 32-bit i686 with overflow checks on, so a length that wraps on a 32-bit target panics in CI instead of passing a bounds check. Its first runs found two such wraps on every family release, fixed here. Closes #836.

## Motivation

#835 found a parser panic only a 32-bit target can reach: `4 + char_count.saturating_mul(2)` saturates to `usize::MAX` on wasm32, the addition wraps to 3 in a release build, the bounds check passes and `&slice[4..3]` panics. In the browser viewer, which runs on wasm32, that aborts the export. Every CI job, fuzz target and corpus run builds for 64 bits, where a `u32` length times 2 cannot overflow, so nothing would catch the next one.

## Changes

- `.github/workflows/ci.yml`: a new job, **32-bit overflow checks (i686)**, on ubuntu-24.04. It fetches the phi-ag/rvt family corpus (`RVT_REQUIRE_CORPUS=1`, as the Linux test job does), installs the `i686-unknown-linux-gnu` target and `gcc-multilib`, and runs `tools/ci/verify-real-files.sh --profile ci --no-fail-fast --target i686-unknown-linux-gnu` with `CARGO_PROFILE_CI_OVERFLOW_CHECKS=true`. The `ci` profile keeps its release optimisation, so the corpus checks run at the usual speed, and overflow checks make any wrap panic. It has its own rust-cache key (`ci-i686`, saved from `main` only), so the shared `ci-release` cache is untouched. `CI passed` requires it.
- `src/partition_room_parameters.rs`: `data_objects`, the one-pass data-object scan (B70), tries a header at every offset of a partition and added the `u32` size it reads there to the offset unchecked. On a 32-bit target a size near 4 GiB from arbitrary bytes wrapped the end around, and a wrapped end below 4 made `end - 4` underflow and the next read panic: in the browser viewer, an aborted export. The addition is checked now, and an end past `usize::MAX` is not a plausible object.
- `src/partition_type_materials.rs`: `opens_unset_map` multiplied a count read from arbitrary bytes by 12 before checking it against its 64-entry limit. Its range check came next, so release builds did not misread, but the product overflowed. The count is checked first.
- `CHANGELOG.md`: `[Unreleased]` entries for the job and both fixes.

## Testing

The job's own runs on this pull request are the test. Each builds the integration binary and the CLIs for i686 and runs the manifest's real-file, CLI, contract and hardening targets on the 11 family releases:

| Commit | i686 result |
|---|---|
| dbdb866 (the job alone) | 233 passed, 11 failed: `partition_room_parameters.rs:353`, "attempt to add with overflow", in every test that exports a family |
| 99b1102 (+ data-object end checked) | 233 passed, 11 failed: `partition_type_materials.rs:258`, "attempt to multiply with overflow" |
| f1c9032 (+ map count checked first) | **244 passed**, 31 skipped (the project-corpus targets, which this job does not fetch) |

Neither fix can change a 64-bit export, where neither sum nor product can overflow, and the 64-bit jobs here are unchanged. The other multiplications and sums of file-read counts in `src/` were read too: each is checked against a limit before it is used (a string's units, a list's entries, a map's count) or uses checked arithmetic. `actionlint` and a YAML load pass on `ci.yml` (actionlint's only notes are the existing `$/.github/actions/fetch-corpus` references, which GitHub runs).

- [x] Verified end to end on real Revit files: the family corpus (phi-ag/rvt, fetched for the run, never committed) on i686
- N/A: not an IFC exporter or performance change

## Audit-honesty checklist

- [x] README / docs reflect the current state after this PR
- [x] Commit messages describe what changed and why
- [x] No emojis in code, docs, or commit messages
- [x] No private paths, personal email addresses, or internal jargon in committed files
- N/A: not a reverse-engineering finding

## Legal and contribution hygiene

- [x] I agree this work is licensable under Apache-2.0 (see `CONTRIBUTING.md`)
- [x] This PR contains no Autodesk proprietary source, decompiled internals, or NDA'd SDK content (see `CLEANROOM.md`)
- [x] No new test fixtures

## Breaking changes

None. A pull request now also needs the i686 job to pass.
@MadsHolten

Copy link
Copy Markdown
Contributor Author

Thanks for the swift process! And thank you so much for making this available!

@DrunkOnJava

Copy link
Copy Markdown
Owner

A follow-up for anyone building rvt for wasm32 or another 32-bit target: the 32-bit CI job your report led to (#836) found two more length sums that could wrap there, one of which could make an export panic. Both fixes shipped in v0.6.1 today.

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