rapx/analysis/safedrop/
corner_handle.rs

1use super::graph::*;
2use crate::analysis::utils::intrinsic_id::*;
3use rustc_span::def_id::DefId;
4
5impl<'tcx> SafeDropGraph<'tcx> {
6    //these function calls are the functions whose MIRs can not be obtained.
7    pub fn corner_handle(
8        &mut self,
9        _left_ssa: usize,
10        _merge_vec: &Vec<usize>,
11        def_id: DefId,
12    ) -> bool {
13        // CASE 1: function::call_mut
14        // #![feature(fn_traits)]
15        // fn main() {
16        //     let x = 1i32;
17        //     let mut c = || {x+1;};
18        //     c.call_mut(());
19        // }
20        if def_id.index.as_usize() == CALL_MUT || def_id.index.as_usize() == CLONE {
21            return true;
22        }
23
24        // CASE 3: intrinsic_offset
25        // For the latest rust version, this def_id of this function is removed. And we give the example below:
26        // #![feature(core_intrinsics)]
27        // use std::intrinsics::offset;
28        // fn main() {
29        //     unsafe {
30        //         let x = Box::new(1);
31        //         let mut ptr = &x as *const _;
32        //         ptr = offset(ptr, 1 as isize);
33        //     }
34        //
35        // }
36        //     bb1: {
37        //         _4 = &_1;
38        //         _3 = &raw const (*_4);
39        //         _2 = _3;
40        //         _6 = _2;
41        //         _7 = const 1_isize;
42        //         _5 = Offset(move _6, move _7);
43        //         _2 = move _5;
44        //         drop(_1) -> [return: bb2, unwind continue];
45        //     }
46        // if def_id.index.as_usize() == 1709 {
47        //     return true;
48        // }
49
50        return false;
51    }
52
53    //the dangling pointer occuring in some functions like drop() is reasonable.
54    pub fn should_check(def_id: DefId) -> bool {
55        let mut def_str = format!("{:?}", def_id);
56        if let Some(x) = def_str.rfind("::") {
57            def_str = def_str.get((x + "::".len())..).unwrap().to_string();
58        }
59        if let Some(_) = def_str.find("drop") {
60            return false;
61        }
62        if let Some(_) = def_str.find("dealloc") {
63            return false;
64        }
65        if let Some(_) = def_str.find("release") {
66            return false;
67        }
68        if let Some(_) = def_str.find("destroy") {
69            return false;
70        }
71        return true;
72    }
73}
74
75//these adt structs use the Rc-kind drop instruction, which we do not focus on.
76pub fn is_corner_adt(str: String) -> bool {
77    if let Some(_) = str.find("cell::RefMut") {
78        return true;
79    }
80    if let Some(_) = str.find("cell::Ref") {
81        return true;
82    }
83    if let Some(_) = str.find("rc::Rc") {
84        return true;
85    }
86    if let Some(_) = str.find("arc::Arc") {
87        return true;
88    }
89    if let Some(_) = str.find("rc::Weak") {
90        return true;
91    }
92    if let Some(_) = str.find("sync::Weak") {
93        return true;
94    }
95    return false;
96}