Skip to content

Commit 256d010

Browse files
authored
Add embed-runtime feature (#1489)
* Add library embedding feature to libafl_libfuzzer * Add comment describing embed-runtime feature and CI test
1 parent 134fe6a commit 256d010

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.github/workflows/build_and_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ jobs:
105105
- name: Run miri tests
106106
run: RUST_BACKTRACE=1 MIRIFLAGS="-Zmiri-disable-isolation" cargo +nightly miri test
107107

108+
# --- test embedding the libafl_libfuzzer_runtime library
109+
- name: Test Build libafl_libfuzzer with embed
110+
run: cargo +nightly test --features=embed-runtime --manifest-path libafl_libfuzzer/Cargo.toml
111+
108112
ubuntu-check:
109113
runs-on: ubuntu-22.04
110114
steps:

libafl_libfuzzer/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ include = [
1414
"/Cargo.toml",
1515
"/build.rs",
1616
"/libafl_libfuzzer_runtime",
17-
"!/libafl_libfuzzer_runtime/target"
17+
"!/libafl_libfuzzer_runtime/target",
1818
]
1919

2020
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -30,6 +30,10 @@ rustversion = "1.0"
3030
arbitrary-derive = ["libfuzzer-sys/arbitrary-derive"]
3131
## Enables fuzzer introspection with LibAFL's `introspection` feature
3232
introspection = []
33+
## Embeds the built libafl_libfuzzer_runtime library into the crate with include_bytes! for use
34+
## in downstream cases like libafl_cc linking the runtime with:
35+
## `-fsanitize=fuzzer-no-link -l:libafl_libfuzzer_runtime.a`
36+
embed-runtime = []
3337

3438
[dependencies]
3539
libfuzzer-sys = { version = "0.4.7", default-features = false }
@@ -39,6 +43,4 @@ document-features = { version = "0.2" }
3943
features = ["document-features"]
4044
all-features = true
4145

42-
rustdoc-args = [
43-
"--cfg", "docsrs",
44-
]
46+
rustdoc-args = ["--cfg", "docsrs"]

libafl_libfuzzer/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ fn main() {
7070
let mut lib_path = custom_lib_dir.join(std::env::var_os("TARGET").unwrap());
7171
lib_path.push("release");
7272

73+
#[cfg(all(feature = "embed-runtime", target_family = "unix"))]
74+
{
75+
// NOTE: lib, .a are added always on unix-like systems as described in:
76+
// https://gist.github.com/novafacing/1389cbb2f0a362d7eb103e67b4468e2b
77+
println!(
78+
"cargo:rustc-env=LIBAFL_LIBFUZZER_RUNTIME_PATH={}",
79+
lib_path.join("libafl_libfuzzer_runtime.a").display()
80+
);
81+
}
82+
7383
println!(
7484
"cargo:rustc-link-search=native={}",
7585
lib_path.to_str().unwrap()

libafl_libfuzzer/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,28 @@ extern "C" {
9090
harness_fn: Option<extern "C" fn(*const u8, usize) -> c_int>,
9191
) -> c_int;
9292
}
93+
94+
#[cfg(all(
95+
feature = "embed-runtime",
96+
target_family = "unix",
97+
// Disable when building with clippy, as it will complain about the missing environment
98+
// variable which is set by the build script, which is not run under clippy.
99+
not(feature = "cargo-clippy")
100+
))]
101+
pub const LIBAFL_LIBFUZZER_RUNTIME_LIBRARY: &'static [u8] =
102+
include_bytes!(env!("LIBAFL_LIBFUZZER_RUNTIME_PATH"));
103+
104+
#[cfg(test)]
105+
mod tests {
106+
#[cfg(all(feature = "embed-runtime", not(feature = "cargo-clippy")))]
107+
#[test]
108+
fn test_embed_runtime_sized() {
109+
use crate::LIBAFL_LIBFUZZER_RUNTIME_LIBRARY;
110+
111+
assert_ne!(
112+
LIBAFL_LIBFUZZER_RUNTIME_LIBRARY.len(),
113+
0,
114+
"Runtime library empty"
115+
);
116+
}
117+
}

0 commit comments

Comments
 (0)