rapx/analysis/opt/checking/bounds_checking/
bounds_loop_push.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
use once_cell::sync::OnceCell;

use rustc_hir::{intravisit, Expr, ExprKind};
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::TypeckResults;
use rustc_span::Span;

use crate::analysis::core::dataflow::graph::Graph;
use crate::analysis::utils::def_path::DefPath;
use crate::utils::log::{
    relative_pos_range, span_to_filename, span_to_first_line, span_to_line_number,
    span_to_source_code, span_to_trimmed_span,
};
use annotate_snippets::{Level, Renderer, Snippet};

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

struct DefPaths {
    vec_push: DefPath,
}

impl DefPaths {
    pub fn new(tcx: &TyCtxt<'_>) -> Self {
        let no_std = NO_STD.lock().unwrap();
        if *no_std {
            Self {
                vec_push: DefPath::new("alloc::vec::Vec::push", tcx),
            }
        } else {
            Self {
                vec_push: DefPath::new("std::vec::Vec::push", tcx),
            }
        }
    }
}

struct LoopFinder<'tcx> {
    typeck_results: &'tcx TypeckResults<'tcx>,
    record: Vec<(Span, Vec<Span>)>,
}

struct PushFinder<'tcx> {
    typeck_results: &'tcx TypeckResults<'tcx>,
    record: Vec<Span>,
}

impl<'tcx> intravisit::Visitor<'tcx> for PushFinder<'tcx> {
    fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
        if let ExprKind::MethodCall(.., span) = ex.kind {
            let def_id = self
                .typeck_results
                .type_dependent_def_id(ex.hir_id)
                .unwrap();
            let target_def_id = (&DEFPATHS.get().unwrap()).vec_push.last_def_id();
            if def_id == target_def_id {
                self.record.push(span);
            }
        }
        intravisit::walk_expr(self, ex);
    }
}

impl<'tcx> intravisit::Visitor<'tcx> for LoopFinder<'tcx> {
    fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
        if let ExprKind::Loop(block, ..) = ex.kind {
            let mut push_finder = PushFinder {
                typeck_results: self.typeck_results,
                record: Vec::new(),
            };
            intravisit::walk_block(&mut push_finder, block);
            // if !push_finder.record.is_empty() {
            //     self.record.push((ex.span, push_finder.record));
            // }
            if push_finder.record.len() == 1 {
                // we only use simple cases
                self.record.push((ex.span, push_finder.record));
            }
        }
        intravisit::walk_expr(self, ex);
    }
}

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

pub struct BoundsLoopPushCheck {
    record: Vec<(Span, Vec<Span>)>,
}

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

    fn check(&mut self, graph: &Graph, tcx: &TyCtxt) {
        let _ = &DEFPATHS.get_or_init(|| DefPaths::new(tcx));
        let def_id = graph.def_id;
        let body = tcx.hir().body_owned_by(def_id.as_local().unwrap());
        let typeck_results = tcx.typeck(def_id.as_local().unwrap());
        let mut loop_finder = LoopFinder {
            typeck_results,
            record: Vec::new(),
        };
        intravisit::walk_body(&mut loop_finder, body);
        self.record = loop_finder.record;
    }

    fn report(&self, _: &Graph) {
        for (loop_span, push_record) in self.record.iter() {
            report_loop_push_bug(*loop_span, push_record);
        }
    }
}

fn report_loop_push_bug(loop_span: Span, push_record: &Vec<Span>) {
    let code_source = span_to_source_code(loop_span);
    let filename = span_to_filename(loop_span);
    let mut snippet = Snippet::source(&code_source)
        .line_start(span_to_line_number(loop_span))
        .origin(&filename)
        .fold(true)
        .annotation(
            Level::Info
                .span(relative_pos_range(
                    loop_span,
                    span_to_trimmed_span(span_to_first_line(loop_span)),
                ))
                .label("A loop operation."),
        );
    for push_span in push_record {
        snippet = snippet.annotation(
            Level::Error
                .span(relative_pos_range(loop_span, *push_span))
                .label("Push happens here."),
        );
    }
    let message = Level::Warning
        .title("Unnecessary bounds checkings detected")
        .snippet(snippet);
    let renderer = Renderer::styled();
    println!("{}", renderer.render(message));
}