Skip to content

Handle srem properly when avoid_div_traps is false.#2763

Merged
cfallin merged 1 commit into
bytecodealliance:mainfrom
cfallin:fix-srem-trap
Mar 25, 2021
Merged

Handle srem properly when avoid_div_traps is false.#2763
cfallin merged 1 commit into
bytecodealliance:mainfrom
cfallin:fix-srem-trap

Conversation

@cfallin
Copy link
Copy Markdown
Member

@cfallin cfallin commented Mar 25, 2021

The codegen for div/rem ops has two modes, depending on the
avoid_div_traps flag: it can either do all checks for trapping
conditions explicitly, and use explicit trap instructions, then use a
hardware divide instruction that will not trap (avoid_div_traps == true);
or it can run in a mode where a hardware FP fault on the divide
instruction implies a Wasm trap (avoid_div_traps == false). Wasmtime
uses the former while Lucet (for example) uses the latter.

It turns out that because we run all our spec tests run under Wasmtime,
we missed a spec corner case that fails in the latter: INT_MIN % -1 == 0
per the spec, but causes a trap with the x86 signed divide/remainder
instruction. Hence, in Lucet, this specific remainder computation would
incorrectly result in a Wasm trap.

This PR fixes the issue by just forcing use of the explicit-checks
implementation for srem even when avoid_div_traps is false.

@cfallin cfallin requested a review from bnjbvr March 25, 2021 05:27
The codegen for div/rem ops has two modes, depending on the
`avoid_div_traps` flag: it can either do all checks for trapping
conditions explicitly, and use explicit trap instructions, then use a
hardware divide instruction that will not trap (`avoid_div_traps ==
true`); or it can run in a mode where a hardware FP fault on the divide
instruction implies a Wasm trap (`avoid_div_traps == false`). Wasmtime
uses the former while Lucet (for example) uses the latter.

It turns out that because we run all our spec tests run under Wasmtime,
we missed a spec corner case that fails in the latter: INT_MIN % -1 == 0
per the spec, but causes a trap with the x86 signed divide/remainder
instruction. Hence, in Lucet, this specific remainder computation would
incorrectly result in a Wasm trap.

This PR fixes the issue by just forcing use of the explicit-checks
implementation for `srem` even when `avoid_div_traps` is false.
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:x64 Issues related to x64 codegen labels Mar 25, 2021
@cfallin cfallin merged commit 8636359 into bytecodealliance:main Mar 25, 2021
@cfallin cfallin deleted the fix-srem-trap branch March 25, 2021 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants