forked from danielplohmann/smda
-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ [performance] Compile regular expressions in IndirectCallAnalyzer #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
r0ny123
wants to merge
5
commits into
master
Choose a base branch
from
perf-optimize-indirect-call-regex-3375033366522080559
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
−19
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
42e7c5e
⚡ [performance] Compile regular expressions in IndirectCallAnalyzer
r0ny123 ab72d23
⚡ [performance] Compile regular expressions in IndirectCallAnalyzer a…
r0ny123 fccaccc
Update smda/intel/IndirectCallAnalyzer.py
r0ny123 d729a39
Add tests for additional register and constant matches
r0ny123 8670707
⚡ [performance] Compile regular expressions in IndirectCallAnalyzer a…
r0ny123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from smda.intel.IndirectCallAnalyzer import IndirectCallAnalyzer | ||
|
|
||
|
|
||
| class IndirectCallAnalyzerTestSuite(unittest.TestCase): | ||
| """Basic tests for IndirectCallAnalyzer regex and logic""" | ||
|
|
||
| def test_regex_matching(self): | ||
| analyzer = IndirectCallAnalyzer(MagicMock()) | ||
|
|
||
| # Test mov <reg>, <reg> | ||
| match = analyzer.RE_MOV_REG_REG.match("eax, ebx") | ||
| self.assertIsNotNone(match) | ||
| self.assertEqual(match.group("reg1"), "eax") | ||
| self.assertEqual(match.group("reg2"), "ebx") | ||
|
|
||
| # Test mov <reg>, <const> | ||
| match = analyzer.RE_MOV_REG_CONST.match("ecx, 0x12345678") | ||
| self.assertIsNotNone(match) | ||
| self.assertEqual(match.group("reg"), "ecx") | ||
| self.assertEqual(match.group("val"), "0x12345678") | ||
|
|
||
| # Test mov <reg>, dword ptr [<addr>] | ||
| match = analyzer.RE_REG_DWORD_PTR_ADDR.match("edx, dword ptr [0x8048000]") | ||
| self.assertIsNotNone(match) | ||
| self.assertEqual(match.group("reg"), "edx") | ||
| self.assertEqual(match.group("addr"), "0x8048000") | ||
|
|
||
| # Test mov <reg>, qword ptr [rip + <addr>] | ||
| match = analyzer.RE_REG_QWORD_PTR_RIP_ADDR.match("rax, qword ptr [rip + 0x1234]") | ||
| self.assertIsNotNone(match) | ||
| self.assertEqual(match.group("reg"), "rax") | ||
| self.assertEqual(match.group("addr"), "0x1234") | ||
|
|
||
| # Test lea <reg>, [<addr>] | ||
| match = analyzer.RE_REG_ADDR.match("rsi, [0x400000]") | ||
| self.assertIsNotNone(match) | ||
| self.assertEqual(match.group("reg"), "rsi") | ||
| self.assertEqual(match.group("addr"), "0x400000") | ||
|
|
||
| def test_processBlock_logic(self): | ||
| disassembler = MagicMock() | ||
| disassembler.resolveApi.return_value = (None, None) | ||
| analyzer = IndirectCallAnalyzer(disassembler) | ||
| analyzer.getDword = MagicMock(return_value=0x12345678) | ||
|
|
||
| analysis_state = MagicMock() | ||
| analyzer.state = analysis_state | ||
| # block is a list of [address, size, mnemonic, op_str] | ||
| block = [ | ||
| [0x401000, 5, "mov", "eax, 0x402000"], | ||
| [0x401005, 2, "mov", "ebx, eax"], | ||
| ] | ||
| registers = {} | ||
| register_name = "ebx" | ||
| processed = [] | ||
| depth = 1 | ||
|
|
||
| # Mock disassembly | ||
| analyzer.disassembly = MagicMock() | ||
| analyzer.disassembly.isAddrWithinMemoryImage.return_value = True | ||
|
|
||
| result = analyzer.processBlock(analysis_state, block, registers, register_name, processed, depth) | ||
|
|
||
| # result should be True because we found an absolute value for the register we were looking for | ||
| self.assertTrue(result, f"processBlock should return True, but returned {result}") | ||
| # eax should have 0x402000 | ||
| self.assertEqual(registers.get("eax"), 0x402000, f"Expected eax to be 0x402000, but got {registers.get('eax')}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great that you've added tests for the regexes! To make them more comprehensive, I suggest adding cases for 64-bit architecture. This would include registers with names that are not 3 characters long (e.g.,
r8,r10) and full 64-bit hexadecimal addresses. This will help ensure the regexes are robust across different architectures.