Skip to content

fix: convert ccall pointer returns to unsigned in run-pipeline-emscriten#1503

Merged
thewtex merged 1 commit intoInsightSoftwareConsortium:mainfrom
PaulHax:unsigned-wasm-pointers
Apr 9, 2026
Merged

fix: convert ccall pointer returns to unsigned in run-pipeline-emscriten#1503
thewtex merged 1 commit intoInsightSoftwareConsortium:mainfrom
PaulHax:unsigned-wasm-pointers

Conversation

@PaulHax
Copy link
Copy Markdown
Collaborator

@PaulHax PaulHax commented Apr 5, 2026

Fix signed pointer overflow in runPipelineEmscripten when WASM heap exceeds 2 GB. Apply >>> 0 to all 7 ccall sites returning pointers. Add regression test.

Problem

Emscripten's ccall returns pointers as signed i32. When a module is reused (web worker pattern) and the heap exceeds 2 GB, pointers above 2^31 wrap negative:

RangeError: Start offset -1778175624 is outside the bounds of the buffer

Reported via VolView issue loading a 1024x1024x172 Float32 image + NIfTI labelmap on the same worker.

Fix

const dataPtr = emscriptenModule.ccall('itk_wasm_output_array_address', 'number', ...) >>> 0

>>> 0 is a no-op for pointers below 2^31, so safe for all heap sizes.

Emscripten precedent

The Emscripten settings reference states: "If the maximum heap size is over 2GB, then pointers must be unsigned in JavaScript." The same bug class was fixed in Emscripten's own library_webgl.js (#20137) and pointer types were changed to unsigned for CAN_ADDRESS_2GB mode (#21278).

…pten

Emscripten's ccall returns WASM pointers as signed i32. When the heap
exceeds 2 GB, pointers above 2^31 wrap negative, causing:

  RangeError: Start offset -N is outside the bounds of the buffer

Apply >>> 0 to all 7 ccall sites that return pointers (4 helper
functions + 3 inline output readers) so they are treated as unsigned.

Adds a regression test that reuses an Emscripten module across two
large-image pipeline calls, reproducing the real-world scenario of
a persistent web worker whose heap accumulates past 2 GB.
Copy link
Copy Markdown
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PaulHax thanks!!

@thewtex thewtex merged commit fc32a96 into InsightSoftwareConsortium:main Apr 9, 2026
68 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.

2 participants