clang wrapper extend api

This commit is contained in:
Andrea Fioraldi 2021-03-23 14:55:55 +01:00
parent 0a3b9f1a96
commit 1d2897442b
2 changed files with 57 additions and 37 deletions

View File

@ -9,13 +9,30 @@ pub enum Error {
/// Wrap a compiler hijacking its arguments
pub trait CompilerWrapper {
/// Set the wrapper arguments parsing a command line set of arguments
fn from_args<'a>(&'a mut self, args: Vec<String>) -> Result<&'a mut Self, Error>;
fn from_args<'a>(&'a mut self, args: &[String]) -> Result<&'a mut Self, Error>;
/// Add a compiler argument
fn add_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
/// Add a compiler argument only when compiling
fn add_cc_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
/// Add a compiler argument only when linking
fn add_link_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error>;
/// Command to run the compiler
fn command(&mut self) -> Result<Vec<String>, Error>;
/// Get if in linking mode
fn is_linking(&self) -> bool;
/// Run the compiler
fn compile(&mut self) -> Result<(), Error>;
fn run(&mut self) -> Result<(), Error> {
// TODO subproc
let args = self.command()?;
println!("{:?}", args);
Ok(())
}
}
/// Wrap Clang
@ -30,11 +47,13 @@ pub struct ClangWrapper {
x_set: bool,
bit_mode: u32,
args: Vec<String>,
base_args: Vec<String>,
cc_args: Vec<String>,
link_args: Vec<String>,
}
impl CompilerWrapper for ClangWrapper {
fn from_args<'a>(&'a mut self, args: Vec<String>) -> Result<&'a mut Self, Error> {
fn from_args<'a>(&'a mut self, args: &[String]) -> Result<&'a mut Self, Error> {
let mut new_args = vec![];
if args.len() < 1 {
return Err(Error::InvalidArguments(
@ -83,26 +102,43 @@ impl CompilerWrapper for ClangWrapper {
// Fuzzing define common among tools
new_args.push("-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION=1".into());
self.args = new_args;
self.base_args = new_args;
Ok(self)
}
fn add_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
self.args.push(arg);
self.base_args.push(arg);
Ok(self)
}
fn compile(&mut self) -> Result<(), Error> {
fn add_cc_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
self.cc_args.push(arg);
Ok(self)
}
fn add_link_arg<'a>(&'a mut self, arg: String) -> Result<&'a mut Self, Error> {
self.link_args.push(arg);
Ok(self)
}
fn command(&mut self) -> Result<Vec<String>, Error> {
let mut args = self.base_args.clone();
if self.linking {
if self.x_set {
self.args.push("-x".into());
self.args.push("none".into());
}
args.push("-x".into());
args.push("none".into());
}
println!("{:?}", self.args);
args.extend_from_slice(self.link_args.as_slice());
} else {
args.extend_from_slice(self.cc_args.as_slice());
}
Ok(())
Ok(args)
}
fn is_linking(&self) -> bool {
self.linking
}
}
@ -117,9 +153,16 @@ impl ClangWrapper {
linking: false,
x_set: false,
bit_mode: 0,
args: vec![],
base_args: vec![],
cc_args: vec![],
link_args: vec![],
}
}
pub fn dont_optimize<'a>(&'a mut self) -> &'a mut Self {
self.optimize = false;
self
}
}
#[cfg(test)]
@ -129,9 +172,9 @@ mod tests {
#[test]
fn test_clang_version() {
ClangWrapper::new("clang", "clang++")
.from_args(vec!["my-clang".into(), "-v".into()])
.from_args(&["my-clang".into(), "-v".into()])
.unwrap()
.compile()
.run()
.unwrap();
}
}

View File

@ -1,23 +0,0 @@
pub const MAP_SIZE: usize = 65536;
pub static mut EDGES_MAP: [u8; MAP_SIZE] = [0; MAP_SIZE];
pub static mut CMP_MAP: [u8; MAP_SIZE] = [0; MAP_SIZE];
pub static mut MAX_EDGES_NUM: usize = 0;
#[no_mangle]
pub unsafe extern "C" fn __sanitizer_cov_trace_pc_guard(guard: *mut u32) {
let pos = *guard as usize;
let val = (EDGES_MAP[pos] as u8).wrapping_add(1);
EDGES_MAP[pos] = val;
}
#[no_mangle]
pub unsafe extern "C" fn __sanitizer_cov_trace_pc_guard_init(mut start: *mut u32, stop: *mut u32) {
if start == stop || *start != 0 { return }
while start < stop {
MAX_EDGES_NUM += 1;
*start = (MAX_EDGES_NUM & (MAP_SIZE -1)) as u32;
start = start.offset(1);
}
}