Fix linking with -z defs (#601)

* Always link no-link-rt when not linking a fuzzer

* Handle dynamic

* fuzzbench

* Handle -z defs

* fix

* clippy

* clippy

* windowa

* fix
This commit is contained in:
Andrea Fioraldi 2022-04-08 18:06:27 +02:00 committed by GitHub
parent bd23f7c916
commit e8f5949aec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 13 deletions

View File

@ -2,7 +2,7 @@ use libafl_cc::{ClangWrapper, CompilerWrapper, LLVMPasses};
use std::env;
pub fn main() {
let args: Vec<String> = env::args().collect();
let mut args: Vec<String> = env::args().collect();
if args.len() > 1 {
let mut dir = env::current_exe().unwrap();
let wrapper_name = dir.file_name().unwrap().to_str().unwrap();
@ -15,6 +15,9 @@ pub fn main() {
dir.pop();
// Must be always present, even without --libafl
args.push("-fsanitize-coverage=trace-pc-guard,trace-cmp".into());
let mut cc = ClangWrapper::new();
#[cfg(target_os = "linux")]
@ -29,7 +32,6 @@ pub fn main() {
.parse_args(&args)
.expect("Failed to parse the command line")
.link_staticlib(&dir, "fuzzbench")
.add_arg("-fsanitize-coverage=trace-pc-guard,trace-cmp")
.add_pass(LLVMPasses::CmpLogRtn)
.run()
.expect("Failed to run the wrapped compiler")

View File

@ -1234,7 +1234,7 @@ pub mod win32_shmem {
let handle = OpenFileMappingA(
FILE_MAP_ALL_ACCESS,
BOOL(0),
PSTR(&map_str_bytes as *const u8 as *mut u8),
PSTR(map_str_bytes.as_ptr() as *mut _),
);
if handle == HANDLE(0) {
return Err(Error::Unknown(format!(

View File

@ -235,7 +235,7 @@ impl<E: HasInProcessHandlers> TimeoutExecutor<E> {
let tp_timer = unsafe {
CreateThreadpoolTimer(
Some(timeout_handler),
&mut GLOBAL_STATE as *mut _ as *mut c_void,
core::ptr::addr_of_mut!(GLOBAL_STATE) as *mut c_void,
&TP_CALLBACK_ENVIRON_V3::default(),
)
};
@ -284,11 +284,11 @@ where
write_volatile(&mut data.tp_timer, self.tp_timer as *mut _ as *mut c_void);
write_volatile(
&mut data.critical,
&mut self.critical as *mut _ as *mut c_void,
core::ptr::addr_of_mut!(self.critical) as *mut c_void,
);
write_volatile(
&mut data.timeout_input_ptr,
&mut data.current_input_ptr as *mut _ as *mut c_void,
data.current_input_ptr as *mut c_void,
);
let tm: i64 = -self.milli_sec * 10 * 1000;
let ft = FILETIME {

View File

@ -67,6 +67,7 @@ pub struct ClangWrapper {
name: String,
is_cpp: bool,
linking: bool,
shared: bool,
x_set: bool,
bit_mode: u32,
need_libafl_arg: bool,
@ -82,6 +83,7 @@ pub struct ClangWrapper {
#[allow(clippy::match_same_arms)] // for the linking = false wip for "shared"
impl CompilerWrapper for ClangWrapper {
#[allow(clippy::too_many_lines)]
fn parse_args<S>(&mut self, args: &[S]) -> Result<&'_ mut Self, Error>
where
S: AsRef<str>,
@ -115,45 +117,64 @@ impl CompilerWrapper for ClangWrapper {
// new_args.push("-fsanitize-coverage=trace-pc-guard".into());
let mut linking = true;
let mut shared = false;
// Detect stray -v calls from ./configure scripts.
if args.len() > 1 && args[1].as_ref() == "-v" {
linking = false;
}
let mut suppress_linking = 0;
for arg in &args[1..] {
match arg.as_ref() {
let mut i = 1;
while i < args.len() {
match args[i].as_ref() {
"--libafl-no-link" => {
suppress_linking += 1;
self.has_libafl_arg = true;
i += 1;
continue;
}
"--libafl" => {
suppress_linking += 1337;
self.has_libafl_arg = true;
i += 1;
continue;
}
"-fsanitize=fuzzer-no-link" => {
suppress_linking += 1;
self.has_libafl_arg = true;
i += 1;
continue;
}
"-fsanitize=fuzzer" => {
suppress_linking += 1337;
self.has_libafl_arg = true;
i += 1;
continue;
}
"-Wl,-z,defs" | "-Wl,--no-undefined" | "--no-undefined" => {
i += 1;
continue;
}
"-z" => {
if i + 1 < args.len() && args[i + 1].as_ref() == "defs" {
i += 2;
continue;
}
}
"-x" => self.x_set = true,
"-m32" => self.bit_mode = 32,
"-m64" => self.bit_mode = 64,
"-c" | "-S" | "-E" => linking = false,
"-shared" => linking = false, // TODO dynamic list?
"-Wl,-z,defs" | "-Wl,--no-undefined" | "--no-undefined" => continue,
"-shared" => {
linking = false;
shared = true;
} // TODO dynamic list?
_ => (),
};
new_args.push(arg.as_ref().to_string());
new_args.push(args[i].as_ref().to_string());
i += 1;
}
if linking && suppress_linking > 0 && suppress_linking < 1337 {
if linking && suppress_linking >= 0 && suppress_linking < 1337 {
linking = false;
new_args.push(
PathBuf::from(env!("OUT_DIR"))
@ -165,6 +186,7 @@ impl CompilerWrapper for ClangWrapper {
}
self.linking = linking;
self.shared = shared;
if self.optimize {
new_args.push("-g".into());
@ -189,7 +211,7 @@ impl CompilerWrapper for ClangWrapper {
}
// MacOS has odd linker behavior sometimes
#[cfg(target_vendor = "apple")]
if linking {
if linking || shared {
new_args.push("-undefined".into());
new_args.push("dynamic_lookup".into());
}
@ -321,6 +343,7 @@ impl ClangWrapper {
name: "".into(),
is_cpp: false,
linking: false,
shared: false,
x_set: false,
bit_mode: 0,
need_libafl_arg: false,