Commit 5c1a5f6
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 8c97ae8 commit 5c1a5f6
4 files changed
Lines changed: 60094 additions & 18 deletions
File tree
- cranelift
- codegen/src
- isa/x64
- opts
- filetests/filetests/isa/x64
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3403 | 3403 | | |
3404 | 3404 | | |
3405 | 3405 | | |
3406 | | - | |
| 3406 | + | |
3407 | 3407 | | |
3408 | 3408 | | |
3409 | | - | |
| 3409 | + | |
3410 | 3410 | | |
3411 | 3411 | | |
3412 | 3412 | | |
3413 | | - | |
| 3413 | + | |
3414 | 3414 | | |
3415 | 3415 | | |
3416 | 3416 | | |
| |||
3419 | 3419 | | |
3420 | 3420 | | |
3421 | 3421 | | |
3422 | | - | |
3423 | | - | |
3424 | | - | |
3425 | | - | |
3426 | | - | |
3427 | | - | |
3428 | | - | |
| 3422 | + | |
| 3423 | + | |
| 3424 | + | |
| 3425 | + | |
| 3426 | + | |
| 3427 | + | |
| 3428 | + | |
| 3429 | + | |
| 3430 | + | |
| 3431 | + | |
| 3432 | + | |
| 3433 | + | |
| 3434 | + | |
| 3435 | + | |
| 3436 | + | |
| 3437 | + | |
| 3438 | + | |
| 3439 | + | |
3429 | 3440 | | |
3430 | 3441 | | |
3431 | 3442 | | |
| |||
3510 | 3521 | | |
3511 | 3522 | | |
3512 | 3523 | | |
3513 | | - | |
| 3524 | + | |
| 3525 | + | |
| 3526 | + | |
| 3527 | + | |
| 3528 | + | |
| 3529 | + | |
| 3530 | + | |
| 3531 | + | |
3514 | 3532 | | |
3515 | 3533 | | |
3516 | 3534 | | |
| |||
3523 | 3541 | | |
3524 | 3542 | | |
3525 | 3543 | | |
3526 | | - | |
3527 | | - | |
3528 | | - | |
3529 | | - | |
3530 | | - | |
3531 | | - | |
3532 | | - | |
3533 | 3544 | | |
3534 | 3545 | | |
3535 | 3546 | | |
| |||
| 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