Search in sources :

Example 1 with BaseType

use of com.google.security.zynamics.binnavi.disassembly.types.BaseType in project binnavi by google.

the class TypeDependenceGraph method willCreateCycle.

/**
   * Tests if adding a member of type memberType to containingType would create a cyclic type
   * declaration.
   *
   * @param containingType The base type that contains memberType.
   * @param memberType The base type of a member contained in containingType.
   * @return Returns whether adding memberType to containingType would create a cyclic type
   *         declaration.
   */
public boolean willCreateCycle(final BaseType containingType, final BaseType memberType) {
    Preconditions.checkNotNull(containingType, "Error: Containing type can not be null.");
    Preconditions.checkNotNull(memberType, "Error: Member type can not be null.");
    if (containingType == memberType) {
        return true;
    }
    // 0) The pre-condition is that the existing graph is cycle free.
    // 1) Contained types is the set of types that are explicitly and implicitly contained within
    // the structure this member belongs to.
    // 2) The intersection of this set and all types that are contained by parentType must be empty.
    // TODO(jannewger): instead of creating a copy, the implementation for determining dependent
    // types should be parameterizable whether to include the contained type or not.
    final Set<BaseType> containedTypes = Sets.newHashSet(determineDependentTypes(containingType));
    containedTypes.remove(containingType);
    final Node startNode = containedRelationMap.get(memberType);
    final Queue<Edge> edgesToVisit = new LinkedList<Edge>();
    for (final EdgeCursor ec = startNode.inEdges(); ec.ok(); ec.next()) {
        edgesToVisit.add((Edge) ec.current());
    }
    final Set<Node> visitednodes = new HashSet<Node>();
    while (!edgesToVisit.isEmpty()) {
        final Edge currentEdge = edgesToVisit.poll();
        final Node nextNode = currentEdge.source();
        final BaseType baseType = containedRelationMap.inverse().get(nextNode);
        if (containedTypes.contains(baseType)) {
            return true;
        }
        if (!visitednodes.contains(nextNode)) {
            for (final EdgeCursor ec = nextNode.inEdges(); ec.ok(); ec.next()) {
                edgesToVisit.add((Edge) ec.current());
            }
        }
        visitednodes.add(nextNode);
    }
    return false;
}
Also used : EdgeCursor(y.base.EdgeCursor) BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) Node(y.base.Node) Edge(y.base.Edge) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 2 with BaseType

use of com.google.security.zynamics.binnavi.disassembly.types.BaseType in project binnavi by google.

the class TypeDependenceGraph method deleteType.

/**
   * Delete the given base type. If other types still depend on this type, the set of affected types
   * is returned.
   *
   * @param baseType The base type to delete.
   * @return Returns The set of base types that depend on the deleted type.
   */
public ImmutableSet<BaseType> deleteType(final BaseType baseType) {
    Preconditions.checkNotNull(baseType, "IE02766: Base type can not be null.");
    final Node containedTypeNode = containedRelationMap.get(baseType);
    Preconditions.checkNotNull(containedTypeNode, "Unable to delete type: corresponding node not found in the dependence graph.");
    final ImmutableSet<BaseType> affectedTypes = determineDependentTypes(baseType);
    containedRelation.removeNode(containedTypeNode);
    containedRelationMap.remove(baseType);
    return affectedTypes;
}
Also used : BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) Node(y.base.Node)

Example 3 with BaseType

use of com.google.security.zynamics.binnavi.disassembly.types.BaseType in project binnavi by google.

the class BaseTypeTableCellRenderer method renderStruct.

private static void renderStruct(final TypeInstance instance, final StyledDocument document, final boolean renderData) {
    final Style structNameStyle = createDeclarationStyle(document);
    final Style structMemberStyle = document.addStyle("STRUCTMEMBERSTYLE", structNameStyle);
    StyleConstants.setForeground(structMemberStyle, Color.GRAY);
    final Style structContentStyle = document.addStyle("STRUCTCONTENTSTYLE", structNameStyle);
    StyleConstants.setForeground(structContentStyle, Color.BLUE);
    StyleConstants.setAlignment(structNameStyle, StyleConstants.ALIGN_RIGHT);
    final BaseType baseType = instance.getBaseType();
    int maxMemberLength = 0;
    for (final TypeMember member : baseType) {
        if (member.getBaseType().getName().length() > maxMemberLength) {
            maxMemberLength = member.getBaseType().getName().length();
        }
    }
    int maxNameLength = 0;
    for (final TypeMember member : baseType) {
        if (member.getName().length() > maxNameLength) {
            maxNameLength = member.getName().length();
        }
    }
    /* Renders type information for structures - construct a string such as:
     *
     * struct STRUCT_NAME { BASE_TYPE_NAME
     */
    try {
        document.remove(0, document.getLength());
        appendString(document, "struct " + baseType.getName() + " {\n", structNameStyle);
        long memberOffset = 0;
        for (final TypeMember member : baseType) {
            appendString(document, "  " + member.getBaseType().getName(), structNameStyle);
            final String separator = Strings.repeat(" ", maxMemberLength - member.getBaseType().getName().length() + 1);
            appendString(document, separator + member.getName(), structMemberStyle);
            appendString(document, ";", structMemberStyle);
            if (renderData) {
                final String dataSeperator = Strings.repeat(".", maxNameLength - member.getName().length() + 1);
                appendString(document, dataSeperator, structNameStyle);
                appendString(document, renderInstanceData(member.getBaseType(), instance.getAddress().getOffset() + memberOffset, instance.getSection()), createDataStyle(document));
                memberOffset += member.getBaseType().getByteSize();
            }
            appendString(document, "\n", structMemberStyle);
        }
        appendString(document, "};", structNameStyle);
    } catch (final BadLocationException exception) {
        CUtilityFunctions.logException(exception);
    }
}
Also used : BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) TypeMember(com.google.security.zynamics.binnavi.disassembly.types.TypeMember) Style(javax.swing.text.Style) BadLocationException(javax.swing.text.BadLocationException)

Example 4 with BaseType

use of com.google.security.zynamics.binnavi.disassembly.types.BaseType in project binnavi by google.

the class PostgreSQLFunctionsLoader method parseFunctionInformation.

/**
   * This function parses a {@link ResultSet result set} from a database query and generates a
   * {@link List} of {@link INaviFunction} from it.
   *
   * @param resultSet The {@link ResultSet} in which the result from the database query is stored.
   * @param provider The {@link SQLProvider} to access the database with.
   * @param module The {@link INaviModule} to which the {@link INaviFunction function(s)} are
   *        associated.
   *
   * @return A {@link List} of {@link INaviFunction functions} parsed from the {@link ResultSet
   *         resultSet}.
   *
   * @throws SQLException if the {@link ResultSet} could not be parsed.
   * @throws CouldntLoadDataException if the associated comments could not be loaded from the
   *         database.
   */
private static List<INaviFunction> parseFunctionInformation(final ResultSet resultSet, final SQLProvider provider, final INaviModule module) throws SQLException, CouldntLoadDataException {
    final List<INaviFunction> functions = Lists.newArrayList();
    final Map<Integer, INaviFunction> commentIdToFunction = new HashMap<Integer, INaviFunction>();
    try {
        while (resultSet.next()) {
            final IAddress address = PostgreSQLHelpers.loadAddress(resultSet, "address");
            final String name = resultSet.getString("name");
            final String originalName = resultSet.getString("original_name");
            Integer globalCommentId = resultSet.getInt("global_comment");
            if (resultSet.wasNull()) {
                globalCommentId = null;
            }
            final String description = resultSet.getString("description");
            final FunctionType type = FunctionType.valueOf(resultSet.getString("type").toUpperCase());
            final String parentModuleName = resultSet.getString("parent_module_name");
            final int parentModuleId = resultSet.getInt("parent_module_id");
            final IAddress parentModuleFunction = resultSet.wasNull() ? null : PostgreSQLHelpers.loadAddress(resultSet, "parent_module_function");
            final Integer nodeCount = resultSet.getInt("bbcount");
            final Integer edgeCount = resultSet.getInt("edgeCount");
            final Integer indegree = resultSet.getInt("incount");
            final Integer outdegree = resultSet.getInt("outcount");
            Integer stackFrameId = resultSet.getInt("stack_frame");
            if (resultSet.wasNull()) {
                stackFrameId = null;
            }
            Integer prototypeId = resultSet.getInt("prototype");
            if (resultSet.wasNull()) {
                prototypeId = null;
            }
            final INaviView view = ViewManager.get(provider).getView(resultSet.getInt("view_id"));
            final BaseType stackFrame = stackFrameId == null ? null : module.getTypeManager().getBaseType(stackFrameId);
            if (stackFrameId != null) {
                module.getTypeManager().setStackFrame(stackFrame);
            }
            final BaseType prototype = prototypeId == null ? null : module.getTypeManager().getBaseType(prototypeId);
            final CFunction function = new CFunction(module, view, address, name, originalName, description, indegree, outdegree, nodeCount, edgeCount, type, parentModuleName, parentModuleId, parentModuleFunction, stackFrame, prototype, provider);
            if (globalCommentId != null) {
                commentIdToFunction.put(globalCommentId, function);
            }
            functions.add(function);
        }
    } finally {
        resultSet.close();
    }
    if (!commentIdToFunction.isEmpty()) {
        final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToFunction.keySet());
        for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
            CommentManager.get(provider).initializeGlobalFunctionComment(commentIdToFunction.get(commentIdToComment.getKey()), commentIdToComment.getValue());
        }
    }
    return functions;
}
Also used : HashMap(java.util.HashMap) FunctionType(com.google.security.zynamics.zylib.disassembly.FunctionType) ArrayList(java.util.ArrayList) CFunction(com.google.security.zynamics.binnavi.disassembly.CFunction) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 5 with BaseType

use of com.google.security.zynamics.binnavi.disassembly.types.BaseType in project binnavi by google.

the class BaseTypeTransferHandler method createOrUpdateTypeSubstitution.

/**
   * Locate operand tree node and use type manager to assign type substitution.
   */
private boolean createOrUpdateTypeSubstitution(final DragAndDropSupportWrapper wrapper) throws UnsupportedFlavorException, IOException, CouldntSaveDataException {
    final INaviOperandTreeNode node = wrapper.determineDropNode();
    if (!isLegalDropNode(node)) {
        return false;
    }
    final BaseType baseType = wrapper.getDroppedBaseType();
    if (node.getTypeSubstitution() != null) {
        typeManager.updateTypeSubstitution(node, node.getTypeSubstitution(), baseType, new ArrayList<TypeMember>(), 0);
    } else {
        // When creating a substitution via drag and drop, the offset is always zero to have a
        // better workflow for the user (otherwise we would need an additional dialog each time).
        typeManager.createTypeSubstitution(node, baseType, node.getOperandPosition(), 0, /* offset */
        node.getInstructionAddress());
    }
    return true;
}
Also used : INaviOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode) BaseType(com.google.security.zynamics.binnavi.disassembly.types.BaseType) TypeMember(com.google.security.zynamics.binnavi.disassembly.types.TypeMember)

Aggregations

BaseType (com.google.security.zynamics.binnavi.disassembly.types.BaseType)18 TypeMember (com.google.security.zynamics.binnavi.disassembly.types.TypeMember)6 BadLocationException (javax.swing.text.BadLocationException)3 Style (javax.swing.text.Style)3 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)2 MemberDialog (com.google.security.zynamics.binnavi.Gui.GraphWindows.types.MemberDialog)2 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)2 TreePath (javax.swing.tree.TreePath)2 Test (org.junit.Test)2 Node (y.base.Node)2 AppendMemberAction (com.google.security.zynamics.binnavi.Gui.GraphWindows.types.actions.AppendMemberAction)1 EditMemberAction (com.google.security.zynamics.binnavi.Gui.GraphWindows.types.actions.EditMemberAction)1 EditTypeAction (com.google.security.zynamics.binnavi.Gui.GraphWindows.types.actions.EditTypeAction)1 InsertMemberAction (com.google.security.zynamics.binnavi.Gui.GraphWindows.types.actions.InsertMemberAction)1 CFunction (com.google.security.zynamics.binnavi.disassembly.CFunction)1 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)1 INaviOperandTreeNode (com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode)1 RawTypeInstance (com.google.security.zynamics.binnavi.disassembly.types.RawTypeInstance)1 Section (com.google.security.zynamics.binnavi.disassembly.types.Section)1 TypeInstance (com.google.security.zynamics.binnavi.disassembly.types.TypeInstance)1