Exemplo n.º 1
0
    def test_Types(self):
        """Types produced different result"""

        file_name = os.path.join(self.test_store, "helloworld")
        bv = binja.BinaryViewType.get_view_of_file(file_name)
        
        preprocessed = binja.preprocess_source("""
        #ifdef nonexistant
        int foo = 1;
        long long foo1 = 1;
        #else
        int bar = 2;
        long long bar1 = 2;
        #endif
        """)
        source = '\n'.join([i.decode("utf-8") for i in preprocessed[0].split('\n') if not b"#line" in i and len(i) > 0])
        typelist = bv.platform.parse_types_from_source(source)
        inttype = binja.Type.int(4)

        tokens = inttype.get_tokens() + inttype.get_tokens_before_name() +  inttype.get_tokens_after_name()
        namedtype = binja.NamedTypeReference()

        retinfo = [[str(i) for i in typelist.variables.popitem()][::-1] for i in range(len(typelist.variables))]
        retinfo += [str(namedtype)]
        
        equalcheck = (inttype == inttype) and not (inttype != inttype)
        retinfo += [str(equalcheck)]
        return retinfo
Exemplo n.º 2
0
    def test_Types(self):
        """Types produced different result"""
        file_name = self.unpackage_file("helloworld")
        bv = binja.BinaryViewType.get_view_of_file(file_name)

        preprocessed = binja.preprocess_source("""
        #ifdef nonexistant
        int foo = 1;
        long long foo1 = 1;
        #else
        int bar = 2;
        long long bar1 = 2;
        #endif
        """)
        source = '\n'.join([
            i.decode('charmap') for i in preprocessed[0].split(b'\n')
            if not b'#line' in i and len(i) > 0
        ])
        typelist = bv.platform.parse_types_from_source(source)
        inttype = binja.Type.int(4)

        tokens = inttype.get_tokens() + inttype.get_tokens_before_name(
        ) + inttype.get_tokens_after_name()
        namedtype = binja.NamedTypeReference()

        retinfo = []
        for i in range(len(typelist.variables)):
            for j in typelist.variables.popitem():
                retinfo.append("Type: " + str(j))
        retinfo.append("Named Type: " + str(namedtype))

        retinfo.append("Type equality: " +
                       str((inttype == inttype) and not (inttype != inttype)))
        return retinfo
Exemplo n.º 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