rapx/analysis/unsafety_isolation/
generate_dot.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
use crate::analysis::unsafety_isolation::UnsafetyIsolationCheck;
use crate::analysis::utils::fn_info::*;
use petgraph::dot::{Config, Dot};
use petgraph::graph::{DiGraph, EdgeReference, NodeIndex};
use petgraph::Graph;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use std::collections::HashSet;
use std::fmt::{self, Write};

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum UigNode {
    Safe(DefId, String),
    Unsafe(DefId, String),
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum UigEdge {
    CallerToCallee,
    ConsToMethod,
}

impl fmt::Display for UigNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UigNode::Safe(_, _) => write!(f, "Safe"),
            UigNode::Unsafe(_, _) => write!(f, "Unsafe"),
        }
    }
}

impl fmt::Display for UigEdge {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UigEdge::CallerToCallee => write!(f, "CallerToCallee"),
            UigEdge::ConsToMethod => write!(f, "ConsToMethod"),
        }
    }
}

// def_id, is_unsafe_function(true, false), function type(0-constructor, 1-method, 2-function)
pub type NodeType = (DefId, bool, usize);

#[derive(Debug, Clone)]
pub struct UigUnit {
    pub caller: NodeType,
    pub caller_cons: Vec<NodeType>,
    pub callee_cons_pair: HashSet<(NodeType, Vec<NodeType>)>,
}

impl UigUnit {
    pub fn new(caller: NodeType, caller_cons: Vec<NodeType>) -> Self {
        Self {
            caller,
            caller_cons,
            callee_cons_pair: HashSet::default(),
        }
    }

    pub fn new_by_pair(
        caller: NodeType,
        caller_cons: Vec<NodeType>,
        callee_cons_pair: HashSet<(NodeType, Vec<NodeType>)>,
    ) -> Self {
        Self {
            caller,
            caller_cons,
            callee_cons_pair,
        }
    }

    pub fn count_basic_units(&self, data: &mut [u32]) {
        if self.caller.1 == true && self.callee_cons_pair.len() == 0 {
            data[0] += 1;
        }
        if self.caller.1 == false && self.caller.2 != 1 {
            for (callee, _) in &self.callee_cons_pair {
                if callee.2 == 1 {
                    data[2] += 1;
                } else {
                    data[1] += 1;
                }
            }
        }
        if self.caller.1 == true && self.caller.2 != 1 {
            for (callee, _) in &self.callee_cons_pair {
                if callee.2 == 1 {
                    data[4] += 1;
                } else {
                    data[3] += 1;
                }
            }
        }
        if self.caller.1 == true && self.caller.2 == 1 {
            let mut unsafe_cons = 0;
            let mut safe_cons = 0;
            for cons in &self.caller_cons {
                if cons.1 == true {
                    unsafe_cons += 1;
                } else {
                    safe_cons += 1;
                }
            }
            if unsafe_cons == 0 && safe_cons == 0 {
                safe_cons = 1;
            }
            for (callee, _) in &self.callee_cons_pair {
                if callee.2 == 1 {
                    data[7] += 1 * safe_cons;
                    data[8] += 1 * unsafe_cons;
                } else {
                    data[5] += 1 * safe_cons;
                    data[6] += 1 * unsafe_cons;
                }
            }
        }
        if self.caller.1 == false && self.caller.2 == 1 {
            let mut unsafe_cons = 0;
            let mut safe_cons = 0;
            for cons in &self.caller_cons {
                if cons.1 == true {
                    unsafe_cons += 1;
                } else {
                    safe_cons += 1;
                }
            }
            if unsafe_cons == 0 && safe_cons == 0 {
                safe_cons = 1;
            }
            for (callee, _) in &self.callee_cons_pair {
                if callee.2 == 1 {
                    data[11] += 1 * safe_cons;
                    data[12] += 1 * unsafe_cons;
                } else {
                    data[9] += 1 * safe_cons;
                    data[10] += 1 * unsafe_cons;
                }
            }
        }
    }

    pub fn get_node_ty(node: NodeType) -> UigNode {
        match (node.1, node.2) {
            (true, 0) => UigNode::Unsafe(node.0, "doublecircle".to_string()),
            (true, 1) => UigNode::Unsafe(node.0, "ellipse".to_string()),
            (true, 2) => UigNode::Unsafe(node.0, "box".to_string()),
            (false, 0) => UigNode::Safe(node.0, "doublecircle".to_string()),
            (false, 1) => UigNode::Safe(node.0, "ellipse".to_string()),
            (false, 2) => UigNode::Safe(node.0, "box".to_string()),
            _ => UigNode::Safe(node.0, "ellipse".to_string()),
        }
    }

    pub fn generate_dot_str(&self) -> String {
        let mut graph: Graph<UigNode, UigEdge> = DiGraph::new();
        let get_edge_attr = |_graph: &Graph<UigNode, UigEdge>,
                             edge_ref: EdgeReference<'_, UigEdge>| {
            match edge_ref.weight() {
                UigEdge::CallerToCallee => "color=black, style=solid",
                UigEdge::ConsToMethod => "color=black, style=dotted",
            }
            .to_owned()
        };
        let get_node_attr = |_graph: &Graph<UigNode, UigEdge>, node_ref: (NodeIndex, &UigNode)| {
            match node_ref.1 {
                UigNode::Safe(def_id, shape) => {
                    format!("label=\"{:?}\", color=black, shape={:?}", def_id, shape)
                }
                UigNode::Unsafe(def_id, shape) => {
                    // let sps = self.get_sp(*def_id);
                    // let mut label = format!("{:?}\n ", def_id);
                    // for sp_name in sps {
                    //     label.push_str(&format!(" {}", sp_name));
                    // }
                    let label = format!("{:?}\n ", def_id);
                    let node_attr = format!("label={:?}, shape={:?}, color=red", label, shape);
                    node_attr
                }
            }
        };

        let caller_node = graph.add_node(Self::get_node_ty(self.caller));
        for caller_cons in &self.caller_cons {
            let caller_cons_node = graph.add_node(Self::get_node_ty(*caller_cons));
            graph.add_edge(caller_cons_node, caller_node, UigEdge::ConsToMethod);
        }
        for (callee, cons) in &self.callee_cons_pair {
            let callee_node = graph.add_node(Self::get_node_ty(*callee));
            for callee_cons in cons {
                let callee_cons_node = graph.add_node(Self::get_node_ty(*callee_cons));
                graph.add_edge(callee_cons_node, callee_node, UigEdge::ConsToMethod);
            }
            graph.add_edge(caller_node, callee_node, UigEdge::CallerToCallee);
        }

        let mut dot_str = String::new();
        let dot = Dot::with_attr_getters(
            &graph,
            // &[Config::NodeNoLabel, Config::EdgeNoLabel],
            &[Config::NodeNoLabel],
            &get_edge_attr,
            &get_node_attr,
        );

        write!(dot_str, "{}", dot).unwrap();
        // println!("{}",dot_str);
        dot_str
    }

    pub fn compare_labels(&self, tcx: TyCtxt<'_>) {
        let caller_sp = get_sp(tcx, self.caller.0);
        // for caller_con in &self.caller_cons {
        //     if caller_con.1 != true {
        //         // if constructor is safe, it won't have labels.
        //         continue;
        //     }
        //     let caller_con_sp = Self::get_sp(caller_con.0);
        //     caller_sp.extend(caller_con_sp); // Merge sp of each unsafe constructor
        // }
        let caller_label: Vec<_> = caller_sp.clone().into_iter().collect();

        let mut combined_callee_sp = HashSet::new();
        for (callee, _sp_vec) in &self.callee_cons_pair {
            let callee_sp = get_sp(tcx, callee.0);
            combined_callee_sp.extend(callee_sp); // Merge sp of each callee
        }
        let combined_labels: Vec<_> = combined_callee_sp.clone().into_iter().collect();

        if caller_sp == combined_callee_sp {
            // println!("----------same sp------------");
            // println!(
            //     "Caller: {:?}.\n--Caller's constructors: {:?}.\n--SP labels: {:?}.",
            //     Self::get_cleaned_def_path_name(self.caller.0),
            //     self.caller_cons
            //         .iter()
            //         .map(|node_type| Self::get_cleaned_def_path_name(node_type.0))
            //         .collect::<Vec<_>>(),
            //     caller_label
            // );
            // println!(
            //     "Callee: {:?}.\n--Combined Callee Labels: {:?}",
            //     self.callee_cons_pair
            //         .iter()
            //         .map(|(node_type, _)| Self::get_cleaned_def_path_name(node_type.0))
            //         .collect::<Vec<_>>(),
            //     combined_labels
            // );
        } else {
            println!("----------unmatched sp------------");
            println!(
                "Caller: {:?}.\n--Caller's constructors: {:?}.\n--SP labels: {:?}.",
                get_cleaned_def_path_name(tcx, self.caller.0),
                self.caller_cons
                    .iter()
                    .map(|node_type| get_cleaned_def_path_name(tcx, node_type.0))
                    .collect::<Vec<_>>(),
                caller_label
            );
            println!(
                "Callee: {:?}.\n--Combined Callee Labels: {:?}",
                self.callee_cons_pair
                    .iter()
                    .map(|(node_type, _)| get_cleaned_def_path_name(tcx, node_type.0))
                    .collect::<Vec<_>>(),
                combined_labels
            );
        }
    }

    pub fn print_self(&self, tcx: TyCtxt<'_>) {
        let caller_sp = get_sp(tcx, self.caller.0);
        let caller_label: Vec<_> = caller_sp.clone().into_iter().collect();

        let mut combined_callee_sp = HashSet::new();
        for (callee, _sp_vec) in &self.callee_cons_pair {
            let callee_sp = get_sp(tcx, callee.0);
            combined_callee_sp.extend(callee_sp); // Merge sp of each callee
        }
        let combined_labels: Vec<_> = combined_callee_sp.clone().into_iter().collect();
        println!(
            "Caller: {:?}.\n--Caller's constructors: {:?}.\n--SP labels: {:?}.",
            get_cleaned_def_path_name(tcx, self.caller.0),
            self.caller_cons
                .iter()
                .filter(|cons| cons.1 == true)
                .map(|node_type| get_cleaned_def_path_name(tcx, node_type.0))
                .collect::<Vec<_>>(),
            caller_label
        );
        println!(
            "Callee: {:?}.\n--Combined Callee Labels: {:?}",
            self.callee_cons_pair
                .iter()
                .map(|(node_type, _)| get_cleaned_def_path_name(tcx, node_type.0))
                .collect::<Vec<_>>(),
            combined_labels
        );
    }
}

#[derive(PartialEq)]
pub enum UigOp {
    DrawPic,
    TypeCount,
}

impl<'tcx> UnsafetyIsolationCheck<'tcx> {
    pub fn get_node_name_by_def_id(&self, def_id: DefId) -> String {
        if let Some(node) = self.nodes.iter().find(|n| n.node_id == def_id) {
            return node.node_name.clone();
        }
        String::new()
    }

    pub fn get_node_type_by_def_id(&self, def_id: DefId) -> usize {
        if let Some(node) = self.nodes.iter().find(|n| n.node_id == def_id) {
            return node.node_type;
        }
        2
    }

    pub fn get_node_unsafety_by_def_id(&self, def_id: DefId) -> bool {
        if let Some(node) = self.nodes.iter().find(|n| n.node_id == def_id) {
            return node.node_unsafety;
        }
        false
    }

    pub fn get_adjacent_nodes_by_def_id(&self, def_id: DefId) -> Vec<DefId> {
        let mut nodes = Vec::new();
        if let Some(node) = self.nodes.iter().find(|n| n.node_id == def_id) {
            nodes.extend(node.callees.clone());
            nodes.extend(node.methods.clone());
            nodes.extend(node.callers.clone());
            nodes.extend(node.constructors.clone());
        }
        nodes
    }

    pub fn get_constructor_nodes_by_def_id(&self, def_id: DefId) -> Vec<DefId> {
        let mut nodes = Vec::new();
        if let Some(node) = self.nodes.iter().find(|n| n.node_id == def_id) {
            nodes.extend(node.constructors.clone());
        }
        nodes
    }
}