rapx/analysis/opt/checking/encoding_checking/
vec_encoding.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
use std::collections::HashSet;

use once_cell::sync::OnceCell;

use rustc_middle::mir::Local;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;

use super::{report_encoding_bug, value_is_from_const};
use crate::analysis::core::dataflow::graph::{DFSStatus, Direction, Graph, GraphNode, NodeOp};
use crate::analysis::utils::def_path::DefPath;

static DEFPATHS: OnceCell<DefPaths> = OnceCell::new();

struct DefPaths {
    string_from_utf8: DefPath,
    string_from_utf8_lossy: DefPath,
    vec_new: DefPath,
    vec_with_capacity: DefPath,
    vec_push: DefPath,
}

impl DefPaths {
    // only supports push operation (can't support direct assignment)
    pub fn new(tcx: &TyCtxt<'_>) -> Self {
        Self {
            string_from_utf8: DefPath::new("std::string::String::from_utf8", tcx),
            string_from_utf8_lossy: DefPath::new("std::string::String::from_utf8_lossy", tcx),
            vec_new: DefPath::new("std::vec::Vec::new", tcx),
            vec_with_capacity: DefPath::new("std::vec::Vec::with_capacity", tcx),
            vec_push: DefPath::new("std::vec::Vec::push", tcx),
        }
    }
}

use crate::analysis::opt::OptCheck;

pub struct VecEncodingCheck {
    record: Vec<Span>,
}

fn extract_vec_if_is_string_from(graph: &Graph, node: &GraphNode) -> Option<Local> {
    let def_paths = &DEFPATHS.get().unwrap();
    for op in node.ops.iter() {
        if let NodeOp::Call(def_id) = op {
            if *def_id == def_paths.string_from_utf8.last_def_id()
                || *def_id == def_paths.string_from_utf8_lossy.last_def_id()
            {
                let in_edge = &graph.edges[node.in_edges[0]];
                return Some(in_edge.src);
            }
        }
    }
    None
}

fn find_upside_vec_new_node(graph: &Graph, node_idx: Local) -> Option<Local> {
    let mut vec_new_node_idx = None;
    let def_paths = &DEFPATHS.get().unwrap();
    // Warning: may traverse all upside nodes and the new result will overwrite on the previous result
    let mut node_operator = |graph: &Graph, idx: Local| -> DFSStatus {
        let node = &graph.nodes[idx];
        for op in node.ops.iter() {
            if let NodeOp::Call(def_id) = op {
                if *def_id == def_paths.vec_new.last_def_id()
                    || *def_id == def_paths.vec_with_capacity.last_def_id()
                {
                    vec_new_node_idx = Some(idx);
                    return DFSStatus::Stop;
                }
            }
        }
        DFSStatus::Continue
    };
    let mut seen = HashSet::new();
    graph.dfs(
        node_idx,
        Direction::Upside,
        &mut node_operator,
        &mut Graph::always_true_edge_validator,
        false,
        &mut seen,
    );
    vec_new_node_idx
}

// todo: we can find downside index node too

fn find_downside_push_node(graph: &Graph, node_idx: Local) -> Vec<Local> {
    let mut push_node_idxs: Vec<Local> = Vec::new();
    let def_paths = &DEFPATHS.get().unwrap();
    // Warning: traverse all downside nodes
    let mut node_operator = |graph: &Graph, idx: Local| -> DFSStatus {
        let node = &graph.nodes[idx];
        for op in node.ops.iter() {
            if let NodeOp::Call(def_id) = op {
                if *def_id == def_paths.vec_push.last_def_id() {
                    push_node_idxs.push(idx);
                    break;
                }
            }
        }
        DFSStatus::Continue
    };
    let mut seen = HashSet::new();
    graph.dfs(
        node_idx,
        Direction::Downside,
        &mut node_operator,
        &mut Graph::always_true_edge_validator,
        true,
        &mut seen,
    );
    push_node_idxs
}

impl OptCheck for VecEncodingCheck {
    fn new() -> Self {
        Self { record: Vec::new() }
    }

    fn check(&mut self, graph: &Graph, tcx: &TyCtxt) {
        let _ = &DEFPATHS.get_or_init(|| DefPaths::new(tcx));
        for node in graph.nodes.iter() {
            if let Some(vec_node_idx) = extract_vec_if_is_string_from(graph, node) {
                if let Some(vec_new_idx) = find_upside_vec_new_node(graph, vec_node_idx) {
                    let vec_push_indice = find_downside_push_node(graph, vec_new_idx);
                    for vec_push_idx in vec_push_indice {
                        let pushed_value_edge = &graph.edges[graph.nodes[vec_push_idx].in_edges[1]]; // The second parameter
                        let pushed_value_idx = pushed_value_edge.src;
                        if !value_is_from_const(graph, pushed_value_idx) {
                            self.record.clear();
                            return;
                        }
                    }
                    self.record.push(node.span);
                }
            }
        }
    }

    fn report(&self, graph: &Graph) {
        for span in self.record.iter() {
            report_encoding_bug(graph, *span);
        }
    }
}