1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::internal_prelude::*;
use std::path::Path;
use std::sync::RwLock;
lazy_static::lazy_static! {
static ref WITNESS_LOCK: RwLock<bool> = RwLock::new(false);
}
pub struct InitializationGuard();
impl InitializationGuard {
fn new_init() -> InitializationGuard {
InitializationGuard()
}
}
impl Drop for InitializationGuard {
fn drop(&mut self) {
let mut guard = WITNESS_LOCK.write().expect("Mutex Poisoned");
*guard = false;
unsafe { syz_shutdown() };
}
}
pub fn initialize() -> Result<InitializationGuard> {
let mut guard = WITNESS_LOCK.write().expect("Mutex poisoned");
check_error(unsafe { syz_initialize() })?;
*guard = true;
Ok(InitializationGuard::new_init())
}
pub struct LibraryConfig {
config: syz_LibraryConfig,
libsndfile_path_string: Option<std::ffi::CString>,
}
#[repr(i32)]
pub enum LogLevel {
Error = SYZ_LOG_LEVEL_ERROR as i32,
Warn = SYZ_LOG_LEVEL_WARN as i32,
Info = SYZ_LOG_LEVEL_INFO as i32,
Debug = SYZ_LOG_LEVEL_DEBUG as i32,
}
impl LibraryConfig {
pub fn new() -> LibraryConfig {
let mut out = LibraryConfig {
config: Default::default(),
libsndfile_path_string: None,
};
unsafe { syz_libraryConfigSetDefaults(&mut out.config as *mut syz_LibraryConfig) };
out
}
pub fn log_level(&mut self, level: LogLevel) {
self.config.log_level = unsafe { std::mem::transmute(level) };
}
pub fn log_to_stderr(&mut self) {
self.config.logging_backend = SYZ_LOGGING_BACKEND_STDERR;
}
pub fn load_libsndfile<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
let c_str = std::ffi::CString::new(
path.as_ref()
.to_str()
.ok_or_else(|| Error::rust_error("Path isn't valid UTF-8"))?,
)
.map_err(|_| Error::rust_error("Unable to convert path to C string"))?;
self.config.libsndfile_path = c_str.as_ptr();
self.libsndfile_path_string = Some(c_str);
Ok(())
}
pub fn initialize(self) -> Result<InitializationGuard> {
let mut guard = WITNESS_LOCK.write().expect("Mutex poisoned");
check_error(unsafe { syz_initializeWithConfig(&self.config as *const syz_LibraryConfig) })?;
*guard = true;
Ok(InitializationGuard::new_init())
}
}
impl Default for LibraryConfig {
fn default() -> LibraryConfig {
LibraryConfig::new()
}
}
pub(crate) fn with_witness<T>(closure: impl FnOnce() -> Result<T>) -> Result<T> {
let guard = WITNESS_LOCK.read().expect("Mutex Poisoned");
if !*guard {
return Err(Error::rust_error("Synthizer is not initialized"));
}
closure()
}