A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift: Observe all stores on retreating edges in alias analysis (#14632)
Nick Fitzgerald0ad3b29bb50b
diff --git a/cranelift/codegen/src/alias_analysis.rs b/cranelift/codegen/src/alias_analysis.rsindex c385ae5b9b..6d789c636e 100644--- a/cranelift/codegen/src/alias_analysis.rs+++ b/cranelift/codegen/src/alias_analysis.rs@@ -65,7 +65,9 @@//! elimination". Note that observing a store is not just loading//! from the location it wrote, all potentially-trapping//! instructions must be treated as observing every store because we-//! must preserve post-trap memory state.+//! must preserve post-trap memory state. Similarly, loop back edges+//! observe every store, because the loop might never terminate and+//! reach the overwriting store.//!//! Which store is the "last store" to a region is flow-sensitive, but//! whether a store is ever observed is *not*: it is observed if there@@ -1025,10 +1027,20 @@ impl<'a> AliasAnalysis<'a> {state.update(func, inst);}- // When a predecessor and successor disagree on the last store to a- // region, we need to mark the predecessor's last store as observed,- // so that dead-store elimination cannot remove it.- visit_block_succs(func, block, |_inst, succ, _from_table| {+ visit_block_succs(func, block, |branch, succ, _from_table| {+ // A loop between a maybe-dead store and its overwriter might+ // never terminate, but if the maybe-dead store traps, then we+ // need to preserve that rather than rewrite the program to+ // infinite loop. Therefore, we observe every store in+ // `LastStores` on retreating edges.+ if self.domtree.is_retreating_edge(block, succ) {+ state.observe_others(func, &mut observed_stores, None, branch);+ return;+ }++ // When a predecessor and successor disagree on the last store+ // to a region, we need to mark the predecessor's last store as+ // observed, so that dead-store elimination cannot remove it.let succ_input = self.block_input.get(&succ)diff --git a/cranelift/codegen/src/dominator_tree.rs b/cranelift/codegen/src/dominator_tree.rsindex a4a457b830..f4007587b7 100644--- a/cranelift/codegen/src/dominator_tree.rs+++ b/cranelift/codegen/src/dominator_tree.rs@@ -188,6 +188,9 @@ struct DominatorTreeNode {idom: PackedOption<Block>,/// Preorder traversal number, zero for unreachable blocks.pre_number: u32,+ /// This block's index in the CFG post-order, meaningless for unreachable+ /// blocks.+ post_number: u32,/// First child node in the domtree.child: PackedOption<Block>,@@ -363,6 +366,22 @@ impl DominatorTree {na.dom_pre_number <= nb.dom_pre_number && na.dom_pre_max >= nb.dom_pre_max}+ /// Returns `true` if the CFG edge `from -> to` is a retreating edge.+ ///+ /// A retreating edge is one that goes from a block to one of its ancestors+ /// in the depth-first spanning tree used to compute this dominator tree, or+ /// to itself. Every cycle in the CFG contains at least one retreating edge,+ /// including irreducible cycles, which have no back edge whose target+ /// dominates its source. In a reducible CFG, the retreating edges are+ /// exactly its back edges.+ ///+ /// Both blocks must be reachable.+ pub fn is_retreating_edge(&self, from: Block, to: Block) -> bool {+ debug_assert!(self.is_reachable(from));+ debug_assert!(self.is_reachable(to));+ self.nodes[to].post_number >= self.nodes[from].post_number+ }+/// Get an iterator over the direct children of `block` in the dominator tree.////// These are the blocks whose immediate dominator is `block`, ordered by@@ -486,6 +505,7 @@ impl DominatorTree {);}Some(TraversalEvent::Exit(block)) => {+ self.nodes[block].post_number = u32::try_from(self.postorder.len()).unwrap();self.postorder.push(block);}None => break,@@ -846,6 +866,65 @@ mod tests {assert!(dt.dominates(jmp21, jmp21, &cur.func.layout));}+ #[test]+ fn retreating_edges() {+ let mut func = Function::new();+ let block0 = func.dfg.make_block();+ let v0 = func.dfg.append_block_param(block0, I32);+ let block1 = func.dfg.make_block();+ let block2 = func.dfg.make_block();+ let block3 = func.dfg.make_block();+ let block4 = func.dfg.make_block();+ let block5 = func.dfg.make_block();++ let mut cur = FuncCursor::new(&mut func);++ // An irreducible cycle between `block1` and `block2`, each of which can+ // be entered directly from `block0`.+ cur.insert_block(block0);+ cur.ins().brif(v0, block1, &[], block2, &[]);++ cur.insert_block(block1);+ cur.ins().brif(v0, block2, &[], block3, &[]);++ cur.insert_block(block2);+ cur.ins().jump(block1, &[]);++ // A natural loop with header `block3` and back edge `block4 -> block3`.+ cur.insert_block(block3);+ cur.ins().jump(block4, &[]);++ cur.insert_block(block4);+ cur.ins().brif(v0, block3, &[], block5, &[]);++ // A self-loop.+ cur.insert_block(block5);+ cur.ins().brif(v0, block5, &[], block1, &[]);++ let cfg = ControlFlowGraph::with_function(cur.func);+ let dt = DominatorTree::with_function(cur.func, &cfg);++ // Exactly one edge of the irreducible cycle retreats, depending on which+ // of its blocks the DFS entered first. Neither block dominates the+ // other.+ assert!(!dt.block_dominates(block1, block2));+ assert!(!dt.block_dominates(block2, block1));+ assert_ne!(+ dt.is_retreating_edge(block1, block2),+ dt.is_retreating_edge(block2, block1),+ );++ assert!(dt.is_retreating_edge(block4, block3));+ assert!(dt.is_retreating_edge(block5, block5));+ assert!(dt.is_retreating_edge(block5, block1));++ assert!(!dt.is_retreating_edge(block0, block1));+ assert!(!dt.is_retreating_edge(block0, block2));+ assert!(!dt.is_retreating_edge(block1, block3));+ assert!(!dt.is_retreating_edge(block3, block4));+ assert!(!dt.is_retreating_edge(block4, block5));+ }+#[test]fn insts_same_block() {let mut func = Function::new();diff --git a/cranelift/filetests/filetests/alias/issue-14573.clif b/cranelift/filetests/filetests/alias/issue-14573.clifnew file mode 100644index 0000000000..6debddeb0e--- /dev/null+++ b/cranelift/filetests/filetests/alias/issue-14573.clif@@ -0,0 +1,239 @@+test optimize precise-output+set opt_level=speed+target aarch64++;; A loop between a store and its overwriter might never terminate, in which+;; case the overwriter never runs. A trapping store before such a loop must+;; still trap, so dead-store elimination must keep it.++;; The `block2` path is an infinite loop that never reaches the overwriter.+function %infinite_loop(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ store user1 aligned region0 v1, v0+ brif v3, block2, block1++block1:+ store user1 aligned region0 v2, v0+ return++block2:+ jump block2+}++; function %infinite_loop(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; store user1 aligned region0 v1, v0+; brif v3, block2, block1+;+; block1:+; store.i32 user1 aligned region0 v2, v0+; return+;+; block2:+; jump block2+; }++;; The overwriter post-dominates the first store, but the loop in `block1` need+;; not terminate at run time.+function %loop_with_exit(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ store user1 aligned region0 v1, v0+ jump block1++block1:+ brif v3, block1, block2++block2:+ store user1 aligned region0 v2, v0+ return+}++; function %loop_with_exit(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; store user1 aligned region0 v1, v0+; jump block1+;+; block1:+; brif.i32 v3, block1, block2+;+; block2:+; store.i32 user1 aligned region0 v2, v0+; return+; }++;; Both stores are in blocks that cannot reach the function's exit.+function %both_divergent(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ brif v2, block1, block4++block1:+ store user1 aligned region0 v1, v0+ brif v3, block2, block3++block2:+ jump block2++block3:+ store user1 aligned region0 v2, v0+ jump block5++block5:+ jump block5++block4:+ return+}++; function %both_divergent(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; brif v2, block1, block4+;+; block1:+; store.i32 user1 aligned region0 v1, v0+; brif.i32 v3, block2, block3+;+; block2:+; jump block2+;+; block3:+; store.i32 user1 aligned region0 v2, v0+; jump block5+;+; block5:+; jump block5+;+; block4:+; return+; }++;; Non-trapping stores must also be kept: if this memory is shared, then other+;; threads can observe the first store while this one spins in the loop.+function %notrap_loop_with_exit(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ store notrap aligned region0 v1, v0+ jump block1++block1:+ brif v3, block1, block2++block2:+ store notrap aligned region0 v2, v0+ return+}++; function %notrap_loop_with_exit(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; store notrap aligned region0 v1, v0+; jump block1+;+; block1:+; brif.i32 v3, block1, block2+;+; block2:+; store.i32 notrap aligned region0 v2, v0+; return+; }++;; An irreducible cycle between `block1` and `block2`, neither of which+;; dominates the other.+function %irreducible_loop(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ store user1 aligned region0 v1, v0+ brif v3, block1, block2++block1:+ brif v3, block2, block3++block2:+ jump block1++block3:+ store user1 aligned region0 v2, v0+ return+}++; function %irreducible_loop(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; store user1 aligned region0 v1, v0+; brif v3, block1, block2+;+; block1:+; brif.i32 v3, block2, block3+;+; block2:+; jump block1+;+; block3:+; store.i32 user1 aligned region0 v2, v0+; return+; }++;; A loop before both stores, or a diamond between them, does not stop the first+;; store from being eliminated.+function %loop_before_both_stores(i64, i32, i32, i32) {+ region0 = 0 "heap"++block0(v0: i64, v1: i32, v2: i32, v3: i32):+ jump block1++block1:+ brif v3, block1, block2++block2:+ store user1 aligned region0 v1, v0+ brif v2, block3, block4++block3:+ jump block5++block4:+ jump block5++block5:+ store user1 aligned region0 v2, v0+ return+}++; function %loop_before_both_stores(i64, i32, i32, i32) fast {+; region0 = 0 "heap"+;+; block0(v0: i64, v1: i32, v2: i32, v3: i32):+; jump block1+;+; block1:+; brif.i32 v3, block1, block2+;+; block2:+; brif.i32 v2, block3, block4+;+; block3:+; jump block5+;+; block4:+; jump block5+;+; block5:+; store.i32 user1 aligned region0 v2, v0+; return+; }+diff --git a/tests/disas/issue-14573-shared-memory.wat b/tests/disas/issue-14573-shared-memory.watnew file mode 100644index 0000000000..9e392a978e--- /dev/null+++ b/tests/disas/issue-14573-shared-memory.wat@@ -0,0 +1,49 @@+;;! target = "x86_64"+;;! test = "optimize"+;;! flags = ["-Wthreads=yes"]++;; Other threads can observe the first store while this one spins in the loop,+;; so it must not be eliminated.++(module+ (memory 1 1 shared)+ (func (param i32)+ (i32.store (i32.const 0) (i32.const 1))+ (if (local.get 0) (then (loop (br 0))))+ (i32.store (i32.const 0) (i32.const 2))+ )+)++;; function u0:0(i64 vmctx, i64, i32) tail {+;; region0 = 123 ""+;; region1 = 160 ""+;; region2 = 136 ""+;; region3 = 215 ""+;; region4 = 105 ""+;; region5 = 171 ""+;; gv0 = vmctx+;; gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8+;; gv2 = load.i64 notrap aligned region1 gv1+24+;; stack_limit = gv2+;;+;; block0(v0: i64, v1: i64, v2: i32):+;; @0020 v4 = iconst.i32 1+;; @0022 v6 = load.i64 notrap aligned readonly can_move region2 v0+48+;; @0022 v7 = load.i64 notrap aligned readonly can_move region3 v6+;; @0022 store little region5 v4, v7 ; v4 = 1+;; @0027 brif v2, block2, block3+;;+;; block2:+;; @0029 jump block4+;;+;; block4:+;; @002b jump block4+;;+;; block3:+;; @0031 v10 = iconst.i32 2+;; @0033 store little region5 v10, v7 ; v10 = 2+;; @0036 jump block1+;;+;; block1:+;; @0036 return+;; }diff --git a/tests/misc_testsuite/issue-14573.wast b/tests/misc_testsuite/issue-14573.wastnew file mode 100644index 0000000000..7fbddd9a88--- /dev/null+++ b/tests/misc_testsuite/issue-14573.wast@@ -0,0 +1,24 @@+;; A trapping store must still trap when a loop that might not terminate lies+;; between it and a store that overwrites it.++(module+ (memory 1 1)+ (func (export "f") (param $addr i32) (param $spin i32)+ (i32.store (local.get $addr) (i32.const 1))+ (if (local.get $spin) (then (loop (br 0))))+ (i32.store (local.get $addr) (i32.const 2))+ )+)++(assert_trap (invoke "f" (i32.const 65536) (i32.const 1)) "out of bounds memory access")++(module+ (memory 1 1)+ (func (export "f") (param $addr i32) (param $spin i32)+ (i32.store (local.get $addr) (i32.const 1))+ (loop (br_if 0 (local.get $spin)))+ (i32.store (local.get $addr) (i32.const 2))+ )+)++(assert_trap (invoke "f" (i32.const 65536) (i32.const 1)) "out of bounds memory access")