Skip to content

Commit 2e21c12

Browse files
authored
Add tests to report.rs (#163)
1 parent 0faf10b commit 2e21c12

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

libcasr/src/report.rs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,197 @@ pub fn cluster_reports(casreps: &[CrashReport]) -> Result<Vec<u32>> {
774774

775775
cluster_stacktraces(&traces)
776776
}
777+
778+
#[cfg(test)]
779+
mod tests {
780+
use super::*;
781+
use crate::constants::*;
782+
use crate::init_ignored_frames;
783+
784+
#[test]
785+
fn test_report_display() {
786+
// TODO: Fill all empty fields
787+
let mut report = CrashReport::new();
788+
report.crashline =
789+
"/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:187:28"
790+
.to_string();
791+
report.date = "2023-09-29T15:31:39.818262+03:00".to_string();
792+
report.uname = "Linux astra-stand 5.15.0-84-generic #93-Ubuntu SMP Tue Sep 5 17:16:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux".to_string();
793+
report.os = "Ubuntu".to_string();
794+
report.os_release = "22.04".to_string();
795+
report.architecture = "amd64".to_string();
796+
report.executable_path =
797+
"/home/hkctkuy/github/casr/casr/tests/casr_tests/bin/load_fuzzer".to_string();
798+
report.proc_environ = vec![
799+
"CARGO=/home/hkctkuy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo"
800+
.to_string(),
801+
];
802+
report.proc_cmdline = "/home/hkctkuy/github/casr/casr/tests/casr_tests/bin/load_fuzzer /home/hkctkuy/github/casr/casr/tests/casr_tests/casrep/libfuzzer_crashes_xlnt/crash-49c4e6270849ab5c9c13f64b98f817efa840d8a3".to_string();
803+
report.stdin = "/home/hkctkuy/github/casr/casr/tests/casr_tests/casrep/afl-out-xlnt-small/afl_main-worker/crashes/id:000007,sig:00,sync:afl_s01-worker,src:000371".to_string();
804+
report.proc_status = vec!["process 2945397".to_string()];
805+
report.proc_maps = vec![
806+
" 0x555555554000 0x555555556000 0x2000 0x0 /usr/local/bin/tiff2pdf"
807+
.to_string(),
808+
];
809+
report.stacktrace = vec![
810+
" #0 0x4ca0e0 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_data() const /gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:187:28".to_string(),
811+
" #1 0x4ca0e0 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_is_local() const /gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:222:26".to_string(),
812+
];
813+
report.disassembly = vec!["==> 0x5e425d: mov eax, dword ptr [rax]".to_string()];
814+
report.asan_report = vec![
815+
"==363912==ERROR: AddressSanitizer: SEGV on unknown address 0xffffffffffffffe0 (pc 0x0000004ca0e0 bp 0x7fffffff9980 sp 0x7fffffff9928 T0)".to_string(),
816+
"==363912==The signal is caused by a READ memory access.".to_string(),
817+
];
818+
report.ubsan_report = vec![
819+
"/home/hkctkuy/github/casr/casr/tests/tmp_tests_casr/test_casr_ubsan/test_ubsan.cpp:4:29: runtime error: signed integer overflow: 65535 * 32769 cannot be represented in type 'int'".to_string(),
820+
"SUMMARY: UndefinedBehaviorSanitizer: signed-integer-overflow /home/hkctkuy/github/casr/casr/tests/tmp_tests_casr/test_casr_ubsan/test_ubsan.cpp:4:29 in".to_string(),
821+
];
822+
report.python_report = vec![
823+
" === Uncaught Python exception: ===".to_string(),
824+
"TypeError: unhashable type: 'list'".to_string(),
825+
];
826+
report.java_report = vec![
827+
"== Java Exception: java.lang.IndexOutOfBoundsException: start 59, end 22, length 60"
828+
.to_string(),
829+
"\tat com.code_intelligence.jazzer.Jazzer.main(Jazzer.java:73)".to_string(),
830+
];
831+
report.go_report = vec![
832+
"fatal error: runtime: out of memory".to_string(),
833+
"".to_string(),
834+
"runtime.throw({0x565860?, 0x200000?})".to_string(),
835+
];
836+
report.rust_report = vec![
837+
"Running: ./artifacts/fuzz_target_1/crash-da39a3ee5e6b4b0d3255bfef95601890afd80709"
838+
.to_string(),
839+
"thread '<unnamed>' panicked at fuzz_targets/fuzz_target_1.rs:6:9:".to_string(),
840+
"index out of bounds: the len is 0 but the index is 10".to_string(),
841+
"stack backtrace:".to_string(),
842+
];
843+
report.source = vec![
844+
"--->83 return utf16_to_utf8(std::u16string(name_array.begin(),"
845+
.to_string(),
846+
];
847+
report.execution_class = ExecutionClass {
848+
severity: "NOT_EXPLOITABLE".to_string(),
849+
short_description: "SourceAv".to_string(),
850+
description: "Access violation on source operand".to_string(),
851+
explanation: "The target crashed on an access violation at an address matching the source operand of the current instruction. This likely indicates a read access violation.".to_string(),
852+
};
853+
report.pid = 16476;
854+
855+
assert_eq!(
856+
format!("{report}"),
857+
vec![
858+
"CrashLine: /gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:187:28".to_string(),
859+
"".to_string(),
860+
"Date: 2023-09-29T15:31:39.818262+03:00".to_string(),
861+
"Uname: Linux astra-stand 5.15.0-84-generic #93-Ubuntu SMP Tue Sep 5 17:16:10 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux".to_string(),
862+
"OS: Ubuntu".to_string(),
863+
"OSRelease: 22.04".to_string(),
864+
"Architecture: amd64".to_string(),
865+
"ExecutablePath: /home/hkctkuy/github/casr/casr/tests/casr_tests/bin/load_fuzzer".to_string(),
866+
"".to_string(),
867+
"===ProcEnviron===".to_string(),
868+
"CARGO=/home/hkctkuy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo".to_string(),
869+
"".to_string(),
870+
"ProcCmdline: /home/hkctkuy/github/casr/casr/tests/casr_tests/bin/load_fuzzer /home/hkctkuy/github/casr/casr/tests/casr_tests/casrep/libfuzzer_crashes_xlnt/crash-49c4e6270849ab5c9c13f64b98f817efa840d8a3".to_string(),
871+
"".to_string(),
872+
"Stdin: /home/hkctkuy/github/casr/casr/tests/casr_tests/casrep/afl-out-xlnt-small/afl_main-worker/crashes/id:000007,sig:00,sync:afl_s01-worker,src:000371".to_string(),
873+
"".to_string(),
874+
"===ProcStatus===".to_string(),
875+
"process 2945397".to_string(),
876+
"".to_string(),
877+
"===ProcFiles===".to_string(),
878+
" 0x555555554000 0x555555556000 0x2000 0x0 /usr/local/bin/tiff2pdf".to_string(),
879+
"".to_string(),
880+
"===CrashSeverity===".to_string(),
881+
"Severity: NOT_EXPLOITABLE".to_string(),
882+
"Short description: SourceAv".to_string(),
883+
"Description: Access violation on source operand".to_string(),
884+
"Explanation: The target crashed on an access violation at an address matching the source operand of the current instruction. This likely indicates a read access violation.".to_string(),
885+
"".to_string(),
886+
"===Stacktrace===".to_string(),
887+
" #0 0x4ca0e0 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_data() const /gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:187:28".to_string(),
888+
" #1 0x4ca0e0 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>::_M_is_local() const /gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:222:26".to_string(),
889+
"".to_string(),
890+
"===AsanReport===".to_string(),
891+
"==363912==ERROR: AddressSanitizer: SEGV on unknown address 0xffffffffffffffe0 (pc 0x0000004ca0e0 bp 0x7fffffff9980 sp 0x7fffffff9928 T0)".to_string(),
892+
"==363912==The signal is caused by a READ memory access.".to_string(),
893+
"".to_string(),
894+
"===UbsanReport===".to_string(),
895+
"/home/hkctkuy/github/casr/casr/tests/tmp_tests_casr/test_casr_ubsan/test_ubsan.cpp:4:29: runtime error: signed integer overflow: 65535 * 32769 cannot be represented in type 'int'".to_string(),
896+
"SUMMARY: UndefinedBehaviorSanitizer: signed-integer-overflow /home/hkctkuy/github/casr/casr/tests/tmp_tests_casr/test_casr_ubsan/test_ubsan.cpp:4:29 in".to_string(),
897+
"".to_string(),
898+
"===PythonReport===".to_string(),
899+
" === Uncaught Python exception: ===".to_string(),
900+
"TypeError: unhashable type: 'list'".to_string(),
901+
"".to_string(),
902+
"===JavaReport===".to_string(),
903+
"== Java Exception: java.lang.IndexOutOfBoundsException: start 59, end 22, length 60".to_string(),
904+
"\tat com.code_intelligence.jazzer.Jazzer.main(Jazzer.java:73)".to_string(),
905+
"".to_string(),
906+
"===GoReport===".to_string(),
907+
"fatal error: runtime: out of memory".to_string(),
908+
"".to_string(),
909+
"runtime.throw({0x565860?, 0x200000?})".to_string(),
910+
"".to_string(),
911+
"===RustReport===".to_string(),
912+
"Running: ./artifacts/fuzz_target_1/crash-da39a3ee5e6b4b0d3255bfef95601890afd80709".to_string(),
913+
"thread '<unnamed>' panicked at fuzz_targets/fuzz_target_1.rs:6:9:".to_string(),
914+
"index out of bounds: the len is 0 but the index is 10".to_string(),
915+
"stack backtrace:".to_string(),
916+
"".to_string(),
917+
"===Source===".to_string(),
918+
"--->83 return utf16_to_utf8(std::u16string(name_array.begin(),".to_string(),
919+
].join("\n"),
920+
);
921+
}
922+
923+
#[test]
924+
fn test_report_dedup() {
925+
let mut report = CrashReport::new();
926+
// Fill asan_report for correct parsing
927+
report.asan_report = vec!["".to_string()];
928+
report.stacktrace = vec![
929+
"#0 0x7ffff7b08c59 /build/glibc-SzIz7B/glibc-2.31/string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:345".to_string(),
930+
"#1 0x4db921 in __asan_memcpy /llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3".to_string(),
931+
"#2 0x10c0819 in xlnt::detail::compound_document::read_directory() /xlnt/source/detail/cryptography/compound_document.cpp:975:34".to_string(),
932+
"#3 0x10bd55b in xlnt::detail::compound_document::compound_document(std::istream&) /xlnt/source/detail/cryptography/compound_document.cpp:517:5".to_string(),
933+
"#4 0x998b40 in (anonymous namespace)::decrypt_xlsx(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::__cxx11::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t> > const&) /xlnt/source/detail/cryptography/xlsx_crypto_consumer.cpp:320:37".to_string(),
934+
];
935+
936+
// Init ignored frames for correct filtering
937+
init_ignored_frames!("cpp");
938+
939+
let res = dedup_reports(&[report.clone(), report.clone()]);
940+
let Ok(res) = res else {
941+
panic!("{}", res.err().unwrap());
942+
};
943+
assert!(res[0]);
944+
assert!(!res[1]);
945+
}
946+
947+
#[test]
948+
fn test_report_cluster() {
949+
let mut report = CrashReport::new();
950+
// Fill asan_report for correct parsing
951+
report.asan_report = vec!["".to_string()];
952+
report.stacktrace = vec![
953+
"#0 0x7ffff7b08c59 /build/glibc-SzIz7B/glibc-2.31/string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:345".to_string(),
954+
"#1 0x4db921 in __asan_memcpy /llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp:22:3".to_string(),
955+
"#2 0x10c0819 in xlnt::detail::compound_document::read_directory() /xlnt/source/detail/cryptography/compound_document.cpp:975:34".to_string(),
956+
"#3 0x10bd55b in xlnt::detail::compound_document::compound_document(std::istream&) /xlnt/source/detail/cryptography/compound_document.cpp:517:5".to_string(),
957+
"#4 0x998b40 in (anonymous namespace)::decrypt_xlsx(std::vector<unsigned char, std::allocator<unsigned char> > const&, std::__cxx11::basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t> > const&) /xlnt/source/detail/cryptography/xlsx_crypto_consumer.cpp:320:37".to_string(),
958+
];
959+
960+
// Init ignored frames for correct filtering
961+
init_ignored_frames!("cpp");
962+
963+
let res = cluster_reports(&[report.clone(), report.clone()]);
964+
let Ok(res) = res else {
965+
panic!("{}", res.err().unwrap());
966+
};
967+
assert_eq!(res[0], 1);
968+
assert_eq!(res[1], 1);
969+
}
970+
}

0 commit comments

Comments
 (0)