Skip to content

Commit 3b4793f

Browse files
committed
add type observer
1 parent 2a8e042 commit 3b4793f

File tree

6 files changed

+779
-422
lines changed

6 files changed

+779
-422
lines changed

libafl_v8/src/executors.rs

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
fmt::{Debug, Formatter},
99
marker::PhantomData,
1010
};
11-
use std::{iter, sync::Arc};
11+
use std::sync::Arc;
1212

1313
use deno_core::{v8, ModuleId, ModuleSpecifier};
1414
use deno_runtime::worker::MainWorker;
@@ -20,7 +20,7 @@ use libafl::{
2020
Error,
2121
};
2222
use tokio::runtime::Runtime;
23-
use v8::{Function, HandleScope, Local, TryCatch};
23+
use v8::{Function, Local, TryCatch};
2424

2525
use crate::{values::IntoJSValue, Mutex};
2626

@@ -118,6 +118,11 @@ where
118118
res
119119
})
120120
}
121+
122+
/// Fetches the ID of the main module for hooking
123+
pub fn main_module_id(&self) -> ModuleId {
124+
self.id
125+
}
121126
}
122127

123128
impl<'rt, EM, I, OT, S, Z> Executor<EM, I, S, Z> for V8Executor<'rt, EM, I, OT, S, Z>
@@ -164,66 +169,3 @@ where
164169
.finish_non_exhaustive()
165170
}
166171
}
167-
168-
#[allow(dead_code)]
169-
fn js_err_to_libafl(scope: &mut TryCatch<HandleScope>) -> Option<Error> {
170-
if !scope.has_caught() {
171-
None
172-
} else {
173-
let exception = scope.exception().unwrap();
174-
let exception_string = exception
175-
.to_string(scope)
176-
.unwrap()
177-
.to_rust_string_lossy(scope);
178-
let message = if let Some(message) = scope.message() {
179-
message
180-
} else {
181-
return Some(Error::illegal_state(format!(
182-
"Provided script threw an error while executing: {}",
183-
exception_string
184-
)));
185-
};
186-
187-
let filename = message.get_script_resource_name(scope).map_or_else(
188-
|| "(unknown)".into(),
189-
|s| s.to_string(scope).unwrap().to_rust_string_lossy(scope),
190-
);
191-
let line_number = message.get_line_number(scope).unwrap_or_default();
192-
193-
let source_line = message
194-
.get_source_line(scope)
195-
.map(|s| s.to_string(scope).unwrap().to_rust_string_lossy(scope))
196-
.unwrap();
197-
198-
let start_column = message.get_start_column();
199-
let end_column = message.get_end_column();
200-
201-
let err_underline = iter::repeat(' ')
202-
.take(start_column)
203-
.chain(iter::repeat('^').take(end_column - start_column))
204-
.collect::<String>();
205-
206-
if let Some(stack_trace) = scope.stack_trace() {
207-
let stack_trace = unsafe { Local::<v8::String>::cast(stack_trace) };
208-
let stack_trace = stack_trace
209-
.to_string(scope)
210-
.map(|s| s.to_rust_string_lossy(scope));
211-
212-
if let Some(stack_trace) = stack_trace {
213-
return Some(Error::illegal_state(format!(
214-
"Encountered uncaught JS exception while executing: {}:{}: {}\n{}\n{}\n{}",
215-
filename,
216-
line_number,
217-
exception_string,
218-
source_line,
219-
err_underline,
220-
stack_trace
221-
)));
222-
}
223-
}
224-
Some(Error::illegal_state(format!(
225-
"Encountered uncaught JS exception while executing: {}:{}: {}\n{}\n{}",
226-
filename, line_number, exception_string, source_line, err_underline
227-
)))
228-
}
229-
}

libafl_v8/src/lib.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,84 @@ pub mod loader;
7777
pub mod observers;
7878
pub mod values;
7979

80+
use std::iter;
81+
8082
pub use deno_core::{self, v8};
8183
pub use deno_runtime;
8284
pub use executors::*;
85+
use libafl::Error;
8386
pub use loader::*;
8487
pub use observers::*;
8588
pub use tokio::{runtime, sync::Mutex};
8689
pub use values::*;
8790

91+
use crate::v8::{HandleScope, Local, TryCatch};
92+
8893
pub(crate) fn forbid_deserialization<T>() -> T {
8994
unimplemented!(
9095
"Deserialization is forbidden for this type; cannot cross a serialization boundary"
9196
)
9297
}
98+
99+
/// Convert a JS error from a try/catch scope into a libafl error
100+
pub fn js_err_to_libafl(scope: &mut TryCatch<HandleScope>) -> Option<Error> {
101+
if !scope.has_caught() {
102+
None
103+
} else {
104+
let exception = scope.exception().unwrap();
105+
let exception_string = exception
106+
.to_string(scope)
107+
.unwrap()
108+
.to_rust_string_lossy(scope);
109+
let message = if let Some(message) = scope.message() {
110+
message
111+
} else {
112+
return Some(Error::illegal_state(format!(
113+
"Provided script threw an error while executing: {}",
114+
exception_string
115+
)));
116+
};
117+
118+
let filename = message.get_script_resource_name(scope).map_or_else(
119+
|| "(unknown)".into(),
120+
|s| s.to_string(scope).unwrap().to_rust_string_lossy(scope),
121+
);
122+
let line_number = message.get_line_number(scope).unwrap_or_default();
123+
124+
let source_line = message
125+
.get_source_line(scope)
126+
.map(|s| s.to_string(scope).unwrap().to_rust_string_lossy(scope))
127+
.unwrap();
128+
129+
let start_column = message.get_start_column();
130+
let end_column = message.get_end_column();
131+
132+
let err_underline = iter::repeat(' ')
133+
.take(start_column)
134+
.chain(iter::repeat('^').take(end_column - start_column))
135+
.collect::<String>();
136+
137+
if let Some(stack_trace) = scope.stack_trace() {
138+
let stack_trace = unsafe { Local::<v8::String>::cast(stack_trace) };
139+
let stack_trace = stack_trace
140+
.to_string(scope)
141+
.map(|s| s.to_rust_string_lossy(scope));
142+
143+
if let Some(stack_trace) = stack_trace {
144+
return Some(Error::illegal_state(format!(
145+
"Encountered uncaught JS exception while executing: {}:{}: {}\n{}\n{}\n{}",
146+
filename,
147+
line_number,
148+
exception_string,
149+
source_line,
150+
err_underline,
151+
stack_trace
152+
)));
153+
}
154+
}
155+
Some(Error::illegal_state(format!(
156+
"Encountered uncaught JS exception while executing: {}:{}: {}\n{}\n{}",
157+
filename, line_number, exception_string, source_line, err_underline
158+
)))
159+
}
160+
}

0 commit comments

Comments
 (0)