Commit 73fd9ae
authored
* Cranelift: x64: fix user-controlled recursion in cmp emission.
We had a set of rules introduced in #11097 that attempted to optimize
the case of testing the result of an `icmp` for a nonzero value. This
allowed optimization of, for example, `(((x == 0) == 0) == 0 ...)` to
a single level, either `x == 0` or `x != 0` depending on even/odd
nesting depth.
Unfortunately this kind of recursion in the backend has a depth
bounded only by the user input, hence creates a DoS vulnerability: the
wrong kind of compiler input can cause a stack overflow in Cranelift
at compilation time. This case is reachable from Wasmtime's Wasm
frontend via the `i32.eqz` operator (for example) as well.
Ideally, this kind of deep rewrite is best done in our mid-end
optimizer, where we think carefully about bounds for recursive
rewrites. The left-hand sides for the backend rules should really be
fixed shapes that correspond to machine instructions, rather than
ad-hoc peephole optimizations in their own right.
This fix thus simply removes the recursion case that causes the
blowup. The patch includes two tests: one with optimizations disabled,
showing correct compilation (without the fix, this case fails to
compile with a stack overflow), and one with optimizations enabled,
showing that the mid-end properly cleans up the nested expression and
we get the expected one-level result anyway.
* Preserve codegen on branches.
This change works by splitting a rule so that the entry point used by
`brif` lowering can still peel off one layer of `icmp` and emit it
directly, without entering the unbounded structural recursion.
It also adds a mid-end rule to catch one case that we were previously
catching in the backend only: `fcmp(...) != 0`.
1 parent efb390d commit 73fd9ae
File tree
4 files changed
+60094
-18
lines changed- cranelift
- codegen/src
- isa/x64
- opts
- filetests/filetests/isa/x64
4 files changed
+60094
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3423 | 3423 | | |
3424 | 3424 | | |
3425 | 3425 | | |
3426 | | - | |
| 3426 | + | |
3427 | 3427 | | |
3428 | 3428 | | |
3429 | | - | |
| 3429 | + | |
3430 | 3430 | | |
3431 | 3431 | | |
3432 | 3432 | | |
3433 | | - | |
| 3433 | + | |
3434 | 3434 | | |
3435 | 3435 | | |
3436 | 3436 | | |
| |||
3439 | 3439 | | |
3440 | 3440 | | |
3441 | 3441 | | |
3442 | | - | |
3443 | | - | |
3444 | | - | |
3445 | | - | |
3446 | | - | |
3447 | | - | |
3448 | | - | |
| 3442 | + | |
| 3443 | + | |
| 3444 | + | |
| 3445 | + | |
| 3446 | + | |
| 3447 | + | |
| 3448 | + | |
| 3449 | + | |
| 3450 | + | |
| 3451 | + | |
| 3452 | + | |
| 3453 | + | |
| 3454 | + | |
| 3455 | + | |
| 3456 | + | |
| 3457 | + | |
| 3458 | + | |
| 3459 | + | |
3449 | 3460 | | |
3450 | 3461 | | |
3451 | 3462 | | |
| |||
3530 | 3541 | | |
3531 | 3542 | | |
3532 | 3543 | | |
3533 | | - | |
| 3544 | + | |
| 3545 | + | |
| 3546 | + | |
| 3547 | + | |
| 3548 | + | |
| 3549 | + | |
| 3550 | + | |
| 3551 | + | |
3534 | 3552 | | |
3535 | 3553 | | |
3536 | 3554 | | |
| |||
3543 | 3561 | | |
3544 | 3562 | | |
3545 | 3563 | | |
3546 | | - | |
3547 | | - | |
3548 | | - | |
3549 | | - | |
3550 | | - | |
3551 | | - | |
3552 | | - | |
3553 | 3564 | | |
3554 | 3565 | | |
3555 | 3566 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
| |||
0 commit comments