Skip to content

Commit

Permalink
refactor alias
Browse files Browse the repository at this point in the history
  • Loading branch information
hxuhack committed Oct 11, 2024
1 parent eaff317 commit d4341b2
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 93 deletions.
82 changes: 82 additions & 0 deletions rap/src/analysis/core/alias.rs
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
pub mod mop;

use std::fmt;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;

//struct to cache the results for analyzed functions.
pub type FnMap = FxHashMap<DefId, FnRetAlias>;

/*
* To store the alias relationships among arguments and return values.
* Each function may have multiple return instructions, leading to different RetAlias.
*/
#[derive(Debug, Clone)]
pub struct FnRetAlias {
pub arg_size: usize,
pub alias_vec: Vec<RetAlias>,
}

impl FnRetAlias {
pub fn new(arg_size: usize) -> FnRetAlias{
Self {
arg_size: arg_size,
alias_vec: Vec::<RetAlias>::new(),
}
}
}

impl fmt::Display for FnRetAlias {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"[{}]",
self.alias_vec.iter()
.map(|alias| format!("{}", alias))
.collect::<Vec<String>>()
.join("")
)
}
}

/*
* To store the alias relationships among arguments and return values.
*/
#[derive(Debug,Clone)]
pub struct RetAlias{
pub left_index: usize,
pub left_field_seq: Vec<usize>,
pub left_may_drop: bool,
pub left_need_drop: bool,
pub right_index: usize,
pub right_field_seq: Vec<usize>,
pub right_may_drop: bool,
pub right_need_drop: bool,
}

impl RetAlias{
pub fn new(left_index: usize, left_may_drop: bool, left_need_drop: bool,
right_index: usize, right_may_drop: bool, right_need_drop: bool) -> RetAlias{
RetAlias {
left_index: left_index,
left_field_seq: Vec::<usize>::new(),
left_may_drop: left_may_drop,
left_need_drop: left_need_drop,
right_index: right_index,
right_field_seq: Vec::<usize>::new(),
right_may_drop: right_may_drop,
right_need_drop: right_need_drop,
}
}

pub fn valuable(&self) -> bool{
return self.left_may_drop && self.right_may_drop;
}
}

impl fmt::Display for RetAlias {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"({},{})",
self.left_index, self.right_index
)
}
}
7 changes: 2 additions & 5 deletions rap/src/analysis/core/alias/mop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxHashSet;
use crate::{rap_info, rap_debug};
use crate::rap_info;
use crate::utils::source::*;
use graph::MopGraph;
use alias::FnRetAlias;
use crate::analysis::core::alias::FnMap;

pub const VISIT_LIMIT:usize = 100;

//struct to cache the results for analyzed functions.
pub type FnMap = FxHashMap<DefId, FnRetAlias>;

pub struct MopAlias<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub fn_map: FnMap,
Expand Down
78 changes: 2 additions & 76 deletions rap/src/analysis/core/alias/mop/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use rustc_middle::ty;
use rustc_middle::mir::{TerminatorKind, Operand, Place, ProjectionElem};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefId;
use std::fmt;
use crate::{rap_info, rap_error};
use crate::rap_error;
use super::graph::*;
use super::types::*;
use super::mop::*;
use super::super::mop::FnMap;
use crate::analysis::core::alias::{FnMap, RetAlias};

impl<'tcx> MopGraph<'tcx> {
/* alias analysis for a single block */
Expand Down Expand Up @@ -266,76 +265,3 @@ impl<'tcx> MopGraph<'tcx> {
return field_id_seq;
}
}
/*
* To store the alias relationships among arguments and return values.
*/
#[derive(Debug,Clone)]
pub struct RetAlias{
pub left_index: usize,
pub left_field_seq: Vec<usize>,
pub left_may_drop: bool,
pub left_need_drop: bool,
pub right_index: usize,
pub right_field_seq: Vec<usize>,
pub right_may_drop: bool,
pub right_need_drop: bool,
}

impl RetAlias{
pub fn new(left_index: usize, left_may_drop: bool, left_need_drop: bool,
right_index: usize, right_may_drop: bool, right_need_drop: bool) -> RetAlias{
RetAlias {
left_index: left_index,
left_field_seq: Vec::<usize>::new(),
left_may_drop: left_may_drop,
left_need_drop: left_need_drop,
right_index: right_index,
right_field_seq: Vec::<usize>::new(),
right_may_drop: right_may_drop,
right_need_drop: right_need_drop,
}
}

pub fn valuable(&self) -> bool{
return self.left_may_drop && self.right_may_drop;
}
}

impl fmt::Display for RetAlias {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"({},{})",
self.left_index, self.right_index
)
}
}
/*
* To store the alias relationships among arguments and return values.
* Each function may have multiple return instructions, leading to different RetAlias.
*/
#[derive(Debug, Clone)]
pub struct FnRetAlias {
pub arg_size: usize,
pub alias_vec: Vec<RetAlias>,
}

impl FnRetAlias {
pub fn new(arg_size: usize) -> FnRetAlias{
Self {
arg_size: arg_size,
alias_vec: Vec::<RetAlias>::new(),
}
}
}

impl fmt::Display for FnRetAlias {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f,
"[{}]",
self.alias_vec.iter()
.map(|alias| format!("{}", alias))
.collect::<Vec<String>>()
.join("")
)
}
}
2 changes: 1 addition & 1 deletion rap/src/analysis/core/alias/mop/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_middle::mir::Operand;
use rustc_middle::mir::Rvalue;
use rustc_span::Span;
use super::alias::*;
use super::types::*;
use crate::analysis::core::alias::FnRetAlias;

#[derive(PartialEq,Debug,Copy,Clone)]
pub enum AssignType {
Expand Down
3 changes: 2 additions & 1 deletion rap/src/analysis/safedrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use rustc_hir::def_id::DefId;

use graph::SafeDropGraph;
use safedrop::*;
use crate::analysis::core::alias::mop::{MopAlias, FnMap};
use crate::analysis::core::alias::mop::MopAlias;
use crate::analysis::core::alias::FnMap;

pub struct SafeDrop<'tcx> {
pub tcx: TyCtxt<'tcx>,
Expand Down
3 changes: 1 addition & 2 deletions rap/src/analysis/safedrop/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use rustc_middle::mir::{TerminatorKind, Operand, Place, ProjectionElem};
use crate::rap_error;
use super::types::*;
use super::graph::*;
use crate::analysis::safedrop::FnMap;
use crate::analysis::core::alias::mop::alias::RetAlias;
use crate::analysis::core::alias::{FnMap, RetAlias};

impl<'tcx> SafeDropGraph<'tcx>{
/* alias analysis for a single block */
Expand Down
2 changes: 1 addition & 1 deletion rap/src/analysis/safedrop/safedrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_middle::mir::{TerminatorKind, Operand};
use rustc_middle::mir::Operand::{Copy, Move, Constant};

use crate::{rap_error};
use crate::analysis::core::alias::mop::{FnMap};
use crate::analysis::core::alias::FnMap;
use crate::analysis::safedrop::SafeDropGraph;

pub const DROP:usize = 1634;
Expand Down
7 changes: 0 additions & 7 deletions test cases/senryx_tests/slice_from_raw_parts/Cargo.lock

This file was deleted.

0 comments on commit d4341b2

Please sign in to comment.