Avoid int literals that overflow a 31-bit target - #31
Open
samoht wants to merge 1 commit into
Open
Conversation
wasm_of_ocaml compiles OCaml int to 31 bits, where 0x7fffffff and
0x40000000 do not fit:
Warning [integer-overflow]: integer 0x40000000 (1073741824) truncated
to 0xc0000000 (-1073741824); the generated code might be incorrect.
Neither site is wrong. In the emulation modules 0x40000000 truncates to
the 31-bit sign bit, which is what without_bit_sign masks off, so the
code is correct on the backend that selects them. optint_native is only
reached when Sys.word_size = 64, and its neighbouring constants already
wrap on a narrower int without warning.
Build the emulation masks from wider literals so nothing is truncated at
compile time, and derive int32_max from uint32_max rather than laundering
a literal that cannot fit. Values are unchanged on 63-bit native, 32-bit
js_of_ocaml and 31-bit wasm_of_ocaml.
samoht
force-pushed
the
fix-wasm-integer-overflow
branch
from
July 30, 2026 18:52
788109b to
ed41b8c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A wasm_of_ocaml build that depends on optint gets three integer-overflow warnings:
None of them is a bug, but a consumer cannot silence them either: they are emitted when optint's own
.wasmais built, so flags on the consuming executable never reach them.In
optint_emul.mlandint63_emul.mlthe warning is a false positive. On a 31-bitintthe sign bit is0x40000000, so the literal truncates to exactly the valuewithout_bit_signmasks off, on the one backend that selects those modules. Building it from anint32/int64literal keeps that value and drops the warning.optint_native.mlis a different case:int32_maxreally does wrap there. But the module is reached only whenSys.word_size = 64, and its neighbours wrap silently already (uint32_maxis-1on a 31-bitint;1 lsl 31and thelsl 32inof_int32are unspecified). So instead of converting a literal that cannot fit, this PR derivesint32_maxfromuint32_maxand records the 64-bit assumption in a comment.To check the values are unchanged I ran a driver over
of_int,of_unsigned_int,to_int32and theInt63equivalents at the boundaries (max_int,min_int, +/-2^30, small negatives) on 63-bit native, 32-bit js_of_ocaml and 31-bit wasm_of_ocaml. Output is identical before and after,dune runtestpasses, and the three warnings are gone.