Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not write to user data directories on build. Fix docs.rs documentation generation #231

Merged
merged 6 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ tempfile = "3"

[features]
reset_lazy_static = ["lazy_static"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
41 changes: 20 additions & 21 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::path::{Path, PathBuf};

use xdg;
Expand All @@ -9,6 +10,23 @@ fn xdg_dir() -> xdg::BaseDirectories {
xdg::BaseDirectories::with_prefix(prefix).unwrap()
}

fn data_dir(dir_name: &str) -> PathBuf {
// For docs.rs builds, use OUT_DIR.
// For other cases, use a XDG data directory.
// It is necessary to use OUT_DIR for docs.rs builds,
// as that is the only place where we can write to.
// The Cargo documentation recommends that build scripts
// place their generated files at OUT_DIR too, but we
// don't change that for now for normal builds.
if cfg!(docsrs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting to see if std::env::var("DOCS_RS").is_ok() here.

Is there a reason to prefer this approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This source file is used by both the build script and the application code. By checking for the feature flag we can prevent the DOCS_RS environment variable from interferring with the application code, and make sure that this code path is taken only on the intended environment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good argument. Let's see what happens.

let path = Path::new(&env::var("OUT_DIR").unwrap()).join(dir_name);
std::fs::create_dir_all(&path).unwrap();
path
} else {
xdg_dir().create_data_directory(dir_name).unwrap()
}
}

const SHORT_COMMIT_HASH_LEN: usize = 7;

pub fn afl_rustc_version() -> String {
Expand All @@ -32,31 +50,12 @@ fn pkg_version() -> String {
ret
}

// Place directories inside the crate when building for docs.rs.
// (Modifying system paths are forbidden.)

#[cfg(docsrs)]
pub fn afl_dir() -> PathBuf {
let path = PathBuf::from("./afl");
std::fs::create_dir_all(&path).unwrap();
path
}

#[cfg(not(docsrs))]
pub fn afl_dir() -> PathBuf {
xdg_dir().create_data_directory("afl").unwrap()
}

#[cfg(docsrs)]
pub fn afl_llvm_rt_dir() -> PathBuf {
let path = PathBuf::from("./afl-llvm-rt");
std::fs::create_dir_all(&path).unwrap();
path
data_dir("afl")
}

#[cfg(not(docsrs))]
pub fn afl_llvm_rt_dir() -> PathBuf {
xdg_dir().create_data_directory("afl-llvm-rt").unwrap()
data_dir("afl-llvm-rt")
}

#[allow(dead_code)]
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::io::{self, Read};
use std::panic;

mod common;
#[doc(hidden)]
pub use common::*;

// those functions are provided by the afl-llvm-rt static library
Expand All @@ -20,6 +21,7 @@ extern "C" {
static __afl_fuzz_ptr: *const u8;
}

#[doc(hidden)]
#[no_mangle]
pub static __afl_sharedmem_fuzzing: i32 = 1;

Expand Down Expand Up @@ -147,6 +149,7 @@ macro_rules! fuzz_nohook {
( $($x:tt)* ) => { $crate::__fuzz!(false, $($x)*) }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __fuzz {
($hook:expr, |$buf:ident| $body:block) => {
Expand Down