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