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:
parent
bd23f7c916
commit
e8f5949aec
@ -2,7 +2,7 @@ use libafl_cc::{ClangWrapper, CompilerWrapper, LLVMPasses};
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let mut args: Vec<String> = env::args().collect();
|
||||||
if args.len() > 1 {
|
if args.len() > 1 {
|
||||||
let mut dir = env::current_exe().unwrap();
|
let mut dir = env::current_exe().unwrap();
|
||||||
let wrapper_name = dir.file_name().unwrap().to_str().unwrap();
|
let wrapper_name = dir.file_name().unwrap().to_str().unwrap();
|
||||||
@ -15,6 +15,9 @@ pub fn main() {
|
|||||||
|
|
||||||
dir.pop();
|
dir.pop();
|
||||||
|
|
||||||
|
// Must be always present, even without --libafl
|
||||||
|
args.push("-fsanitize-coverage=trace-pc-guard,trace-cmp".into());
|
||||||
|
|
||||||
let mut cc = ClangWrapper::new();
|
let mut cc = ClangWrapper::new();
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@ -29,7 +32,6 @@ pub fn main() {
|
|||||||
.parse_args(&args)
|
.parse_args(&args)
|
||||||
.expect("Failed to parse the command line")
|
.expect("Failed to parse the command line")
|
||||||
.link_staticlib(&dir, "fuzzbench")
|
.link_staticlib(&dir, "fuzzbench")
|
||||||
.add_arg("-fsanitize-coverage=trace-pc-guard,trace-cmp")
|
|
||||||
.add_pass(LLVMPasses::CmpLogRtn)
|
.add_pass(LLVMPasses::CmpLogRtn)
|
||||||
.run()
|
.run()
|
||||||
.expect("Failed to run the wrapped compiler")
|
.expect("Failed to run the wrapped compiler")
|
||||||
|
@ -1234,7 +1234,7 @@ pub mod win32_shmem {
|
|||||||
let handle = OpenFileMappingA(
|
let handle = OpenFileMappingA(
|
||||||
FILE_MAP_ALL_ACCESS,
|
FILE_MAP_ALL_ACCESS,
|
||||||
BOOL(0),
|
BOOL(0),
|
||||||
PSTR(&map_str_bytes as *const u8 as *mut u8),
|
PSTR(map_str_bytes.as_ptr() as *mut _),
|
||||||
);
|
);
|
||||||
if handle == HANDLE(0) {
|
if handle == HANDLE(0) {
|
||||||
return Err(Error::Unknown(format!(
|
return Err(Error::Unknown(format!(
|
||||||
|
@ -235,7 +235,7 @@ impl<E: HasInProcessHandlers> TimeoutExecutor<E> {
|
|||||||
let tp_timer = unsafe {
|
let tp_timer = unsafe {
|
||||||
CreateThreadpoolTimer(
|
CreateThreadpoolTimer(
|
||||||
Some(timeout_handler),
|
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(),
|
&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.tp_timer, self.tp_timer as *mut _ as *mut c_void);
|
||||||
write_volatile(
|
write_volatile(
|
||||||
&mut data.critical,
|
&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(
|
write_volatile(
|
||||||
&mut data.timeout_input_ptr,
|
&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 tm: i64 = -self.milli_sec * 10 * 1000;
|
||||||
let ft = FILETIME {
|
let ft = FILETIME {
|
||||||
|
@ -67,6 +67,7 @@ pub struct ClangWrapper {
|
|||||||
name: String,
|
name: String,
|
||||||
is_cpp: bool,
|
is_cpp: bool,
|
||||||
linking: bool,
|
linking: bool,
|
||||||
|
shared: bool,
|
||||||
x_set: bool,
|
x_set: bool,
|
||||||
bit_mode: u32,
|
bit_mode: u32,
|
||||||
need_libafl_arg: bool,
|
need_libafl_arg: bool,
|
||||||
@ -82,6 +83,7 @@ pub struct ClangWrapper {
|
|||||||
|
|
||||||
#[allow(clippy::match_same_arms)] // for the linking = false wip for "shared"
|
#[allow(clippy::match_same_arms)] // for the linking = false wip for "shared"
|
||||||
impl CompilerWrapper for ClangWrapper {
|
impl CompilerWrapper for ClangWrapper {
|
||||||
|
#[allow(clippy::too_many_lines)]
|
||||||
fn parse_args<S>(&mut self, args: &[S]) -> Result<&'_ mut Self, Error>
|
fn parse_args<S>(&mut self, args: &[S]) -> Result<&'_ mut Self, Error>
|
||||||
where
|
where
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
@ -115,45 +117,64 @@ impl CompilerWrapper for ClangWrapper {
|
|||||||
// new_args.push("-fsanitize-coverage=trace-pc-guard".into());
|
// new_args.push("-fsanitize-coverage=trace-pc-guard".into());
|
||||||
|
|
||||||
let mut linking = true;
|
let mut linking = true;
|
||||||
|
let mut shared = false;
|
||||||
// Detect stray -v calls from ./configure scripts.
|
// Detect stray -v calls from ./configure scripts.
|
||||||
if args.len() > 1 && args[1].as_ref() == "-v" {
|
if args.len() > 1 && args[1].as_ref() == "-v" {
|
||||||
linking = false;
|
linking = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut suppress_linking = 0;
|
let mut suppress_linking = 0;
|
||||||
for arg in &args[1..] {
|
let mut i = 1;
|
||||||
match arg.as_ref() {
|
while i < args.len() {
|
||||||
|
match args[i].as_ref() {
|
||||||
"--libafl-no-link" => {
|
"--libafl-no-link" => {
|
||||||
suppress_linking += 1;
|
suppress_linking += 1;
|
||||||
self.has_libafl_arg = true;
|
self.has_libafl_arg = true;
|
||||||
|
i += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
"--libafl" => {
|
"--libafl" => {
|
||||||
suppress_linking += 1337;
|
suppress_linking += 1337;
|
||||||
self.has_libafl_arg = true;
|
self.has_libafl_arg = true;
|
||||||
|
i += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
"-fsanitize=fuzzer-no-link" => {
|
"-fsanitize=fuzzer-no-link" => {
|
||||||
suppress_linking += 1;
|
suppress_linking += 1;
|
||||||
self.has_libafl_arg = true;
|
self.has_libafl_arg = true;
|
||||||
|
i += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
"-fsanitize=fuzzer" => {
|
"-fsanitize=fuzzer" => {
|
||||||
suppress_linking += 1337;
|
suppress_linking += 1337;
|
||||||
self.has_libafl_arg = true;
|
self.has_libafl_arg = true;
|
||||||
|
i += 1;
|
||||||
continue;
|
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,
|
"-x" => self.x_set = true,
|
||||||
"-m32" => self.bit_mode = 32,
|
"-m32" => self.bit_mode = 32,
|
||||||
"-m64" => self.bit_mode = 64,
|
"-m64" => self.bit_mode = 64,
|
||||||
"-c" | "-S" | "-E" => linking = false,
|
"-c" | "-S" | "-E" => linking = false,
|
||||||
"-shared" => linking = false, // TODO dynamic list?
|
"-shared" => {
|
||||||
"-Wl,-z,defs" | "-Wl,--no-undefined" | "--no-undefined" => continue,
|
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;
|
linking = false;
|
||||||
new_args.push(
|
new_args.push(
|
||||||
PathBuf::from(env!("OUT_DIR"))
|
PathBuf::from(env!("OUT_DIR"))
|
||||||
@ -165,6 +186,7 @@ impl CompilerWrapper for ClangWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.linking = linking;
|
self.linking = linking;
|
||||||
|
self.shared = shared;
|
||||||
|
|
||||||
if self.optimize {
|
if self.optimize {
|
||||||
new_args.push("-g".into());
|
new_args.push("-g".into());
|
||||||
@ -189,7 +211,7 @@ impl CompilerWrapper for ClangWrapper {
|
|||||||
}
|
}
|
||||||
// MacOS has odd linker behavior sometimes
|
// MacOS has odd linker behavior sometimes
|
||||||
#[cfg(target_vendor = "apple")]
|
#[cfg(target_vendor = "apple")]
|
||||||
if linking {
|
if linking || shared {
|
||||||
new_args.push("-undefined".into());
|
new_args.push("-undefined".into());
|
||||||
new_args.push("dynamic_lookup".into());
|
new_args.push("dynamic_lookup".into());
|
||||||
}
|
}
|
||||||
@ -321,6 +343,7 @@ impl ClangWrapper {
|
|||||||
name: "".into(),
|
name: "".into(),
|
||||||
is_cpp: false,
|
is_cpp: false,
|
||||||
linking: false,
|
linking: false,
|
||||||
|
shared: false,
|
||||||
x_set: false,
|
x_set: false,
|
||||||
bit_mode: 0,
|
bit_mode: 0,
|
||||||
need_libafl_arg: false,
|
need_libafl_arg: false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user