Skip to main content

rapx/check/opt/checking/encoding_checking/
vec_encoding.rs

1use crate::analysis::dataflow::*;
2use rustc_middle::{mir::Local, ty::TyCtxt};
3use rustc_span::Span;
4
5use super::{report_encoding_bug, value_is_from_const};
6
7crate::def_paths! {
8    string_from_utf8: "std::string::String::from_utf8",
9    string_from_utf8_lossy: "std::string::String::from_utf8_lossy",
10    vec_new: "std::vec::Vec::new",
11    vec_with_capacity: "std::vec::Vec::with_capacity",
12    vec_push: "std::vec::Vec::push",
13}
14
15
16use crate::check::opt::OptCheck;
17
18pub struct VecEncodingCheck {
19    record: Vec<Span>,
20}
21
22fn extract_vec_if_is_string_from(graph: &Graph, node: &GraphNode) -> Option<Local> {
23    let def_paths = &DEFPATHS.get().unwrap();
24    for op in node.ops.iter() {
25        if let NodeOp::Call(def_id) = op {
26            if *def_id == def_paths.string_from_utf8.last_def_id()
27                || *def_id == def_paths.string_from_utf8_lossy.last_def_id()
28            {
29                let in_edge = &graph.edges[node.in_edges[0]];
30                return Some(in_edge.src);
31            }
32        }
33    }
34    None
35}
36
37fn find_upside_vec_new_node(graph: &Graph, node_idx: Local) -> Option<Local> {
38    let def_paths = &DEFPATHS.get().unwrap();
39    graph.find_first_node(
40        node_idx,
41        Direction::Upside,
42        &mut |graph: &Graph, idx: Local| {
43            let node = &graph.nodes[idx];
44            for op in node.ops.iter() {
45                if let NodeOp::Call(def_id) = op {
46                    if *def_id == def_paths.vec_new.last_def_id()
47                        || *def_id == def_paths.vec_with_capacity.last_def_id()
48                    {
49                        return true;
50                    }
51                }
52            }
53            false
54        },
55        &mut Graph::always_true_edge_validator,
56    )
57}
58
59fn find_downside_push_node(graph: &Graph, node_idx: Local) -> Vec<Local> {
60    let def_paths = &DEFPATHS.get().unwrap();
61    graph.find_all_nodes(
62        node_idx,
63        Direction::Downside,
64        &mut |graph: &Graph, idx: Local| {
65            let node = &graph.nodes[idx];
66            for op in node.ops.iter() {
67                if let NodeOp::Call(def_id) = op {
68                    if *def_id == def_paths.vec_push.last_def_id() {
69                        return true;
70                    }
71                }
72            }
73            false
74        },
75        &mut Graph::always_true_edge_validator,
76    )
77}
78
79impl OptCheck for VecEncodingCheck {
80    fn new() -> Self {
81        Self { record: Vec::new() }
82    }
83
84    fn check(&mut self, graph: &Graph, tcx: &TyCtxt) {
85        let _ = &DEFPATHS.get_or_init(|| DefPaths::new(tcx));
86        for node in graph.nodes.iter() {
87            if let Some(vec_node_idx) = extract_vec_if_is_string_from(graph, node) {
88                if let Some(vec_new_idx) = find_upside_vec_new_node(graph, vec_node_idx) {
89                    let vec_push_indice = find_downside_push_node(graph, vec_new_idx);
90                    for vec_push_idx in vec_push_indice {
91                        let pushed_value_edge = &graph.edges[graph.nodes[vec_push_idx].in_edges[1]]; // The second parameter
92                        let pushed_value_idx = pushed_value_edge.src;
93                        if !value_is_from_const(graph, pushed_value_idx) {
94                            self.record.clear();
95                            return;
96                        }
97                    }
98                    self.record.push(node.span);
99                }
100            }
101        }
102    }
103
104    fn report(&self, graph: &Graph) {
105        for span in self.record.iter() {
106            report_encoding_bug(graph, *span);
107        }
108    }
109
110    fn cnt(&self) -> usize {
111        self.record.len()
112    }
113}