Skip to content

Commit

Permalink
fix: get diagnostics working again, and make sure the attributes are …
Browse files Browse the repository at this point in the history
…found
  • Loading branch information
LiHRaM committed Aug 16, 2023
1 parent 9e5c83b commit eb225ca
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 217 deletions.
222 changes: 97 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ test = false

[dependencies]
tracing = "0.1"
tracing-subscriber = { version = "0.2.18", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
# for more information.
rustc-workspace-hack = "1.0.0"

[dev-dependencies]
compiletest_rs = { version = "0.7", features = ["tmp"] }
rustc_version = "0.3"
compiletest_rs = { version = "0.10.2", features = ["tmp"] }
rustc_version = "0.4.0"
colored = "2"

[package.metadata.rust-analyzer]
Expand Down
18 changes: 0 additions & 18 deletions src/analysis/errors.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/analysis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod taint_analysis;

pub(crate) mod errors;
mod taint_domain;
13 changes: 6 additions & 7 deletions src/analysis/taint_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
rc::Rc,
};

use rustc_errors::struct_span_err;
use rustc_hir::def_id::DefId;
use rustc_index::bit_set::BitSet;
use rustc_middle::{
Expand Down Expand Up @@ -153,7 +154,7 @@ impl<'tcx, 'inter, 'intra> Analysis<'intra> for TaintAnalysis<'tcx, 'inter> {
}
}

impl<'tcx> std::fmt::Debug for TransferFunction<'_, '_, '_> {
impl std::fmt::Debug for TransferFunction<'_, '_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{:?}", &self.state))
}
Expand Down Expand Up @@ -242,7 +243,7 @@ where
Rvalue::NullaryOp(_, _) => {}
Rvalue::Discriminant(_) => {}
Rvalue::Aggregate(_, _) => {}
Rvalue::ShallowInitBox(_, _) | Rvalue::CopyForDeref(_) => todo!()
Rvalue::ShallowInitBox(_, _) | Rvalue::CopyForDeref(_) => {}
}
}

Expand Down Expand Up @@ -351,7 +352,7 @@ where
key: &(DefId, Vec<Option<bool>>),
) -> Option<Option<BitSet<Local>>> {
let contexts = self.contexts.borrow();
contexts.get(key).map(|res| res.clone())
contexts.get(key).cloned()
}

fn t_visit_source_destination(&mut self, destination: &Place) {
Expand All @@ -370,10 +371,8 @@ where
false
}
}) {
self.tcx.sess.emit_err(super::errors::TaintedSink {
fn_name: name,
span: *span,
});
struct_span_err!(self.tcx.sess, *span, T0001, "function `{}` received tainted input", name)
.emit();
}
}
}
39 changes: 18 additions & 21 deletions src/analysis/taint_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,33 @@ impl TaintDomain<Local> for PointsAwareTaintDomain<'_, Local> {

impl PointsAwareTaintDomain<'_, Local> {
pub(crate) fn add_ref(&mut self, from: &Place, to: &Place) {
let set = self.map.entry(from.local).or_insert_with(HashSet::new);
let set = self.map.entry(from.local).or_default();
set.insert(to.local);
}

fn get_aliases(&mut self, ix: Local) -> HashSet<Local> {
let children = {
let mut result = HashSet::new();
result.insert(ix);
let mut previous_size = result.len();

loop {
for (key, set) in self.map.iter() {
if result.contains(key) {
for l in set.iter() {
result.insert(*l);
}
let mut result = HashSet::new();
result.insert(ix);
let mut previous_size = result.len();

loop {
for (key, set) in self.map.iter() {
if result.contains(key) {
for l in set.iter() {
result.insert(*l);
}
}
}

let current_size = result.len();
if previous_size != current_size {
previous_size = current_size;
} else {
break;
}
let current_size = result.len();
if previous_size != current_size {
previous_size = current_size;
} else {
break;
}
}

result
};
children
result
}
}

Expand Down
22 changes: 7 additions & 15 deletions src/bins/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ extern crate rustc_session;
extern crate rustc_span;

use eval::main;
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, EarlyErrorHandler};
use taint::eval;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};

fn main() {
// TODO/FIXME: Get those working!
//rustc_driver::install_ice_hook();
//rustc_driver::init_rustc_env_logger();
rustc_driver::install_ice_hook("https://github.com/LiHRaM/taint/issues", |_| ());
rustc_driver::init_rustc_env_logger(&EarlyErrorHandler::new(ErrorOutputType::default()));
init_tracing();

let mut rustc_args: Vec<String> = vec![];
Expand Down Expand Up @@ -111,15 +108,10 @@ where

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

let main_id = entry_def_id; //.to_def_id();
main::eval_main(tcx, main_id);
tcx.sess.struct_err("no main function found").emit();
tcx.sess.abort_if_errors();
}
}
14 changes: 4 additions & 10 deletions src/eval/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use hir::intravisit::Visitor;
use rustc_ast::AttrKind;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_span::Symbol;

use crate::errors::InvalidVariant;

/// Find all attributes in a crate which originate from the `taint` tool.
pub struct TaintAttributeFinder<'tcx> {
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -52,13 +51,11 @@ 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().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 def_id = item_id.owner.to_def_id();
let attrs = self.tcx.hir().attrs(item_id);
for attr in attrs {
if let AttrKind::Normal(ref kind) = attr.kind {
Expand All @@ -71,10 +68,7 @@ impl TaintAttributeFinder<'_> {
} else if symbol == &sym_sanitizer {
self.info.sanitizers.push(def_id)
} else {
self.tcx.sess.emit_err(InvalidVariant {
attr_name: symbol.to_ident_string(),
span: item.span(),
});
struct_span_err!(self.tcx.sess, item.span(), T0002, "Taint attribute `{}` is invalid. We currently only support `source`, `sink`, and `sanitizer`", symbol.to_ident_string()).emit();
};
break;
}
Expand Down
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#![feature(rustc_private)]
#![feature(box_patterns)]

extern crate rustc_driver;
extern crate rustc_apfloat;
extern crate rustc_ast;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_fluent_macro;
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_dataflow;
extern crate rustc_session;
Expand All @@ -23,10 +22,3 @@ 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: 0 additions & 7 deletions src/messages.ftl

This file was deleted.

0 comments on commit eb225ca

Please sign in to comment.