rapx/
lib.rs

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
#![feature(rustc_private)]
#![feature(box_patterns)]

#[macro_use]
pub mod utils;
pub mod analysis;

extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_index;
extern crate rustc_interface;
extern crate rustc_metadata;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
extern crate stable_mir;
use crate::analysis::core::heap_item::TypeAnalysis;
use analysis::core::alias::mop::MopAlias;
use analysis::core::api_dep::ApiDep;
use analysis::core::call_graph::CallGraph;
use analysis::core::dataflow::DataFlow;
use analysis::core::range_analysis::SSATrans;
use analysis::opt::Opt;
use analysis::rcanary::rCanary;
use analysis::safedrop::SafeDrop;
use analysis::senryx::{CheckLevel, SenryxCheck};
use analysis::unsafety_isolation::{UigInstruction, UnsafetyIsolationCheck};
use analysis::utils::show_mir::ShowMir;
use rustc_data_structures::sync::Lrc;
use rustc_driver::{Callbacks, Compilation};
use rustc_interface::interface::Compiler;
use rustc_interface::{Config, Queries};
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
use rustc_session::search_paths::PathKind;
use std::path::PathBuf;

// Insert rustc arguments at the beginning of the argument list that RAP wants to be
// set per default, for maximal validation power.
pub static RAP_DEFAULT_ARGS: &[&str] = &["-Zalways-encode-mir", "-Zmir-opt-level=0"];

pub type Elapsed = (i64, i64);

#[derive(Debug, Copy, Clone, Hash)]
pub struct RapCallback {
    rcanary: bool,
    safedrop: bool,
    verify: bool,
    infer: bool,
    unsafety_isolation: usize,
    mop: bool,
    callgraph: bool,
    api_dep: bool,
    show_mir: bool,
    dataflow: usize,
    opt: bool,
    heap_item: bool,
    ssa: bool,
}

#[allow(clippy::derivable_impls)]
impl Default for RapCallback {
    fn default() -> Self {
        Self {
            rcanary: false,
            safedrop: false,
            verify: false,
            infer: false,
            unsafety_isolation: 0,
            mop: false,
            callgraph: false,
            api_dep: false,
            show_mir: false,
            dataflow: 0,
            opt: false,
            heap_item: false,
            ssa: false,
        }
    }
}

impl Callbacks for RapCallback {
    fn config(&mut self, config: &mut Config) {
        config.override_queries = Some(|_, providers| {
            providers.extern_queries.used_crate_source = |tcx, cnum| {
                let mut providers = Providers::default();
                rustc_metadata::provide(&mut providers);
                let mut crate_source = (providers.extern_queries.used_crate_source)(tcx, cnum);
                // HACK: rustc will emit "crate ... required to be available in rlib format, but
                // was not found in this form" errors once we use `tcx.dependency_formats()` if
                // there's no rlib provided, so setting a dummy path here to workaround those errors.
                Lrc::make_mut(&mut crate_source).rlib = Some((PathBuf::new(), PathKind::All));
                crate_source
            };
        });
    }

    fn after_analysis<'tcx>(
        &mut self,
        _compiler: &Compiler,
        queries: &'tcx Queries<'tcx>,
    ) -> Compilation {
        rap_trace!("Execute after_analysis() of compiler callbacks");
        queries
            .global_ctxt()
            .unwrap()
            .enter(|tcx| start_analyzer(tcx, *self));
        rap_trace!("analysis done");
        Compilation::Continue
    }
}

impl RapCallback {
    pub fn enable_rcanary(&mut self) {
        self.rcanary = true;
    }

    pub fn is_rcanary_enabled(&self) -> bool {
        self.rcanary
    }

    pub fn enable_mop(&mut self) {
        self.mop = true;
    }

    pub fn is_mop_enabled(&self) -> bool {
        self.mop
    }

    pub fn enable_safedrop(&mut self) {
        self.safedrop = true;
    }

    pub fn is_safedrop_enabled(&self) -> bool {
        self.safedrop
    }

    pub fn enable_unsafety_isolation(&mut self, x: usize) {
        self.unsafety_isolation = x;
    }

    pub fn is_unsafety_isolation_enabled(&self) -> usize {
        self.unsafety_isolation
    }

    pub fn enable_api_dep(&mut self) {
        self.api_dep = true;
    }

    pub fn is_api_dep_enabled(self) -> bool {
        self.api_dep
    }

    pub fn enable_verify(&mut self) {
        self.verify = true;
    }

    pub fn is_verify_enabled(&self) -> bool {
        self.verify
    }

    pub fn enable_infer(&mut self) {
        self.infer = true;
    }

    pub fn is_infer_enabled(&self) -> bool {
        self.infer
    }

    pub fn enable_callgraph(&mut self) {
        self.callgraph = true;
    }

    pub fn is_callgraph_enabled(&self) -> bool {
        self.callgraph
    }

    pub fn enable_show_mir(&mut self) {
        self.show_mir = true;
    }

    pub fn is_show_mir_enabled(&self) -> bool {
        self.show_mir
    }

    pub fn enable_dataflow(&mut self, x: usize) {
        self.dataflow = x;
    }

    pub fn is_dataflow_enabled(self) -> usize {
        self.dataflow
    }

    pub fn enable_opt(&mut self) {
        self.opt = true;
    }

    pub fn is_opt_enabled(self) -> bool {
        self.opt
    }

    pub fn enable_heap_item(&mut self) {
        self.heap_item = true;
    }

    pub fn is_heap_item_enabled(self) -> bool {
        self.heap_item
    }
    pub fn enable_ssa_transform(&mut self) {
        self.ssa = true;
    }
    pub fn is_ssa_transform_enabled(self) -> bool {
        self.ssa
    }
}

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum RapPhase {
    Cleanup,
    Cargo,
    Rustc,
    LLVM, // unimplemented yet
}

pub fn start_analyzer(tcx: TyCtxt, callback: RapCallback) {
    let _rcanary: Option<rCanary> = if callback.is_rcanary_enabled() {
        let mut rcx = rCanary::new(tcx);
        rcx.start();
        Some(rcx)
    } else {
        None
    };

    if callback.is_mop_enabled() {
        MopAlias::new(tcx).start();
    }

    if callback.is_safedrop_enabled() {
        SafeDrop::new(tcx).start();
    }

    if callback.is_heap_item_enabled() {
        let rcx_boxed = Box::new(rCanary::new(tcx));
        let rcx = Box::leak(rcx_boxed);
        let mut type_analysis = TypeAnalysis::new(rcx);
        type_analysis.start();
        type_analysis.output();
    }

    let x = callback.is_unsafety_isolation_enabled();
    match x {
        1 => UnsafetyIsolationCheck::new(tcx).start(UigInstruction::StdSp),
        2 => UnsafetyIsolationCheck::new(tcx).start(UigInstruction::Doc),
        3 => UnsafetyIsolationCheck::new(tcx).start(UigInstruction::Upg),
        4 => UnsafetyIsolationCheck::new(tcx).start(UigInstruction::Ucons),
        _ => {}
    }

    if callback.is_verify_enabled() {
        let check_level = CheckLevel::High;
        SenryxCheck::new(tcx, 2).start(check_level, true);
    }

    if callback.is_infer_enabled() {
        let check_level = CheckLevel::High;
        SenryxCheck::new(tcx, 2).start(check_level, false);
    }

    if callback.is_show_mir_enabled() {
        ShowMir::new(tcx).start();
    }

    if callback.is_api_dep_enabled() {
        ApiDep::new(tcx).start();
    }

    match callback.is_dataflow_enabled() {
        1 => DataFlow::new(tcx, false).start(),
        2 => DataFlow::new(tcx, true).start(),
        _ => {}
    }

    if callback.is_callgraph_enabled() {
        CallGraph::new(tcx).start();
    }

    if callback.is_opt_enabled() {
        Opt::new(tcx).start();
    }
    if callback.is_ssa_transform_enabled() {
        SSATrans::new(tcx, false).start();
    }
}