rapx/
cli.rs

1mod analyze;
2pub use analyze::*;
3use clap::{Args, Subcommand, ValueEnum};
4
5#[derive(Args, Debug, Clone)]
6pub struct RapxArgs {
7    #[command(subcommand)]
8    pub command: Commands,
9    #[arg(long, help = "specify the timeout seconds in running rapx")]
10    pub timeout: Option<u64>,
11    #[arg(long, help = "specify the tested package in the workspace")]
12    pub test_crate: Option<String>,
13}
14
15// NOTE: docstring is automatically used to generate help messages,
16// so please use it to explain the command instead of `help` attribute in `arg` macro.
17#[derive(Debug, Clone, Subcommand)]
18pub enum Commands {
19    /// perform various analyses on the crate, e.g., alias analysis, callgraph generation
20    #[command(arg_required_else_help = true)]
21    Analyze {
22        #[command(subcommand)]
23        kind: AnalysisKind,
24    },
25    /// check potential vulnerabilities in the crate,
26    /// e.g., use-after-free, memory leak
27    Check {
28        /// detect use-after-free/double-free
29        #[arg(
30            short = 'f',
31            num_args=0..=1,
32            default_missing_value = "1",
33            long,
34        )]
35        uaf: Option<usize>,
36
37        /// detect memory leakage
38        #[arg(short = 'm', long)]
39        mleak: bool,
40
41        /// automatically detect code optimization chances
42        #[arg(short = 'o', long, default_missing_value = "default")]
43        opt: Option<OptLevel>,
44
45        /// (under development) infer the safety properties required by unsafe APIs.
46        #[arg(long)]
47        infer: bool,
48
49        /// (under development) verify if the safety requirements of unsafe API are satisfied.
50        #[arg(long)]
51        verify: bool,
52
53        /// (under development) verify if the safety requirements of unsafe API are satisfied.
54        #[arg(long)]
55        verify_std: bool,
56    },
57    /// extract unsafe APIs and output a JSON document
58    #[command(arg_required_else_help = true)]
59    Extract {
60        #[command(subcommand)]
61        kind: ExtractKind,
62    },
63}
64
65#[derive(Debug, Clone, Copy, ValueEnum)]
66pub enum OptLevel {
67    Report,
68    Default,
69    All,
70}
71
72// use command string to automatically generate help messages
73#[derive(Debug, Clone, Copy, Subcommand)]
74pub enum ExtractKind {
75    /// output all `pub unsafe` APIs of the current crate as JSON
76    UnsafeApis,
77    /// output all `pub unsafe` APIs of the Rust standard library as JSON
78    StdUnsafeApis,
79}
80
81impl RapxArgs {
82    pub fn init_env(&self) {
83        let Commands::Check {
84            uaf: Some(level), ..
85        } = self.command
86        else {
87            return;
88        };
89        unsafe {
90            std::env::set_var("MOP", level.to_string());
91        }
92    }
93}