LLVMMemoryCopyFillLowering: in-bounds `memory.fill`/`memory.copy` trap on a 4 GiB memory
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 62/100
Research direction
The bug is in src/passes/LLVMMemoryCopyFillLowering.cpp: the generated __memory_fill bounds check (around lines 213-225) and __memory_copy (around lines 123-130) compute the memory size in bytes with i32 arithmetic, which wraps to 0 at 65536 pages. Reproduce with the WAT in the issue using wasm-opt --llvm-memory-copy-fill-lowering, then run the output to confirm the unreachable trap. Done when the bounds check is done in i64 and the 65536-page module no longer traps, with the existing bulk-memory lowering tests still passing.
Written by the indexing model from the issue text.
Description
Repro
(module
(memory 65536)
(func (export "f")
(memory.fill (i32.const 0) (i32.const 0) (i32.const 1))))
$ wasm-opt in.wat --enable-bulk-memory --llvm-memory-copy-fill-lowering -o out.wasm
$ wasmtime run --invoke f in.wat
$ wasmtime run --invoke f out.wasm
Error: failed to run main module `out.wasm`
Caused by:
0: failed to invoke `f`
1: error while executing at wasm backtrace:
0: 0x44 - <unknown>!<wasm function 1>
1: 0x32 - <unknown>!<wasm function 0>
2: wasm trap: wasm `unreachable` instruction executed
f returns normally. After --llvm-memory-copy-fill-lowering, f traps (unreachable).
In pseudocode, the generated __memory_fill checks:
if (dst + size > memory.size << 16) trap // i32: 65536 << 16 = 0
With 65535 pages it does not trap.
Cause
The memory size in bytes is computed in i32, which wraps to 0 for 65536 pages:
__memory_copy has the same computation:
The pass header excludes pointer overflow, but this access does not overflow; only the computed memory size does.
Possible fix
Do the bounds check in i64.
Question
I'm not familiar with how this pass is used in practice. It seems to work as a polyfill for targets without bulk memory, so I'm not sure whether a module with a 65536-page memory actually reaches it.
- Dominant language
- WebAssembly
- Stars
- 8.7k
- Forks
- 913
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 92
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from WebAssembly/binaryen
-
DAE2 crashes on call.without.effects with a non ref.func expressionPossibly taken @stevenfontanella claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
WebAssembly/binaryen#9404 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
WebAssembly/binaryen#9135 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 Half a day Newbie friendliness 76/100
WebAssembly/binaryen#9018 · 3 comments ·
Maintainers usually reply within 1 day
-
Remove `call.without.effects` intrinsicPossibly taken @stevenfontanella claimed this 2 days ago. Open
WebAssembly/binaryen#9399 · 1 assignee ·
Maintainers usually reply within 1 day
-
Inlining: --inline-max-combined-binary-size is not enforced when several functions are inlined into one caller in the same iterationPossibly taken @kripken claimed this 6 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 58/100
WebAssembly/binaryen#9384 · 1 assignee ·
Maintainers usually reply within 1 day
All issues in WebAssembly/binaryen
Similar issues
-
area:compiler effort:S impact:med platform:windows type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area:Analysis enhancement
Difficulty 1/5 1-3 hours Newbie friendliness 69/100
facebookresearch/atlas-lean#5118 ·
Maintainers usually reply within 1 day
-
drools: declared enum with 128 or more literals fails at class initialization (NegativeArraySizeException)Possibly taken @drccrd claimed this today. Openarea:rules
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/incubator-kie#7306 · 3 comments ·
Maintainers usually reply within 2 days
-
bug css P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day