rapx/analysis/unsafety_isolation/
std_unsafety_isolation.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
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
use crate::analysis::utils::fn_info::*;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::CRATE_DEF_INDEX;
use rustc_middle::ty::Visibility;
use rustc_middle::{ty, ty::TyCtxt};
use std::collections::HashMap;
use std::collections::HashSet;
// use crate::analysis::unsafety_isolation::draw_dot::render_dot_graphs;

use super::{
    generate_dot::{NodeType, UigUnit},
    UnsafetyIsolationCheck,
};

impl<'tcx> UnsafetyIsolationCheck<'tcx> {
    pub fn handle_std_unsafe(&mut self) {
        self.get_all_std_unsafe_def_id_by_treat_std_as_local_crate(self.tcx);
        // self.get_units_data(self.tcx);
        let mut dot_strs = Vec::new();
        for uig in &self.uigs {
            let dot_str = uig.generate_dot_str();
            // uig.compare_labels(self.tcx);
            dot_strs.push(dot_str);
        }
        for uig in &self.single {
            let dot_str = uig.generate_dot_str();
            // uig.compare_labels(self.tcx);
            dot_strs.push(dot_str);
        }
        // println!("single {:?}",self.single.len());
        // render_dot_graphs(dot_strs);
    }

    pub fn get_all_std_unsafe_def_id_by_rustc_extern_crates(
        &mut self,
        tcx: TyCtxt<'tcx>,
    ) -> HashSet<DefId> {
        let mut visited = HashSet::new();
        let mut unsafe_fn = HashSet::new();
        for &crate_num in tcx.crates(()).iter() {
            let crate_name = tcx.crate_name(crate_num).to_string();
            // if crate_name == "std" || crate_name == "core" || crate_name == "alloc" {
            if crate_name == "core" {
                let crate_root = DefId {
                    krate: crate_num,
                    index: CRATE_DEF_INDEX,
                };
                self.process_def_id(tcx, crate_root, &mut visited, &mut unsafe_fn);
                println!(
                    "{:?} has {:?} MIR instances totally, with {:?} unsafe fns.",
                    crate_name,
                    visited.len(),
                    unsafe_fn.len()
                );
            }
        }
        // Self::print_hashset(&unsafe_fn);
        unsafe_fn
    }

    pub fn get_all_std_unsafe_def_id_by_treat_std_as_local_crate(
        &mut self,
        tcx: TyCtxt<'tcx>,
    ) -> HashSet<DefId> {
        let mut unsafe_fn = HashSet::new();
        let def_id_sets = tcx.mir_keys(());
        let mut sp_count_map: HashMap<String, usize> = HashMap::new();

        for local_def_id in def_id_sets {
            let def_id = local_def_id.to_def_id();
            if Self::filter_mir(def_id) {
                continue;
            }
            if tcx.def_kind(def_id) == DefKind::Fn || tcx.def_kind(def_id) == DefKind::AssocFn {
                // if Self::check_safety(tcx, def_id)
                //     && Self::check_visibility(tcx, def_id)
                if check_visibility(tcx, def_id) {
                    if check_safety(tcx, def_id) {
                        let sp_set = get_sp(tcx, def_id);
                        if sp_set.len() != 0 {
                            unsafe_fn.insert(def_id);
                            // println!("unsafe fn : {:?}", UigUnit::get_cleaned_def_path_name(self.tcx, def_id));
                        }
                        for sp in sp_set {
                            *sp_count_map.entry(sp).or_insert(0) += 1;
                        }
                    }
                    self.insert_uig(def_id, get_callees(tcx, def_id), get_cons(tcx, def_id));
                }
            }
        }
        self.analyze_struct();
        // self.analyze_uig();
        // for (sp, count) in &sp_count_map {
        //     println!("SP: {}, Count: {}", sp, count);
        // }
        // println!("uig and single len : {:?} and {:?}", self.uigs.len(), self.single.len());
        // println!("unsafe fn len {}", unsafe_fn.len());
        unsafe_fn
    }

    pub fn analyze_uig(&self) {
        let mut pro1 = Vec::new();
        let mut enc1 = Vec::new();
        for uig in &self.uigs {
            if uig.caller.1 != true && uig.caller.2 == 2 {
                enc1.push(uig.clone());
            } else if uig.caller.1 == true && uig.caller.2 == 2 {
                pro1.push(uig.clone());
            }
        }

        let mut no_unsafe_con = Vec::new();
        for uig in pro1 {
            let mut flag = 0;
            for con in &uig.caller_cons {
                if con.1 == true {
                    flag = 1;
                }
            }
            if flag == 0 {
                no_unsafe_con.push(uig.clone());
            }
        }
    }

    pub fn analyze_struct(&self) {
        let mut cache = HashSet::new();
        for uig in &self.uigs {
            self.get_struct(uig.caller.0, &mut cache);
        }
        for uig in &self.single {
            self.get_struct(uig.caller.0, &mut cache);
        }
    }

    pub fn get_struct(&self, def_id: DefId, cache: &mut HashSet<DefId>) {
        let tcx = self.tcx;
        let mut safe_constructors = Vec::new();
        let mut unsafe_constructors = Vec::new();
        let mut unsafe_methods = Vec::new();
        let mut safe_methods = Vec::new();
        let mut mut_methods = Vec::new();
        let mut struct_name = "".to_string();
        let mut ty_flag = 0;
        if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
            if let Some(impl_id) = assoc_item.impl_container(tcx) {
                // get struct ty
                let ty = tcx.type_of(impl_id).skip_binder();
                if let Some(adt_def) = ty.ty_adt_def() {
                    if adt_def.is_union() {
                        ty_flag = 1;
                    } else if adt_def.is_enum() {
                        ty_flag = 2;
                    }
                    let adt_def_id = adt_def.did();
                    struct_name = get_cleaned_def_path_name(tcx, adt_def_id);
                    if !cache.insert(adt_def_id) {
                        return;
                    }
                    if !check_visibility(self.tcx, adt_def_id) {
                        return;
                    }
                    let impl_vec = get_impls_for_struct(self.tcx, adt_def_id);
                    for impl_id in impl_vec {
                        let associated_items = tcx.associated_items(impl_id);
                        for item in associated_items.in_definition_order() {
                            if let ty::AssocKind::Fn = item.kind {
                                let item_def_id = item.def_id;
                                if !check_visibility(self.tcx, item_def_id) {
                                    continue;
                                }
                                if get_type(self.tcx, item_def_id) == 0
                                    && check_safety(self.tcx, item_def_id) == true
                                {
                                    unsafe_constructors.push(item_def_id);
                                }
                                if get_type(self.tcx, item_def_id) == 0
                                    && check_safety(self.tcx, item_def_id) == false
                                {
                                    safe_constructors.push(item_def_id);
                                }
                                if get_type(self.tcx, item_def_id) == 1
                                    && check_safety(self.tcx, item_def_id) == true
                                {
                                    unsafe_methods.push(item_def_id);
                                }
                                if get_type(self.tcx, item_def_id) == 1
                                    && check_safety(self.tcx, item_def_id) == false
                                {
                                    if get_callees(tcx, item_def_id).len() != 0 {
                                        safe_methods.push(item_def_id);
                                    }
                                }
                                if get_type(self.tcx, item_def_id) == 1
                                    && has_mut_self_param(self.tcx, item_def_id) == true
                                {
                                    mut_methods.push(item_def_id);
                                }
                            }
                        }
                    }
                }
            }
        }
        if struct_name == "".to_string()
            || (unsafe_constructors.len() == 0
                && unsafe_methods.len() == 0
                && safe_methods.len() == 0)
        {
            return;
        }
        if ty_flag == 0 {
            println!("Struct:{:?}", struct_name);
        }
        if ty_flag == 1 {
            println!("Union:{:?}", struct_name);
        }
        if ty_flag == 2 {
            println!("Enum:{:?}", struct_name);
        }
        println!("Safe Cons: {}", safe_constructors.len());
        // for safe_cons in safe_constructors {
        //     println!(" {:?}", get_cleaned_def_path_name(tcx, safe_cons));
        // }
        println!("Unsafe Cons: {}", unsafe_constructors.len());
        // for unsafe_cons in unsafe_constructors {
        //     println!(" {:?}", get_cleaned_def_path_name(tcx, unsafe_cons));
        // }
        println!("Unsafe Methods: {}", unsafe_methods.len());
        // for method in unsafe_methods {
        //     println!(" {:?}", get_cleaned_def_path_name(tcx, method));
        // }
        println!("Safe Methods with unsafe callee: {}", safe_methods.len());
        // for method in safe_methods {
        //     println!(" {:?}", get_cleaned_def_path_name(tcx, method));
        // }
        println!("Mut self Methods: {}", mut_methods.len());
        // for method in mut_methods {
        //     println!(" {:?}", get_cleaned_def_path_name(tcx, method));
        // }
    }

    pub fn get_units_data(&mut self, tcx: TyCtxt<'tcx>) {
        // [uf/um, sf-uf, sf-um, uf-uf, uf-um, um(sf)-uf, um(uf)-uf, um(sf)-um, um(uf)-um, sm(sf)-uf, sm(uf)-uf, sm(sf)-um, sm(uf)-um]
        let mut basic_units_data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        let def_id_sets = tcx.mir_keys(());
        for local_def_id in def_id_sets {
            let def_id = local_def_id.to_def_id();
            if Self::filter_mir(def_id) {
                continue;
            }
            if tcx.def_kind(def_id) == DefKind::Fn || tcx.def_kind(def_id) == DefKind::AssocFn {
                if check_visibility(tcx, def_id) {
                    self.insert_uig(def_id, get_callees(tcx, def_id), get_cons(tcx, def_id));
                }
            }
        }
        for uig in &self.uigs {
            uig.count_basic_units(&mut basic_units_data);
        }
        for single in &self.single {
            single.count_basic_units(&mut basic_units_data);
        }
        println!("basic_units_data : {:?}", basic_units_data);
    }

    pub fn process_def_id(
        &mut self,
        tcx: TyCtxt<'tcx>,
        def_id: DefId,
        visited: &mut HashSet<DefId>,
        unsafe_fn: &mut HashSet<DefId>,
    ) {
        if !visited.insert(def_id) || Self::filter_mir(def_id) {
            return;
        }
        match tcx.def_kind(def_id) {
            DefKind::Fn | DefKind::AssocFn => {
                if check_safety(tcx, def_id) && self.tcx.visibility(def_id) == Visibility::Public {
                    unsafe_fn.insert(def_id);
                    self.insert_uig(def_id, get_callees(tcx, def_id), get_cons(tcx, def_id));
                }
            }
            DefKind::Mod => {
                for child in tcx.module_children(def_id) {
                    if let Some(child_def_id) = child.res.opt_def_id() {
                        self.process_def_id(tcx, child_def_id, visited, unsafe_fn);
                    }
                }
            }
            DefKind::Impl { of_trait: _ } => {
                for item in tcx.associated_item_def_ids(def_id) {
                    self.process_def_id(tcx, *item, visited, unsafe_fn);
                }
            }
            DefKind::Struct => {
                let impls = tcx.inherent_impls(def_id);
                for impl_def_id in impls {
                    self.process_def_id(tcx, *impl_def_id, visited, unsafe_fn);
                }
            }
            DefKind::Ctor(_of, _kind) => {
                if tcx.is_mir_available(def_id) {
                    let _mir = tcx.optimized_mir(def_id);
                }
            }
            _ => {
                // println!("{:?}",tcx.def_kind(def_id));
            }
        }
    }

    pub fn filter_mir(def_id: DefId) -> bool {
        let def_id_fmt = format!("{:?}", def_id);
        def_id_fmt.contains("core_arch")
            || def_id_fmt.contains("::__")
            || def_id_fmt.contains("backtrace_rs")
            || def_id_fmt.contains("stack_overflow")
            || def_id_fmt.contains("thread_local")
            || def_id_fmt.contains("raw_vec")
            || def_id_fmt.contains("sys_common")
            || def_id_fmt.contains("adapters")
            || def_id_fmt.contains("sys::sync")
            || def_id_fmt.contains("personality")
            || def_id_fmt.contains("collections::btree::borrow")
            || def_id_fmt.contains("num::int_sqrt")
            || def_id_fmt.contains("collections::btree::node")
            || def_id_fmt.contains("collections::btree::navigate")
            || def_id_fmt.contains("core_simd")
            || def_id_fmt.contains("unique")
    }

    pub fn insert_uig(
        &mut self,
        caller: DefId,
        callee_set: HashSet<DefId>,
        caller_cons: Vec<NodeType>,
    ) {
        let mut pairs = HashSet::new();
        for callee in &callee_set {
            let callee_cons = get_cons(self.tcx, *callee);
            pairs.insert((generate_node_ty(self.tcx, *callee), callee_cons));
        }
        if !check_safety(self.tcx, caller) && callee_set.len() == 0 {
            return;
        }
        let uig = UigUnit::new_by_pair(generate_node_ty(self.tcx, caller), caller_cons, pairs);
        if callee_set.len() > 0 {
            self.uigs.push(uig);
        } else {
            self.single.push(uig);
        }
    }
}