Skip to content

Instantly share code, notes, and snippets.

@cstorey
Last active September 29, 2017 23:40
Show Gist options
  • Save cstorey/8ee3b1353237f13d63c73ffbb1a12d83 to your computer and use it in GitHub Desktop.
Save cstorey/8ee3b1353237f13d63c73ffbb1a12d83 to your computer and use it in GitHub Desktop.
use std::ptr;
/*
Experiments in recovering coverage from the sanitizer runtime. Build with cargo, adding:
RUSTFLAGS="-C passes=sancov -C llvm-args=-sanitizer-coverage-level=3 -Z sanitizer=address -C opt-level=3 -C debug-assertions=on -C debuginfo=2"
*/
extern "C" {
pub fn __sanitizer_cov_init();
pub fn __sanitizer_cov_dump();
pub fn __sanitizer_dump_coverage(pcs: *const usize, len: usize);
pub fn __sanitizer_maybe_open_cov_file(name:
*const ::std::os::raw::c_char)
-> isize;
pub fn __sanitizer_get_total_unique_coverage() -> usize;
pub fn __sanitizer_get_total_unique_caller_callee_pairs() -> usize;
pub fn __sanitizer_reset_coverage();
pub fn __sanitizer_get_coverage_guards(data: *mut *mut usize) -> usize;
pub fn __sanitizer_get_number_of_counters() -> usize;
pub fn __sanitizer_update_counter_bitset_and_clear_counters(bitset: *mut u8) -> usize;
}
#[inline(never)]
fn hi() {
println!("Hi!")
}
fn dump() {
let unique = unsafe { __sanitizer_get_total_unique_coverage() };
let pairs = unsafe { __sanitizer_get_total_unique_caller_callee_pairs() };
println!("Coverage: unique:{}; indirect pairs: {}", unique, pairs);
// let mut guards : *mut usize = ptr::null_mut();
// let n_guards = unsafe { __sanitizer_get_coverage_guards(&mut guards) };
// let guards_vec = (0..n_guards).map(|i| -unsafe { *guards.offset(i as isize) } ).collect::<Vec<_>>();
// println!("{:?}",guards_vec);
unsafe { __sanitizer_reset_coverage() }
}
fn main() {
dump();
use std::env::args;
for _ in args() {
hi()
}
dump()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment