rapx/analysis/opt/checking/encoding_checking/
vec_encoding.rsuse 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 {
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();
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
}
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();
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]]; 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);
}
}
}