OptimizeInstructions: `select (memory.grow 1) (memory.grow 1) c` is folded to one operand and returns the wrong result
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 68/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- wasm
- Domaine
- compilers
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- WebAssembly
- Étoiles
- 8.7k
- Forks
- 900
- Merge moyen
- 2 j 2 h
- PR mergées (30 j)
- 101
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de WebAssembly/binaryen
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
WebAssembly/binaryen#9135 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 Une demi-journée Accessibilité débutants 76/100
WebAssembly/binaryen#9018 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 76/100
WebAssembly/binaryen#9291 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
WebAssembly/binaryen#9257 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 45/100
WebAssembly/binaryen#9249 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de WebAssembly/binaryen
Issues similaires
-
Slang input: int8_t4_packed and uint8_t4_packed are copied into CrossGLPeut-être pris @ninglinLiu l’a pris aujourd’hui. Ouvertearea:frontend backend:slang bug direction:input
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
Decompiler: convertCharRef() in xml.y overflows its int4 accumulator on long character referencesPeut-être pris @kaayzouee l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
NationalSecurityAgency/ghidra#10009 ·
Les mainteneurs répondent en général sous 2 jours
-
[Bug] tree-sitter-bash parses `$_` / `${_}` as variable_name instead of special_variable_nameOuverte
Difficulté 2/5 Moins d'une heure Accessibilité débutants 85/100
MoonshotAI/kimi-code#4367 ·
Les mainteneurs répondent en général sous 1 jour
-
clang:frontend code-cleanup HLSL
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
llvm/llvm-project#235100 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
paritytech/polkavm#442 ·