panicgraph 0.1.11

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
//! Command line parsing.

#[cfg(feature = "serve")]
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs};
use std::path::PathBuf;

#[cfg(feature = "serve")]
use anyhow::Context;
use anyhow::{Result, bail};
use clap::{Args as Group, Parser, Subcommand, ValueEnum};

use crate::{CategorySet, StdMode, parse_selector};

/// Trailing help that explains the shared vocabulary once.
const SELECTOR_HELP: &str = "\
A category LIST is comma separated. It accepts category names plus the group
aliases `oom`, `assumed`, `default`, and `all`. Run `panicgraph kinds` for the
names.

Allocation failure, capacity overflow, and standard library precondition
checks are assumed impossible by default: every growable collection reaches
the first two, and the third only exists in a standard library built with
undefined behaviour checks enabled. Pass `--suppress ''` to see everything.

EXIT CODES
  0  nothing to report
  1  findings, or a failed check
  2  the tool could not complete
";

/// Reports which functions can panic, why, and through what call path.
#[derive(Debug, Parser)]
#[command(name = "panicgraph", version, about, long_about = None)]
#[command(after_help = SELECTOR_HELP)]
pub struct Cli {
    /// What to do. Reports the findings when omitted.
    #[command(subcommand)]
    pub command: Option<Command>,

    #[command(flatten)]
    pub scope: Scope,

    #[command(flatten)]
    pub policy: Policy,

    /// Serve an interactive view instead of printing.
    ///
    /// A bare port binds the loopback interface only. Listening more widely
    /// has to be asked for, because this serves the source of the crate
    /// being analysed.
    #[cfg(feature = "serve")]
    #[arg(short = 'l', long, value_name = "PORT|HOST:PORT", global = true)]
    pub listen: Option<String>,

    /// How to render the result.
    #[arg(long, value_enum, default_value_t = Format::Human, global = true)]
    pub format: Format,
}

/// Which crate, and how it is built.
#[derive(Debug, Clone, Group)]
pub struct Scope {
    /// Directory of the crate to analyse.
    #[arg(long, value_name = "DIR", global = true)]
    pub manifest_dir: Option<PathBuf>,

    /// Cargo package to analyse.
    #[arg(short, long, value_name = "PKG", global = true)]
    pub package: Option<String>,

    /// Cargo profile to analyse.
    ///
    /// The profile decides the answer: overflow checks do not exist in a
    /// build that has them turned off.
    #[arg(long, default_value = "release", global = true)]
    pub profile: String,

    /// How the standard library is supplied.
    ///
    /// Defaults to the shipped library, and to `full` for `check` and
    /// `baseline`, which read the categories rather than the count.
    #[arg(long = "std", value_enum, global = true)]
    pub std_mode: Option<Std>,

    /// The compiler's MIR optimization level to build the analysis with.
    ///
    /// The compiler settles some checks itself before the analysis reads a
    /// body, and a higher level settles more: level 3 turns on its own
    /// dataflow constant propagation. The artifact then differs from the
    /// one a plain build produces, so the report names the level.
    #[arg(long, value_name = "N", value_parser = clap::value_parser!(u8).range(0..=4), global = true)]
    pub mir_opt_level: Option<u8>,

    /// Build the crate's test targets as well, so the instantiations they
    /// make of its generic functions join the analysis.
    ///
    /// Only those instantiations are reported: the tests themselves are
    /// not, and the crate's own code compiled again for its unit tests is
    /// not reported twice. A test build needs the dev-dependencies; one
    /// that fails is said so and left out rather than failing the
    /// analysis.
    #[arg(long, global = true)]
    pub with_tests: bool,
}

/// What the analysis is allowed to assume.
#[derive(Debug, Clone, Group)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "each flag is an independent, orthogonal policy choice"
)]
pub struct Policy {
    /// Categories to assume impossible.
    #[arg(long, value_name = "LIST", default_value = "default", global = true)]
    pub suppress: String,

    /// Report only these categories.
    #[arg(long, value_name = "LIST", global = true)]
    pub only: Option<String>,

    /// Ignore vtable and function pointer edges.
    #[arg(long, global = true)]
    pub static_only: bool,

    /// Follow candidate targets for dyn and function pointer calls.
    ///
    /// Candidates are every concrete implementation of the trait and every
    /// reachable function reified to a pointer of a matching signature. The
    /// dyn-call and fn-pointer categories remain either way: candidates
    /// narrow what the unknown code could be, they do not close the set.
    #[arg(long, global = true)]
    pub candidates: bool,

    /// Check each finding against the compiled artifact and say which
    /// survived the optimizer.
    ///
    /// A finding the artifact confirms still calls a panic entry point; an
    /// absent one was removed by the optimizer; the rest cannot be settled
    /// at the binary level. The verdict annotates the finding, it never
    /// removes it.
    #[arg(long, global = true)]
    pub verify: bool,

    /// How closures report: as their own functions, or folded into the
    /// function each is written in.
    ///
    /// Separate is the precise view: a panic contained by a catch belongs
    /// to the closure, and folding it upward would pin it on a caller that
    /// cannot raise it. Parent is the compact view for triage.
    #[arg(long, value_enum, default_value = "separate", global = true)]
    pub closures: Closures,

    /// Include dependencies, not just the local crate.
    #[arg(long, global = true)]
    pub all_crates: bool,

    /// How a generic function reports: as written, or through the
    /// instantiations the build makes of it.
    ///
    /// As written, a generic body is read with its parameters left open, so
    /// a check on a const parameter or on the size of a type parameter can
    /// fail and a call through a bound is unknown. Instantiated reports
    /// what the build's own uses of the function do, and falls back to the
    /// written body only for a function nothing instantiates. Test crates
    /// count as uses under `--with-tests`.
    #[arg(long, value_enum, default_value = "written", global = true)]
    pub generics: Generics,
}

/// What the user asked the tool to do.
#[derive(Debug, Clone, Subcommand)]
pub enum Command {
    /// Report every function that can panic.
    Analyze,

    /// Explain how one function reaches a panic.
    Why {
        /// Part of the function's path, for example `parse` or `Vec::push`.
        function: String,
    },

    /// Fail when functions that must not panic can.
    ///
    /// With no gate given, no function in the crate may panic, which is the
    /// check an allocation free or embedded crate wants.
    Check(Check),

    /// Write the current findings so later runs can gate on changes.
    Baseline {
        /// Where to write them.
        #[arg(value_name = "FILE")]
        file: PathBuf,
    },

    /// List the panic categories and what they mean.
    Kinds,
}

/// The gates a check applies.
///
/// The default gates nothing, which is what `baseline` wants: it records the
/// findings rather than judging them.
#[derive(Debug, Clone, Default, Group)]
pub struct Check {
    /// Functions matching this pattern must not panic. May be repeated.
    #[arg(long, value_name = "REGEX")]
    pub forbid: Vec<String>,

    /// Functions matching this pattern are exempt. May be repeated.
    ///
    /// Applied after the forbidding patterns, so a broad rule can carry a
    /// short list of known exceptions rather than being weakened.
    #[arg(long, value_name = "REGEX")]
    pub allow: Vec<String>,

    /// Fail when more than this many functions can panic.
    #[arg(long, value_name = "N")]
    pub max: Option<usize>,

    /// Fail only on findings absent from this file.
    #[arg(long, value_name = "FILE")]
    pub baseline: Option<PathBuf>,

    /// Treat a function that reaches code the analysis could not read as a
    /// failure.
    ///
    /// This covers every assumed category: unknown, foreign, dyn-call,
    /// fn-pointer, and generic-bound. None of them means clean; each names
    /// where visibility ended.
    #[arg(long)]
    pub fail_on_unknown: bool,
}

/// How generic functions report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Generics {
    /// The body as written, with its parameters left open.
    Written,
    /// The instantiations the build makes, falling back to the written body
    /// where there are none.
    Instantiated,
}

impl Generics {
    /// The name used on the command line and in reports.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Written => "written",
            Self::Instantiated => "instantiated",
        }
    }
}

/// How closures report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Closures {
    /// Each closure reports as its own function.
    Separate,
    /// A closure reports under the function it is written in.
    Parent,
}

impl Command {
    /// The name used on the command line and in messages.
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Analyze => "analyze",
            Self::Why { .. } => "why",
            Self::Check(_) => "check",
            Self::Baseline { .. } => "baseline",
            Self::Kinds => "kinds",
        }
    }
}

impl Closures {
    /// The name used on the command line and in reports.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Separate => "separate",
            Self::Parent => "parent",
        }
    }
}

/// Output rendering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Format {
    /// Human readable text.
    Human,
    /// Machine readable JSON.
    Json,
    /// Workflow commands a continuous integration log will annotate.
    Github,
    /// A standalone flame graph that can be opened or attached to a report.
    #[cfg(feature = "svg")]
    Svg,
}

/// The standard library a command reads when the user does not name one.
///
/// A gate is read by its category names, not its count, and the shipped
/// library hides them: a reachable `unwrap` inside it reports as `unknown`.
/// Rebuilding costs one sub-minute build that later runs reuse, so `check`
/// and `baseline` pay it. They also have to agree with each other, since a
/// baseline written against one library disagrees with a check run against
/// the other about every function.
const fn default_std(command: &Command) -> StdMode {
    match command {
        Command::Check(_) | Command::Baseline { .. } => StdMode::Full,
        _ => StdMode::Shipped,
    }
}

/// How the standard library is supplied.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Std {
    /// The precompiled library shipped with the toolchain.
    Shipped,
    /// Rebuilt from source with its bodies kept, so nothing is opaque.
    Full,
}

impl From<Std> for StdMode {
    fn from(value: Std) -> Self {
        match value {
            Std::Shipped => Self::Shipped,
            Std::Full => Self::Full,
        }
    }
}

/// The resolved settings the rest of the program reads.
#[derive(Debug, Clone)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "each flag is an independent, orthogonal policy choice"
)]
pub struct Args {
    /// The subcommand.
    pub command: Command,
    /// Categories assumed impossible.
    pub suppress: CategorySet,
    /// When set, only these categories are reported.
    pub only: Option<CategorySet>,
    /// Cargo profile to analyse.
    pub profile: String,
    /// How the standard library is supplied.
    pub std_mode: StdMode,
    /// The compiler's MIR optimization level, when one was asked for.
    pub mir_opt_level: Option<u8>,
    /// Build the test targets too, for the instantiations they make.
    pub with_tests: bool,
    /// How generic functions report.
    pub generics: Generics,
    /// Output rendering.
    pub format: Format,
    /// Ignore vtable and function pointer edges.
    pub static_only: bool,
    /// Follow candidate targets for dyn and function pointer calls.
    pub candidates: bool,
    /// Annotate findings with what the compiled artifact still contains.
    pub verify: bool,
    /// How closures report.
    pub closures: Closures,
    /// Report functions from dependencies as well as the local crate.
    pub all_crates: bool,
    /// Directory holding the crate to analyse.
    pub manifest_dir: Option<PathBuf>,
    /// Cargo package to analyse.
    pub package: Option<String>,
    /// When set, serve an interactive view on this address.
    #[cfg(feature = "serve")]
    pub listen: Option<SocketAddr>,
}

impl Cli {
    /// Resolves the parsed command line into the settings used downstream.
    ///
    /// # Errors
    ///
    /// Returns an error for an unknown category name or a listen address
    /// that does not resolve.
    pub fn resolve(self) -> Result<Args> {
        #[cfg(feature = "serve")]
        let listen = self.listen.as_deref().map(listen_addr).transpose()?;
        let only = self.policy.only.as_deref().map(selector).transpose()?;
        let command = self.command.unwrap_or(Command::Analyze);
        // A drawing is a picture of the whole graph, so it is the report
        // and nothing else. Rendering prose instead would answer a question
        // that was not asked.
        // The view is the graph with the report drawn over it, so there
        // is nothing for it to show of a command that answers one question.
        #[cfg(feature = "serve")]
        if listen.is_some() && !matches!(command, Command::Analyze) {
            bail!(
                "`--listen` serves the graph, so it belongs to the analysis \
                 rather than to `{}`",
                command.name()
            );
        }
        if self.format == Format::Svg && !matches!(command, Command::Analyze) {
            bail!(
                "`--format svg` draws the whole graph, so it belongs \
                 to the analysis rather than to `{}`",
                command.name()
            );
        }
        let std_mode = self
            .scope
            .std_mode
            .map_or_else(|| default_std(&command), StdMode::from);
        Ok(Args {
            command,
            suppress: selector(&self.policy.suppress)?,
            only,
            profile: self.scope.profile,
            std_mode,
            mir_opt_level: self.scope.mir_opt_level,
            with_tests: self.scope.with_tests,
            generics: self.policy.generics,
            format: self.format,
            static_only: self.policy.static_only,
            candidates: self.policy.candidates,
            verify: self.policy.verify,
            closures: self.policy.closures,
            all_crates: self.policy.all_crates,
            manifest_dir: self.scope.manifest_dir,
            package: self.scope.package,
            #[cfg(feature = "serve")]
            listen,
        })
    }
}

/// Parses arguments, without the program name.
///
/// # Errors
///
/// Returns an error when the arguments are rejected or cannot be resolved.
pub fn parse<I, S>(input: I) -> Result<Args>
where
    I: IntoIterator<Item = S>,
    S: Into<std::ffi::OsString> + Clone,
{
    let mut argv: Vec<std::ffi::OsString> = vec!["panicgraph".into()];
    argv.extend(input.into_iter().map(Into::into));
    Cli::try_parse_from(argv)?.resolve()
}

/// Resolves a `PORT` or `HOST:PORT` listen argument.
///
/// # Errors
///
/// Returns an error if the argument is neither a port number nor an address
/// that resolves.
#[cfg(feature = "serve")]
fn listen_addr(text: &str) -> Result<SocketAddr> {
    if let Ok(port) = text.parse::<u16>() {
        return Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port));
    }
    let mut resolved = text
        .to_socket_addrs()
        .with_context(|| format!("could not resolve `{text}`"))?;
    match resolved.next() {
        Some(addr) => Ok(addr),
        None => bail!("`{text}` resolved to no address"),
    }
}

/// Parses a comma separated category selector.
fn selector(text: &str) -> Result<CategorySet> {
    match parse_selector(text) {
        Ok(set) => Ok(set),
        Err(bad) => bail!(
            "unknown panic category `{bad}`; run `panicgraph kinds` for \
             the list"
        ),
    }
}