From a0cf5857956a2ec64a9be62f1283e4f65b3f2bd1 Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Thu, 30 May 2019 19:18:13 -0400 Subject: Improve Error Handling and Add Some Documentation --- python/architecture.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'python') diff --git a/python/architecture.py b/python/architecture.py index c72f8048..e0ccfeeb 100644 --- a/python/architecture.py +++ b/python/architecture.py @@ -1056,6 +1056,11 @@ class Architecture(with_metaclass(_ArchitectureMetaClass, object)): log.log_error(traceback.format_exc()) def _assemble(self, ctxt, code, addr, result, errors): + """ + This function calls the `assemble` command for the actual architecture plugin. + If the plugin does not provide an `assemble(self, code, addr)`-style function, + it uses the default function provided in CoreArchitecture. + """ try: data = self.assemble(code, addr) if data is None: @@ -1064,6 +1069,10 @@ class Architecture(with_metaclass(_ArchitectureMetaClass, object)): ctypes.memmove(buf, data, len(data)) core.BNSetDataBufferContents(result, buf, len(data)) return True + except ValueError as e: # Overriden `assemble` functions should raise a ValueError if the input was invalid (with a reasonable error message) + log.log_error(traceback.format_exc()) + errors[0] = core.BNAllocString(str(e)) + return False except: log.log_error(traceback.format_exc()) errors[0] = core.BNAllocString("Unhandled exception during assembly.\n") @@ -1769,6 +1778,8 @@ class Architecture(with_metaclass(_ArchitectureMetaClass, object)): .. note :: It is important that the assembler used accepts a syntax identical to the one emitted by the \ disassembler. This will prevent confusing the user. + If there is an error in the input assembly, this function should raise a ValueError (with a reasonable error message). + :param str code: string representation of the instructions to be assembled :param int addr: virtual address that the instructions will be loaded at :return: the bytes for the assembled instructions -- cgit v1.3.1