Пример #1
0
 def test_Enumeration(self):
     """Enumeration produced different result"""
     inttype = binja.Type.int(4)
     enum = binja.Enumeration()
     enum.a = 1
     enum.append("a", 1)
     enum.append("b", 2)
     enum.replace(0, "a", 2)
     enum.remove(0)
     retinfo = [str(enum)]
     retinfo += [str((enum == enum) and not (enum != enum))]
     return retinfo
Пример #2
0
def enum_decl(node: Cursor, bv: bn.BinaryView):
    bn.log.log_debug(f'enum_decl: Processing enum {node.type.spelling} {node.spelling}')

    enum = bn.Enumeration()
    for enum_member in node.get_children():
        enum.append(enum_member.spelling, enum_member.enum_value)

    try:
        if node.spelling:
            enum_name = node.spelling
        else:
            enum_name = node.type.spelling
        bv.define_user_type(enum_name, bn.Type.enumeration_type(bv.arch, enum))
        bn.log.log_debug(f'enum_decl: Successfully processed enum {node.spelling}')
        return node.spelling, bn.Type.enumeration_type(bv.arch, enum)
    except Exception as e:
        bn.log.log_debug(f'enum_decl: Failed Processing enum {node.spelling} with exception {e}')
Пример #3
0
    def _construct_binja_type(self,
                              ty: Type,
                              as_specifier: bool = False) -> bn.Type:
        assert (not isinstance(ty, str))
        if ty.uuid in self._s2b_types:
            if as_specifier and ty.name is not None:
                ntrc = bn.NamedTypeReferenceClass.UnknownNamedTypeClass
                if ty.composite_type is not None:
                    if ty.composite_type == CompositeType.CLASS_TYPE:
                        ntrc = bn.NamedTypeReferenceClass.ClassNamedTypeClass
                    elif ty.composite_type == CompositeType.STRUCT_TYPE:
                        ntrc = bn.NamedTypeReferenceClass.StructNamedTypeClass
                    elif ty.composite_type == CompositeType.UNION_TYPE:
                        ntrc = bn.NamedTypeReferenceClass.UnionNamedTypeClass
                    elif ty.composite_type == CompositeType.ENUM_TYPE:
                        ntrc = bn.NamedTypeReferenceClass.EnumNamedTypeClass
                binja_type = bn.Type.named_type(
                    bn.NamedTypeReference(name=ty.name,
                                          type_id=self._generate_typeid(
                                              ty.name),
                                          type_class=ntrc))
            else:
                return self._s2b_types[ty.uuid]

        bv = self._binary_view
        if ty.scalar_type:
            if ty.scalar_type == ScalarType.BASE_TYPE:
                if ty.name in self._base_types:
                    binja_type = self._base_types[ty.name]
                else:
                    try:
                        # If this is a parseable type, do that.
                        binja_type, _ = bv.parse_type_string(ty.name)
                        self._base_types[ty.name] = binja_type
                        self._builtin_types.add(ty.name)
                    except:
                        # Otherwise, create a named type reference.
                        binja_type = bn.Type.named_type(
                            bn.NamedTypeReference(
                                name=ty.name,
                                type_id=self._generate_typeid(ty.name)))
                        self._base_types[ty.name] = binja_type
            elif ty.scalar_type == ScalarType.POINTER_TYPE:
                target_type = self._construct_binja_type(
                    ty.element, as_specifier=as_specifier)
                binja_type = bn.Type.pointer(
                    bv.arch,
                    target_type,
                    ref_type=bn.ReferenceType.PointerReferenceType)
            elif ty.scalar_type == ScalarType.REFERENCE_TYPE:
                target_type = self._construct_binja_type(
                    ty.element, as_specifier=as_specifier)
                binja_type = bn.Type.pointer(
                    bv.arch,
                    target_type,
                    ref_type=bn.ReferenceType.ReferenceReferenceType)
            elif ty.scalar_type == ScalarType.RVALUE_REFERENCE_TYPE:
                target_type = self._construct_binja_type(
                    ty.element, as_specifier=as_specifier)
                binja_type = bn.Type.pointer(
                    bv.arch,
                    target_type,
                    ref_type=bn.ReferenceType.RValueReferenceType)
            elif ty.scalar_type == ScalarType.ARRAY_TYPE:
                element_type = self._construct_binja_type(
                    ty.element, as_specifier=as_specifier)
                count = 0 if ty.array_count is None else ty.array_count
                if count > 65535:
                    count = 0
                binja_type = bn.Type.array(element_type, count)
        elif ty.composite_type:
            if as_specifier and ty.name is not None:
                ntrc = bn.NamedTypeReferenceClass.UnknownNamedTypeClass
                if ty.composite_type == CompositeType.CLASS_TYPE:
                    ntrc = bn.NamedTypeReferenceClass.ClassNamedTypeClass
                elif ty.composite_type == CompositeType.STRUCT_TYPE:
                    ntrc = bn.NamedTypeReferenceClass.StructNamedTypeClass
                elif ty.composite_type == CompositeType.UNION_TYPE:
                    ntrc = bn.NamedTypeReferenceClass.UnionNamedTypeClass
                elif ty.composite_type == CompositeType.ENUM_TYPE:
                    ntrc = bn.NamedTypeReferenceClass.EnumNamedTypeClass
                binja_type = bn.Type.named_type(
                    bn.NamedTypeReference(name=ty.name,
                                          type_id=self._generate_typeid(
                                              ty.name),
                                          type_class=ntrc))
            else:
                if ty.composite_type in [
                        CompositeType.CLASS_TYPE, CompositeType.STRUCT_TYPE
                ]:
                    struct = bn.Structure()
                    struct.type = bn.StructureType.StructStructureType if ty.composite_type == CompositeType.STRUCT_TYPE else bn.StructureType.ClassStructureType
                    if ty.byte_size is not None:
                        struct.width = ty.byte_size
                    for m in ty.members:
                        member_type = self._construct_binja_type(
                            m.element, as_specifier=True)
                        member_name = m.name if m.name is not None else ''
                        if m.offset is not None:
                            struct.insert(m.offset, member_type, member_name)
                    binja_type = bn.Type.structure_type(struct)
                elif ty.composite_type == CompositeType.UNION_TYPE:
                    union = bn.Structure()
                    union.type = bn.StructureType.UnionStructureType
                    if ty.byte_size is not None:
                        union.width = ty.byte_size
                    for m in ty.members:
                        member_type = self._construct_binja_type(
                            m.element, as_specifier=as_specifier)
                        member_name = m.name if m.name is not None else ''
                        if m.offset is not None:
                            union.insert(m.offset, member_type, member_name)
                    binja_type = bn.Type.structure_type(union)
                elif ty.composite_type == CompositeType.ENUM_TYPE:
                    e = bn.Enumeration()
                    for m in ty.members:
                        e.append(m.name, m.offset)
                    binja_type = bn.Type.enumeration_type(
                        bv.arch, e, ty.byte_size)
                elif ty.composite_type == CompositeType.FUNCTION_TYPE:
                    has_variable_args = False
                    ret = self._construct_binja_type(ty.element,
                                                     as_specifier=True)
                    params = []
                    for param in ty.members:
                        if param.element == Type.variadic():
                            has_variable_args = True
                        else:
                            params.append(
                                self._construct_binja_type(param.element,
                                                           as_specifier=True))
                    binja_type = bn.Type.function(
                        ret, params, variable_arguments=has_variable_args)
                elif ty.composite_type == CompositeType.PTR_TO_MEMBER_TYPE:
                    binja_type = self._construct_binja_type(ty.members[1],
                                                            as_specifier=True)
        elif ty.name is not None:
            ntrc = bn.NamedTypeReferenceClass.TypedefNamedTypeClass
            binja_type = bn.Type.named_type(
                bn.NamedTypeReference(name=ty.name,
                                      type_id=self._generate_typeid(ty.name),
                                      type_class=ntrc))
        else:
            if ty.element is None:
                print(ty.__dict__)
            assert (ty.element is not None)
            binja_type = self._construct_binja_type(
                ty.element, as_specifier=as_specifier).mutable_copy()
            if ty.is_constant:
                binja_type.const = True
            if ty.is_volatile:
                binja_type.volatile = True

        return binja_type