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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! The gate a continuous integration run applies to the findings.
//!
//! A check answers one question: are the functions that must not panic still
//! unable to? Everything here exists to make the answer legible when it is
//! no, and quiet when it is yes.

use std::{fmt::Write as _, fs, path::Path};

use anyhow::{Context, Result, bail, ensure};
use regex::RegexSet;
use serde_json::{Value, json};

use crate::{
    Category, CategorySet, Graph, Solution,
    args::{Args, Check, Format},
    report::{self, workflow_location},
    util::{Map, Set},
};

/// The format version written into a baseline.
const BASELINE_VERSION: u32 = 2;

/// The categories a baseline records, keyed by crate and function name. An
/// entry from an older baseline has an empty crate.
pub type Recorded = Map<(String, String), Vec<String>>;

/// One function that can panic.
#[derive(Debug, Clone)]
pub struct Finding {
    /// The function's readable path.
    pub function: String,
    /// The crate it is defined in.
    pub krate: String,
    /// Where it is defined.
    pub loc: Option<String>,
    /// The categories it can raise, in reporting order.
    pub categories: Vec<String>,
}

/// Why a finding failed the gate.
#[derive(Debug, Clone)]
pub enum Reason {
    /// The function is covered by a pattern that forbids panicking.
    Forbidden,
    /// The function is absent from the baseline, or gained a category.
    New,
    /// The analysis could not classify what it reaches.
    Unclassified,
}

impl Reason {
    /// A short phrase naming the failure.
    const fn describe(&self) -> &'static str {
        match self {
            Self::Forbidden => "must not panic",
            Self::New => "not in the baseline",
            Self::Unclassified => "reaches an unclassified panic",
        }
    }
}

/// One failure of the gate.
#[derive(Debug, Clone)]
pub struct Violation {
    /// The offending finding.
    pub finding: Finding,
    /// Why it failed.
    pub reason: Reason,
}

/// Everything a check concluded.
#[derive(Debug, Default)]
pub struct Outcome {
    /// Every local function that can panic.
    pub findings: Vec<Finding>,
    /// The ones that failed a gate.
    pub violations: Vec<Violation>,
    /// Functions the baseline recorded that no longer panic.
    pub fixed: Vec<String>,
    /// Set when more functions panic than the ceiling allows.
    pub over_max: Option<(usize, usize)>,
}

impl Outcome {
    /// Whether anything failed.
    #[must_use]
    pub const fn failed(&self) -> bool {
        !self.violations.is_empty() || self.over_max.is_some()
    }
}

/// Applies the gate to a solved graph.
///
/// # Errors
///
/// Returns an error for an unusable pattern or an unreadable baseline.
pub fn run(
    graph: &Graph,
    solution: &Solution,
    args: &Args,
    check: &Check,
) -> Result<Outcome> {
    let findings = collect(graph, solution, args);
    let mut outcome = Outcome {
        findings,
        ..Outcome::default()
    };

    let forbid = compile(&check.forbid, "--forbid")?;
    let allow = compile(&check.allow, "--allow")?;
    // With no gate at all the whole crate is covered, which is the check a
    // crate that must not panic asks for. Naming any gate replaces that
    // default rather than stacking with it, so a ceiling means a ceiling.
    let gate_everything = check.forbid.is_empty()
        && check.max.is_none()
        && check.baseline.is_none();

    let baseline = check
        .baseline
        .as_deref()
        .map(|path| read_baseline(path, args))
        .transpose()?;

    for finding in &outcome.findings {
        if allow.is_match(&finding.function) {
            continue;
        }
        let covered = gate_everything || forbid.is_match(&finding.function);
        let reason = baseline.as_ref().map_or_else(
            || covered.then_some(Reason::Forbidden),
            |known| is_new(known, finding).then_some(Reason::New),
        );
        let reason = reason.or_else(|| {
            let assumed = |name: &String| {
                name.parse::<Category>()
                    .is_ok_and(|c| CategorySet::assumed().contains(c))
            };
            // A pattern scopes which functions are asked about. A ceiling
            // or a baseline narrows how many findings may fail, not what
            // counts as unreadable, so neither takes this question away.
            let asked =
                check.forbid.is_empty() || forbid.is_match(&finding.function);
            (check.fail_on_unknown
                && asked
                && finding.categories.iter().any(assumed))
            .then_some(Reason::Unclassified)
        });
        if let Some(reason) = reason {
            outcome.violations.push(Violation {
                finding: finding.clone(),
                reason,
            });
        }
    }

    if let Some(known) = &baseline {
        // Each name is live under its crate and under no crate, which is how
        // an entry from an older baseline names it.
        let live: Set<(&str, &str)> = outcome
            .findings
            .iter()
            .flat_map(|f| {
                let name = f.function.as_str();
                [(f.krate.as_str(), name), ("", name)]
            })
            .collect();
        outcome.fixed = known
            .iter()
            .filter(|((krate, name), _)| {
                !live.contains(&(krate.as_str(), name.as_str()))
            })
            .filter(|(_, recorded)| in_view(args.only, recorded))
            .map(|((_, name), _)| name.clone())
            .collect();
        outcome.fixed.sort();
    }

    if let Some(max) = check.max
        && outcome.findings.len() > max
    {
        outcome.over_max = Some((outcome.findings.len(), max));
    }

    Ok(outcome)
}

/// Whether the reported categories could have shown a baseline entry.
///
/// Absence from the findings means a function no longer panics only when the
/// analysis was looking for what the baseline recorded. Under `--only`, an
/// entry outside the selection is not gone, it is out of view, and calling it
/// fixed would send the reader off to refresh a baseline that is current.
fn in_view(only: Option<CategorySet>, recorded: &[String]) -> bool {
    let Some(only) = only else {
        return true;
    };
    recorded
        .iter()
        .filter_map(|name| name.parse::<Category>().ok())
        .any(|category| only.contains(category))
}

/// Whether a finding is absent from the baseline, or has grown a category.
/// An entry with no crate matches the name in any crate.
fn is_new(known: &Recorded, finding: &Finding) -> bool {
    known
        .get(&(finding.krate.clone(), finding.function.clone()))
        .or_else(|| known.get(&(String::new(), finding.function.clone())))
        .is_none_or(|recorded| {
            finding
                .categories
                .iter()
                .any(|category| !recorded.contains(category))
        })
}

/// Every local function that can panic under the solved policy.
///
/// A generic function has one node per instantiation and they all report
/// under the same name, so they are merged: the gate asks whether a function
/// can panic, and it can if any of its instantiations can.
fn collect(graph: &Graph, solution: &Solution, args: &Args) -> Vec<Finding> {
    report::collect(graph, solution, args)
        .into_iter()
        .map(|found| Finding {
            function: found.name.to_owned(),
            krate: found.krate.to_owned(),
            // Instantiations of one generic function share a definition,
            // so the first that records one names them all.
            loc: found
                .ids
                .iter()
                .find_map(|id| graph.body(*id).loc.as_ref())
                .map(ToString::to_string),
            categories: found
                .categories
                .iter()
                .map(|c| c.name().to_owned())
                .collect(),
        })
        .collect()
}

/// Compiles a set of patterns, naming the flag when one is unusable.
fn compile(patterns: &[String], flag: &str) -> Result<RegexSet> {
    RegexSet::new(patterns)
        .with_context(|| format!("a pattern given to {flag} is not valid"))
}

/// Writes the findings so a later run can gate on what changed.
///
/// # Errors
///
/// Returns an error if the file cannot be written.
pub fn write_baseline(
    path: &Path,
    args: &Args,
    findings: &[Finding],
) -> Result<()> {
    let doc = json!({
        "version": BASELINE_VERSION,
        "profile": args.profile,
        "std_mode": args.std_mode.name(),
        "mir_opt_level": args.mir_opt_level,
        "features": args.features.describe(),
        "suppressed": args.suppress.names(),
        "closures": args.closures.name(),
        "generics": args.generics.name(),
        "all_crates": args.all_crates,
        "static_only": args.static_only,
        "candidates": args.candidates,
        "findings": findings.iter().map(|f| json!({
            "crate": f.krate,
            "function": f.function,
            "categories": f.categories,
        })).collect::<Vec<_>>(),
    });
    let text = serde_json::to_string_pretty(&doc)?;
    fs::write(path, format!("{text}\n"))
        .with_context(|| format!("could not write {}", path.display()))
}

/// Reads a baseline into the categories recorded per function.
///
/// # Errors
///
/// Returns an error if the file is missing, unreadable, or not a baseline.
pub fn read_baseline(path: &Path, args: &Args) -> Result<Recorded> {
    let text = fs::read_to_string(path).with_context(|| {
        format!(
            "could not read {}; write one with `panicgraph baseline {}`",
            path.display(),
            path.display()
        )
    })?;
    let doc: Value = serde_json::from_str(&text)
        .with_context(|| format!("{} is not valid json", path.display()))?;
    let version = doc.get("version").and_then(Value::as_u64).unwrap_or(0);
    if version != u64::from(BASELINE_VERSION) {
        bail!(
            "{} was written by a different version of this tool; write a \
             fresh one with `panicgraph baseline {}`",
            path.display(),
            path.display()
        );
    }
    settings_agree(&doc, args).with_context(|| {
        format!(
            "{} does not describe this run; write a fresh one with \
             `panicgraph baseline {}`",
            path.display(),
            path.display()
        )
    })?;

    // A baseline decides what counts as new, so a field it left out is not
    // an empty answer: reading one that way would report every finding as
    // new, or call a function fixed because its record was unreadable.
    let entries =
        doc.get("findings")
            .and_then(Value::as_array)
            .with_context(|| {
                format!("{} records no list of findings", path.display())
            })?;
    let mut out = Map::default();
    for (at, entry) in entries.iter().enumerate() {
        let name = entry.get("function").and_then(Value::as_str).with_context(
            || format!("finding {at} in {} names no function", path.display()),
        )?;
        // Older baselines name no crate.
        let krate = entry
            .get("crate")
            .map_or(Some(""), Value::as_str)
            .with_context(|| {
                format!(
                    "{name} in {} names a crate that is not a name",
                    path.display()
                )
            })?
            .to_owned();
        let list = entry
            .get("categories")
            .and_then(Value::as_array)
            .with_context(|| {
                format!("{name} in {} lists no categories", path.display())
            })?;
        let mut categories = Vec::with_capacity(list.len());
        for value in list {
            let category = value.as_str().with_context(|| {
                format!(
                    "{name} in {} records a category that is not a name",
                    path.display()
                )
            })?;
            categories.push(category.to_owned());
        }
        ensure!(
            out.insert((krate, name.to_owned()), categories).is_none(),
            "{name} is recorded twice in {}",
            path.display()
        );
    }
    Ok(out)
}

/// Rejects a baseline recorded under settings this run does not share.
///
/// The library decides which categories are visible at all, the profile
/// decides which checks exist, and the suppression policy decides which are
/// reported. A baseline written under any other answer describes a
/// different question, so comparing against it means nothing.
/// Names a MIR optimization level for a message.
fn describe_level(level: Option<u8>) -> String {
    level.map_or_else(
        || "the profile's own mir opt level".to_owned(),
        |level| format!("mir opt level {level}"),
    )
}

fn settings_agree(doc: &Value, args: &Args) -> Result<()> {
    let field = |name: &str| {
        doc.get(name)
            .and_then(Value::as_str)
            .unwrap_or("unrecorded")
            .to_owned()
    };
    let profile = field("profile");
    ensure!(
        profile == args.profile,
        "it was written for the {profile} profile, not {}",
        args.profile
    );
    let std_mode = field("std_mode");
    ensure!(
        std_mode == args.std_mode.name(),
        "it was written against the {std_mode} standard library, not {}",
        args.std_mode.name()
    );
    let level = doc
        .get("mir_opt_level")
        .and_then(Value::as_u64)
        .and_then(|level| u8::try_from(level).ok());
    ensure!(
        level == args.mir_opt_level,
        "it was written at {}, not {}",
        describe_level(level),
        describe_level(args.mir_opt_level)
    );
    let features = doc
        .get("features")
        .and_then(Value::as_str)
        .unwrap_or("default");
    ensure!(
        features == args.features.describe(),
        "it was written with the {features} features, not {}",
        args.features.describe()
    );
    let recorded = doc.get("suppressed").and_then(Value::as_array).map_or(
        CategorySet::EMPTY,
        |list| {
            list.iter()
                .filter_map(Value::as_str)
                .filter_map(|name| name.parse::<Category>().ok())
                .collect()
        },
    );
    ensure!(
        recorded == args.suppress,
        "it was written while suppressing a different set of categories"
    );
    // These decide which functions the report names and which edges it
    // follows, so a baseline written under any of them describes a
    // different set of functions. Unlike `--only`, nothing about a recorded
    // entry says whether the change could have hidden it, so there is no
    // per entry filter to fall back on.
    let closures = field("closures");
    ensure!(
        closures == args.closures.name(),
        "it was written with closures reporting as {closures}, not {}",
        args.closures.name()
    );
    let generics = doc
        .get("generics")
        .and_then(Value::as_str)
        .unwrap_or("written");
    ensure!(
        generics == args.generics.name(),
        "it was written with generic functions reporting as {generics}, \
         not {}",
        args.generics.name()
    );
    let flag =
        |name: &str| doc.get(name).and_then(Value::as_bool).unwrap_or_default();
    ensure!(
        flag("all_crates") == args.all_crates,
        "it was written over a different set of crates"
    );
    ensure!(
        flag("static_only") == args.static_only,
        "it was written reading a different set of call edges"
    );
    ensure!(
        flag("candidates") == args.candidates,
        "it was written reading a different set of call targets"
    );
    Ok(())
}

/// Renders the outcome.
///
/// # Errors
///
/// Returns an error if JSON serialisation fails.
pub fn render(
    outcome: &Outcome,
    args: &Args,
    check: &Check,
    out: &mut String,
) -> Result<()> {
    match args.format {
        #[cfg(feature = "svg")]
        Format::Svg => human(outcome, check, out),
        Format::Human => human(outcome, check, out),
        Format::Github => github(outcome, out),
        Format::Json => {
            let doc = json!({
                "passed": !outcome.failed(),
                "analysed": outcome.findings.len(),
                "violations": outcome.violations.iter().map(|v| json!({
                    "function": v.finding.function,
                    "reason": v.reason.describe(),
                    "categories": v.finding.categories,
                    "location": v.finding.loc,
                })).collect::<Vec<_>>(),
                "fixed": outcome.fixed,
            });
            out.push_str(&serde_json::to_string_pretty(&doc)?);
            out.push('\n');
        }
    }
    Ok(())
}

/// The suffix that makes a count read as English.
const fn plural(count: usize) -> &'static str {
    if count == 1 { "" } else { "s" }
}

/// Writes the human readable verdict.
fn human(outcome: &Outcome, check: &Check, out: &mut String) {
    if !outcome.violations.is_empty() {
        let _ = writeln!(
            out,
            "{} function{} must not panic and can:\n",
            outcome.violations.len(),
            plural(outcome.violations.len())
        );
        for violation in &outcome.violations {
            let _ = writeln!(out, "{}", violation.finding.function);
            if let Some(loc) = &violation.finding.loc {
                let _ = writeln!(out, "    at {loc}");
            }
            let _ = writeln!(
                out,
                "    {} ({})",
                violation.finding.categories.join(", "),
                violation.reason.describe()
            );
        }
        out.push('\n');
    }

    if let Some((actual, max)) = outcome.over_max {
        let _ = writeln!(
            out,
            "{actual} functions can panic, which is more than the {max} \
             allowed.\n"
        );
    }

    if !outcome.fixed.is_empty() {
        let _ = writeln!(
            out,
            "{} function{} in the baseline no longer panics. Refresh it \
             with `panicgraph baseline`.",
            outcome.fixed.len(),
            plural(outcome.fixed.len())
        );
        for name in outcome.fixed.iter().take(10) {
            let _ = writeln!(out, "    {name}");
        }
        out.push('\n');
    }

    if outcome.failed() {
        let _ = writeln!(
            out,
            "Run `panicgraph why <function>` to see how one of them gets \
             there."
        );
        return;
    }

    // Say which gate passed. A check that reports something other than what
    // was asked for reads as though it checked the wrong thing.
    let total = outcome.findings.len();
    if check.baseline.is_some() {
        let _ = writeln!(
            out,
            "No panic that the baseline does not already record. {total} \
             function{} can panic.",
            plural(total)
        );
    } else if let Some(max) = check.max {
        let _ = writeln!(
            out,
            "{total} function{} can panic, within the {max} allowed.",
            plural(total)
        );
    } else if check.forbid.is_empty() {
        let _ = writeln!(out, "No function can panic under this policy.");
    } else {
        let _ = writeln!(
            out,
            "No function matching {} can panic. {total} can in total.",
            check.forbid.join(" or ")
        );
    }
}

/// Writes workflow commands a continuous integration log will annotate.
fn github(outcome: &Outcome, out: &mut String) {
    for violation in &outcome.violations {
        let where_at = workflow_location(violation.finding.loc.as_deref());
        let _ = writeln!(
            out,
            "::error {where_at}title=Function can panic::{} can panic with \
             {} ({})",
            violation.finding.function,
            violation.finding.categories.join(", "),
            violation.reason.describe()
        );
    }
    if let Some((actual, max)) = outcome.over_max {
        let _ = writeln!(
            out,
            "::error title=Too many panicking functions::{actual} functions \
             can panic, more than the {max} allowed"
        );
    }
    for name in &outcome.fixed {
        let _ = writeln!(
            out,
            "::notice title=Baseline is stale::{name} no longer panics"
        );
    }
}