OptimizeInstructions: `select (memory.grow 1) (memory.grow 1) c` is folded to one operand and returns the wrong result
Mantenedores costumam responder em até 1 dia
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 68/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Ativa
- Stack de tecnologia
- wasm
- Domínio
- compilers
Direção de pesquisa
Start at the line 2893 check in OptimizeInstructions and reproduce the issue with the supplied wasm-opt commands and WebAssembly modules. Verify the fix with --optimize-instructions --fuzz-exec for the memory.grow, array.get, atomic RMW, and table.grow cases, ensuring optimized results match the originals.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Summary
areConsecutiveInputsEqual treats two syntactically equal operands as equal in value when their children have no effects. That is wrong for operands that themselves write state (memory.grow, table.grow, atomic RMW), which return a different value on each evaluation.
Root cause
The check at line 2893, fallthroughEffects.orderedBefore(fallthroughChildEffects), leaves the operand itself out so that an idempotent call does not interfere with itself. As a result, an operand that reads and writes the same state, like memory.grow, is never compared with itself, and isGenerative (calls and allocations only) does not catch it. A possible fix is to also reject an operand whose own effects interfere with a second evaluation of itself, keeping the exemption for idempotent calls only.
Affected passes
Only OptimizeInstructions.
Reproducer
(module (memory 1 5)
(func (export "s") (result i32)
(select (memory.grow (i32.const 1)) (memory.grow (i32.const 1)) (i32.const 1))))
$ wasm-opt in.wat -all --optimize-instructions --print
(func $0 (type $0) (result i32)
(drop
(memory.grow
(i32.const 1)
)
)
(drop
(i32.const 1)
)
(memory.grow
(i32.const 1)
)
)
The condition is 1, so the select must return the first grow's result:
$ wasm-opt in.wat -all --optimize-instructions --fuzz-exec -o /dev/null
[fuzz-exec] export s
[fuzz-exec] note result: s => 1
[fuzz-exec] export s
[fuzz-exec] note result: s => 2
[fuzz-exec] comparing s
values not identical! 2 != 1
[fuzz-exec] optimization passes changed results
Second reproducer:
(module
(type $a (array (mut i32)))
(memory 1 10)
(func (export "f") (result i32)
(array.get $a
(array.new_fixed $a 2 (memory.grow (i32.const 1)) (memory.grow (i32.const 1)))
(i32.const 1))))
$ wasm-opt in.wat --enable-reference-types --enable-gc --optimize-instructions --fuzz-exec -o /dev/null
[fuzz-exec] export f
[fuzz-exec] note result: f => 2
[fuzz-exec] export f
[fuzz-exec] note result: f => 1
[fuzz-exec] comparing f
values not identical! 1 != 2
[fuzz-exec] optimization passes changed results
Expected vs actual
select, c = 1: the original returns the first grow's result, 1; the optimized module returns 2. array.get ... 1: the original gives 2; the optimized module gives 1. Two i32.atomic.rmw.adds and table.grow behave the same.
Version
Reproduced on upstream main at 4d8ac549e2ab9b283246ea95e79ebe139ca579ac (wasm-opt version 133).
AI was used as part of the process of finding this issue. I have manually checked and reproduced it.
- Linguagem predominante
- WebAssembly
- Estrelas
- 8.7k
- Forks
- 893
- Merge médio
- 1d 18h
- PRs com merge (30d)
- 95
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de WebAssembly/binaryen
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 88/100
WebAssembly/binaryen#9135 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 Meio dia Facilidade para iniciantes 76/100
WebAssembly/binaryen#9018 · 3 comentários ·
Mantenedores costumam responder em até 1 dia
-
Memory64Lowering: table.get/table.set keep i64 index on lowered table64, output fails validationAberta
Dificuldade 3/5 Meio dia Facilidade para iniciantes 66/100
WebAssembly/binaryen#9245 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 65/100
WebAssembly/binaryen#9244 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 62/100
WebAssembly/binaryen#9243 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de WebAssembly/binaryen
Issues semelhantes
-
Default-import note suggests `import * as process` for velt:process, which does not name the builtinAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
category:runtime
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
Re-enable -fno-lto testAbertatests
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
rust-lang/rustc_codegen_gcc#993 ·
Mantenedores costumam responder em até 1 dia
-
an:enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
Mantenedores costumam responder em até 1 dia