diff options
| author | Peter LaFosse <peter@vector35.com> | 2022-01-19 10:52:34 -0500 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2022-01-19 15:20:47 -0500 |
| commit | f8186c868eba120e8fa87eb8138ffdc52c1dfa16 (patch) | |
| tree | c9e3ccca9440875f2ee54c79582ab3da5af0e2c1 /python | |
| parent | 6f5fd10c589cd7bf525d91a60a0dc66bc4f93e25 (diff) | |
Turn some asserts back into exceptions
Diffstat (limited to 'python')
| -rw-r--r-- | python/basicblock.py | 8 | ||||
| -rw-r--r-- | python/binaryview.py | 59 | ||||
| -rw-r--r-- | python/databuffer.py | 3 | ||||
| -rw-r--r-- | python/function.py | 134 | ||||
| -rw-r--r-- | python/highlevelil.py | 5 | ||||
| -rw-r--r-- | python/mediumlevelil.py | 7 |
6 files changed, 142 insertions, 74 deletions
diff --git a/python/basicblock.py b/python/basicblock.py index c73fd604..d504933b 100644 --- a/python/basicblock.py +++ b/python/basicblock.py @@ -223,7 +223,8 @@ class BasicBlock: def _make_edges(self, edges, count:int, direction:bool) -> List[BasicBlockEdge]: assert edges is not None, "Got empty edges list from core" - assert self.view is not None, "Attempting to get BasicBlock edges when BinaryView is None" + if self.view is None: + raise ValueError("Attempting to get BasicBlock edges when BinaryView is None") result:List[BasicBlockEdge] = [] try: for i in range(0, count): @@ -371,9 +372,10 @@ class BasicBlock: @property def annotations(self) -> List[List['_function.InstructionTextToken']]: """List of automatic annotations for the start of this block (read-only)""" - assert self.arch is not None, "attempting to get annotation from BasicBlock without architecture" + if self.arch is None: + raise ValueError("attempting to get annotation from BasicBlock without architecture") if self.function is None: - raise Exception("Attempting to call BasicBlock.annotations when BinaryView is None") + raise ValueError("Attempting to call BasicBlock.annotations when Function is None") return self.function.get_block_annotations(self.start, self.arch) diff --git a/python/binaryview.py b/python/binaryview.py index e61e573c..acc437c7 100644 --- a/python/binaryview.py +++ b/python/binaryview.py @@ -4717,7 +4717,8 @@ class BinaryView: >>> bv.add_user_data_tag(here, tag) >>> """ - assert isinstance(tag_type, TagType), f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}" + if not isinstance(tag_type, TagType): + raise ValueError(f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}") tag_handle = core.BNCreateTag(tag_type.handle, data) assert tag_handle is not None, "core.BNCreateTag returned None" tag = Tag(tag_handle) @@ -6226,7 +6227,8 @@ class BinaryView: (type_obj, new_name) = self.parse_type_string(type_obj) if name is None: name = new_name - assert name is not None, "name can only be None if named type is derived from string passed to type_obj" + if name is None: + raise ValueError("name can only be None if named type is derived from string passed to type_obj") _name = _types.QualifiedName(name)._to_core_struct() core.BNDefineUserAnalysisType(self.handle, _name, type_obj.handle) @@ -6351,14 +6353,15 @@ class BinaryView: if name is not None: _name = _types.QualifiedName(name) if not isinstance(lib, typelibrary.TypeLibrary): - raise ValueError("lib must be a TypeLibrary object") + raise TypeError("lib must be a TypeLibrary object") if isinstance(type_obj, str): (type_obj, new_name) = self.parse_type_string(type_obj) if name is None: _name = new_name if not isinstance(type_obj, (_types.Type, _types.TypeBuilder)): - raise ValueError("type_obj must be a Type object") - assert _name is not None, "name can only be None if named type is derived from string passed to type_obj" + raise TypeError("type_obj must be a Type object") + if _name is None: + raise ValueError("name can only be None if named type is derived from string passed to type_obj") core.BNBinaryViewExportTypeToTypeLibrary(self.handle, lib.handle, _name._to_core_struct(), type_obj.handle) def export_object_to_library(self, lib:typelibrary.TypeLibrary, name:Optional[str], type_obj:StringOrType) -> None: @@ -6378,14 +6381,15 @@ class BinaryView: if name is not None: _name = _types.QualifiedName(name) if not isinstance(lib, typelibrary.TypeLibrary): - raise ValueError("lib must be a TypeLibrary object") + raise TypeError("lib must be a TypeLibrary object") if isinstance(type_obj, str): (type_obj, new_name) = self.parse_type_string(type_obj) if name is None: _name = new_name if not isinstance(type_obj, (_types.Type, _types.TypeBuilder)): - raise ValueError("type_obj must be a Type object") - assert _name is not None, "name can only be None if named type is derived from string passed to type_obj" + raise TypeError("type_obj must be a Type object") + if _name is None: + raise ValueError("name can only be None if named type is derived from string passed to type_obj") core.BNBinaryViewExportObjectToTypeLibrary(self.handle, lib.handle, _name._to_core_struct(), type_obj.handle) def register_platform_types(self, platform:'_platform.Platform') -> None: @@ -7026,13 +7030,13 @@ class BinaryView: def debug_info(self, value: "debuginfo.DebugInfo") -> None: """Sets the debug info for the current binary view""" if not isinstance(value, debuginfo.DebugInfo): - assert False, "Attempting to set debug_info to something which isn't and instance of 'DebugInfo'" + raise ValueError("Attempting to set debug_info to something which isn't and instance of 'DebugInfo'") core.BNSetDebugInfo(self.handle, value.handle) def apply_debug_info(self, value: "debuginfo.DebugInfo") -> None: """Sets the debug info and applies its contents to the current binary view""" if not isinstance(value, debuginfo.DebugInfo): - assert False, "Attempting to apply_debug_info with something which isn't and instance of 'DebugInfo'" + raise ValueError("Attempting to apply_debug_info with something which isn't and instance of 'DebugInfo'") core.BNApplyDebugInfo(self.handle, value.handle) def query_metadata(self, key:str) -> 'metadata.MetadataValueType': @@ -7962,8 +7966,10 @@ class StructuredDataView(object): s = self._bv.get_type_by_name(self._structure_name) if isinstance(s, _types.NamedTypeReferenceType): s = s.target(self._bv) - assert s is not None, f"Failed to find type: {structure_name}" - assert isinstance(s, _types.StructureType), f"{self._structure_name} is not a StructureTypeClass, got: {type(s)}" + if s is None: + raise ValueError(f"Failed to find type: {structure_name}") + if not isinstance(s, _types.StructureType): + raise ValueError(f"{self._structure_name} is not a StructureTypeClass, got: {type(s)}") self._structure = s for m in self._structure.members: @@ -8027,7 +8033,8 @@ class TypedDataAccessor: endian:Endianness def __post_init__(self): - assert isinstance(self.type, _types.Type), "Attempting to create TypedDataAccessor with TypeBuilder" + if not isinstance(self.type, _types.Type): + raise TypeError("Attempting to create TypedDataAccessor with TypeBuilder") def __bytes__(self): return self.view.read(self.address, len(self)) @@ -8039,7 +8046,8 @@ class TypedDataAccessor: _type = self.type if isinstance(_type, _types.NamedTypeReferenceType): _type = _type.target(self.view) - assert _type is not None + if _type is None: + raise ValueError(f"Couldn't get target of type {_type}") return len(_type) def __int__(self): @@ -8057,12 +8065,16 @@ class TypedDataAccessor: if isinstance(_type, _types.NamedTypeReferenceType): _type = _type.target(self.view) if isinstance(_type, _types.ArrayType) and isinstance(key, int): - assert key < _type.count, f"Index {key} out of bounds array has {_type.count} elements" + if key >= _type.count: + raise ValueError(f"Index {key} out of bounds array has {_type.count} elements") return TypedDataAccessor(_type.element_type, key * len(_type.element_type), self.view, self.endian) - assert isinstance(_type, _types.StructureType), "Can't get member of non-structure" - assert isinstance(key, str), "Must use string to get member of structure" + if not isinstance(_type, _types.StructureType): + raise ValueError("Can't get member of non-structure") + if not isinstance(key, str): + raise ValueError("Must use string to get member of structure") m = _type[key] - assert m is not None, f"Member {key} doesn't exist in structure" + if m is None: + raise ValueError(f"Member {key} doesn't exist in structure") return TypedDataAccessor(m.type.immutable_copy(), self.address + m.offset, self.view, self.endian) @staticmethod @@ -8100,7 +8112,8 @@ class TypedDataAccessor: _types.WideCharType, _types.WideCharBuilder, _types.PointerType, _types.PointerBuilder, _types.EnumerationType, _types.EnumerationBuilder) - assert isinstance(self.type, integral_types), f"Can't set the value of type {type(self.type)} to int value" + if not isinstance(self.type, integral_types): + raise TypeError(f"Can't set the value of type {type(self.type)} to int value") to_write = data.to_bytes(len(self), TypedDataAccessor.byte_order(self.endian)) # type: ignore elif isinstance(data, float) and isinstance(self.type, (_types.FloatType, _types.FloatBuilder)): endian = "<" if self.endian == Endianness.LittleEndian else ">" @@ -8119,10 +8132,12 @@ class TypedDataAccessor: assert count == len(to_write), "Unable to write all bytes to the location, segment might not have file backing" def _value_helper(self, _type:'_types.Type', data:bytes) -> Any: - assert isinstance(_type, _types.Type), f"Attempting to get value of TypeBuilder of type {type(_type)}" + if not isinstance(_type, _types.Type): + raise TypeError(f"Attempting to get value of TypeBuilder of type {type(_type)}") if isinstance(_type, _types.NamedTypeReferenceType): target = _type.target(self.view) - assert target is not None + if target is None: + raise ValueError("Couldn't find target for type") _type = target if isinstance(_type, (_types.VoidType, _types.FunctionType)): #, _types.VarArgsType, _types.ValueType)): @@ -8156,7 +8171,7 @@ class TypedDataAccessor: result.append(TypedDataAccessor(_type.element_type, self.address + offset, self.view, self.endian).value) return result else: - assert False, f"Unhandled `Type` {type(_type)}" + raise TypeError(f"Unhandled `Type` {type(_type)}") # for backward compatibility diff --git a/python/databuffer.py b/python/databuffer.py index fea7bb82..df16cc54 100644 --- a/python/databuffer.py +++ b/python/databuffer.py @@ -36,7 +36,8 @@ class DataBuffer: elif isinstance(contents, str): self.handle = core.BNCreateDataBuffer(contents.encode("utf-8"), len(contents.encode("utf-8"))) else: - assert isinstance(contents, bytes) + if not isinstance(contents, bytes): + raise TypeError(f"type {type(contents)} not convertable to DataBuffer") self.handle = core.BNCreateDataBuffer(contents, len(contents)) def __del__(self): diff --git a/python/function.py b/python/function.py index 3fbe90c0..8bfd2405 100644 --- a/python/function.py +++ b/python/function.py @@ -437,7 +437,7 @@ class Function: return self._arch else: arch = core.BNGetFunctionArchitecture(self.handle) - assert arch is not None + assert arch is not None, "core.BNGetFunctionArchitecture returned None" self._arch = architecture.CoreArchitecture._from_cache(arch) return self._arch @@ -621,7 +621,7 @@ class Function: try: for i in range(0, count.value): core_tag = core.BNNewTagReference(tags[i]) - assert core_tag is not None + assert core_tag is not None, "core.BNNewTagReference returned None" result.append(binaryview.Tag(core_tag)) return result finally: @@ -662,7 +662,8 @@ class Function: :return: The created Tag :rtype: Tag """ - assert isinstance(tag_type, binaryview.TagType), f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}" + if not isinstance(tag_type, binaryview.TagType): + raise TypeError(f"type is not a TagType instead got {type(tag_type)} : {repr(tag_type)}") if arch is None: if self.arch is None: raise Exception(f"Can't call {_function_name_()} for function with no architecture specified") @@ -750,7 +751,7 @@ class Function: result = [] for i in range(count.value): core_tag = core.BNNewTagReference(tags[i]) - assert core_tag is not None + assert core_tag is not None, "core.BNNewTagReference returned None" result.append(binaryview.Tag(core_tag)) return result finally: @@ -1646,7 +1647,8 @@ class Function: <undetermined> """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch reg = arch.get_reg_index(reg) value = core.BNGetRegisterValueAfterInstruction(self.handle, arch.handle, addr, reg) @@ -1867,7 +1869,8 @@ class Function: <range: 0x8 to 0xffffffff> """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch value = core.BNGetStackContentsAtInstruction(self.handle, arch.handle, addr, offset, size) result = variable.RegisterValue.from_BNRegisterValue(value, arch) @@ -1876,7 +1879,8 @@ class Function: def get_stack_contents_after(self, addr:int, offset:int, size:int, arch:Optional['architecture.Architecture']=None) -> 'variable.RegisterValue': if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch value = core.BNGetStackContentsAfterInstruction(self.handle, arch.handle, addr, offset, size) result = variable.RegisterValue.from_BNRegisterValue(value, arch) @@ -1885,7 +1889,8 @@ class Function: def get_parameter_at(self, addr:int, func_type:Optional['types.Type'], i:int, arch:Optional['architecture.Architecture']=None) -> 'variable.RegisterValue': if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch _func_type = None @@ -1906,7 +1911,8 @@ class Function: :rtype: None """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch core.BNRemoveUserAddressTagsOfType(self.handle, arch.handle, addr, tag_type.handle) @@ -1921,7 +1927,8 @@ class Function: def get_regs_read_by(self, addr:int, arch:Optional['architecture.Architecture']=None) -> List['architecture.RegisterName']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() regs = core.BNGetRegistersReadByInstruction(self.handle, arch.handle, addr, count) @@ -1934,7 +1941,8 @@ class Function: def get_regs_written_by(self, addr:int, arch:Optional['architecture.Architecture']=None) -> List['architecture.RegisterName']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() regs = core.BNGetRegistersWrittenByInstruction(self.handle, arch.handle, addr, count) @@ -1955,7 +1963,8 @@ class Function: :rtype: None """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch core.BNRemoveAutoAddressTag(self.handle, arch.handle, addr, tag.handle) @@ -1969,14 +1978,16 @@ class Function: :rtype: None """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch core.BNRemoveAutoAddressTagsOfType(self.handle, arch.handle, addr, tag_type.handle) def get_stack_vars_referenced_by(self, addr:int, arch:Optional['architecture.Architecture']=None) -> List['variable.StackVariableReference']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() refs = core.BNGetStackVariablesReferencedByInstruction(self.handle, arch.handle, addr, count) @@ -2029,7 +2040,8 @@ class Function: def get_lifted_il_at(self, addr:int, arch:Optional['architecture.Architecture']=None) -> Optional['lowlevelil.LowLevelILInstruction']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch idx = core.BNGetLiftedILForInstruction(self.handle, arch.handle, addr) @@ -2053,7 +2065,8 @@ class Function: [<il: push(rbp)>] """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() instrs = core.BNGetLiftedILInstructionsForAddress(self.handle, arch.handle, addr, count) @@ -2134,7 +2147,8 @@ class Function: def get_constants_referenced_by(self, addr:int, arch:'architecture.Architecture'=None) -> List[variable.ConstantReference]: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() refs = core.BNGetConstantsReferencedByInstruction(self.handle, arch.handle, addr, count) @@ -2165,7 +2179,8 @@ class Function: def get_lifted_il_flag_uses_for_definition(self, i:'lowlevelil.InstructionIndex', flag:'architecture.FlagType') -> List['lowlevelil.LowLevelILInstruction']: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") flag = self.arch.get_flag_index(flag) count = ctypes.c_ulonglong() instrs = core.BNGetLiftedILFlagUsesForDefinition(self.handle, i, flag, count) @@ -2178,7 +2193,8 @@ class Function: def get_lifted_il_flag_definitions_for_use(self, i:'lowlevelil.InstructionIndex', flag:'architecture.FlagType') -> List['lowlevelil.InstructionIndex']: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") flag = self.arch.get_flag_index(flag) count = ctypes.c_ulonglong() @@ -2192,7 +2208,8 @@ class Function: def get_flags_read_by_lifted_il_instruction(self, i:'lowlevelil.InstructionIndex') -> \ List['architecture.FlagName']: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") count = ctypes.c_ulonglong() flags = core.BNGetFlagsReadByLiftedILInstruction(self.handle, i, count) @@ -2206,7 +2223,8 @@ class Function: def get_flags_written_by_lifted_il_instruction(self, i:'lowlevelil.InstructionIndex') -> \ List['architecture.FlagName']: count = ctypes.c_ulonglong() - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") flags = core.BNGetFlagsWrittenByLiftedILInstruction(self.handle, i, count) assert flags is not None, "core.BNGetFlagsWrittenByLiftedILInstruction returned None" @@ -2237,7 +2255,8 @@ class Function: def set_auto_indirect_branches(self, source:int, branches:List[Tuple['architecture.Architecture', int]], source_arch:Optional['architecture.Architecture']=None) -> None: if source_arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") source_arch = self.arch branch_list = (core.BNArchitectureAndAddress * len(branches))() for i in range(len(branches)): @@ -2248,7 +2267,8 @@ class Function: def set_user_indirect_branches(self, source:int, branches:List[Tuple['architecture.Architecture', int]], source_arch:Optional['architecture.Architecture']=None) -> None: if source_arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") source_arch = self.arch branch_list = (core.BNArchitectureAndAddress * len(branches))() for i in range(len(branches)): @@ -2258,7 +2278,8 @@ class Function: def get_indirect_branches_at(self, addr:int, arch:Optional['architecture.Architecture']=None) -> List['variable.IndirectBranchInfo']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() branches = core.BNGetIndirectBranchesAt(self.handle, arch.handle, addr, count) @@ -2430,7 +2451,8 @@ class Function: :param Architecture arch: (optional) Architecture of the instruction or IL line containing the token """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch return IntegerDisplayType(core.BNGetIntegerConstantDisplayType(self.handle, arch.handle, instr_addr, value, operand)) @@ -2444,7 +2466,8 @@ class Function: :param Architecture arch: (optional) Architecture of the instruction or IL line containing the token """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if isinstance(display_type, str): display_type = IntegerDisplayType[display_type] @@ -2478,7 +2501,8 @@ class Function: <block: x86_64@0x100000f30-0x100000f50> """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch block = core.BNGetFunctionBasicBlockAtAddress(self.handle, arch.handle, addr) if not block: @@ -2493,7 +2517,8 @@ class Function: <color: #ff00ff> """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch color = core.BNGetInstructionHighlight(self.handle, arch.handle, addr) if color.style == HighlightColorStyle.StandardHighlightColor: @@ -2516,7 +2541,8 @@ class Function: :param Architecture arch: (optional) Architecture of the instruction if different from self.arch """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if not isinstance(color, HighlightStandardColor) and not isinstance(color, _highlight.HighlightColor): raise ValueError("Specified color is not one of HighlightStandardColor, _highlight.HighlightColor") @@ -2539,7 +2565,8 @@ class Function: >>> current_function.set_user_instr_highlight(here, highlight.HighlightColor(red=0xff, blue=0xff, green=0)) """ if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if not isinstance(color, HighlightStandardColor) and not isinstance(color, _highlight.HighlightColor): raise ValueError("Specified color is not one of HighlightStandardColor, highlight.HighlightColor") @@ -2588,7 +2615,8 @@ class Function: def get_stack_var_at_frame_offset(self, offset:int, addr:int, arch:Optional['architecture.Architecture']=None) -> \ Optional['variable.Variable']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch found_var = core.BNVariableNameAndType() if not core.BNGetStackVariableAtFrameOffset(self.handle, arch.handle, addr, offset, found_var): @@ -2623,7 +2651,8 @@ class Function: def set_auto_call_stack_adjustment(self, addr:int, adjust:Union[int, 'types.SizeWithConfidence'], arch:Optional['architecture.Architecture']=None) -> None: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if not isinstance(adjust, types.SizeWithConfidence): adjust = types.SizeWithConfidence(adjust) @@ -2632,7 +2661,8 @@ class Function: def set_auto_call_reg_stack_adjustment(self, addr:int, adjust:Mapping['architecture.RegisterStackName', int], arch:Optional['architecture.Architecture']=None) -> None: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch adjust_buf = (core.BNRegisterStackAdjustment * len(adjust))() i = 0 @@ -2649,7 +2679,8 @@ class Function: def set_auto_call_reg_stack_adjustment_for_reg_stack(self, addr:int, reg_stack:'architecture.RegisterStackType', adjust, arch:Optional['architecture.Architecture']=None) -> None: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch reg_stack = arch.get_reg_stack_index(reg_stack) if not isinstance(adjust, types.RegisterStackAdjustmentWithConfidence): @@ -2662,7 +2693,8 @@ class Function: if isinstance(adjust_type, str): (adjust_type, _) = self.view.parse_type_string(adjust_type) if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if adjust_type is None: tc = None @@ -2673,7 +2705,8 @@ class Function: def set_call_stack_adjustment(self, addr:int, adjust:Union[int, 'types.SizeWithConfidence'], arch:Optional['architecture.Architecture']=None): if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if not isinstance(adjust, types.SizeWithConfidence): adjust = types.SizeWithConfidence(adjust) @@ -2683,7 +2716,8 @@ class Function: adjust:Mapping['architecture.RegisterStackName', 'types.RegisterStackAdjustmentWithConfidence'], arch:Optional['architecture.Architecture']=None) -> None: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch adjust_buf = (core.BNRegisterStackAdjustment * len(adjust))() i = 0 @@ -2700,7 +2734,8 @@ class Function: def set_call_reg_stack_adjustment_for_reg_stack(self, addr:int, reg_stack:'architecture.RegisterStackType', adjust:Union[int, 'types.RegisterStackAdjustmentWithConfidence'], arch:Optional['architecture.Architecture']=None) -> None: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch reg_stack = arch.get_reg_stack_index(reg_stack) if not isinstance(adjust, types.RegisterStackAdjustmentWithConfidence): @@ -2710,7 +2745,8 @@ class Function: def get_call_type_adjustment(self, addr:int, arch:Optional['architecture.Architecture']=None) -> Optional['types.Type']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch result = core.BNGetCallTypeAdjustment(self.handle, arch.handle, addr) if not result.type: @@ -2720,7 +2756,8 @@ class Function: def get_call_stack_adjustment(self, addr:int, arch:Optional['architecture.Architecture']=None) -> 'types.SizeWithConfidence': if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch result = core.BNGetCallStackAdjustment(self.handle, arch.handle, addr) return types.SizeWithConfidence(result.value, confidence = result.confidence) @@ -2728,7 +2765,8 @@ class Function: def get_call_reg_stack_adjustment(self, addr:int, arch:Optional['architecture.Architecture']=None) -> \ Dict['architecture.RegisterStackName', 'types.RegisterStackAdjustmentWithConfidence']: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch count = ctypes.c_ulonglong() adjust = core.BNGetCallRegisterStackAdjustment(self.handle, arch.handle, addr, count) @@ -2743,7 +2781,8 @@ class Function: def get_call_reg_stack_adjustment_for_reg_stack(self, addr:int, reg_stack:'architecture.RegisterStackType', arch:Optional['architecture.Architecture']=None) -> 'types.RegisterStackAdjustmentWithConfidence': if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch reg_stack = arch.get_reg_stack_index(reg_stack) adjust = core.BNGetCallRegisterStackAdjustmentForRegisterStack(self.handle, arch.handle, addr, reg_stack) @@ -2752,7 +2791,8 @@ class Function: def is_call_instruction(self, addr:int, arch:Optional['architecture.Architecture']=None) -> bool: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch return core.BNIsCallInstruction(self.handle, arch.handle, addr) @@ -3008,7 +3048,8 @@ class Function: count = ctypes.c_ulonglong(0) if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch if length is None: @@ -3110,7 +3151,8 @@ class Function: def get_instruction_containing_address(self, addr:int, arch:Optional['architecture.Architecture']=None) -> \ Optional[int]: if arch is None: - assert self.arch is not None, f"Can't call {_function_name_()} for function with no architecture specified" + if self.arch is None: + raise ValueError(f"Can't call {_function_name_()} for function with no architecture specified") arch = self.arch start = ctypes.c_ulonglong() @@ -3265,7 +3307,7 @@ class DisassemblyTextRenderer: def get_instruction_annotations(self, addr:int) -> List['InstructionTextToken']: count = ctypes.c_ulonglong() tokens = core.BNGetDisassemblyTextRendererInstructionAnnotations(self.handle, addr, count) - assert tokens is not None + assert tokens is not None, "core.BNGetDisassemblyTextRendererInstructionAnnotations returned None" result = InstructionTextToken._from_core_struct(tokens, count.value) core.BNFreeInstructionText(tokens, count.value) return result diff --git a/python/highlevelil.py b/python/highlevelil.py index b757727d..647f8503 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -626,7 +626,8 @@ class HighLevelILInstruction(BaseILInstruction): ExpressionIndex(self.core_instr.operands[operand_index])) def get_intrinsic(self, operand_index:int) -> 'lowlevelil.ILIntrinsic': - assert self.function.arch is not None, "Attempting to create ILIntrinsic from function with no Architecture" + if self.function.arch is None: + raise ValueError("Attempting to create ILIntrinsic from function with no Architecture") return lowlevelil.ILIntrinsic(self.function.arch, architecture.IntrinsicIndex(self.core_instr.operands[operand_index])) @@ -2197,7 +2198,7 @@ class HighLevelILFunction: def ssa_form(self) -> 'HighLevelILFunction': """High level IL in SSA form (read-only)""" result = core.BNGetHighLevelILSSAForm(self.handle) - assert result is not None + assert result is not None, "core.BNGetHighLevelILSSAForm returned None" return HighLevelILFunction(self._arch, result, self._source_function) @property diff --git a/python/mediumlevelil.py b/python/mediumlevelil.py index 5c960383..e0ebe708 100644 --- a/python/mediumlevelil.py +++ b/python/mediumlevelil.py @@ -69,6 +69,13 @@ class SSAVariable: def __repr__(self): return f"<ssa {self.var} version {self.version}>" + @property + def name(self) -> str: + return self.var.name + + @property + + class MediumLevelILLabel: def __init__(self, handle:Optional[core.BNMediumLevelILLabel]=None): |
