Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
722 changes: 512 additions & 210 deletions rapx/Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions rapx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ toml = "0.8.23"
itertools = "0.14.0"
safety-parser = "0.4.1"
syn = { version = "2", features = ["extra-traits", "full"] }
fs_extra = "1.3.0"
bit-set = "0.8.0"
rust_intervals = "0.3.0"
clap = { version = "4.5.60" }
clap-cargo = "0.18.3"
shlex = "1.3.0"
color-print = "0.3.7"
fs4 = "0.13.1"
indexmap = "2.13.0"
serde_yaml = "0.9.34"
anyhow = "1.0.102"
insta = { version = "1.46.3", features = ["yaml"] }
[features]
backtraces = ["snafu/backtraces", "snafu/backtraces-impl-backtrace-crate"]

Expand Down
1 change: 0 additions & 1 deletion rapx/src/analysis/core/alias_analysis/mfp/interproc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Interprocedural analysis utilities
extern crate rustc_mir_dataflow;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::{Body, TerminatorKind};
use rustc_mir_dataflow::ResultsCursor;
Expand Down
1 change: 0 additions & 1 deletion rapx/src/analysis/core/alias_analysis/mfp/intraproc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate rustc_mir_dataflow;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_middle::{
Expand Down
1 change: 0 additions & 1 deletion rapx/src/analysis/core/alias_analysis/mfp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod interproc;
pub mod intraproc;
pub mod transfer;

extern crate rustc_mir_dataflow;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_middle::mir::{Operand, TerminatorKind};
Expand Down
117 changes: 117 additions & 0 deletions rapx/src/analysis/core/api_dependency/fuzzable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use rustc_hir::LangItem;
use rustc_middle::ty::{self, Ty, TyCtxt, TyKind};
use rustc_span::sym;

fn is_fuzzable_std_ty<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, depth: usize) -> bool {
match ty.kind() {
ty::Adt(def, args) => {
if tcx.is_lang_item(def.did(), LangItem::String) {
return true;
}
if tcx.is_diagnostic_item(sym::Vec, def.did())
&& is_fuzzable_ty(args.type_at(0), tcx, depth + 1)
{
return true;
}
if tcx.is_diagnostic_item(sym::Arc, def.did())
&& is_fuzzable_ty(args.type_at(0), tcx, depth + 1)
{
return true;
}
false
}
_ => false,
}
}

fn is_non_fuzzable_std_ty<'tcx>(ty: Ty<'tcx>, _tcx: TyCtxt<'tcx>) -> bool {
let name = format!("{}", ty);
match name.as_str() {
"core::alloc::LayoutError" => return true,
_ => {}
}
false
}

const MAX_DEPTH: usize = 64;
pub fn is_fuzzable_ty<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, depth: usize) -> bool {
if depth > MAX_DEPTH {
return false;
}

if is_fuzzable_std_ty(ty, tcx, depth + 1) {
return true;
}

if is_non_fuzzable_std_ty(ty, tcx) {
return false;
}

match ty.kind() {
// Basical data type
TyKind::Bool
| TyKind::Char
| TyKind::Int(_)
| TyKind::Uint(_)
| TyKind::Float(_)
| TyKind::Str => true,

// Infer
TyKind::Infer(
ty::InferTy::IntVar(_)
| ty::InferTy::FreshIntTy(_)
| ty::InferTy::FloatVar(_)
| ty::InferTy::FreshFloatTy(_),
) => true,

// Reference, Array, Slice
TyKind::Ref(_, inner_ty, _) | TyKind::Slice(inner_ty) => {
is_fuzzable_ty(inner_ty.peel_refs(), tcx, depth + 1)
}

TyKind::Array(inner_ty, const_) => {
if const_.try_to_value().is_none() {
return false;
}
is_fuzzable_ty(inner_ty.peel_refs(), tcx, depth + 1)
}

// Tuple
TyKind::Tuple(tys) => tys
.iter()
.all(|inner_ty| is_fuzzable_ty(inner_ty.peel_refs(), tcx, depth + 1)),

// ADT
TyKind::Adt(adt_def, args) => {
if adt_def.is_union() {
return false;
}

if adt_def.is_variant_list_non_exhaustive() {
return false;
}

// if adt contain region, then we consider it non-fuzzable
if args.iter().any(|arg| arg.as_region().is_some()) {
return false;
}

// if any field is not public or not fuzzable, then we consider it non-fuzzable
if !adt_def.all_fields().all(|field| {
field.vis.is_public() && is_fuzzable_ty(field.ty(tcx, args), tcx, depth + 1)
}) {
return false;
}

// empty enum cannot be instantiated
if adt_def.is_enum() && adt_def.variants().is_empty() {
return false;
}

true
}

// 其他类型默认不可 Fuzz
_ => false,
}
}
4 changes: 2 additions & 2 deletions rapx/src/analysis/core/api_dependency/graph/avail.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::super::visitor::FnVisitor;
use super::super::visit::FnVisitor;
use super::ApiDependencyGraph;
use super::Config;
use super::dep_edge::DepEdge;
use super::dep_node::{DepNode, desc_str};
use super::dep_node::DepNode;
use super::transform::TransformKind;
use super::ty_wrapper::TyWrapper;
use super::utils;
Expand Down
10 changes: 6 additions & 4 deletions rapx/src/analysis/core/api_dependency/graph/dep_edge.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use rustc_middle::ty::{self, Mutability, Ty};
use serde::Serialize;
use std::{fmt::Display, sync::OnceLock};

use super::transform::TransformKind;

#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[derive(Clone, Copy, Eq, PartialEq, Debug, Serialize)]
#[serde(tag = "type")]
pub enum DepEdge {
Arg(usize),
Arg { no: usize },
Ret,
Transform(TransformKind),
}

impl Display for DepEdge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DepEdge::Arg(no) => write!(f, "{}", no),
DepEdge::Arg { no } => write!(f, "{}", no),
DepEdge::Ret => write!(f, "r"),
DepEdge::Transform(kind) => write!(f, "Transform({})", kind),
}
Expand All @@ -22,7 +24,7 @@ impl Display for DepEdge {

impl DepEdge {
pub fn arg(no: usize) -> DepEdge {
DepEdge::Arg(no)
DepEdge::Arg { no }
}
pub fn ret() -> DepEdge {
DepEdge::Ret
Expand Down
19 changes: 7 additions & 12 deletions rapx/src/analysis/core/api_dependency/graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,12 @@ use rustc_middle::{

use rustc_hir::def_id::DefId;

#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
enum IntrinsicKind {
Borrow,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum DepNode<'tcx> {
Api(DefId, ty::GenericArgsRef<'tcx>),
Ty(TyWrapper<'tcx>),
}

pub fn desc_str<'tcx>(node: DepNode<'tcx>, tcx: TyCtxt<'tcx>) -> String {
match node {
DepNode::Api(def_id, args) => tcx.def_path_str_with_args(def_id, args),
DepNode::Ty(ty) => ty.desc_str(tcx),
}
}

impl<'tcx> DepNode<'tcx> {
pub fn api(id: impl IntoQueryParam<DefId>, args: ty::GenericArgsRef<'tcx>) -> DepNode<'tcx> {
DepNode::Api(id.into_query_param(), args)
Expand Down Expand Up @@ -62,4 +50,11 @@ impl<'tcx> DepNode<'tcx> {
}
}
}

pub fn desc_str(&self, tcx: TyCtxt<'tcx>) -> String {
match self {
DepNode::Api(def_id, args) => tcx.def_path_str_with_args(*def_id, *args),
DepNode::Ty(ty) => ty.desc_str(tcx),
}
}
}
Loading
Loading