Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Nekrasov committed Feb 28, 2025
1 parent b8449da commit 1d20151
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 89 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Create report from AddressSanitizer output:

Create report from MemorySanitizer output:

$ clang++ -fsanitize=memory -O0 casr/tests/casr_tests/test_msan.cpp -o test_msan
$ clang++ -fsanitize=memory -O0 -g casr/tests/casr_tests/test_msan.cpp -o test_msan
$ casr-san -o msan.casrep -- ./test_msan

Create report from UndefinedBehaviorSanitizer output:
Expand Down
5 changes: 4 additions & 1 deletion casr/src/bin/casr-python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ fn main() -> Result<()> {
let python_stderr_list: Vec<String> =
python_stderr.split('\n').map(|l| l.to_string()).collect();

let re = Regex::new(r"==\d+==\s*ERROR: (LeakSanitizer|AddressSanitizer|libFuzzer):").unwrap();
let re = Regex::new(
r"==\d+==\s*(ERROR: (LeakSanitizer|AddressSanitizer|libFuzzer)|WARNING: MemorySanitizer): ",
)
.unwrap();
if python_stderr_list.iter().any(|line| re.is_match(line)) {
let python_stdout = String::from_utf8_lossy(&python_result.stdout);
let python_stdout_list: Vec<String> =
Expand Down
39 changes: 16 additions & 23 deletions casr/src/bin/casr-san.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ fn main() -> Result<()> {
.map(|l| l.trim_end().to_string())
.collect();
} else {
// Get ASAN report.
// Get ASAN or MSAN report.
let san_stderr_list: Vec<String> = sanitizers_stderr
.split('\n')
.map(|l| l.trim_end().to_string())
Expand All @@ -221,34 +221,27 @@ fn main() -> Result<()> {
let rmsan_start = Regex::new(r"==\d+==\s*WARNING: MemorySanitizer:").unwrap();
if let Some(report_start) = san_stderr_list
.iter()
.position(|line| rasan_start.is_match(line))
.position(|line| rasan_start.is_match(line) || rmsan_start.is_match(line))
{
// Set ASAN report in casr report.
let report_end = san_stderr_list.iter().rposition(|s| !s.is_empty()).unwrap() + 1;
report.asan_report = Vec::from(&san_stderr_list[report_start..report_end]);
let context = AsanContext(report.asan_report.clone());
let severity = context.severity();
if let Ok(severity) = severity {
report.execution_class = severity;
} else {
eprintln!("Couldn't estimate severity. {}", severity.err().unwrap());
let report_slice = &san_stderr_list[report_start..report_end];

match rasan_start.is_match(&san_stderr_list[report_start]) {
true => report.asan_report = report_slice.to_vec(),
false => report.msan_report = report_slice.to_vec(),
}
report.stacktrace = AsanStacktrace::extract_stacktrace(&report.asan_report.join("\n"))?;
} else if let Some(report_start) = san_stderr_list
.iter()
.position(|line| rmsan_start.is_match(line))
{
// Set MSAN report in casr report.
let report_end = san_stderr_list.iter().rposition(|s| !s.is_empty()).unwrap() + 1;
report.msan_report = Vec::from(&san_stderr_list[report_start..report_end]);
let context = AsanContext(report.msan_report.clone());
let severity = context.severity();
if let Ok(severity) = severity {

let context = AsanContext(report_slice.to_vec());
if let Ok(severity) = context.severity() {
report.execution_class = severity;
} else {
eprintln!("Couldn't estimate severity. {}", severity.err().unwrap());
eprintln!(
"Couldn't estimate severity. {}",
context.severity().err().unwrap()
);

Check warning on line 241 in casr/src/bin/casr-san.rs

View check run for this annotation

Codecov / codecov/patch

casr/src/bin/casr-san.rs#L238-L241

Added lines #L238 - L241 were not covered by tests
}
report.stacktrace = AsanStacktrace::extract_stacktrace(&report.msan_report.join("\n"))?;

report.stacktrace = AsanStacktrace::extract_stacktrace(&report_slice.join("\n"))?;
} else {
// Get termination signal.
if let Some(signal) = sanitizers_result.status.signal() {
Expand Down
131 changes: 67 additions & 64 deletions casr/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3079,70 +3079,6 @@ fn test_casr_san() {
} else {
panic!("Couldn't parse json report file.");
}

// Msan test
let paths = [
abs_path("tests/casr_tests/test_msan.cpp"),
abs_path("tests/tmp_tests_casr/test_msan"),
];

let clang = Command::new("bash")
.arg("-c")
.arg(format!(
"clang++ -fsanitize=memory -O0 {} -o {}",
&paths[0], &paths[1]
))
.status()
.expect("failed to execute clang++");

assert!(clang.success());

let output = Command::new(*EXE_CASR_SAN)
.args(["--stdout", "--", &paths[1]])
.output()
.expect("failed to start casr-san");

assert!(
output.status.success(),
"Stdout: {}\n. Stderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);

let report: Result<Value, _> = serde_json::from_slice(&output.stdout);
if let Ok(report) = report {
let severity_type = report["CrashSeverity"]["Type"].as_str().unwrap();
let severity_desc = report["CrashSeverity"]["ShortDescription"]
.as_str()
.unwrap()
.to_string();
let stacktrace = report["Stacktrace"]
.as_array()
.unwrap()
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();

assert!(stacktrace.len() > 2);
assert!(stacktrace[0].contains("in main"));
assert_eq!(severity_type, "NOT_EXPLOITABLE");
assert_eq!(severity_desc, "use-of-uninitialized-value");
assert!(
report["CrashLine"]
.as_str()
.unwrap()
.eq("tests/casr_tests/test_msan.cpp:12:9")
// We build a test on ubuntu18 and run it on ubuntu20.
// Debug information is broken.
|| report["CrashLine"]
.as_str()
.unwrap()
.contains("test_msan+0x") // We can't hardcode the offset because we rebuild tests every time.
);
} else {
panic!("Couldn't parse json report file.");
}
let _ = std::fs::remove_file(&paths[1]);
// Test casr-san stdin
let paths = [
abs_path("tests/casr_tests/test_asan_stdin.cpp"),
Expand Down Expand Up @@ -3300,6 +3236,73 @@ fn test_casr_san() {
panic!("Couldn't parse json report file.");
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_casr_san_msan() {
let paths = [
abs_path("tests/casr_tests/test_msan.cpp"),
abs_path("tests/tmp_tests_casr/test_msan"),
];

let clang = Command::new("bash")
.arg("-c")
.arg(format!(
"clang++ -fsanitize=memory -O0 {} -o {}",
&paths[0], &paths[1]
))
.status()
.expect("failed to execute clang++");

assert!(clang.success());

let output = Command::new(*EXE_CASR_SAN)
.args(["--stdout", "--", &paths[1]])
.output()
.expect("failed to start casr-san");

assert!(
output.status.success(),
"Stdout: {}\n. Stderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);

let report: Result<Value, _> = serde_json::from_slice(&output.stdout);
if let Ok(report) = report {
let severity_type = report["CrashSeverity"]["Type"].as_str().unwrap();
let severity_desc = report["CrashSeverity"]["ShortDescription"]
.as_str()
.unwrap()
.to_string();
let stacktrace = report["Stacktrace"]
.as_array()
.unwrap()
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();

assert!(stacktrace.len() > 2);
assert!(stacktrace[0].contains("in main"));
assert_eq!(severity_type, "NOT_EXPLOITABLE");
assert_eq!(severity_desc, "use-of-uninitialized-value");
assert!(
report["CrashLine"]
.as_str()
.unwrap()
.eq("tests/casr_tests/test_msan.cpp:12:9")
// We build a test on ubuntu18 and run it on ubuntu20.
// Debug information is broken.
|| report["CrashLine"]
.as_str()
.unwrap()
.contains("test_msan+0x") // We can't hardcode the offset because we rebuild tests every time.
);
} else {
panic!("Couldn't parse json report file.");
}
let _ = std::fs::remove_file(&paths[1]);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_casr_san_segf_near_null() {
Expand Down
1 change: 1 addition & 0 deletions libcasr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! It can analyze crashes from different sources:
//!
//! * AddressSanitizer
//! * MemorySanitizer
//! * UndefinedBehaviorSanitizer
//! * Gdb output
//!
Expand Down

0 comments on commit 1d20151

Please sign in to comment.