Skip to main content

weir/
post_dominates.rs

1//! `post_dominates`  -  query the post-dominator tree.
2//!
3//! Per node `n` and target `t`, write 1 iff `n` post-dominates `t`
4//! (every path from `t` to function exit goes through `n`). This is
5//! the dual of dominance and is needed for control-dependence
6//! analysis + the `csrf_missing_token` rule's path checks.
7
8use vyre::ir::Program;
9use vyre_primitives::bitset::and::bitset_and;
10use vyre_primitives::graph::csr_forward_traverse::bitset_words;
11
12pub(crate) const OP_ID: &str = "weir::post_dominates";
13
14/// Build a post-dominator query Program.
15///
16/// Inputs:
17/// - `pdom_set`: per-node bitset where bit `m` is set in node `n`'s
18///   row iff `n` post-dominates `m` (host-supplied via
19///   classical post-dominator construction).
20/// - `target_set`: per-node bitset of target nodes being queried.
21/// - `out`:        per-node bitset; bit `n` set iff `n` post-
22///   dominates SOME target in `target_set`.
23#[must_use]
24pub fn post_dominates(node_count: u32, pdom_set: &str, target_set: &str, out: &str) -> Program {
25    let words = bitset_words(node_count);
26    vyre_harness::region::tag_program(OP_ID, bitset_and(pdom_set, target_set, out, words))
27}
28
29/// reference oracle.
30#[must_use]
31#[cfg(any(test, feature = "cpu-parity"))]
32#[deprecated(
33    note = "reference oracle only; production code must dispatch the Weir Program on a concrete GPU backend or use weir::oracle for parity evidence"
34)]
35pub(crate) fn cpu_ref(pdom_set: &[u32], target_set: &[u32]) -> Vec<u32> {
36    vyre_primitives::bitset::and::cpu_ref(pdom_set, target_set)
37}
38
39/// Soundness marker for [`post_dominates`].
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub struct PostDominates;
42impl super::soundness::SoundnessTagged for PostDominates {
43    fn soundness(&self) -> super::soundness::Soundness {
44        super::soundness::Soundness::Exact
45    }
46}
47
48inventory::submit! {
49    vyre_harness::OpEntry::new(
50        OP_ID,
51        || post_dominates(4, "pdom", "target", "out"),
52        Some(|| {
53            let u32s = crate::dispatch_decode::pack_u32;
54            vec![vec![u32s(&[0b1111]), u32s(&[0b0011]), u32s(&[0])]]
55        }),
56        Some(|| {
57            let u32s = crate::dispatch_decode::pack_u32;
58            vec![vec![u32s(&[0b0011])]]
59        }),
60    )
61}
62
63#[cfg(test)]
64#[allow(deprecated)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn pdom_intersect_target_set() {
70        assert_eq!(cpu_ref(&[0b1111], &[0b0011]), vec![0b0011]);
71    }
72
73    #[test]
74    fn no_pdom_returns_empty() {
75        assert_eq!(cpu_ref(&[0], &[0xFFFF]), vec![0]);
76    }
77
78    #[test]
79    fn empty_target_returns_empty() {
80        assert_eq!(cpu_ref(&[0xFFFF], &[0]), vec![0]);
81    }
82
83    #[test]
84    fn partial_overlap() {
85        assert_eq!(cpu_ref(&[0xFF00], &[0x0FF0]), vec![0x0F00]);
86    }
87}