rapx/analysis/opt/checking/encoding_checking/
string_push.rs

1use std::collections::HashSet;
2
3use once_cell::sync::OnceCell;
4
5use rustc_middle::mir::Local;
6use rustc_middle::ty::TyCtxt;
7use rustc_span::Span;
8
9use super::value_is_from_const;
10use crate::analysis::core::dataflow::graph::{DFSStatus, Direction, Graph, GraphNode, NodeOp};
11use crate::analysis::utils::def_path::DefPath;
12use crate::utils::log::{
13    relative_pos_range, span_to_filename, span_to_line_number, span_to_source_code,
14};
15use annotate_snippets::{Level, Renderer, Snippet};
16
17static DEFPATHS: OnceCell<DefPaths> = OnceCell::new();
18
19struct DefPaths {
20    string_new: DefPath,
21    string_push: DefPath,
22}
23
24impl DefPaths {
25    pub fn new(tcx: &TyCtxt<'_>) -> Self {
26        Self {
27            string_new: DefPath::new("std::string::String::new", tcx),
28            string_push: DefPath::new("std::string::String::push", tcx),
29        }
30    }
31}
32
33use crate::analysis::opt::OptCheck;
34
35pub struct StringPushCheck {
36    record: Vec<Span>,
37}
38
39fn extract_value_if_is_string_push(graph: &Graph, node: &GraphNode) -> Option<Local> {
40    let def_paths = DEFPATHS.get().unwrap();
41    for op in node.ops.iter() {
42        if let NodeOp::Call(def_id) = op {
43            if *def_id == def_paths.string_push.last_def_id() {
44                let push_value_idx = graph.edges[node.in_edges[1]].src; //the secod parameter
45                return Some(push_value_idx);
46            }
47        }
48    }
49    None
50}
51
52fn find_upside_string_new(graph: &Graph, node_idx: Local) -> Option<Local> {
53    let mut string_new_node_idx = None;
54    let def_paths = DEFPATHS.get().unwrap();
55    let mut node_operator = |graph: &Graph, idx: Local| -> DFSStatus {
56        let node = &graph.nodes[idx];
57        for op in node.ops.iter() {
58            if let NodeOp::Call(def_id) = op {
59                if *def_id == def_paths.string_new.last_def_id() {
60                    string_new_node_idx = Some(idx);
61                    return DFSStatus::Stop;
62                }
63            }
64        }
65        DFSStatus::Continue
66    };
67    let mut seen = HashSet::new();
68    graph.dfs(
69        node_idx,
70        Direction::Upside,
71        &mut node_operator,
72        &mut Graph::always_true_edge_validator,
73        false,
74        &mut seen,
75    );
76    string_new_node_idx
77}
78
79impl OptCheck for StringPushCheck {
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_idx, node) in graph.nodes.iter_enumerated() {
87            if let Some(pushed_value_idx) = extract_value_if_is_string_push(graph, node) {
88                if find_upside_string_new(graph, node_idx).is_some() {
89                    if !value_is_from_const(graph, pushed_value_idx) {
90                        self.record.clear(); // Warning: Not rigorous, push of other string may cause clear
91                        return;
92                    }
93                    self.record.push(node.span);
94                }
95            }
96        }
97    }
98
99    fn report(&self, graph: &Graph) {
100        if !self.record.is_empty() {
101            report_string_push_bug(graph, &self.record);
102        }
103    }
104
105    fn cnt(&self) -> usize {
106        self.record.len()
107    }
108}
109
110fn report_string_push_bug(graph: &Graph, spans: &Vec<Span>) {
111    let code_source = span_to_source_code(graph.span);
112    let filename = span_to_filename(graph.span);
113    let mut snippet = Snippet::source(&code_source)
114        .line_start(span_to_line_number(graph.span))
115        .origin(&filename)
116        .fold(true);
117    for span in spans.iter() {
118        snippet = snippet.annotation(
119            Level::Error
120                .span(relative_pos_range(graph.span, *span))
121                .label("Checked here."),
122        )
123    }
124    let message = Level::Warning
125        .title("Unnecessary encoding checkings detected")
126        .snippet(snippet)
127        .footer(Level::Help.title("Use unsafe APIs instead."));
128    let renderer = Renderer::styled();
129    println!("{}", renderer.render(message));
130}