Skip to content

Commit

Permalink
Fix running on Windows
Browse files Browse the repository at this point in the history
Add directory where asan DLL is to path when running.
  • Loading branch information
RossSmyth committed Jul 24, 2024
1 parent aef5d88 commit 2ecaf38
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ toml = "0.5.9"
rustc_version = "0.4.0"
cargo_metadata = "0.18.1"

[target.'cfg(target_env = "msvc")'.dependencies]
cc = "1.1"

[dev-dependencies]
assert_cmd = "2.0.7"
predicates = "2.1.4"
11 changes: 11 additions & 0 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@ impl FuzzProject {
cmd.arg(format!("-fork={}", run.jobs));
}

#[cfg(target_env = "msvc")]
{
use crate::utils::{append_to_pathvar, get_asan_path};
// On Windows asan is in a DLL. This DLL is not on PATH by default, so the recommended
// action is to add the directory to PATH when running.
let asan = get_asan_path().with_context(|| "could not find AddressSanitizer DLL")?;
let new_path =
append_to_pathvar(asan).with_context(|| "could not get PATH variable")?;
cmd.env("PATH", new_path);
}

// When libfuzzer finds failing inputs, those inputs will end up in the
// artifacts directory. To easily filter old artifacts from new ones,
// get the current time, and then later we only consider files modified
Expand Down
28 changes: 28 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
use std::ffi::OsString;

/// The default target to pass to cargo, to workaround issue #11.
pub fn default_target() -> &'static str {
current_platform::CURRENT_PLATFORM
}

/// Gets the path to the asan DLL required for the asan instrumented binary to run.
#[cfg(target_env = "msvc")]
pub fn get_asan_path() -> Option<std::path::PathBuf> {
// The asan DLL sits next to cl & link.exe. So grab the parent path.
Some(
cc::windows_registry::find_tool(default_target(), "link.exe")?
.path()
.parent()?
.to_owned(),
)
}

/// Append a value to the PATH variable
#[cfg(target_env = "msvc")]
pub fn append_to_pathvar(path: std::path::PathBuf) -> Option<OsString> {
use std::env;

if let Some(current) = env::var_os("PATH") {
let mut current = env::split_paths(&current).collect::<Vec<_>>();
current.push(path);
return env::join_paths(current).ok();
}

return None;
}

0 comments on commit 2ecaf38

Please # to comment.