diff --git a/src/patcherex2/components/assemblers/keystone.py b/src/patcherex2/components/assemblers/keystone.py index 46ac6a1..356b381 100644 --- a/src/patcherex2/components/assemblers/keystone.py +++ b/src/patcherex2/components/assemblers/keystone.py @@ -15,6 +15,11 @@ def __init__(self, p, arch: int, mode: int) -> None: self.ks = keystone.Ks(arch, mode) def _assemble(self, code: str, base=0, **kwargs) -> bytes: - binary, _ = self.ks.asm(code, base) - logger.debug(f"Assembled bytes: {bytes(binary)}") - return bytes(binary) + try: + binary, _ = self.ks.asm(code, base) + logger.debug(f"Assembled bytes: {bytes(binary)}") + return bytes(binary) + except Exception as e: + raise Exception( + f'Failed to assemble: """\n{code}\n"""\nat base: {hex(base)}' + ) from e diff --git a/src/patcherex2/components/assemblers/keystone_arm.py b/src/patcherex2/components/assemblers/keystone_arm.py index bfef22f..c21ede2 100644 --- a/src/patcherex2/components/assemblers/keystone_arm.py +++ b/src/patcherex2/components/assemblers/keystone_arm.py @@ -19,7 +19,12 @@ def __init__(self, p) -> None: ) def _assemble(self, code: str, base=0, is_thumb=False) -> bytes: - ks = self.ks_thumb if is_thumb else self.ks_arm - binary, _ = ks.asm(code, base) - logger.debug(f"Assembled bytes: {bytes(binary)}") - return bytes(binary) + try: + ks = self.ks_thumb if is_thumb else self.ks_arm + binary, _ = ks.asm(code, base) + logger.debug(f"Assembled bytes: {bytes(binary)}") + return bytes(binary) + except Exception as e: + raise Exception( + f'Failed to assemble: """\n{code}\n"""\nat base: {hex(base)}' + ) from e