You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect execution of openjph compiled file with SIMD #4315
I've done some work to narrow this down but unfortunately I'm not being super successful at the moment. My strategy for narrowing this down has been:
Modify the CLI to print the sha256 of memory after running the wasm
Modify fuel to get checked before every single instruction instead of just loop headers and function entries
Next I have a script which runs the binary on aarch64 and x86_64 with a binary-search-of-sorts. This pins down the precise fuel count where with one more fuel the execution differs. My goal here is to find the precise wasm instruction at which execution diverges here. This quickly-ish found results and I started sprinkling "debug stores" within the *.wat. Basically I'd take a value of the stack, shove it in a temporary global, store the global to linear memory address 0, and then load the global back onto the stack. The goal here was a quick-and-dirty way of debugging the wasm value stack.
In doing this I've narrowed this down to function 40 in the input file above. Specifically these two instructions:
Unfortunately this v128.const value is not actually going into i8x16.swizzle. With my "debug store" inserted between these two instructions it turns out that the value stored in memory is indeed not the v128.const here. Instead some other bit-pattern gets stored in memory and naturally when that gets fed into i8x16.swizzle everything goes awry.
Other than that though I have now hit a wall and am unable to make progress debugging this. Function 40 is huge and it's tough to explore the disassembly. Using rr I can find the portion of the function that actually creates the value which eventually gets fed into i8x16.swizzle. This should feed in the v128.const value but it is not. Unfortunately I can't figure out how to translate this random snippet of assembly back to the source file.
I have confirmed, though, that 0.35.0 reproduces this issue so I don't believe this is a problem with recent developments like regalloc2, recent ISLE migrations, or the alias analysis pass added.
Others who are more familiar with the backend may know more about how to debug this though. IIRC there's some optimizations around constants, constant pools, and trying to not always reify something into a register or something like that, and something may be going awry there.
... aaaand as I am writing this up I took another look at the definition of the Swizzle instruction. This looks suspicious, specifically treating the mask as a writable reg since I think in this case it's the mask that's getting corrupted going into the swizzle instruction.
I added a move from the input to a temporary register and that didn't actually fix the original program but it did surprisingly allow it to make more progress. I think that means there's actually at least two bugs here!
Doing my bisection again I think I found a much simpler reproduction, namely:
where the movss I don't believe is correct, I think that needs to be movdqa or something sized larger.
I'm running out of steam for tonight so I think this is as far as I'll get today. I'm actually quite worried that fuzzing hasn't discovered anything in this area. These are somewhat trivial bugs which in theory should be found almost immediately by any halfway decent fuzzing.
I realize now I forgot to say this earlier but thanks @yurydelendik for taking the time to file this! We'll be making an 0.38.1 release with these fixes soon and a few other fixes for Cranelift (unrelated to this).
We also plan on filing a CVE about these micompiles because while they don't affect hosts themselves this could affect in-wasm execution which is often just as important in some environments.
Activity
yurydelendik commentedon Jun 24, 2022
Also, works on wasmtime for ARM64
alexcrichton commentedon Jun 25, 2022
I've done some work to narrow this down but unfortunately I'm not being super successful at the moment. My strategy for narrowing this down has been:
Next I have a script which runs the binary on aarch64 and x86_64 with a binary-search-of-sorts. This pins down the precise fuel count where with one more fuel the execution differs. My goal here is to find the precise wasm instruction at which execution diverges here. This quickly-ish found results and I started sprinkling "debug stores" within the
*.wat. Basically I'd take a value of the stack, shove it in a temporary global, store the global to linear memory address 0, and then load the global back onto the stack. The goal here was a quick-and-dirty way of debugging the wasm value stack.In doing this I've narrowed this down to function 40 in the input file above. Specifically these two instructions:
Unfortunately this
v128.constvalue is not actually going intoi8x16.swizzle. With my "debug store" inserted between these two instructions it turns out that the value stored in memory is indeed not thev128.consthere. Instead some other bit-pattern gets stored in memory and naturally when that gets fed intoi8x16.swizzleeverything goes awry.Other than that though I have now hit a wall and am unable to make progress debugging this. Function 40 is huge and it's tough to explore the disassembly. Using
rrI can find the portion of the function that actually creates the value which eventually gets fed intoi8x16.swizzle. This should feed in thev128.constvalue but it is not. Unfortunately I can't figure out how to translate this random snippet of assembly back to the source file.I have confirmed, though, that 0.35.0 reproduces this issue so I don't believe this is a problem with recent developments like regalloc2, recent ISLE migrations, or the alias analysis pass added.
Others who are more familiar with the backend may know more about how to debug this though. IIRC there's some optimizations around constants, constant pools, and trying to not always reify something into a register or something like that, and something may be going awry there.
... aaaand as I am writing this up I took another look at the definition of the
Swizzleinstruction. This looks suspicious, specifically treating the mask as a writable reg since I think in this case it's the mask that's getting corrupted going into the swizzle instruction.I added a move from the input to a temporary register and that didn't actually fix the original program but it did surprisingly allow it to make more progress. I think that means there's actually at least two bugs here!
Doing my bisection again I think I found a much simpler reproduction, namely:
I don't think that our codegen for the
selectinstruction is correct for v128 values. Namely this produces:where the
movssI don't believe is correct, I think that needs to bemovdqaor something sized larger.I'm running out of steam for tonight so I think this is as far as I'll get today. I'm actually quite worried that fuzzing hasn't discovered anything in this area. These are somewhat trivial bugs which in theory should be found almost immediately by any halfway decent fuzzing.
x64: Fix codegen for the `i8x16.swizzle` instruction
alexcrichton commentedon Jun 25, 2022
Ok I can confirm that with #4317 and #4318 the original test case now has the same behavior on arm64 and on x86_64
x64: Fix codegen for the `i8x16.swizzle` instruction (#4318)
x64: Fix codegen for the `i8x16.swizzle` instruction (bytecodeallianc…
alexcrichton commentedon Jun 27, 2022
I realize now I forgot to say this earlier but thanks @yurydelendik for taking the time to file this! We'll be making an 0.38.1 release with these fixes soon and a few other fixes for Cranelift (unrelated to this).
We also plan on filing a CVE about these micompiles because while they don't affect hosts themselves this could affect in-wasm execution which is often just as important in some environments.
Backport simd fixes to 0.38.0 (#4334)
alexcrichton commentedon Jun 27, 2022
I believe everything is now merged so I'm going to close this.
x64: Fix codegen for the `i8x16.swizzle` instruction (bytecodeallianc…