-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make py_cstest a proper package to not run into weird import problems
- Loading branch information
Showing
12 changed files
with
250 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
MANIFEST | ||
dist/ | ||
src/ | ||
capstone/lib | ||
capstone/include | ||
pyx/lib | ||
|
This file contains 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,18 @@ | ||
# Copyright © 2024 Rot127 <[email protected]> | ||
# SPDX-License-Identifier: BSD-3 | ||
|
||
[project] | ||
name = "py_cstest" | ||
version = "0.1.0" | ||
dependencies = [ | ||
"pyyaml >= 6.0.2", | ||
"capstone >= 5.0.0", | ||
] | ||
requires-python = ">= 3.8" | ||
|
||
[tool.setuptools] | ||
packages = ["py_cstest"] | ||
package-dir = {"" = "src"} | ||
|
||
[project.scripts] | ||
py_cstest = "py_cstest.cstest:main" |
This file contains 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,6 @@ | ||
Metadata-Version: 2.1 | ||
Name: py_cstest | ||
Version: 0.1.0 | ||
Requires-Python: >=3.8 | ||
Requires-Dist: pyyaml>=6.0.2 | ||
Requires-Dist: capstone>=5.0.0 |
11 changes: 11 additions & 0 deletions
11
bindings/python/py_cstest/src/py_cstest.egg-info/SOURCES.txt
This file contains 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,11 @@ | ||
README.md | ||
pyproject.toml | ||
src/py_cstest/compare.py | ||
src/py_cstest/cs_modes.py | ||
src/py_cstest/cstest.py | ||
src/py_cstest.egg-info/PKG-INFO | ||
src/py_cstest.egg-info/SOURCES.txt | ||
src/py_cstest.egg-info/dependency_links.txt | ||
src/py_cstest.egg-info/entry_points.txt | ||
src/py_cstest.egg-info/requires.txt | ||
src/py_cstest.egg-info/top_level.txt |
1 change: 1 addition & 0 deletions
1
bindings/python/py_cstest/src/py_cstest.egg-info/dependency_links.txt
This file contains 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 @@ | ||
|
2 changes: 2 additions & 0 deletions
2
bindings/python/py_cstest/src/py_cstest.egg-info/entry_points.txt
This file contains 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,2 @@ | ||
[console_scripts] | ||
py_cstest = py_cstest.cstest:main |
2 changes: 2 additions & 0 deletions
2
bindings/python/py_cstest/src/py_cstest.egg-info/requires.txt
This file contains 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,2 @@ | ||
pyyaml>=6.0.2 | ||
capstone>=5.0.0 |
This file contains 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 @@ | ||
py_cstest |
This file contains 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,155 @@ | ||
# Copyright © 2024 Rot127 <[email protected]> | ||
# SPDX-License-Identifier: BSD-3 | ||
|
||
import capstone | ||
import logging as log | ||
import re | ||
|
||
|
||
def compare_asm_text(a_insn: capstone.CsInsn, expected: str, arch_bits: int) -> bool: | ||
actual = f"{a_insn.mnemonic} {a_insn.op_str}" | ||
actual = actual.strip() | ||
actual = re.sub(r"\s+", " ", actual) | ||
# Replace hex numbers with decimals | ||
for hex_num in re.findall(r"0x[0-9a-fA-F]+", actual): | ||
actual = re.sub(hex_num, f"{int(hex_num, base=16)}", actual) | ||
# Replace negatives with twos-complement | ||
for num in re.findall(r"\d+", actual): | ||
actual = re.sub(num, f"{~(num % (1 << arch_bits)) + 1}", actual) | ||
actual = actual.lower() | ||
|
||
if actual != expected: | ||
log.error( | ||
"Normalized asm-text doesn't match:\n" | ||
f"decoded: '{actual}'\n" | ||
f"expected: '{expected}'\n" | ||
) | ||
return False | ||
return True | ||
|
||
|
||
def compare_str(actual: str, expected: str, msg: str) -> bool: | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_tbool(actual: bool, expected: int, msg: str) -> bool: | ||
if expected == 0: | ||
# Unset | ||
return True | ||
|
||
if (expected < 0 and actual) or (expected > 0 and not actual): | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_uint8(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFF | ||
expected = expected & 0xFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_int8(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFF | ||
expected = expected & 0xFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_uint16(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFF | ||
expected = expected & 0xFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_int16(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFF | ||
expected = expected & 0xFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_uint32(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFFFFFF | ||
expected = expected & 0xFFFFFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_int32(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFFFFFF | ||
expected = expected & 0xFFFFFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_uint64(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFFFFFFFFFFFFFF | ||
expected = expected & 0xFFFFFFFFFFFFFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_int64(actual: int, expected: int, msg: str) -> bool: | ||
actual = actual & 0xFFFFFFFFFFFFFFFF | ||
expected = expected & 0xFFFFFFFFFFFFFFFF | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_fp(actual: float, expected: float, msg: str) -> bool: | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_enum(actual, expected, msg: str) -> bool: | ||
enum_val = getattr(capstone, expected) | ||
if not enum_val: | ||
log.error(f"capstone package doesn't have the an attribute '{expected}'") | ||
return False | ||
if actual != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True | ||
|
||
|
||
def compare_bit_flags(actual: int, expected: list[str], msg: str) -> bool: | ||
for flag in expected: | ||
enum_val = getattr(capstone, flag) | ||
if not enum_val: | ||
log.error(f"capstone package doesn't have the an attribute '{expected}'") | ||
return False | ||
if not actual & enum_val: | ||
log.error(f"{msg}: In {actual:x} the flag {expected} isn't set.") | ||
return False | ||
return True | ||
|
||
|
||
def compare_reg(handle: capstone.Cs, actual: int, expected: str, msg: str) -> bool: | ||
if handle.reg_name(actual) != expected: | ||
log.error(f"{msg}: {actual} != {expected}") | ||
return False | ||
return True |
This file contains 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,41 @@ | ||
# Copyright © 2024 Rot127 <[email protected]> | ||
# SPDX-License-Identifier: BSD-3 | ||
|
||
import capstone as cs | ||
|
||
configs = { | ||
"CS_OPT_DETAIL": {"type": cs.CS_OPT_DETAIL, "val": cs.CS_OPT_ON}, | ||
"CS_OPT_DETAIL_REAL": { | ||
"type": cs.CS_OPT_DETAIL, | ||
"val": cs.CS_OPT_DETAIL_REAL | cs.CS_OPT_ON, | ||
}, | ||
"CS_OPT_SKIPDATA": {"type": cs.CS_OPT_SKIPDATA, "val": cs.CS_OPT_ON}, | ||
"CS_OPT_UNSIGNED": {"type": cs.CS_OPT_UNSIGNED, "val": cs.CS_OPT_ON}, | ||
"CS_OPT_NO_BRANCH_OFFSET": { | ||
"type": cs.CS_OPT_NO_BRANCH_OFFSET, | ||
"val": cs.CS_OPT_ON, | ||
}, | ||
"CS_OPT_SYNTAX_DEFAULT": { | ||
"type": cs.CS_OPT_SYNTAX, | ||
"val": cs.CS_OPT_SYNTAX_DEFAULT, | ||
}, | ||
"CS_OPT_SYNTAX_INTEL": {"type": cs.CS_OPT_SYNTAX, "val": cs.CS_OPT_SYNTAX_INTEL}, | ||
"CS_OPT_SYNTAX_ATT": {"type": cs.CS_OPT_SYNTAX, "val": cs.CS_OPT_SYNTAX_ATT}, | ||
"CS_OPT_SYNTAX_NOREGNAME": { | ||
"type": cs.CS_OPT_SYNTAX, | ||
"val": cs.CS_OPT_SYNTAX_NOREGNAME, | ||
}, | ||
"CS_OPT_SYNTAX_MASM": {"type": cs.CS_OPT_SYNTAX, "val": cs.CS_OPT_SYNTAX_MASM}, | ||
"CS_OPT_SYNTAX_MOTOROLA": { | ||
"type": cs.CS_OPT_SYNTAX, | ||
"val": cs.CS_OPT_SYNTAX_MOTOROLA, | ||
}, | ||
"CS_OPT_SYNTAX_CS_REG_ALIAS": { | ||
"type": cs.CS_OPT_SYNTAX, | ||
"val": cs.CS_OPT_SYNTAX_CS_REG_ALIAS, | ||
}, | ||
"CS_OPT_SYNTAX_PERCENT": { | ||
"type": cs.CS_OPT_SYNTAX, | ||
"val": cs.CS_OPT_SYNTAX_PERCENT, | ||
}, | ||
} |
This file contains 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