Skip to content

Commit

Permalink
Attempt to port to 1.73.0-nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
disconnect3d committed Aug 16, 2023
1 parent 1c150f4 commit 9e5c83b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 45 deletions.
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2021-04-23"
channel = "nightly-2023-08-15"
components = [
"rust-src",
"rustc-dev",
Expand Down
16 changes: 7 additions & 9 deletions src/analysis/errors.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use rustc_macros::SessionDiagnostic;
use rustc_macros::Diagnostic;
use rustc_span::Span;

#[derive(SessionDiagnostic)]
#[error = "T0001"]
#[derive(Diagnostic)]
#[diag(taint_func_received_tainted_input, code="T0001")]
pub(crate) struct TaintedSink {
pub fn_name: String,
#[message = "function `{fn_name}` received tainted input"]
#[label = "sink function"]
#[label(taint_sink_function)]
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[error = "T0002"]
#[derive(Diagnostic)]
#[diag(taint_attribute_is_invalid, code="T0002")]
pub(crate) struct InvalidVariant {
pub attr_name: String,
#[message = "Taint attribute `{attr_name}` is invalid. We currently only support `source`, `sink`, and `sanitizer`"]
#[label = "invalid taint attribute"]
#[label(taint_invalid_taint_attribute)]
pub span: Span,
}
29 changes: 12 additions & 17 deletions src/analysis/taint_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_middle::{
ty::{TyCtxt, TyKind},
};

use rustc_mir::dataflow::{Analysis, AnalysisDomain, Forward};
use rustc_mir_dataflow::{Analysis, AnalysisDomain, Forward, CallReturnPlaces};
use rustc_span::Span;

use tracing::instrument;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<'inter> AnalysisDomain<'inter> for TaintAnalysis<'_, '_> {

impl<'tcx, 'inter, 'intra> Analysis<'intra> for TaintAnalysis<'tcx, 'inter> {
fn apply_statement_effect(
&self,
&mut self,
state: &mut Self::Domain,
statement: &Statement<'intra>,
location: Location,
Expand All @@ -126,7 +126,7 @@ impl<'tcx, 'inter, 'intra> Analysis<'intra> for TaintAnalysis<'tcx, 'inter> {
}

fn apply_terminator_effect(
&self,
&mut self,
state: &mut Self::Domain,
terminator: &Terminator<'intra>,
location: Location,
Expand All @@ -144,12 +144,10 @@ impl<'tcx, 'inter, 'intra> Analysis<'intra> for TaintAnalysis<'tcx, 'inter> {
}

fn apply_call_return_effect(
&self,
&mut self,
_state: &mut Self::Domain,
_block: BasicBlock,
_func: &Operand<'intra>,
_args: &[Operand<'intra>],
_return_place: Place<'intra>,
_return_place: CallReturnPlaces<'_, 'intra>,
) {
// do nothing
}
Expand Down Expand Up @@ -244,6 +242,7 @@ where
Rvalue::NullaryOp(_, _) => {}
Rvalue::Discriminant(_) => {}
Rvalue::Aggregate(_, _) => {}
Rvalue::ShallowInitBox(_, _) | Rvalue::CopyForDeref(_) => todo!()
}
}

Expand All @@ -252,7 +251,7 @@ where
&mut self,
func: &Constant,
args: &[Operand],
destination: &Option<(Place, BasicBlock)>,
destination: &Place,
span: &Span,
) {
let name = func.to_string();
Expand All @@ -274,7 +273,7 @@ where
&mut self,
args: &[Operand],
id: &rustc_hir::def_id::DefId,
destination: &Option<(Place, BasicBlock)>,
destination: &Place,
) {
let init = args
.iter()
Expand Down Expand Up @@ -355,16 +354,12 @@ where
contexts.get(key).map(|res| res.clone())
}

fn t_visit_source_destination(&mut self, destination: &Option<(Place, BasicBlock)>) {
if let Some((place, _)) = destination {
self.state.set_taint(place.local, true);
}
fn t_visit_source_destination(&mut self, destination: &Place) {
self.state.set_taint(destination.local, true);
}

fn t_visit_sanitizer_destination(&mut self, destination: &Option<(Place, BasicBlock)>) {
if let Some((place, _)) = destination {
self.state.set_taint(place.local, false);
}
fn t_visit_sanitizer_destination(&mut self, destination: &Place) {
self.state.set_taint(destination.local, false);
}

fn t_visit_sink(&mut self, name: String, args: &[Operand], span: &Span) {
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/taint_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::collections::HashSet;

use rustc_index::{bit_set::BitSet, vec::Idx};
use rustc_index::{bit_set::BitSet, Idx};
use rustc_middle::mir::{Local, Place};
use tracing::instrument;

Expand Down
17 changes: 10 additions & 7 deletions src/bins/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use hir::def_id::LOCAL_CRATE;
use rustc_driver::Compilation;
use rustc_hir as hir;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::ErrorOutputType;
use rustc_session::{config::ErrorOutputType, EarlyErrorHandler};
use taint::eval;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};

fn main() {
rustc_driver::install_ice_hook();
rustc_driver::init_rustc_env_logger();
// TODO/FIXME: Get those working!
//rustc_driver::install_ice_hook();
//rustc_driver::init_rustc_env_logger();
init_tracing();

let mut rustc_args: Vec<String> = vec![];
Expand Down Expand Up @@ -89,6 +90,7 @@ impl rustc_driver::Callbacks for TaintCompilerCallbacks {
/// All the work we do happens after analysis, so that we can make assumptions about the validity of the MIR.
fn after_analysis<'tcx>(
&mut self,
_handler: &EarlyErrorHandler,
compiler: &rustc_interface::interface::Compiler,
queries: &'tcx rustc_interface::Queries<'tcx>,
) -> Compilation {
Expand All @@ -104,19 +106,20 @@ fn enter_with_fn<'tcx, TyCtxtFn>(queries: &'tcx rustc_interface::Queries<'tcx>,
where
TyCtxtFn: Fn(TyCtxt),
{
queries.global_ctxt().unwrap().peek_mut().enter(enter_fn);
queries.global_ctxt().unwrap().enter(enter_fn);
}

/// Perform the taint analysis.
fn mir_analysis(tcx: TyCtxt) {
let (entry_def_id, _) = if let Some((entry_def, x)) = tcx.entry_fn(LOCAL_CRATE) {
let (entry_def_id, _) = if let Some((entry_def, x)) = tcx.entry_fn(()) {
(entry_def, x)
} else {
let msg =
"this tool currently only supports taint analysis on programs with a main function";
rustc_session::early_error(ErrorOutputType::default(), msg);
//rustc_session::early_error(ErrorOutputType::default(), msg);
panic!(msg);
};

let main_id = entry_def_id.to_def_id();
let main_id = entry_def_id; //.to_def_id();
main::eval_main(tcx, main_id);
}
16 changes: 10 additions & 6 deletions src/eval/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hir::itemlikevisit::ItemLikeVisitor;
use hir::intravisit::Visitor;
use rustc_ast::AttrKind;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -52,13 +52,17 @@ impl<'tcx> TaintAttributeFinder<'tcx> {

impl TaintAttributeFinder<'_> {
fn visit_hir_id(&mut self, item_id: hir::HirId) {
let def_id = self.tcx.hir().local_def_id(item_id).to_def_id();
//let def_id = self.tcx.hir().local_def_id(item_id).to_def_id();
//let def_id = self.tcx.hir().body_owner_def_id(rustc_hir::BodyId { hir_id: item_id }).into(); // panics
let def_id = self.tcx.hir().get_parent_item(item_id).into();

let sym_source = Symbol::intern("source");
let sym_sink = Symbol::intern("sink");
let sym_sanitizer = Symbol::intern("sanitizer");
let attrs = self.tcx.hir().attrs(item_id);
for attr in attrs {
if let AttrKind::Normal(ref item, _) = attr.kind {
if let AttrKind::Normal(ref kind) = attr.kind {
let item = &kind.item;
if let Some(symbol) = get_taint_attr(item) {
if symbol == &sym_source {
self.info.sources.push(def_id)
Expand All @@ -70,16 +74,16 @@ impl TaintAttributeFinder<'_> {
self.tcx.sess.emit_err(InvalidVariant {
attr_name: symbol.to_ident_string(),
span: item.span(),
})
}
});
};
break;
}
}
}
}
}

impl<'v> ItemLikeVisitor<'v> for TaintAttributeFinder<'_> {
impl<'v> Visitor<'v> for TaintAttributeFinder<'_> {
fn visit_item(&mut self, item: &'v rustc_hir::Item<'_>) {
self.visit_hir_id(item.hir_id());
}
Expand Down
4 changes: 2 additions & 2 deletions src/eval/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_mir::dataflow::Analysis;
use rustc_mir_dataflow::Analysis;

use crate::eval::attributes::TaintAttributeFinder;
use crate::taint_analysis::TaintAnalysis;

pub fn eval_main(tcx: TyCtxt<'_>, main_id: DefId) {
// Find all functions in the current crate that have been tagged
let mut finder = TaintAttributeFinder::new(tcx);
tcx.hir().krate().visit_all_item_likes(&mut finder);
tcx.hir().visit_all_item_likes_in_crate(&mut finder);

let entry = tcx.optimized_mir(main_id);

Expand Down
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(rustc_private)]
#![feature(box_syntax)]
#![feature(box_patterns)]

extern crate rustc_driver;
extern crate rustc_apfloat;
extern crate rustc_ast;
extern crate rustc_data_structures;
Expand All @@ -10,8 +10,10 @@ extern crate rustc_hir;
extern crate rustc_index;
extern crate rustc_interface;
extern crate rustc_macros;
//extern crate rustc_builtin_macros;
extern crate rustc_fluent_macro;
extern crate rustc_middle;
extern crate rustc_mir;
extern crate rustc_mir_dataflow;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_target;
Expand All @@ -21,3 +23,10 @@ mod analysis;
pub mod eval;

pub use analysis::*;
// The fluent_messages! macro generates translations for diagnostic messages
// see https://rustc-dev-guide.rust-lang.org/diagnostics/translation.html
// The two imports below are required by this macro
use rustc_fluent_macro::fluent_messages;
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};

fluent_messages! { "./messages.ftl" }
7 changes: 7 additions & 0 deletions src/messages.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
taint_attribute_is_invalid = Taint attribute `{$attr_name}` is invalid. We currently only support 'source', 'sink', and 'sanitizer'
taint_func_received_tainted_input = function `{$fn_name}` received tainted input
taint_invalid_taint_attribute = invalid taint attribute
taint_sink_function = sink function

0 comments on commit 9e5c83b

Please sign in to comment.