weirflow 0.1.0

GPU-first dataflow analysis primitives for Vyre and Santh compiler pipelines.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Tests for `reachability_witness`. Wired via
// `#[path = "reachability_witness_tests.rs"] mod tests;`.

use super::*;

#[path = "reachability_witness_prepared_tests.rs"]
mod prepared_tests;

fn dummy_attrs(n: usize) -> Vec<NodeAttr> {
    (0..n as u32)
        .map(|i| NodeAttr {
            byte_start: i * 10,
            byte_end: (i + 1) * 10,
            file_idx: 0,
        })
        .collect()
}

#[test]
fn trivial_self_path() {
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 3,
        sink_file: "a.c".to_string(),
        sink_node: 3,
    };
    let attrs = dummy_attrs(5);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..5).map(|i| format!("stmt{i}")).collect();
    let path = extract_path(
        &seed,
        &[0xFFFF],
        &[],
        &[0; 6],
        &[],
        &[],
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect("trivial path must reconstruct");
    assert_eq!(path.statements.len(), 1);
    assert_eq!(path.statements[0].node_id, 3);
}

#[test]
fn no_path_when_endpoints_not_reached() {
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 3,
    };
    let attrs = dummy_attrs(4);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..4).map(|i| format!("stmt{i}")).collect();
    // Reachability bitmask has bit 0 but not bit 3.
    let err = extract_path(
        &seed,
        &[0b0001],
        &[],
        &[0, 1, 2, 3, 3],
        &[1, 2, 3],
        &[edge_kind::ASSIGNMENT; 3],
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("no path expected");
    assert_eq!(err, PathError::NoPath);
}

#[test]
fn witness_graph_rejects_orphan_prefix_edges() {
    let attrs = dummy_attrs(2);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..2).map(|i| format!("stmt{i}")).collect();
    let err = try_prepare_witness_graph(
        &[1, 1, 1],
        &[0],
        &[edge_kind::ASSIGNMENT],
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("nonzero first CSR offset must be rejected");
    assert!(
        matches!(err, PathError::MalformedGraph { ref fix, .. } if fix.contains("edge_offsets[0]")),
        "unexpected diagnostic: {err:?}"
    );
}

#[test]
fn linear_chain_reconstructs() {
    // Graph: 0 → 1 → 2 → 3 (all assignment edges)
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 3,
    };
    let attrs = dummy_attrs(4);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..4).map(|i| format!("stmt{i}")).collect();
    let edge_offsets = vec![0, 1, 2, 3, 3];
    let edge_targets = vec![1u32, 2, 3];
    let edge_kind_mask = vec![edge_kind::ASSIGNMENT; 3];
    let source_reach = vec![0b1111];
    let path = extract_path(
        &seed,
        &source_reach,
        &[],
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect("linear-chain path must reconstruct");
    let ids: Vec<u32> = path.statements.iter().map(|s| s.node_id).collect();
    assert_eq!(ids, vec![0, 1, 2, 3]);
    assert_eq!(path.statements[0].file, "a.c");
    assert_eq!(path.statements[0].adapter, "c-c11");
}

#[test]
fn ignores_non_ifds_edges() {
    // Graph: 0 →[CONTROL] 1 →[ASSIGNMENT] 2.
    // CONTROL edges shouldn't propagate IFDS taint, so the path
    // walker shouldn't follow 1 ← 0 even though both are reached.
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 2,
    };
    let attrs = dummy_attrs(3);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..3).map(|i| format!("stmt{i}")).collect();
    let edge_offsets = vec![0, 1, 2, 2];
    let edge_targets = vec![1u32, 2];
    let edge_kind_mask = vec![edge_kind::CONTROL, edge_kind::ASSIGNMENT];
    let source_reach = vec![0b111];
    let err = extract_path(
        &seed,
        &source_reach,
        &[],
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("no IFDS-eligible predecessor of 1 → no path");
    assert_eq!(err, PathError::NoPath);
}

/// AUDIT 2026-04-27 finding 2: the walker must reject any
/// predecessor whose bit is set in the sanitizer mask. Without
/// this, the witness silently crosses a sanitizer.
#[test]
fn rejects_sanitizer_on_path() {
    // Graph: 0 → 1 → 2 → 3 (all assignment, all reached).
    // Node 1 is a sanitizer; the walker must refuse to use it
    // as a predecessor → no path source(0)→sink(3).
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 3,
    };
    let attrs = dummy_attrs(4);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..4).map(|i| format!("stmt{i}")).collect();
    let edge_offsets = vec![0, 1, 2, 3, 3];
    let edge_targets = vec![1u32, 2, 3];
    let edge_kind_mask = vec![edge_kind::ASSIGNMENT; 3];
    let source_reach = vec![0b1111];
    let sanitizer_mask = vec![0b0010]; // bit 1 = sanitizer
    let err = extract_path(
        &seed,
        &source_reach,
        &sanitizer_mask,
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("witness must not cross sanitizer");
    assert_eq!(err, PathError::NoPath);
}

/// Negative twin of `rejects_sanitizer_on_path`: when the
/// sanitizer is set on a node OFF the path, reconstruction
/// proceeds normally.
#[test]
fn sanitizer_off_path_does_not_block_reconstruction() {
    // Graph: 0 → 1 → 2; node 5 is a sanitizer but is not on
    // the source→sink path. Witness must still reconstruct.
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 2,
    };
    let attrs = dummy_attrs(6);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..6).map(|i| format!("stmt{i}")).collect();
    let edge_offsets = vec![0, 1, 2, 2, 2, 2, 2];
    let edge_targets = vec![1u32, 2];
    let edge_kind_mask = vec![edge_kind::ASSIGNMENT; 2];
    let source_reach = vec![0b111111];
    let sanitizer_mask = vec![0b100000]; // bit 5 = sanitizer
    let path = extract_path(
        &seed,
        &source_reach,
        &sanitizer_mask,
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect("off-path sanitizer must not block reconstruction");
    assert_eq!(
        path.statements
            .iter()
            .map(|s| s.node_id)
            .collect::<Vec<_>>(),
        vec![0, 1, 2]
    );
}

/// AUDIT 2026-04-27 finding 4: depth overrun must surface a
/// `DepthExceeded` variant carrying the partial chain so the
/// caller can distinguish it from the genuine `NoPath` case.
#[test]
fn depth_exceeded_returns_partial_chain() {
    // A long linear chain longer than MAX_PATH_DEPTH. The walker
    // must hit the limit and return DepthExceeded instead of
    // silently failing.
    let n = MAX_PATH_DEPTH + 50;
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: (n as u32) - 1,
    };
    let attrs = dummy_attrs(n);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..n).map(|i| format!("stmt{i}")).collect();
    // edge_offsets[i] = i so each node has one outgoing edge to
    // i+1; final node has no outgoing edge.
    let mut edge_offsets: Vec<u32> = (0..=n as u32).collect();
    // Last entry must equal edge count (n-1). Patch.
    if let Some(last) = edge_offsets.last_mut() {
        *last = (n - 1) as u32;
    }
    // Last-but-one too: nodes [0..n-1] each have 1 outgoing edge.
    let edge_targets: Vec<u32> = (1..n as u32).collect();
    let edge_kind_mask: Vec<u32> = vec![edge_kind::ASSIGNMENT; n - 1];
    // Reach mask: every node reached.
    let words = n.div_ceil(32);
    let source_reach = vec![u32::MAX; words];
    let err = extract_path(
        &seed,
        &source_reach,
        &[],
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("path longer than MAX_PATH_DEPTH must surface");
    match err {
        PathError::DepthExceeded { partial_chain } => {
            // Walker took exactly MAX_PATH_DEPTH steps after the
            // initial sink push; the partial chain is sink-first
            // and at most MAX_PATH_DEPTH + 1 entries long.
            assert!(partial_chain.len() >= MAX_PATH_DEPTH);
            assert_eq!(partial_chain[0], (n as u32) - 1);
        }
        other => panic!("expected DepthExceeded, got {other:?}"),
    }
}

/// AUDIT 2026-04-27 finding 5: a seed naming a node that's out
/// of bounds for `pg_node_attrs` must surface as
/// `NodeOutOfBounds`, not panic on `visited[current]`.
#[test]
fn out_of_bounds_seed_surfaces_as_error() {
    let seed = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 9999, // way past pg_node_attrs.len()
    };
    let attrs = dummy_attrs(4);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..4).map(|i| format!("stmt{i}")).collect();
    let err = extract_path(
        &seed,
        &[u32::MAX; 4],
        &[],
        &[0, 1, 2, 3, 3],
        &[1, 2, 3],
        &[edge_kind::ASSIGNMENT; 3],
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect_err("OOB seed must surface as error, not panic");
    match err {
        PathError::NodeOutOfBounds { node, node_count } => {
            assert_eq!(node, 9999);
            assert_eq!(node_count, 4);
        }
        other => panic!("expected NodeOutOfBounds, got {other:?}"),
    }
}

/// AUDIT 2026-04-27 finding 3: per-source reachability masks.
/// When `source_reach` represents source A but the seed names
/// source A, the walker must follow A's chain  -  not the
/// aggregate-over-all-sources chain that another source B might
/// admit. Demonstration: graph has two sources reaching a
/// shared sink via different intermediate paths; the walker
/// must follow the seeded source's mask, not an aggregate.
#[test]
fn per_source_mask_drives_path_choice() {
    // Graph: 0 → 2 → 4 (source A's chain)
    //         1 → 3 → 4 (source B's chain)
    let attrs = dummy_attrs(5);
    let files = vec!["a.c".to_string()];
    let descs: Vec<String> = (0..5).map(|i| format!("stmt{i}")).collect();
    let edge_offsets = vec![0, 1, 2, 3, 4, 4];
    let edge_targets = vec![2u32, 3, 4, 4];
    let edge_kind_mask = vec![edge_kind::ASSIGNMENT; 4];

    // Source A's mask: reaches 0, 2, 4 (NOT 1, NOT 3).
    let source_a_reach = vec![0b10101];
    let seed_a = PathSeed {
        source_file: "a.c".to_string(),
        source_node: 0,
        sink_file: "a.c".to_string(),
        sink_node: 4,
    };
    let path_a = extract_path(
        &seed_a,
        &source_a_reach,
        &[],
        &edge_offsets,
        &edge_targets,
        &edge_kind_mask,
        &attrs,
        &files,
        &descs,
        "c-c11",
    )
    .expect("source A's path must reconstruct");
    assert_eq!(
        path_a
            .statements
            .iter()
            .map(|s| s.node_id)
            .collect::<Vec<_>>(),
        vec![0, 2, 4]
    );

    // If we (incorrectly) handed the AGGREGATE mask to the
    // walker AND seeded with source A, the walker might pick
    // node 1 or 3 as a predecessor of 4  -  which is source B's
    // chain. The per-source mask above prevents that.
}

#[test]
fn exploded_reachability_decodes_to_statement_mask() {
    let nodes = vec![
        vyre_primitives::graph::exploded::encode_node(0, 0, 7).expect("valid node"),
        vyre_primitives::graph::exploded::encode_node(0, 1, 7).expect("valid node"),
        vyre_primitives::graph::exploded::encode_node(1, 0, 3).expect("valid node"),
    ];
    let block_to_statement = vec![2, 5, 8, 13];
    let mask = exploded_reachability_to_statement_mask(&nodes, 2, &block_to_statement, Some(7), 16)
        .expect("valid exploded reachability must decode");

    assert!(super::bit_is_set(&mask, 2));
    assert!(super::bit_is_set(&mask, 5));
    assert!(!super::bit_is_set(&mask, 8));
    assert!(!super::bit_is_set(&mask, 13));
}

#[test]
fn exploded_reachability_rejects_missing_block_map_slot() {
    let node = vyre_primitives::graph::exploded::encode_node(2, 3, 0).expect("valid node");
    let err = exploded_reachability_to_statement_mask(&[node], 4, &[0, 1], None, 8)
        .expect_err("missing block map slot must surface");
    assert_eq!(
        err,
        ExplodedDecodeError::BlockOutOfBounds {
            proc_id: 2,
            block_id: 3,
            slot: 11,
            slot_count: 2,
        }
    );
}

#[test]
fn exploded_reachability_rejects_statement_oob() {
    let node = vyre_primitives::graph::exploded::encode_node(0, 0, 0).expect("valid node");
    let err = exploded_reachability_to_statement_mask(&[node], 1, &[9], None, 4)
        .expect_err("statement map must stay inside statement table");
    assert_eq!(
        err,
        ExplodedDecodeError::StatementOutOfBounds {
            statement: 9,
            statement_count: 4,
        }
    );
}