panicgraph 0.2.1

Reports which functions can panic, why, and through what call path.
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
438
439
440
441
442
443
//! Behaviour of the suppression-aware solver.

mod support;

use panicgraph::{
    Category, CategorySet, FuncKey, Graph, Policy, Solver, witness,
};

use crate::support::{BodyBuilder, graph as build};

/// Solves a set of bodies under one suppression policy.
fn solve(
    bodies: Vec<panicgraph::Body>,
    suppressed: CategorySet,
) -> (Graph, panicgraph::Solution) {
    solve_with(bodies, suppressed, panicgraph::solve::Edges::default())
}

/// Solves a set of bodies under a full policy.
fn solve_with(
    bodies: Vec<panicgraph::Body>,
    suppressed: CategorySet,
    edges: panicgraph::solve::Edges,
) -> (Graph, panicgraph::Solution) {
    let graph = build(bodies);
    let policy = Policy { suppressed, edges };
    let solution = Solver::new(&graph, policy)
        .solve()
        .expect("the solver should converge on a finite graph");
    (graph, solution)
}

/// Looks a function up by name.
fn id(graph: &Graph, name: &str) -> panicgraph::FuncId {
    graph
        .id_of(&FuncKey(name.to_owned()))
        .expect("the function should be in the graph")
}

#[test]
fn suppression_is_transitive() {
    let bodies = vec![
        BodyBuilder::new("caller").calls("grow").build(),
        BodyBuilder::new("grow")
            .panics(Category::CapacityOverflow)
            .build(),
    ];

    let (graph, solution) = solve(bodies.clone(), CategorySet::EMPTY);
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::CapacityOverflow),
        "without suppression the caller inherits the allocation panic"
    );

    let (graph, solution) = solve(bodies, CategorySet::oom());
    assert!(
        solution.is_clean(id(&graph, "caller")),
        "a caller that only panics through allocation is clean once \
         allocation is suppressed"
    );
}

#[test]
fn unrelated_panics_survive_suppression() {
    let bodies = vec![
        BodyBuilder::new("caller")
            .calls("grow")
            .calls("index")
            .build(),
        BodyBuilder::new("grow")
            .panics(Category::CapacityOverflow)
            .build(),
        BodyBuilder::new("index").panics(Category::Index).build(),
    ];

    let (graph, solution) = solve(bodies, CategorySet::oom());
    let enabled = solution.enabled(id(&graph, "caller"));
    assert!(
        enabled.contains(Category::Index),
        "suppressing allocation must not hide an index panic"
    );
    assert!(
        !enabled.contains(Category::CapacityOverflow),
        "the allocation panic is suppressed"
    );
}

#[test]
fn cleanup_reachable_only_through_a_suppressed_panic_is_suppressed() {
    // `caller` calls `grow`, which can only fail by exhausting capacity.
    // While that failure unwinds, a drop runs and panics. Assuming the
    // allocation succeeds means the drop never runs either.
    let bodies = vec![
        BodyBuilder::new("caller")
            .calls("grow")
            .calls_on_unwind_of("drop_glue", 0)
            .build(),
        BodyBuilder::new("grow")
            .panics(Category::CapacityOverflow)
            .build(),
        BodyBuilder::new("drop_glue")
            .panics(Category::Explicit)
            .build(),
    ];

    let (graph, solution) = solve(bodies.clone(), CategorySet::EMPTY);
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Explicit),
        "without suppression the unwind path reaches the panicking drop"
    );

    let (graph, solution) = solve(bodies, CategorySet::oom());
    assert!(
        solution.is_clean(id(&graph, "caller")),
        "the drop is only reachable while the suppressed panic unwinds, so \
         it is unreachable too"
    );
}

#[test]
fn cleanup_reachable_normally_survives_suppression() {
    // The same panicking drop, but now also on the ordinary path. It must
    // still be reported.
    let bodies = vec![
        BodyBuilder::new("caller")
            .calls("grow")
            .calls("drop_glue")
            .build(),
        BodyBuilder::new("grow")
            .panics(Category::CapacityOverflow)
            .build(),
        BodyBuilder::new("drop_glue")
            .panics(Category::Explicit)
            .build(),
    ];

    let (graph, solution) = solve(bodies, CategorySet::oom());
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Explicit),
        "a drop on the normal path is unaffected by allocation suppression"
    );
}

#[test]
fn recursion_converges() {
    let bodies = vec![
        BodyBuilder::new("a").calls("b").build(),
        BodyBuilder::new("b")
            .calls("a")
            .panics(Category::Index)
            .build(),
    ];

    let (graph, solution) = solve(bodies, CategorySet::EMPTY);
    assert!(
        solution.enabled(id(&graph, "a")).contains(Category::Index),
        "a cycle must still propagate the panic"
    );
}

#[test]
fn missing_callees_are_unknown_not_clean() {
    let bodies = vec![BodyBuilder::new("caller").calls("absent").build()];

    let (graph, solution) = solve(bodies, CategorySet::EMPTY);
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Unknown),
        "a callee with no recorded body is unknown, never assumed clean"
    );
}

#[test]
fn witness_names_the_panicking_function() {
    let bodies = vec![
        BodyBuilder::new("caller").calls("middle").build(),
        BodyBuilder::new("middle").calls("leaf").build(),
        BodyBuilder::new("leaf").panics(Category::Unwrap).build(),
    ];

    let (graph, solution) = solve(bodies, CategorySet::EMPTY);
    let path = witness::find(
        &graph,
        &solution,
        id(&graph, "caller"),
        Category::Unwrap,
    )
    .expect("the unwrap should be reachable");

    assert_eq!(graph.body(path.func).display, "leaf");
    assert_eq!(path.hops.len(), 2, "the path runs caller -> middle -> leaf");
    assert_eq!(path.terminal, panicgraph::Terminal::Site(0));
}

#[test]
fn witness_is_absent_when_suppressed() {
    let bodies = vec![
        BodyBuilder::new("caller").calls("grow").build(),
        BodyBuilder::new("grow")
            .panics(Category::CapacityOverflow)
            .build(),
    ];

    let (graph, solution) = solve(bodies, CategorySet::oom());
    assert!(
        witness::find(
            &graph,
            &solution,
            id(&graph, "caller"),
            Category::CapacityOverflow,
        )
        .is_none(),
        "a suppressed category has no witness"
    );
}

#[test]
fn an_exact_name_wins_over_a_longer_one_that_contains_it() {
    // `why` explains the first match, so a search for a function must not
    // land on the closure defined inside it.
    let graph = build(vec![
        BodyBuilder::new("fold::{closure#0}").build(),
        BodyBuilder::new("outer::fold::inner").build(),
        BodyBuilder::new("fold").build(),
    ]);

    let matches = graph.find_by_display("fold");

    assert_eq!(matches.len(), 3);
    assert_eq!(graph.body(matches[0]).display, "fold");
}

#[test]
fn a_barrier_contains_unwinding_panics_but_not_aborts() {
    let (graph, solution) = solve(
        vec![
            BodyBuilder::new("callee")
                .panics(Category::Explicit)
                .aborts(Category::AllocFailure)
                .build(),
            BodyBuilder::new("caller")
                .calls_behind_barrier("callee")
                .build(),
        ],
        CategorySet::EMPTY,
    );
    let caller = id(&graph, "caller");
    assert!(
        !solution.enabled(caller).contains(Category::Explicit),
        "the unwinding panic must stop at the barrier"
    );
    assert!(
        solution.enabled(caller).contains(Category::AllocFailure),
        "an abort cannot be caught, so it must cross the barrier"
    );
    assert!(
        !solution.unwinds(caller),
        "a caller whose only panic aborts cannot unwind"
    );
}

#[test]
fn cleanup_gated_on_a_barrier_call_stays_dead() {
    let (graph, solution) = solve(
        vec![
            BodyBuilder::new("callee")
                .panics(Category::Explicit)
                .build(),
            BodyBuilder::new("dropper").panics(Category::Index).build(),
            BodyBuilder::new("caller")
                .calls_behind_barrier("callee")
                .calls_on_unwind_of("dropper", 0)
                .build(),
        ],
        CategorySet::EMPTY,
    );
    let caller = id(&graph, "caller");
    assert!(
        !solution.enabled(caller).contains(Category::Index),
        "nothing unwinds out of a barrier call, so its cleanup cannot run"
    );
}

#[test]
fn a_candidate_edge_is_followed_only_when_asked() {
    let bodies = || {
        vec![
            BodyBuilder::new("one_impl").panics(Category::Index).build(),
            BodyBuilder::new("caller")
                .calls_candidate("one_impl")
                .build(),
        ]
    };
    let (graph, solution) = solve(bodies(), CategorySet::EMPTY);
    assert!(
        !solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Index),
        "a candidate is not a proven target, so it stays unfollowed by \
         default"
    );
    let (graph, solution) = solve_with(
        bodies(),
        CategorySet::EMPTY,
        panicgraph::solve::Edges {
            follow_inexact: true,
            candidates: true,
        },
    );
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Index),
        "asking for candidates follows the edge"
    );
}

#[test]
fn a_panic_that_cannot_leave_its_function_crosses_a_barrier() {
    // An `extern "C"` function that panics aborts at its own boundary, so a
    // catch around a call to it contains nothing.
    let (graph, solution) = solve(
        vec![
            BodyBuilder::new("callback")
                .panics_without_leaving(Category::Explicit)
                .build(),
            BodyBuilder::new("caller")
                .calls_behind_barrier("callback")
                .build(),
        ],
        CategorySet::EMPTY,
    );
    let callback = id(&graph, "callback");
    assert!(
        !solution.unwinds(callback),
        "a panic that aborts at the function's boundary does not unwind \
         out of it"
    );
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Explicit),
        "an abort cannot be caught, so the catch must not contain it"
    );
}

#[test]
fn a_call_that_must_not_unwind_turns_the_callee_unwind_into_an_abort() {
    let (graph, solution) = solve(
        vec![
            BodyBuilder::new("leaf").panics(Category::Index).build(),
            BodyBuilder::new("guarded")
                .calls_without_unwinding("leaf")
                .build(),
            BodyBuilder::new("caller")
                .calls_behind_barrier("guarded")
                .build(),
        ],
        CategorySet::EMPTY,
    );
    assert!(
        !solution.unwinds(id(&graph, "guarded")),
        "nothing unwinds past a call its function must not unwind past"
    );
    assert!(
        solution
            .enabled(id(&graph, "caller"))
            .contains(Category::Index),
        "the index panic aborts on its way out, so no catch contains it"
    );
}

#[test]
fn cleanup_still_runs_before_a_panic_aborts_at_the_boundary() {
    // The drop runs while the panic unwinds, before the function aborts, so
    // what it raises is reachable. It sits in a cleanup block, which cannot
    // unwind.
    let mut caller = BodyBuilder::new("caller")
        .panics_without_leaving(Category::Explicit)
        .calls_without_unwinding("dropper")
        .build();
    caller.calls[0].guard = panicgraph::Guard {
        normal: false,
        origins: vec![panicgraph::UnwindOrigin::Site(0)],
    };
    let (graph, solution) = solve(
        vec![
            caller,
            BodyBuilder::new("dropper").panics(Category::Index).build(),
        ],
        CategorySet::EMPTY,
    );
    let enabled = solution.enabled(id(&graph, "caller"));
    assert!(
        enabled.contains(Category::Index),
        "the cleanup reached while the panic unwinds still runs, got \
         {enabled:?}"
    );
    assert!(
        !solution.unwinds(id(&graph, "caller")),
        "and nothing leaves the function unwinding"
    );
}

#[test]
fn ignoring_indirect_calls_keeps_what_the_analysis_could_not_read() {
    // Only vtable and function pointer calls are indirect; generic and
    // unresolved calls still stand for code the analysis could not read.
    let (graph, solution) = solve_with(
        vec![
            BodyBuilder::new("caller")
                .calls_unresolved(panicgraph::EdgeKind::Vtable)
                .calls_unresolved(panicgraph::EdgeKind::FnPtr)
                .calls_unresolved(panicgraph::EdgeKind::Generic)
                .calls_unresolved(panicgraph::EdgeKind::Unresolved)
                .build(),
        ],
        CategorySet::EMPTY,
        panicgraph::solve::Edges {
            follow_inexact: false,
            candidates: false,
        },
    );
    let enabled = solution.enabled(id(&graph, "caller"));
    assert!(
        !enabled.contains(Category::DynCall)
            && !enabled.contains(Category::FnPointer),
        "indirect calls are ignored, got {enabled:?}"
    );
    assert!(
        enabled.contains(Category::GenericBound)
            && enabled.contains(Category::Unknown),
        "a generic call and an unresolved one are not indirect, got \
         {enabled:?}"
    );
}