Debian Codegraph @

gcc/rust/checks/errors/borrowck/ffi-polonius/src/lib.rs

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 mod gccrs_ffi;
20
21 use gccrs_ffi::FFIVector;
22 use polonius_engine::{AllFacts, Atom, FactTypes, Output};
23 use std::fmt::Debug;
24 use std::hash::Hash;
25
26 extern "C" {
27 fn FFIVector__new() -> *mut FFIVector;
28 fn FFIVector__new_vec_pair() -> *mut FFIVector;
29 fn FFIVector__new_vec_triple() -> *mut FFIVector;
30 fn FFIVector__push(vector: *mut FFIVector, element: usize);
31 fn FFIVector__push_vec_pair(
32 vector: *mut FFIVector,
33 element: gccrs_ffi::Pair<usize, *mut FFIVector>,
34 );
35 fn FFIVector__push_vec_triple(
36 vector: *mut FFIVector,
37 element: gccrs_ffi::Triple<usize, usize, usize>,
38 );
39 }
40
41 /// A single fact value.
42 /// For simplicity we use one type for all facts.
43 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
44 struct GccrsAtom(usize);
45
46 impl Atom for GccrsAtom {
47 fn index(self) -> usize {
48 self.0
49 }
50 }
51
52 impl From<usize> for GccrsAtom {
53 fn from(inner: usize) -> GccrsAtom {
54 GccrsAtom(inner)
55 }
56 }
57
58 impl From<GccrsAtom> for usize {
59 fn from(atom: GccrsAtom) -> Self {
60 atom.index()
61 }
62 }
63
64 impl From<&GccrsAtom> for usize {
65 fn from(atom: &GccrsAtom) -> Self {
66 atom.index()
67 }
68 }
69
70 #[derive(Debug, Clone, Copy, Default)]
71 struct GccrsFacts;
72
73 impl FactTypes for GccrsFacts {
74 type Origin = GccrsAtom;
75 type Loan = GccrsAtom;
76 type Point = GccrsAtom;
77 type Variable = GccrsAtom;
78 type Path = GccrsAtom;
79 }
80
81 impl From<gccrs_ffi::FactsView> for AllFacts<GccrsFacts> {
82 fn from(input: gccrs_ffi::FactsView) -> Self {
83 AllFacts::<GccrsFacts> {
84 loan_issued_at: input.loan_issued_at.into(),
85 universal_region: input.universal_region.into(),
86 cfg_edge: input.cfg_edge.into(),
87 loan_killed_at: input.loan_killed_at.into(),
88 subset_base: input.subset_base.into(),
89 loan_invalidated_at: input.loan_invalidated_at.into(),
90 var_used_at: input.var_used_at.into(),
91 var_defined_at: input.var_defined_at.into(),
92 var_dropped_at: input.var_dropped_at.into(),
93 use_of_var_derefs_origin: input.use_of_var_derefs_origin.into(),
94 drop_of_var_derefs_origin: input.drop_of_var_derefs_origin.into(),
95 child_path: input.child_path.into(),
96 path_is_var: input.path_is_var.into(),
97 path_assigned_at_base: input.path_assigned_at_base.into(),
98 path_moved_at_base: input.path_moved_at_base.into(),
99 path_accessed_at_base: input.path_accessed_at_base.into(),
100 known_placeholder_subset: input.known_placeholder_subset.into(),
101 placeholder: input.placeholder.into(),
102 }
103 }
104 }
105
106 fn print_point(point: GccrsAtom) {
107 let val: usize = point.into();
108 // Point is a 32 bit unsigned integer
109 // 16 15 1
110 // xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx x
111 // ^~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~ ^
112 // | | |
113 // basic_block | start/mid
114 // statement
115 // the left most 16 bits store the basic block number
116 // the right most bit, represents the start/mid status
117 // the remaining 15 bits between these two represent the statement
118 let mid = val % 2 == 1;
119 let bb = val >> 16;
120 // firstly we can get rid of right most bit by performing left shift once
121 let hide_left_most_bit = val >> 1;
122 // now we only need the 15 bits on the right
123 // we can mask the remaining bits by performing bitwise AND with fifteen
124 // 1's which in hexadecimal is 0x7FFF
125 let stmt = hide_left_most_bit & 0x7FFF;
126 eprint!("{}(bb{}[{}])", if mid { "Mid" } else { "Start" }, bb, stmt);
127 }
128
129 /// Run the polonius analysis on the given facts (for a single function).
130 /// Right now, results are only printed and not propagated back to the gccrs.
131 #[no_mangle]
132 pub unsafe extern "C" fn polonius_run(
133 input: gccrs_ffi::FactsView,
134 dump_enabled: bool,
135 ) -> gccrs_ffi::Output {
136 let facts = AllFacts::<GccrsFacts>::from(input);
137 let output = Output::compute(
138 &facts,
139 polonius_engine::Algorithm::DatafrogOpt,
140 dump_enabled,
141 );
142
143 if dump_enabled {
144 eprintln!("Subsets:");
145 let mut subset_vec: Vec<_> = output.subset.iter().collect();
146 subset_vec.sort_by_key(|&(point, _)| point);
147 for (point, subsets) in subset_vec {
148 print_point(*point);
149 eprintln!(": {{");
150 for (&lhs, rhss) in subsets {
151 for &rhs in rhss {
152 eprintln!(" {} <= {}", usize::from(lhs), usize::from(rhs));
153 }
154 }
155 eprintln!("}}");
156 }
157
158 // Print origin live on entry
159 eprintln!("Origin live on entry:");
160 let mut origin_vec: Vec<_> = output.origin_live_on_entry.iter().collect();
161 origin_vec.sort_by_key(|&(point, _)| point);
162 for (point, origins) in origin_vec {
163 print_point(*point);
164 eprintln!(": {{");
165 for &origin in origins {
166 eprintln!(" {}", usize::from(origin));
167 }
168 eprintln!("}}");
169 }
170
171 eprintln!("Origin contains loan at:");
172 let mut origin_vec: Vec<_> = output.origin_contains_loan_at.iter().collect();
173 origin_vec.sort_by_key(|&(point, _)| point);
174 for (point, origins) in origin_vec {
175 print_point(*point);
176 eprintln!(": {{");
177 for (&origin, loans) in origins {
178 eprintln!(
179 " {}:{:?}",
180 usize::from(origin),
181 loans.iter().map(|&e| usize::from(e)).collect::<Vec<_>>()
182 );
183 }
184 eprintln!("}}");
185 }
186
187 eprintln!("Polonius analysis completed. Results:");
188 if output.errors.len() > 0 {
189 eprintln!("Errors:");
190 for (&point, errors) in &output.errors {
191 print_point(point);
192 eprintln!(": {:?}", errors);
193 }
194 }
195 if output.subset_errors.len() > 0 {
196 eprintln!("Subset errors:");
197 for (&point, errors) in &output.subset_errors {
198 print_point(point);
199 eprintln!(": {:?}", errors);
200 }
201 }
202 if output.move_errors.len() > 0 {
203 eprintln!("Move errors:");
204 for (&point, moves) in &output.move_errors {
205 print_point(point);
206 eprintln!("{:?}", moves);
207 }
208 }
209 }
210
211 let loan_errors = FFIVector__new_vec_pair();
212 for (keys, values) in output.errors.iter() {
213 let loans_vec = FFIVector__new();
214 for loan in values.iter() {
215 let loan: usize = loan.into();
216 FFIVector__push(loans_vec, loan);
217 }
218 let point: usize = keys.into();
219 let pair = gccrs_ffi::Pair {
220 first: point,
221 second: loans_vec,
222 };
223 FFIVector__push_vec_pair(loan_errors, pair);
224 }
225
226 let move_errors = FFIVector__new_vec_pair();
227 for (keys, values) in output.move_errors.iter() {
228 let paths_vec = FFIVector__new();
229 for path in values.iter() {
230 let path: usize = path.into();
231 FFIVector__push(paths_vec, path);
232 }
233 let point: usize = keys.into();
234 let pair = gccrs_ffi::Pair {
235 first: point,
236 second: paths_vec,
237 };
238 FFIVector__push_vec_pair(move_errors, pair);
239 }
240
241 let subset_errors = FFIVector__new_vec_triple();
242 for (key, value) in output.subset_errors.iter() {
243 let point: usize = key.into();
244 for origin_pair in value.iter() {
245 let origin_1: usize = origin_pair.0.into();
246 let origin_2: usize = origin_pair.1.into();
247 let triple = gccrs_ffi::Triple {
248 first: point,
249 second: origin_1,
250 third: origin_2,
251 };
252 FFIVector__push_vec_triple(subset_errors, triple);
253 }
254 }
255
256 return gccrs_ffi::Output {
257 loan_errors,
258 move_errors,
259 subset_errors,
260 };
261 }