-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy patherror.rs
More file actions
312 lines (272 loc) · 10.1 KB
/
Copy patherror.rs
File metadata and controls
312 lines (272 loc) · 10.1 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Contains all error types that can be returned by this crate.
use std::alloc;
use std::error;
use std::fmt;
use std::io;
/// Convenient `Result` type for custom errors.
pub type Result<T> = std::result::Result<T, Error>;
// -----------------------------------------------------------------------------------------------
// Errors - General
// -----------------------------------------------------------------------------------------------
/// Main error structure which is just a simple wrapper for all errors that can be returned by the
/// fuzzer.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
/// Core-related errors.
Core(CoreError),
/// Crash-related errors.
Crash(CrashError),
/// Exception-related errors.
Exception(ExceptionError),
/// Hook-related errors.
Hook(HookError),
/// Hypervisor-related errors.
Hypervisor(applevisor::HypervisorError),
/// Loader-related errors.
Loader(LoaderError),
/// Memory-related errors.
Memory(MemoryError),
/// Generic user-defined errors.
Generic(String),
}
impl error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Core(e) => write!(f, "[Core error] {}", e),
Error::Crash(e) => write!(f, "[Crash error] {}", e),
Error::Exception(e) => write!(f, "[Exception error] {}", e),
Error::Hook(e) => write!(f, "[Hook error] {}", e),
Error::Loader(e) => write!(f, "[Loader error] {}", e),
Error::Memory(e) => write!(f, "[Memory error] {}", e),
Error::Hypervisor(e) => write!(f, "[Hypervisor error] {}", e),
Error::Generic(e) => write!(f, "[Error] {}", e),
}
}
}
impl From<CoreError> for Error {
fn from(error: CoreError) -> Self {
Error::Core(error)
}
}
impl From<CrashError> for Error {
fn from(error: CrashError) -> Self {
Error::Crash(error)
}
}
impl From<ExceptionError> for Error {
fn from(error: ExceptionError) -> Self {
Error::Exception(error)
}
}
impl From<HookError> for Error {
fn from(error: HookError) -> Self {
Error::Hook(error)
}
}
impl From<LoaderError> for Error {
fn from(error: LoaderError) -> Self {
Error::Loader(error)
}
}
impl From<MemoryError> for Error {
fn from(error: MemoryError) -> Self {
Error::Memory(error)
}
}
impl From<applevisor::HypervisorError> for Error {
fn from(error: applevisor::HypervisorError) -> Self {
Error::Hypervisor(error)
}
}
impl From<alloc::LayoutError> for Error {
fn from(error: alloc::LayoutError) -> Self {
Error::Memory(MemoryError::LayoutError(error))
}
}
impl From<std::fmt::Error> for Error {
fn from(error: std::fmt::Error) -> Self {
Error::Crash(CrashError::FmtError(error))
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Core(CoreError::IoError(format!("{}", error)))
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Core
// -----------------------------------------------------------------------------------------------
/// Core-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CoreError {
InvalidConfiguration,
/// The corpus at the specified path is empty.
EmptyCorpus(String),
/// Corpus testcase generated a crash.
CorpusCrash(std::path::PathBuf),
/// The testcase provided is invalid.
InvalidTestcase,
/// An I/O error occurred while processing the corpus.
IoError(String),
/// Too many workers are trying to be spawned.
TooManyWorkers(u32),
/// User-defined core error.
Generic(String),
}
impl error::Error for CoreError {}
impl fmt::Display for CoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CoreError::InvalidConfiguration => write!(f, "invalid configuration type"),
CoreError::EmptyCorpus(e) => write!(f, "corpus at {} is empty", e),
CoreError::CorpusCrash(p) => {
write!(f, "a corpus element crashed the fuzzer: {}", p.display())
}
CoreError::InvalidTestcase => write!(f, "testcase is invalid"),
CoreError::IoError(e) => write!(f, "{}", e),
CoreError::TooManyWorkers(n) => write!(f, "maximum number of workers reached ({})", n),
CoreError::Generic(e) => write!(f, "{}", e),
}
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Crash
// -----------------------------------------------------------------------------------------------
/// Crash-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CrashError {
/// A format error occurred.
FmtError(std::fmt::Error),
/// User-defined core error.
Generic(String),
}
impl error::Error for CrashError {}
impl fmt::Display for CrashError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CrashError::FmtError(e) => write!(f, "{}", e),
CrashError::Generic(e) => write!(f, "{}", e),
}
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Exception
// -----------------------------------------------------------------------------------------------
/// Exception-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExceptionError {
/// The exception type is not implemented.
UnimplementedException(u64),
/// User-defined exception error.
Generic(String),
}
impl error::Error for ExceptionError {}
impl fmt::Display for ExceptionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ExceptionError::UnimplementedException(e) => {
write!(f, "unimplemented exception ({:?})", e)
}
ExceptionError::Generic(e) => write!(f, "{}", e),
}
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Hook
// -----------------------------------------------------------------------------------------------
/// Hook-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HookError {
/// The hook already exists at this address.
HookAlreadyExists(u64),
/// The hook type is invalid.
InvalidHookType(u16),
/// There is no hook at the given address.
UnknownHook(u64),
/// User-defined hook error.
Generic(String),
}
impl error::Error for HookError {}
impl fmt::Display for HookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
HookError::HookAlreadyExists(a) => write!(f, "hook already exists ({:#x})", a),
HookError::InvalidHookType(t) => write!(f, "invalid hook type ({:#x})", t),
HookError::UnknownHook(a) => write!(f, "unknown hook ({:#x})", a),
HookError::Generic(e) => write!(f, "{}", e),
}
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Loader
// -----------------------------------------------------------------------------------------------
/// Loader-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LoaderError {
/// The symbol is unknown.
UnknownSymbol(String),
/// User-defined loader error.
Generic(String),
}
impl error::Error for LoaderError {}
impl fmt::Display for LoaderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LoaderError::UnknownSymbol(s) => write!(f, "unknown symbol: {}", s),
LoaderError::Generic(e) => write!(f, "{}", e),
}
}
}
// -----------------------------------------------------------------------------------------------
// Errors - Memory
// -----------------------------------------------------------------------------------------------
/// Memory-related errors.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MemoryError {
/// The address we're trying to map already exists in the page table.
AlreadyMapped(u64),
/// The slab is an unexpected state.
CorruptedSlab,
/// The address is invalid.
InvalidAddress(u64),
/// The index is invalid.
InvalidIndex(usize),
/// The size is invalid.
InvalidSize(usize),
/// Wrapper for `alloc::LayoutError`.
LayoutError(alloc::LayoutError),
/// The allocator is out of memory.
OutOfMemory,
/// The operation between an address and a size resulted in an overflow.
Overflow(u64, usize),
/// The address is not aligned as expected.
UnalignedAddress(u64),
/// The size is not aligned as expected.
UnalignedSize(usize),
/// The address we're trying to access has not been allocated.
UnallocatedMemoryAccess(u64),
/// User-defined memory error.
Generic(String),
}
impl error::Error for MemoryError {}
impl fmt::Display for MemoryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MemoryError::AlreadyMapped(a) => write!(f, "address is already mapped: {:#x}", a),
MemoryError::CorruptedSlab => write!(f, "corrupted slab"),
MemoryError::InvalidAddress(a) => write!(f, "invalid address: {:#x}", a),
MemoryError::InvalidIndex(i) => write!(f, "invalid index: {:#x}", i),
MemoryError::InvalidSize(s) => write!(f, "invalid size: {:#x}", s),
MemoryError::LayoutError(e) => write!(f, "layout error: {}", e),
MemoryError::OutOfMemory => write!(f, "the allocator ran out of memory"),
MemoryError::Overflow(a, s) => write!(f, "an overflow occurred: {:#x}, {:#x}", a, s),
MemoryError::UnalignedAddress(a) => write!(f, "unaligned address: ({:#x})", a),
MemoryError::UnalignedSize(s) => write!(f, "unaligned size: ({:#x})", s),
MemoryError::UnallocatedMemoryAccess(x) => {
write!(f, "access to unallocated memory at address {:#x}", x)
}
MemoryError::Generic(e) => write!(f, "{}", e),
}
}
}