rapx/analysis/core/alias/mop/mop.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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
use super::graph::*;
use super::*;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::Operand::{Constant, Copy, Move};
use rustc_middle::mir::TerminatorKind;
use std::collections::{HashMap, HashSet};
use std::env;
impl<'tcx> MopGraph<'tcx> {
pub fn split_check(
&mut self,
bb_index: usize,
fn_map: &mut FnMap,
recursion_set: &mut HashSet<DefId>,
) {
/* duplicate the status before visiting a path; */
let backup_values = self.values.clone(); // duplicate the status when visiting different paths;
let backup_constant = self.constant.clone();
let backup_alias_set = self.alias_set.clone();
self.check(bb_index, fn_map, recursion_set);
/* restore after visit */
self.alias_set = backup_alias_set;
self.values = backup_values;
self.constant = backup_constant;
}
pub fn split_check_with_cond(
&mut self,
bb_index: usize,
path_discr_id: usize,
path_discr_val: usize,
fn_map: &mut FnMap,
recursion_set: &mut HashSet<DefId>,
) {
/* duplicate the status before visiting a path; */
let backup_values = self.values.clone(); // duplicate the status when visiting different paths;
let backup_constant = self.constant.clone();
let backup_alias_set = self.alias_set.clone();
/* add control-sensitive indicator to the path status */
self.constant.insert(path_discr_id, path_discr_val);
self.check(bb_index, fn_map, recursion_set);
/* restore after visit */
self.alias_set = backup_alias_set;
self.values = backup_values;
self.constant = backup_constant;
}
// the core function of the safedrop.
pub fn check(
&mut self,
bb_index: usize,
fn_map: &mut FnMap,
recursion_set: &mut HashSet<DefId>,
) {
self.visit_times += 1;
if self.visit_times > VISIT_LIMIT {
return;
}
let cur_block = self.blocks[self.scc_indices[bb_index]].clone();
self.alias_bb(self.scc_indices[bb_index]);
self.alias_bbcall(self.scc_indices[bb_index], fn_map, recursion_set);
if self.child_scc.get(&self.scc_indices[bb_index]).is_some() {
let init_index = self.scc_indices[bb_index];
let (init_block, cur_targets, scc_block_set) =
self.child_scc.get(&init_index).unwrap().clone();
for enum_index in cur_targets.all_targets() {
let backup_values = self.values.clone();
let backup_constant = self.constant.clone();
let mut block_node = if bb_index == init_index {
init_block.clone()
} else {
self.blocks[bb_index].clone()
};
if !block_node.switch_stmts.is_empty() {
let TerminatorKind::SwitchInt { discr: _, targets } =
block_node.switch_stmts[0].kind.clone()
else {
unreachable!();
};
if cur_targets == targets {
block_node.next = FxHashSet::default();
block_node.next.insert(enum_index.index());
}
}
let mut work_list = Vec::new();
let mut work_set = FxHashSet::<usize>::default();
work_list.push(bb_index);
work_set.insert(bb_index);
while !work_list.is_empty() {
let current_node = work_list.pop().unwrap();
block_node.scc_sub_blocks.push(current_node);
let real_node = if current_node != init_index {
self.blocks[current_node].clone()
} else {
init_block.clone()
};
if real_node.switch_stmts.is_empty() {
for next in &real_node.next {
block_node.next.insert(*next);
}
} else {
let TerminatorKind::SwitchInt {
discr: _,
ref targets,
} = real_node.switch_stmts[0].kind
else {
unreachable!();
};
if cur_targets == *targets {
block_node.next.insert(enum_index.index());
} else {
for next in &real_node.next {
block_node.next.insert(*next);
}
}
}
if real_node.switch_stmts.is_empty() {
for next in &real_node.next {
if scc_block_set.contains(next) && !work_set.contains(next) {
work_set.insert(*next);
work_list.push(*next);
}
}
} else {
let TerminatorKind::SwitchInt {
discr: _,
ref targets,
} = real_node.switch_stmts[0].kind
else {
unreachable!();
};
if cur_targets == *targets {
let next_index = enum_index.index();
if scc_block_set.contains(&next_index)
&& !work_set.contains(&next_index)
{
work_set.insert(next_index);
work_list.push(next_index);
}
} else {
for next in &real_node.next {
if scc_block_set.contains(next) && !work_set.contains(next) {
work_set.insert(*next);
work_list.push(*next);
}
}
}
}
}
/* remove next nodes which are already in the current SCC */
let mut to_remove = Vec::new();
for i in block_node.next.iter() {
if self.scc_indices[*i] == init_index {
to_remove.push(*i);
}
}
for i in to_remove {
block_node.next.remove(&i);
}
for i in block_node.scc_sub_blocks.clone() {
self.alias_bb(i);
self.alias_bbcall(i, fn_map, recursion_set);
}
/* Reach a leaf node, check bugs */
match block_node.next.len() {
0 => {}
_ => {
/*
* Equivalent to self.check(cur_block.next[0]..);
* We cannot use [0] for FxHashSet.
*/
for next in block_node.next {
self.check(next, fn_map, recursion_set);
}
}
}
self.values = backup_values;
self.constant = backup_constant;
}
return;
}
let mut order = vec![];
order.push(vec![]);
/* Handle cases if the current block is a merged scc block with sub block */
if !cur_block.scc_sub_blocks.is_empty() {
match env::var_os("FAST_ALIAS") {
Some(_) => {
order.push(cur_block.scc_sub_blocks.clone());
}
_ => {
self.calculate_scc_order(
&mut cur_block.scc_sub_blocks.clone(),
&mut vec![],
&mut order,
&mut HashMap::new(),
bb_index,
bb_index,
&mut HashSet::new(),
);
}
}
}
let backup_values = self.values.clone(); // duplicate the status when visiting different paths;
let backup_constant = self.constant.clone();
let backup_alias_set = self.alias_set.clone();
let backup_fn_map = fn_map.clone();
let backup_recursion_set = recursion_set.clone();
for scc_each in order {
self.alias_set = backup_alias_set.clone();
self.values = backup_values.clone();
self.constant = backup_constant.clone();
*fn_map = backup_fn_map.clone();
*recursion_set = backup_recursion_set.clone();
if !scc_each.is_empty() {
for idx in scc_each {
self.alias_bb(idx);
self.alias_bbcall(idx, fn_map, recursion_set);
}
}
let cur_block = cur_block.clone();
/* Reach a leaf node, check bugs */
match cur_block.next.len() {
0 => {
let results_nodes = self.values.clone();
self.merge_results(results_nodes);
return;
}
1 => {
/*
* Equivalent to self.check(cur_block.next[0]..);
* We cannot use [0] for FxHashSet.
*/
for next in cur_block.next {
self.check(next, fn_map, recursion_set);
}
return;
}
_ => { // multiple blocks
}
}
/* Begin: handle the SwitchInt statement. */
let mut single_target = false;
let mut sw_val = 0;
let mut sw_target = 0; // Single target
let mut path_discr_id = 0; // To avoid analyzing paths that cannot be reached with one enum type.
let mut sw_targets = None; // Multiple targets of SwitchInt
if !cur_block.switch_stmts.is_empty() && cur_block.scc_sub_blocks.is_empty() {
if let TerminatorKind::SwitchInt {
ref discr,
ref targets,
} = cur_block.switch_stmts[0].clone().kind
{
match discr {
Copy(p) | Move(p) => {
let place = self.projection(false, *p);
if let Some(father) = self.disc_map.get(&self.values[place].local) {
if let Some(constant) = self.constant.get(father) {
if *constant != usize::MAX {
single_target = true;
sw_val = *constant;
}
}
if self.values[place].local == place {
path_discr_id = *father;
sw_targets = Some(targets.clone());
}
}
}
Constant(c) => {
single_target = true;
let param_env = self.tcx.param_env(self.def_id);
if let Some(val) = c.const_.try_eval_target_usize(self.tcx, param_env) {
sw_val = val as usize;
}
}
}
if single_target {
/* Find the target based on the value;
* Since sw_val is a const, only one target is reachable.
* Filed 0 is the value; field 1 is the real target.
*/
for iter in targets.iter() {
if iter.0 as usize == sw_val {
sw_target = iter.1.as_usize();
break;
}
}
/* No target found, choose the default target.
* The default targets is not included within the iterator.
* We can only obtain the default target based on the last item of all_targets().
*/
if sw_target == 0 {
let all_target = targets.all_targets();
sw_target = all_target[all_target.len() - 1].as_usize();
}
}
}
}
/* End: finish handling SwitchInt */
// fixed path since a constant switchInt value
if single_target {
self.check(sw_target, fn_map, recursion_set);
} else {
// Other cases in switchInt terminators
if let Some(targets) = sw_targets {
for iter in targets.iter() {
if self.visit_times > VISIT_LIMIT {
continue;
}
let next_index = iter.1.as_usize();
let path_discr_val = iter.0 as usize;
self.split_check_with_cond(
next_index,
path_discr_id,
path_discr_val,
fn_map,
recursion_set,
);
}
let all_targets = targets.all_targets();
let next_index = all_targets[all_targets.len() - 1].as_usize();
let path_discr_val = usize::MAX; // to indicate the default path;
self.split_check_with_cond(
next_index,
path_discr_id,
path_discr_val,
fn_map,
recursion_set,
);
} else {
for i in cur_block.next {
if self.visit_times > VISIT_LIMIT {
continue;
}
let next_index = i;
self.split_check(next_index, fn_map, recursion_set);
}
}
}
}
}
pub fn calculate_scc_order(
&mut self,
scc: &Vec<usize>,
path: &mut Vec<usize>,
ans: &mut Vec<Vec<usize>>,
disc_map: &mut HashMap<usize, usize>,
idx: usize,
root: usize,
visit: &mut HashSet<usize>,
) {
if idx == root && !path.is_empty() {
ans.push(path.clone());
return;
}
visit.insert(idx);
let term = &self.terms[idx].clone();
match term {
TerminatorKind::SwitchInt {
ref discr,
ref targets,
} => match discr {
Copy(p) | Move(p) => {
let place = self.projection(false, *p);
if let Some(father) = self.disc_map.get(&self.values[place].local) {
let father = *father;
if let Some(constant) = disc_map.get(&father) {
let constant = *constant;
for t in targets.iter() {
if t.0 as usize == constant {
let target = t.1.as_usize();
if path.contains(&target) {
continue;
}
path.push(target);
self.calculate_scc_order(
scc, path, ans, disc_map, target, root, visit,
);
path.pop();
}
}
} else {
for t in targets.iter() {
let constant = t.0 as usize;
let target = t.1.as_usize();
if path.contains(&target) {
continue;
}
path.push(target);
disc_map.insert(father, constant);
self.calculate_scc_order(
scc, path, ans, disc_map, target, root, visit,
);
disc_map.remove(&father);
path.pop();
}
}
} else {
if let Some(constant) = disc_map.get(&place) {
let constant = *constant;
for t in targets.iter() {
if t.0 as usize == constant {
let target = t.1.as_usize();
if path.contains(&target) {
continue;
}
path.push(target);
self.calculate_scc_order(
scc, path, ans, disc_map, target, root, visit,
);
path.pop();
}
}
} else {
for t in targets.iter() {
let constant = t.0 as usize;
let target = t.1.as_usize();
if path.contains(&target) {
continue;
}
path.push(target);
disc_map.insert(place, constant);
self.calculate_scc_order(
scc, path, ans, disc_map, target, root, visit,
);
disc_map.remove(&place);
path.pop();
}
let constant = targets.iter().len();
let target = targets.otherwise().as_usize();
if !path.contains(&target) {
path.push(target);
disc_map.insert(place, constant);
self.calculate_scc_order(
scc, path, ans, disc_map, target, root, visit,
);
disc_map.remove(&place);
path.pop();
}
}
}
}
_ => {}
},
_ => {
for bidx in self.blocks[idx].next.clone() {
if !scc.contains(&bidx) && bidx != root {
continue;
}
if bidx != root {
path.push(bidx);
self.calculate_scc_order(scc, path, ans, disc_map, bidx, root, visit);
path.pop();
} else {
self.calculate_scc_order(scc, path, ans, disc_map, bidx, root, visit);
}
}
}
}
visit.remove(&idx);
}
}