Repository navigation
Conversation
When a target exits printing a very long single line without newlines (e.g. `d8` throwing a JavaScript `ReferenceError` on a 100-400 KB identifier after a crash is fixed on trunk), `StackParser.parse()` backtracks quadratically across eight regex checks: 1. Seven sanitizer regexes (`SAN_ABRT_REGEX`, `SAN_BREAKPOINT_REGEX`, `SAN_FPE_REGEX`, `SAN_ILL_REGEX`, `SAN_TRAP_REGEX`, `SAN_SEGV_REGEX`, `WINDOWS_SAN_ILL_REGEX`) start with `.*[a-zA-Z]+Sanitizer`, backtracking in `O(N^2)` across long runs of ASCII letters when the line does not match. 2. `StackParser.parse()` calls `ANDROID_SIGTRAP_REGEX.search(line)` on `r'.*signal.*\(SIGTRAP...'` before `update_state_on_match`, causing `O(N^2)` `.search()` scans across every starting offset of any long line. Replace `.*[a-zA-Z]+Sanitizer` with `.*[a-zA-Z]Sanitizer` (greedy `.*` already consumes preceding letters on any match, preserving identical matches and capture groups while making non-matches `O(N)`) and pass `address_from_group=1` directly to `update_state_on_match` for `ANDROID_SIGTRAP_REGEX`.
smolkaj
left a comment
There was a problem hiding this comment.
Nice catch, awesome improvement!
In one place where actually changing the semantics of the regex because it contains a capture group -- let's double check that this is intendended and safe. Otherwise LGTM!
| SAN_ABRT_REGEX = re.compile(r'.*[a-zA-Z]+Sanitizer: ABRT ') | ||
| SAN_BREAKPOINT_REGEX = re.compile(r'.*[a-zA-Z]+Sanitizer: breakpoint ') | ||
| # Match a single letter before 'Sanitizer' since greedy '.*' already consumes | ||
| # any preceding letters; '.*[a-zA-Z]+' causes O(N^2) backtracking on long lines. |
There was a problem hiding this comment.
Will this comment -- especially the reference to '.*[a-zA-Z]+' -- still make sense after this PR has been submitted?
I think not. I'd suggest simply removing.
| SAN_ADDR_REGEX = re.compile(r'.*(ERROR: [a-zA-Z]+Sanitizer)[: ]*(.*) on ' | ||
| r'(unknown address |address |)([xX0-9a-fA-F]+)') | ||
| SAN_SEGV_REGEX = re.compile(r'.*([a-zA-Z]+Sanitizer).*(SEGV|access-violation) ' | ||
| SAN_SEGV_REGEX = re.compile(r'.*([a-zA-Z]Sanitizer).*(SEGV|access-violation) ' |
There was a problem hiding this comment.
This changes the capture group, i.e. the part inside the parens.
Why is that safe? Are we using the capture today?
There was a problem hiding this comment.
It doesn't really change the result since regex is greedy the ' will swallow all [a-zA-Z] that + would capture, so the final captured group is the same.
In any case this group is not used, only group 3
smolkaj
left a comment
There was a problem hiding this comment.
Have you considered if there is a decent way that we can actually test performance, so that a future performance regression would be caught and prevented? We'd want to make sure the test isn't flaky, since test execution times can have pretty high variance.
One potential idea: Could we perhaps run the O(n) code on inputs of size k and 100k, and check that the runtime grows by far less than 10'000x? Say < 1000x?
Not a strict blocker for this CL, could be done as a follow up or omitted if it turns out to be too tricky to implement and make reliable.
Done, added the 1k vs 100k < 1000x check using min-of-3 time.process_time() runs plus a 5s ITIMER_VIRTUAL CPU-time cap (~125 ms median). Verified 0 flakes across 300+ runs (100 serial + 200 across 16 parallel workers), and 55/55 expected failures across 11 injected regressions (5 runs each for all 8 reverted regexes hitting the 5s cap, plus 3 sub-5s letter/digit/whitespace O(N^2) cases caught by < 1000x |
I think it would be great if you could share more details on this. I think this is a great addition! |
| state, | ||
| new_type='Trap', | ||
| new_address=(mte_match.group(1) if mte_match else '')) | ||
| address_from_group=1) |
There was a problem hiding this comment.
I think it'd be good to have a small comment here explaining the magical number 1.
|
AFAIU src/clusterfuzz/tests/test.py does not run in CI or butler.py py_unittest. The SIGTRAP tests in stack_analyzer_test.py only cover GDB and V8, so ANDROID_SIGTRAP_REGEX has no positive presubmit test. What do you think about adding a small test in stack_analyzer_test.py for an Android SIGTRAP line verifying Trap and the extracted fault address? Could be something similar to this... def test_android_sigtrap_extracts_fault_address(self):
"""Tests that Android SIGTRAP lines set Trap and extract fault addr."""
stacktrace = (
'Fatal signal 5 (SIGTRAP), code 1 (TRAP_BRKPT), '
'fault addr 0x5d9fdb4188 in tid 27246 (example_mte_fuz)\n'
'signal 5 (SIGTRAP), code 1 (TRAP_BRKPT), '
'fault addr 0x0000005d9fdb4188 (read)\n'
)
actual_state = StackParser().parse(stacktrace)
self.assertEqual(actual_state.crash_type, 'Trap')
self.assertEqual(actual_state.crash_address, '0x005d9fdb4188') |
Bug: 582424297
Context
When a target exits printing a very long single line without newlines (for example,
d8throwing a JavaScriptReferenceErroron a 100–400 KB identifier after a crash is fixed on trunk),CrashResult.is_crash()invokesStackParser.parse()twice per round and stalls for tens of minutes per round, causingprogression_taskto time out and leave fixed testcases open (similar to #3978):SAN_ABRT_REGEX,SAN_BREAKPOINT_REGEX,SAN_FPE_REGEX,SAN_ILL_REGEX,SAN_TRAP_REGEX,SAN_SEGV_REGEX,WINDOWS_SAN_ILL_REGEX) start with.*[a-zA-Z]+Sanitizer, which backtracks quadratically (O(N^2)) across long runs of ASCII letters on every non-matching line.StackParser.parse()callsANDROID_SIGTRAP_REGEX.search(line)(r'.*signal.*\(SIGTRAP...') unconditionally beforeupdate_state_on_match, causingO(N^2).search()scans across every starting offset of any long line (including non-alphabetic lines).Fix description
.*[a-zA-Z]+Sanitizerwith.*[a-zA-Z]Sanitizeracross those 7 regexes. Because leading.*is greedy, it already consumes all preceding letters on any match (preserving the exact same matched strings and capture groups), while making non-matching linesO(N).address_from_group=1directly toupdate_state_on_match(ANDROID_SIGTRAP_REGEX, ...)so it uses.match(line)once instead of callingANDROID_SIGTRAP_REGEX.search(line)first.Testing
test_parse_long_line_no_dosinstack_analyzer_test.py(100 KB line parses in ~20 ms instead of ~80 s); verifiedtest_mte_sigtrap_crashinsrc/clusterfuzz/tests/test.pyand allappengineandcoreunit tests pass.ReferenceErroroutputs, and verified that they now complete cleanly in seconds.