Skip to content

Commit a278357

Browse files
authored
Add whole-archive feature to libafl_targets (#1544)
* maybe fix linkage? * fix hack CI * interceptors * do not call strstr and friends * format * whoops * enforce nightly; fixup linkage by featuring interceptors * skip libafl_libfuzzer in stable cargo hack check * oops * packed_bundled_libs is stablised
1 parent a013ad6 commit a278357

File tree

6 files changed

+320
-40
lines changed

6 files changed

+320
-40
lines changed

.github/workflows/build_and_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ jobs:
145145
# Skipping `python` as it has to be built with the `maturin` tool
146146
# `agpl`, `nautilus` require nightly
147147
# `sancov_pcguard_edges` is tested seperately
148-
run: LLVM_CONFIG=llvm-config-15 cargo hack check --each-feature --clean-per-run --exclude-features=prelude,agpl,nautilus,python,sancov_pcguard_edges,arm,aarch64,i386,be,systemmode --no-dev-deps
148+
run: LLVM_CONFIG=llvm-config-15 cargo hack check --workspace --each-feature --clean-per-run --exclude-features=prelude,agpl,nautilus,python,sancov_pcguard_edges,arm,aarch64,i386,be,systemmode,whole_archive --no-dev-deps --exclude libafl_libfuzzer
149149
- name: Check nightly features
150150
run: cargo +nightly check --features=agpl && cargo +nightly check --features=nautilus
151151

libafl_libfuzzer/libafl_libfuzzer_runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ crate-type = ["staticlib", "rlib"]
3232
[dependencies]
3333
libafl = { path = "../../libafl", default-features = false, features = ["std", "derive", "llmp_compression", "rand_trait", "regex", "errors_backtrace", "serdeany_autoreg", "tui_monitor"] }
3434
libafl_bolts = { path = "../../libafl_bolts", default-features = false, features = ["std", "derive", "llmp_compression", "rand_trait", "serdeany_autoreg", "errors_backtrace"] }
35-
libafl_targets = { path = "../../libafl_targets", features = ["sancov_8bit", "sancov_cmplog", "libfuzzer", "libfuzzer_oom", "libfuzzer_define_run_driver", "sanitizers_flags"] }
35+
libafl_targets = { path = "../../libafl_targets", features = ["sancov_8bit", "sancov_cmplog", "libfuzzer", "libfuzzer_oom", "libfuzzer_define_run_driver", "libfuzzer_interceptors", "sanitizers_flags", "whole_archive"] }
3636

3737
ahash = { version = "0.8.3", default-features = false }
3838
libc = "0.2.139"

libafl_targets/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,25 @@ std = ["libafl/std"]
2121
libfuzzer = ["std", "sanitizer_interfaces"]
2222
libfuzzer_no_link_main = ["libfuzzer"]
2323
libfuzzer_define_run_driver = ["libfuzzer"]
24+
libfuzzer_interceptors = ["libfuzzer", "sancov_cmplog"]
2425
libfuzzer_oom = ["libfuzzer"]
2526
sanitizers_flags = []
2627
pointer_maps = []
2728
sancov_pcguard_edges = []
2829
sancov_pcguard_hitcounts = []
2930
sancov_value_profile = []
3031
sancov_8bit = []
31-
sancov_cmplog = []
32+
sancov_cmplog = [] # Defines cmp and __sanitizer_weak_hook functions. Use libfuzzer_interceptors to define interceptors (only compatible with Linux)
3233
sancov_pcguard = ["sancov_pcguard_hitcounts"]
3334
sanitizer_interfaces = []
3435
clippy = [] # Ignore compiler warnings during clippy
3536
observers = ["meminterval", "ahash"]
37+
whole_archive = [] # use +whole-archive to ensure the presence of weak symbols
3638

3739
[build-dependencies]
3840
bindgen = "0.68"
3941
cc = { version = "1.0", features = ["parallel"] }
42+
rustversion = "1.0"
4043

4144
[dependencies]
4245
libafl = { path = "../libafl", version = "0.11.1", default-features = false, features = [] }

libafl_targets/build.rs

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
33
use std::{env, fs::File, io::Write, path::Path};
44

5+
#[cfg(feature = "whole_archive")]
6+
#[rustversion::not(nightly)]
7+
compile_error!("Must use a nightly release with whole_archive!");
8+
59
#[allow(clippy::too_many_lines)]
610
fn main() {
711
let out_dir = env::var_os("OUT_DIR").unwrap();
@@ -72,6 +76,10 @@ fn main() {
7276

7377
#[cfg(unix)]
7478
sancov_cmp.flag("-Wno-sign-compare");
79+
#[cfg(feature = "whole_archive")]
80+
{
81+
sancov_cmp.link_lib_modifier("+whole-archive");
82+
}
7583

7684
#[cfg(feature = "sancov_value_profile")]
7785
{
@@ -125,12 +133,32 @@ fn main() {
125133
let mut libfuzzer = cc::Build::new();
126134
libfuzzer.file(src_dir.join("libfuzzer.c"));
127135

136+
#[cfg(feature = "whole_archive")]
137+
{
138+
libfuzzer.link_lib_modifier("+whole-archive");
139+
}
140+
128141
#[cfg(feature = "libfuzzer_no_link_main")]
129142
libfuzzer.define("FUZZER_NO_LINK_MAIN", "1");
130143
#[cfg(feature = "libfuzzer_define_run_driver")]
131144
libfuzzer.define("FUZZER_DEFINE_RUN_DRIVER", "1");
132145

133146
libfuzzer.compile("libfuzzer");
147+
148+
#[cfg(feature = "libfuzzer_interceptors")]
149+
{
150+
println!("cargo:rerun-if-changed=src/libfuzzer/FuzzerInterceptors.cpp");
151+
152+
let mut interceptors = cc::Build::new();
153+
interceptors.file(src_dir.join("libfuzzer/FuzzerInterceptors.cpp"));
154+
155+
#[cfg(feature = "whole_archive")]
156+
{
157+
interceptors.link_lib_modifier("+whole-archive");
158+
}
159+
160+
interceptors.cpp(true).compile("interceptors");
161+
}
134162
}
135163

136164
println!("cargo:rerun-if-changed=src/common.h");
@@ -159,6 +187,11 @@ fn main() {
159187

160188
let mut common = cc::Build::new();
161189

190+
#[cfg(feature = "whole_archive")]
191+
{
192+
common.link_lib_modifier("+whole-archive");
193+
}
194+
162195
#[cfg(feature = "sanitizers_flags")]
163196
{
164197
common.define("DEFAULT_SANITIZERS_OPTIONS", "1");
@@ -168,7 +201,14 @@ fn main() {
168201

169202
println!("cargo:rerun-if-changed=src/coverage.c");
170203

171-
cc::Build::new()
204+
let mut coverage = cc::Build::new();
205+
206+
#[cfg(feature = "whole_archive")]
207+
{
208+
coverage.link_lib_modifier("+whole-archive");
209+
}
210+
211+
coverage
172212
.file(src_dir.join("coverage.c"))
173213
.define("EDGES_MAP_SIZE", Some(&*format!("{edges_map_size}")))
174214
.define("ACCOUNTING_MAP_SIZE", Some(&*format!("{acc_map_size}")))
@@ -177,49 +217,47 @@ fn main() {
177217
println!("cargo:rerun-if-changed=src/cmplog.h");
178218
println!("cargo:rerun-if-changed=src/cmplog.c");
179219

180-
#[cfg(unix)]
220+
let mut cmplog = cc::Build::new();
221+
222+
#[cfg(feature = "whole_archive")]
181223
{
182-
cc::Build::new()
183-
.flag("-Wno-pointer-sign") // UNIX ONLY FLAGS
184-
.flag("-Wno-sign-compare")
185-
.define("CMP_MAP_SIZE", Some(&*format!("{cmp_map_size}")))
186-
.define(
187-
"AFLPP_CMPLOG_MAP_W",
188-
Some(&*format!("{aflpp_cmplog_map_w}")),
189-
)
190-
.define(
191-
"AFLPP_CMPLOG_MAP_H",
192-
Some(&*format!("{aflpp_cmplog_map_h}")),
193-
)
194-
.define("CMPLOG_MAP_W", Some(&*format!("{cmplog_map_w}")))
195-
.define("CMPLOG_MAP_H", Some(&*format!("{cmplog_map_h}")))
196-
.file(src_dir.join("cmplog.c"))
197-
.compile("cmplog");
224+
cmplog.link_lib_modifier("+whole-archive");
198225
}
199226

200-
#[cfg(not(unix))]
227+
#[cfg(unix)]
201228
{
202-
cc::Build::new()
203-
.define("CMP_MAP_SIZE", Some(&*format!("{cmp_map_size}")))
204-
.define(
205-
"AFLPP_CMPLOG_MAP_W",
206-
Some(&*format!("{aflpp_cmplog_map_w}")),
207-
)
208-
.define(
209-
"AFLPP_CMPLOG_MAP_H",
210-
Some(&*format!("{aflpp_cmplog_map_h}")),
211-
)
212-
.define("CMPLOG_MAP_W", Some(&*format!("{cmplog_map_w}")))
213-
.define("CMPLOG_MAP_H", Some(&*format!("{cmplog_map_h}")))
214-
.file(src_dir.join("cmplog.c"))
215-
.compile("cmplog");
229+
cmplog
230+
.flag("-Wno-pointer-sign") // UNIX ONLY FLAGS
231+
.flag("-Wno-sign-compare");
216232
}
217233

234+
cmplog
235+
.define("CMP_MAP_SIZE", Some(&*format!("{cmp_map_size}")))
236+
.define(
237+
"AFLPP_CMPLOG_MAP_W",
238+
Some(&*format!("{aflpp_cmplog_map_w}")),
239+
)
240+
.define(
241+
"AFLPP_CMPLOG_MAP_H",
242+
Some(&*format!("{aflpp_cmplog_map_h}")),
243+
)
244+
.define("CMPLOG_MAP_W", Some(&*format!("{cmplog_map_w}")))
245+
.define("CMPLOG_MAP_H", Some(&*format!("{cmplog_map_h}")))
246+
.file(src_dir.join("cmplog.c"))
247+
.compile("cmplog");
248+
218249
#[cfg(unix)]
219250
{
220251
println!("cargo:rerun-if-changed=src/forkserver.c");
221252

222-
cc::Build::new()
253+
let mut forkserver = cc::Build::new();
254+
255+
#[cfg(feature = "whole_archive")]
256+
{
257+
forkserver.link_lib_modifier("+whole-archive");
258+
}
259+
260+
forkserver
223261
.file(src_dir.join("forkserver.c"))
224262
.compile("forkserver");
225263
}
@@ -228,7 +266,14 @@ fn main() {
228266
{
229267
println!("cargo:rerun-if-changed=src/windows_asan.c");
230268

231-
cc::Build::new()
269+
let mut winasan = cc::Build::new();
270+
271+
#[cfg(feature = "whole_archive")]
272+
{
273+
winasan.link_lib_modifier("+whole-archive");
274+
}
275+
276+
winasan
232277
.file(src_dir.join("windows_asan.c"))
233278
.compile("windows_asan");
234279
}

libafl_targets/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//! `libafl_targets` contains runtime code, injected in the target itself during compilation.
2-
//!
3-
//!
42
#![no_std]
53
#![deny(rustdoc::broken_intra_doc_links)]
64
#![deny(clippy::all)]

0 commit comments

Comments
 (0)