Skip to content

Commit

Permalink
Replace Mutex use with -- -M <unique_id>
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Feb 18, 2025
1 parent a77b2b7 commit 8df5b26
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 33 deletions.
7 changes: 4 additions & 3 deletions cargo-test-fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,12 +1163,13 @@ fn fuzz_command(
&input_dir,
"-o",
&output_dir.to_string_lossy(),
"-M",
"default",
]
.into_iter()
.map(String::from),
);
if opts.zzargs.iter().find(|&arg| arg == "-M").is_none() {
args.extend(["-M".to_owned(), "default".to_owned()]);
}
if !config.sufficient_cpus {
args.extend(["-V".to_owned(), opts.slice.to_string()]);
} else if let Some(max_total_time) = opts.max_total_time {
Expand All @@ -1190,7 +1191,7 @@ fn fuzz_command(
);

let mut command = Command::new("cargo");
command.envs(envs).args(&args);
command.envs(envs).args(args);
debug!("{:?}", command);
command
}
Expand Down
9 changes: 6 additions & 3 deletions cargo-test-fuzz/tests/integration/auto_generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::ensure;
use internal::dirs::corpus_directory_from_target;
use predicates::prelude::*;
use std::fs::{read_dir, remove_dir_all};
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const MAX_TOTAL_TIME: &str = "60";

Expand Down Expand Up @@ -30,8 +30,6 @@ fn auto_generate_empty() {
#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[test]
fn auto_generate_nonempty() {
let _lock = crate::ASSERT_MUTEX.lock();

auto_generate("assert", "target", true, "Auto-generated", 1);
}

Expand All @@ -43,13 +41,18 @@ fn auto_generate(krate: &str, target: &str, success: bool, pattern: &str, n: usi
remove_dir_all(&corpus).unwrap_or_default();

retry(3, || {
let id = unique_id();

let assert = examples::test_fuzz(krate, target)
.unwrap()
.args([
"--no-ui",
"--run-until-crash",
"--max-total-time",
MAX_TOTAL_TIME,
"--",
"-M",
&id,
])
.logged_assert();

Expand Down
12 changes: 5 additions & 7 deletions cargo-test-fuzz/tests/integration/consolidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::ensure;
use internal::dirs::corpus_directory_from_target;
use predicates::prelude::*;
use std::fs::{read_dir, remove_dir_all};
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const CRASH_MAX_TOTAL_TIME: &str = "60";

Expand All @@ -11,8 +11,6 @@ const HANG_MAX_TOTAL_TIME: &str = "120";
#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[test]
fn consolidate_crashes() {
let _lock = crate::ASSERT_MUTEX.lock();

consolidate(
"assert",
"target",
Expand All @@ -28,8 +26,6 @@ fn consolidate_crashes() {
#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[test]
fn consolidate_hangs() {
let _lock = crate::PARSE_DURATION_MUTEX.lock();

consolidate(
"parse_duration",
"parse",
Expand All @@ -53,7 +49,9 @@ fn consolidate(krate: &str, target: &str, fuzz_args: &[&str], pattern: &str) {
assert_eq!(1, read_dir(&corpus).unwrap().count());

retry(3, || {
let mut args = vec!["--no-ui"];
let id = unique_id();

let mut args = vec!["--no-ui", "--", "-M", &id];
args.extend_from_slice(fuzz_args);

examples::test_fuzz(krate, target)
Expand All @@ -64,7 +62,7 @@ fn consolidate(krate: &str, target: &str, fuzz_args: &[&str], pattern: &str) {

examples::test_fuzz(krate, target)
.unwrap()
.args(["--consolidate"])
.args(["--consolidate", "--", "-M", &id])
.logged_assert()
.success();

Expand Down
10 changes: 5 additions & 5 deletions cargo-test-fuzz/tests/integration/fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use internal::dirs::corpus_directory_from_target;
use predicates::prelude::*;
use std::fs::remove_dir_all;
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const MAX_TOTAL_TIME: &str = "60";

#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[test]
fn fuzz_assert() {
let _lock = crate::ASSERT_MUTEX.lock();

fuzz("assert", false);
}

Expand All @@ -32,16 +30,18 @@ fn fuzz(krate: &str, persistent: bool) {
.success();

retry(3, || {
let id = unique_id();

let mut command = examples::test_fuzz(krate, "target").unwrap();

let mut args = vec!["--exit-code", "--run-until-crash"];
if persistent {
args.push("--persistent");
}
args.extend_from_slice(&["--max-total-time", MAX_TOTAL_TIME]);
args.extend_from_slice(&["--max-total-time", MAX_TOTAL_TIME, "--", "-M", &id]);

command
.args(&args)
.args(args)
.logged_assert()
.try_code(predicate::eq(1))
})
Expand Down
13 changes: 11 additions & 2 deletions cargo-test-fuzz/tests/integration/fuzz_cast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use predicates::prelude::*;
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const MAX_TOTAL_TIME: &str = "60";

Expand All @@ -11,6 +11,8 @@ fn fuzz_cast() {
.success();

for use_cast_checks in [false, true] {
let id = unique_id();

let mut args = vec![
"--exit-code",
"--run-until-crash",
Expand All @@ -23,6 +25,7 @@ fn fuzz_cast() {
} else {
0
};
args.extend(["--", "-M", &id]);
retry(3, || {
examples::test_fuzz("cast", "target")
.unwrap()
Expand All @@ -34,7 +37,13 @@ fn fuzz_cast() {
if use_cast_checks {
examples::test_fuzz("cast", "target")
.unwrap()
.args(["--replay=crashes", "--features=test-fuzz/cast_checks"])
.args([
"--replay=crashes",
"--features=test-fuzz/cast_checks",
"--",
"-M",
&id,
])
.logged_assert()
.success()
.stdout(predicate::str::contains("invalid cast"));
Expand Down
7 changes: 6 additions & 1 deletion cargo-test-fuzz/tests/integration/fuzz_generic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use internal::dirs::corpus_directory_from_target;
use predicates::prelude::*;
use std::{fs::remove_dir_all, sync::Mutex};
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const MAX_TOTAL_TIME: &str = "60";

Expand Down Expand Up @@ -38,13 +38,18 @@ fn fuzz(test: &str, code: i32) {
assert!(corpus.exists());

retry(3, || {
let id = unique_id();

examples::test_fuzz("generic", "struct_target")
.unwrap()
.args([
"--exit-code",
"--run-until-crash",
"--max-total-time",
MAX_TOTAL_TIME,
"--",
"-M",
&id,
])
.logged_assert()
.try_code(predicate::eq(code))
Expand Down
7 changes: 6 additions & 1 deletion cargo-test-fuzz/tests/integration/fuzz_parallel.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use internal::dirs::output_directory_from_target;
use predicates::prelude::*;
use std::{ffi::OsStr, fs::remove_dir_all};
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

const CPUS: &str = "2";
const TIME_SLICE: &str = "30";

#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]
#[test]
fn fuzz_parallel() {
let id = unique_id();

for i in 0..6 {
let output_dir = output_directory_from_target("parallel", &format!("target_{i}"));
remove_dir_all(output_dir).unwrap_or_default();
Expand All @@ -29,6 +31,9 @@ fn fuzz_parallel() {
CPUS,
"--slice",
TIME_SLICE,
"--",
"-M",
&id,
])
.logged_assert()
.try_code(predicate::eq(1))
Expand Down
5 changes: 0 additions & 5 deletions cargo-test-fuzz/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Mutex;

mod auto_generate;
mod build;
mod consolidate;
Expand All @@ -10,6 +8,3 @@ mod fuzz_generic;
mod fuzz_parallel;
mod generic_args;
mod replay;

static ASSERT_MUTEX: Mutex<()> = Mutex::new(());
static PARSE_DURATION_MUTEX: Mutex<()> = Mutex::new(());
11 changes: 6 additions & 5 deletions cargo-test-fuzz/tests/integration/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use internal::dirs::corpus_directory_from_target;
use predicates::prelude::*;
use rlimit::Resource;
use std::fs::remove_dir_all;
use testing::{examples, retry, CommandExt};
use testing::{examples, retry, unique_id, CommandExt};

// smoelius: MEMORY_LIMIT must be large enough for the build process to complete.
const MEMORY_LIMIT: u64 = 1024 * 1024 * 1024;
Expand Down Expand Up @@ -39,8 +39,6 @@ fn replay_crashes() {
#[allow(clippy::trivial_regex)]
#[test]
fn replay_hangs() {
let _lock = crate::PARSE_DURATION_MUTEX.lock();

replay(
"parse_duration",
"parse",
Expand All @@ -51,6 +49,8 @@ fn replay_hangs() {
}

fn replay(krate: &str, target: &str, fuzz_args: &[&str], object: Object, re: &str) {
let id = unique_id();

let corpus = corpus_directory_from_target(krate, target);

// smoelius: `corpus` is distinct for all tests. So there is no race here.
Expand All @@ -64,12 +64,12 @@ fn replay(krate: &str, target: &str, fuzz_args: &[&str], object: Object, re: &st

examples::test_fuzz(krate, target)
.unwrap()
.args(["--reset"])
.args(["--reset", "--", "-M", &id])
.logged_assert()
.success();

retry(3, || {
let mut args = vec!["--no-ui"];
let mut args = vec!["--no-ui", "--", "-M", &id];
args.extend_from_slice(fuzz_args);

examples::test_fuzz(krate, target)
Expand All @@ -88,6 +88,7 @@ fn replay(krate: &str, target: &str, fuzz_args: &[&str], object: Object, re: &st
Object::Crashes => "--replay=crashes",
Object::Hangs => "--replay=hangs",
}])
.args(["--", "-M", &id])
.logged_assert()
.success()
.try_stdout(predicate::str::is_match(re).unwrap())
Expand Down
2 changes: 1 addition & 1 deletion testing/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn test_fuzz_all() -> Result<Command> {
];

let mut command = Command::cargo_bin("cargo-test-fuzz")?;
command.args(&args);
command.args(args);
Ok(command)
}

Expand Down
8 changes: 8 additions & 0 deletions testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod command_ext;
use std::sync::atomic::{AtomicU32, Ordering};

pub use command_ext::CommandExt;

pub mod examples;
Expand All @@ -10,3 +12,9 @@ pub use crate::retry::*;
fn init() {
env_logger::init();
}

static ID: AtomicU32 = AtomicU32::new(0);

pub fn unique_id() -> String {
ID.fetch_add(1, Ordering::SeqCst).to_string()
}

0 comments on commit 8df5b26

Please sign in to comment.