Skip to content

Commit

Permalink
[libcasr] Add JS stacktrace parsing (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaDarochek authored Oct 21, 2023
1 parent 0ac3deb commit f654bcd
Show file tree
Hide file tree
Showing 9 changed files with 468 additions and 8 deletions.
13 changes: 11 additions & 2 deletions libcasr/fuzz/fuzz_targets/parse_stacktrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libcasr::{
init_ignored_frames,
java::JavaStacktrace,
python::PythonStacktrace,
js::JsStacktrace,
stacktrace::{CrashLineExt, Filter, ParseStacktrace, Stacktrace},
};

Expand All @@ -17,8 +18,8 @@ fuzz_target!(|data: &[u8]| {
return;
}
let s = String::from_utf8_lossy(&data[1..]);
init_ignored_frames!("cpp", "rust", "python", "go", "java");
match data[0] % 5 {
init_ignored_frames!("cpp", "rust", "python", "go", "java", "js");
match data[0] % 6 {
0 => {
// Asan
if let Ok(raw) = AsanStacktrace::extract_stacktrace(&s) {
Expand Down Expand Up @@ -51,6 +52,14 @@ fuzz_target!(|data: &[u8]| {
}
}
}
4 => {
// JS
if let Ok(raw) = JsStacktrace::extract_stacktrace(&s) {
if let Ok(st) = JsStacktrace::parse_stacktrace(&raw) {
let _ = st.crash_line();
}
}
}
_ => {
// Gdb
if let Ok(raw) = GdbStacktrace::extract_stacktrace(&s) {
Expand Down
2 changes: 1 addition & 1 deletion libcasr/fuzz/init_corpus/gdb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
 #0 0x00000000005e3099 in xlnt::detail::compound_document::read_directory (this=0x7fffffffcee0) at /xlnt/source/detail/cryptography/compound_document.cpp:975
 #0 0x00000000005e3099 in xlnt::detail::compound_document::read_directory (this=0x7fffffffcee0) at /xlnt/source/detail/cryptography/compound_document.cpp:975
#1 0x00000000005e2956 in xlnt::detail::compound_document::compound_document (this=0x7fffffffcee0, in=...) at /xlnt/source/detail/cryptography/compound_document.cpp:517
#2 0x000000000048a45c in (anonymous namespace)::decrypt_xlsx (bytes=std::vector of length 18655, capacity 32768 = {...}, password=u\"VelvetSweatshop\") at /xlnt/source/detail/cryptography/xlsx_crypto_consumer.cpp:320
#3 0x000000000048a2d9 in xlnt::detail::decrypt_xlsx (data=std::vector of length 18655, capacity 32768 = {...}, password=) at /xlnt/source/detail/cryptography/xlsx_crypto_consumer.cpp:339
Expand Down
13 changes: 13 additions & 0 deletions libcasr/fuzz/init_corpus/js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Error: 1
at eval (eval at <anonymous> (eval at g (/fuzz/FuzzTarget_jazzer.js:7:7)), <anonymous>:4:23)
at eval (eval at g (/fuzz/FuzzTarget_jazzer.js:7:7), <anonymous>:8:13)
at g (/fuzz/FuzzTarget_jazzer.js:14:13)
at fuzz (/fuzz/FuzzTarget_jazzer.js:29:9)
at module.exports.fuzz (/fuzz/FuzzTarget_jazzer.js:51:5)
at result (/fuzz/node_modules/@jazzer.js/core/core.ts:335:15)
Error: 2
at eval (eval at g (/fuzz/FuzzTarget_jazzer.js:7:7), <anonymous>:9:54)
at g (/fuzz/FuzzTarget_jazzer.js:14:13)
at fuzz (/fuzz/FuzzTarget_jazzer.js:29:9)
at module.exports.fuzz (/fuzz/FuzzTarget_jazzer.js:51:5)
at result (/fuzz/node_modules/@jazzer.js/core/core.ts:335:15)
18 changes: 18 additions & 0 deletions libcasr/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub const STACK_FRAME_FUNCTION_IGNORE_REGEXES_JAVA: &[&str] = &[
r"^javax\.",
];

/// Regular expressions for JS functions to be ignored.
pub const STACK_FRAME_FUNCTION_IGNORE_REGEXES_JS: &[&str] = &[
// TODO
r"^<anonymous>$",
];

/// Regular expressions for python functions to be ignored.
pub const STACK_FRAME_FUNCTION_IGNORE_REGEXES_PYTHON: &[&str] = &[
// TODO
Expand Down Expand Up @@ -228,6 +234,18 @@ pub const STACK_FRAME_FILEPATH_IGNORE_REGEXES_JAVA: &[&str] = &[
r"^[^.]$",
];

/// Regular expressions for paths to JS files that should be ignored.
pub const STACK_FRAME_FILEPATH_IGNORE_REGEXES_JS: &[&str] = &[
// TODO
// Anonymous functions
r"^<anonymous>$",
// Native locations (within V8’s libraries)
r"^native$",
// JS internal modules
r"^(|node:)internal/?",
r"^(|node:)events/?",
];

/// Regular expressions for paths to python files that should be ignored.
pub const STACK_FRAME_FILEPATH_IGNORE_REGEXES_PYTHON: &[&str] = &[
// TODO
Expand Down
4 changes: 2 additions & 2 deletions libcasr/src/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ParseStacktrace for JavaStacktrace {
conv_counter: usize,
}
// Get java stack trace.
let re = Regex::new(r"(?m)^(?:Caused by:|Exception in thread|== Java Exception:)(?:.|\n)*?((?:\n(?:\s|\t)+at .*\(.*\))+)(?:\n(?:\s|\t)+\.\.\. (\d+) more)?").unwrap();
let re = Regex::new(r"(?m)^(?:Caused by:|Exception in thread|== Java Exception:)(?:.|\n)*?((?:\n\s+at .*\(.*\))+)(?:\n\s+\.\.\. (\d+) more)?").unwrap();
let mut blocks = Vec::new();
for cap in re.captures_iter(stream) {
let body: Vec<&'_ str> = cap
Expand Down Expand Up @@ -66,7 +66,7 @@ impl ParseStacktrace for JavaStacktrace {
}

fn parse_stacktrace_entry(entry: &str) -> Result<StacktraceEntry> {
let re = Regex::new(r"(?:\s|\t)*at (.*)\((.*)\)").unwrap();
let re = Regex::new(r"\s*at (.*)\((.*)\)").unwrap();

let Some(cap) = re.captures(entry) else {
return Err(Error::Casr(format!(
Expand Down
Loading

0 comments on commit f654bcd

Please sign in to comment.