Miscompilation of Wasm's `rem_s` instruction
- Vorherrschende Sprache
- Rust
- Sterne
- 115
- Forks
- 84
- Ø Merge
- 1 T. 8 Std.
- Gemergte PRs (30 T.)
- 15
Beschreibung
### Problem
Wasm's `(i32|i64).rem_s` is compiled to the `(i32|i64)::checked_mod` intrinsic. However they behave differently for `(MIN, -1)`:
- Wasm's `rem_s` does not trap and returns 0
- The `checked_mod` intrinsic traps because the underlying division overflows
### Not (yet) surfaced by tests
Existing tests have not surfaced this. Rust's `(i32|i64)::(overflowing|checked)_rem` hit this path, however rustc emits wasm that explicitly checks edge cases, including `(MIN, -1)`. So the bug is hidden. See #1196 for context.
#1207 adds coverage.
### Reproduction
Run the ignored test from #1207
### Proposed solution
Add intrinsic `(i32|i64)::wrapping_mod` and compile `(i32|i64).rem_s` to that.
### TODO
- [x] `i32` #1233
- [ ] `i64`
Beitragsleitfaden
Rechercherichtung
The issue describes a miscompilation of Wasm's signed remainder instruction. Start by examining the compiler's intrinsic mapping for `rem_s` and the existing `checked_mod` intrinsic. Review the linked PR #1207 for the ignored test case to understand the exact failure. The solution involves adding a new `wrapping_mod` intrinsic and updating the code generation. Look at the changes in #1233 for the i32 implementation as a reference for the i64 work.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- rust, wasm
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100