Skip to content

Commit

Permalink
Add cross-crate test case
Browse files Browse the repository at this point in the history
  • Loading branch information
JustusAdam committed Jul 26, 2024
1 parent 74fafba commit 61b973e
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
76 changes: 76 additions & 0 deletions crates/paralegal-flow/tests/cross-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#![feature(rustc_private)]
#[macro_use]
extern crate lazy_static;

use paralegal_flow::test_utils::*;
use paralegal_spdg::Identifier;

const CRATE_DIR: &str = "tests/cross-crate";

lazy_static! {
static ref TEST_CRATE_ANALYZED: bool = {
paralegal_flow_command(CRATE_DIR)
.status()
.unwrap()
.success()
};
}

macro_rules! define_test {
($name:ident: $ctrl:ident -> $block:block) => {
define_test!($name: $ctrl, $name -> $block);
};
($name:ident: $ctrl:ident, $ctrl_name:ident -> $block:block) => {
paralegal_flow::define_flow_test_template!(TEST_CRATE_ANALYZED, CRATE_DIR, $name: $ctrl, $ctrl_name -> $block);
};
}

define_test!(basic : graph -> {
let src_fn = graph.function("src");
let src = graph.call_site(&src_fn);
let not_src_fn = graph.function("not_src");
let not_src = graph.call_site(&not_src_fn);
let target_fn = graph.function("target");
let target = graph.call_site(&target_fn);
assert!(src.output().flows_to_data(&target.input()));
assert!(!not_src.output().flows_to_data(&target.input()));
});

define_test!(basic_marker: graph -> {

let marker = Identifier::new_intern("mark");
assert!(dbg!(&graph.spdg().markers).iter().any(|(_, markers)| markers.contains(&marker)))
});

define_test!(assigns_marker: graph -> {
let sources = graph.marked(Identifier::new_intern("source"));
let mark = graph.marked(Identifier::new_intern("mark"));
let target = graph.marked(Identifier::new_intern("target"));
assert!(!sources.is_empty());
assert!(!mark.is_empty());
assert!(!target.is_empty());
assert!(sources.flows_to_data(&mark));
assert!(mark.flows_to_data(&target));
});

define_test!(basic_generic : graph -> {
let src_fn = graph.function("src");
let src = graph.call_site(&src_fn);
let not_src_fn = graph.function("not_src");
let not_src = graph.call_site(&not_src_fn);
let target_fn = graph.function("target");
let target = graph.call_site(&target_fn);
assert!(src.output().flows_to_data(&target.input()));
assert!(!not_src.output().flows_to_data(&target.input()));
});

define_test!(assigns_marker_generic: graph -> {
let sources = graph.marked(Identifier::new_intern("source"));
let mark = graph.marked(Identifier::new_intern("mark"));
let target = graph.marked(Identifier::new_intern("target"));
assert!(!sources.is_empty());
assert!(!mark.is_empty());
assert!(!target.is_empty());
assert!(sources.flows_to_data(&mark));
assert!(mark.flows_to_data(&target));
});
57 changes: 57 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["dependency", "entry"]
6 changes: 6 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/dependency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "dependency"
version = "0.0.1"

[dependencies]
paralegal = { path = "../../../../paralegal" }
30 changes: 30 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/dependency/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub fn find_me(a: usize, _b: usize) -> usize {
a
}

#[paralegal::marker(mark, return)]
pub fn source() -> usize {
0
}

#[paralegal::marker(mark, return)]
fn taint_it<A>(_: A) -> A {
unimplemented!()
}

pub fn assign_marker(a: usize) -> usize {
taint_it(a)
}

pub fn find_me_generic<A>(a: A, _b: A) -> A {
a
}

#[paralegal::marker(mark, return)]
pub fn generic_source<A>() -> A {
unimplemented!()
}

pub fn assign_marker_generic<A>(a: A) -> A {
taint_it(a)
}
7 changes: 7 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/entry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "entry"
version = "0.0.1"

[dependencies]
dependency = { path = "../dependency" }
paralegal = { path = "../../../../paralegal" }
43 changes: 43 additions & 0 deletions crates/paralegal-flow/tests/cross-crate/entry/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extern crate dependency;

use dependency::{assign_marker, assign_marker_generic, find_me, find_me_generic, source};

#[paralegal::marker(source, return)]
fn src() -> usize {
0
}

#[paralegal::marker(not_source)]
fn not_src() -> usize {
1
}

#[paralegal::marker(target, arguments = [0])]
fn target(u: usize) {}

#[paralegal::analyze]
fn basic() {
target(find_me(src(), not_src()))
}

#[paralegal::analyze]
fn basic_marker() {
target(source());
}

#[paralegal::analyze]
fn assigns_marker() {
target(assign_marker(src()));
}

#[paralegal::analyze]
fn basic_generic() {
target(find_me_generic(src(), not_src()))
}

#[paralegal::analyze]
fn assigns_marker_generic() {
target(assign_marker_generic(src()));
}

fn main() {}

0 comments on commit 61b973e

Please sign in to comment.