-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from antithesishq/code-review
Initial code review
- Loading branch information
Showing
8 changed files
with
90 additions
and
138 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,54 @@ | ||
use libc::c_char; | ||
use libloading::{Library, Symbol}; | ||
use libc::{c_char, size_t}; | ||
use libloading::{Library}; | ||
use serde_json::{Value}; | ||
use std::ffi::{CString}; | ||
use std::io::{Error}; | ||
use std::sync::{Once, Mutex, Arc}; | ||
|
||
use crate::internal::{LibHandler}; | ||
|
||
static LIB_NAME: &str = "/usr/lib/libmockstar.so"; | ||
const LIB_NAME: &str = "/usr/lib/libmockstar.so"; | ||
|
||
pub fn has_voidstar() -> bool { | ||
load_voidstar(); | ||
LIB_VOIDSTAR.lock().unwrap().is_some() | ||
} | ||
|
||
static LIB_VOIDSTAR: Mutex<Option<Arc<Library>>> = Mutex::new(None); | ||
|
||
fn load_voidstar() { | ||
static LOAD_VOIDSTAR: Once = Once::new(); | ||
LOAD_VOIDSTAR.call_once(|| { | ||
let result = unsafe { | ||
Library::new(LIB_NAME) | ||
}; | ||
let mut lib_voidstar = LIB_VOIDSTAR.lock().unwrap(); | ||
*lib_voidstar = result.ok().map(Arc::new); | ||
}); | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct VoidstarHandler { | ||
voidstar_lib: Arc<Library>, | ||
// Not used directly but exists to ensure the library is loaded | ||
// and all the following function pointers points to valid memory. | ||
_lib: Library, | ||
// SAFETY: The memory pointed by `s` must be valid up to `l` bytes. | ||
fuzz_json_data: unsafe fn(s: *const c_char, l: size_t), | ||
fuzz_get_random: fn() -> u64, | ||
} | ||
|
||
impl VoidstarHandler { | ||
pub fn new() -> Self { | ||
load_voidstar(); | ||
let lib = LIB_VOIDSTAR.lock().unwrap().as_ref().unwrap().clone(); | ||
VoidstarHandler{ | ||
voidstar_lib: lib, | ||
pub fn try_load() -> Result<Self, libloading::Error> { | ||
// SAFETY: | ||
// - The `libvoidstar`/`libmockstar `libraries that we intended to load | ||
// should not have initalization procedures that requires special arrangments at loading time. | ||
// Otherwise, loading an arbitrary library that happens to be at `LIB_NAME` is an unsupported case. | ||
// - Similarly, we load symbols by names and assume they have the expected signatures, | ||
// and loading arbitrary symbols that happen to take those names are unsupported. | ||
// - `fuzz_json_data` and `fuzz_get_random` copy the function pointers, | ||
// but they would be valid as we bind their lifetime to the library they are from | ||
// by storing all of them in the `VoidstarHandler` struct. | ||
unsafe { | ||
let lib = Library::new(LIB_NAME)?; | ||
let fuzz_json_data = *lib.get(b"fuzz_json_data\0")?; | ||
let fuzz_get_random = *lib.get(b"fuzz_get_random\0")?; | ||
Ok(VoidstarHandler { _lib: lib, fuzz_json_data, fuzz_get_random }) | ||
} | ||
} | ||
} | ||
|
||
impl LibHandler for VoidstarHandler { | ||
fn output(&mut self, value: &Value) -> Result<(), Error> { | ||
fn output(&self, value: &Value) -> Result<(), Error> { | ||
let payload = value.to_string(); | ||
// SAFETY: The data pointer and length passed into `fuzz_json_data` points to valid memory | ||
// that we just initialized above. | ||
unsafe { | ||
let json_data_func: Symbol<unsafe extern fn(s: *const c_char)> = self.voidstar_lib.get(b"fuzz_json_data").unwrap(); | ||
let payload = CString::new(value.to_string())?; | ||
json_data_func(payload.as_ptr()); | ||
(self.fuzz_json_data)(payload.as_bytes().as_ptr() as *const c_char, payload.len()); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn random(&self) -> u64 { | ||
unsafe { | ||
let get_random_func: Symbol<unsafe extern fn() -> u64> = self.voidstar_lib.get(b"fuzz_get_random").unwrap(); | ||
get_random_func() | ||
} | ||
(self.fuzz_get_random)() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
#[macro_use] | ||
extern crate lazy_static; | ||
|
||
pub mod assert; | ||
pub mod lifecycle; | ||
pub mod random; | ||
|
Oops, something went wrong.