Search in sources :

Example 1 with RawTypeSubstitution

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

the class PostgreSQLTypeFunctions method loadRawTypeSubstitutions.

/**
   * Loads all {@link RawTypeSubstitution} for the given module from the database.
   *
   * @param connection The {@link Connection} to access the database with.
   * @param module The {@link INaviModule} to load the {@link RawTypeSubstitution} for.
   *
   * @return The {@link List} of {@link RawTypeSubstitution} for the given {@link INaviModule}.
   *
   * @throws CouldntLoadDataException if the {@link RawTypeSubstitution} could not be loaded from
   *         the database.
   */
public static List<RawTypeSubstitution> loadRawTypeSubstitutions(final Connection connection, final INaviModule module) throws CouldntLoadDataException {
    Preconditions.checkNotNull(connection, "Error: connection argument can not be null");
    Preconditions.checkNotNull(module, "Error: module argument can not be null");
    final String query = " SELECT * FROM load_type_substitutions(?) ";
    final List<RawTypeSubstitution> rawSubstitutions = new ArrayList<RawTypeSubstitution>();
    try {
        final PreparedStatement statement = connection.prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        final ResultSet results = statement.executeQuery();
        try {
            while (results.next()) {
                final long address = results.getLong("address");
                final int position = results.getInt("position");
                final int expressionId = results.getInt("expression_id");
                final int baseTypeId = results.getInt("base_type_id");
                final Array arr = results.getArray("path");
                Integer[] path = (Integer[]) arr.getArray();
                if (results.wasNull()) {
                    path = new Integer[0];
                }
                Integer offset = results.getInt("offset");
                if (results.wasNull()) {
                    offset = null;
                }
                rawSubstitutions.add(new RawTypeSubstitution(new CAddress(address), position, expressionId, baseTypeId, path, offset));
            }
        } finally {
            results.close();
            statement.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return rawSubstitutions;
}
Also used : SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) Array(java.sql.Array) BigInteger(java.math.BigInteger) ResultSet(java.sql.ResultSet) RawTypeSubstitution(com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution)

Example 2 with RawTypeSubstitution

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

the class MockSqlProvider method updateTypeSubstitution.

@Override
public void updateTypeSubstitution(final TypeSubstitution substitution, final BaseType baseType, final List<Integer> memberPath, final int offset, final INaviModule module) {
    final RawTypeSubstitution rawSubstitution = findSubstitution(substitution.getAddress().toBigInteger(), substitution.getPosition(), substitution.getOperandTreeNode().getId(), module);
    if (rawSubstitution != null) {
        substitutions.remove(module, rawSubstitution);
        substitutions.put(module, new RawTypeSubstitution(substitution.getAddress(), substitution.getPosition(), substitution.getOperandTreeNode().getId(), baseType.getId(), Arrays.copyOf(memberPath.toArray(), memberPath.size(), Integer[].class), offset));
    } else {
        throw new IllegalStateException("Trying to update non-existing type substitution.");
    }
}
Also used : BigInteger(java.math.BigInteger) RawTypeSubstitution(com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution)

Example 3 with RawTypeSubstitution

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

the class PostgreSQLTypesNotificationParser method informExpressionTypesNotification.

/**
   * Informs about necessary state changes related to {@link TypeSubstitution type substitutions}.
   *
   * @param container The {@link TypesNotificationContainer} holding the parsed
   *        {@link PGNotification notification} information.
   * @param provider The {@link SQLProvider} used to access the database with.
   *
   * @throws CouldntLoadDataException if the required information could not be loaded from the
   *         database.
   */
private void informExpressionTypesNotification(final TypesNotificationContainer container, final SQLProvider provider) throws CouldntLoadDataException {
    final INaviModule module = provider.findModule(container.getModuleId());
    final TypeManager typeManager = module.getTypeManager();
    final INaviOperandTreeNode node = findOperandTreeNode(provider, container.getModuleId(), new CAddress(container.getAddress().get()), container.position().get(), container.expressionId().get());
    if (node == null) {
        return;
    }
    if (container.getDatabaseOperation().equals("INSERT")) {
        final RawTypeSubstitution rawSubstitution = provider.loadTypeSubstitution(module, container.getAddress().get(), container.position().get(), container.expressionId().get());
        typeManager.initializeTypeSubstitution(node, rawSubstitution);
    } else if (container.getDatabaseOperation().equals("UPDATE")) {
        final RawTypeSubstitution rawSubstitution = provider.loadTypeSubstitution(module, container.getAddress().get(), container.position().get(), container.expressionId().get());
        typeManager.updateTypeSubstitution(node, rawSubstitution.getBaseTypeId(), rawSubstitution.getPath(), rawSubstitution.getOffset());
    } else if (container.getDatabaseOperation().equals("DELETE")) {
        typeManager.removeTypeSubstitutionInstance(node.getTypeSubstitution());
    } else {
        throw new IllegalStateException("Error: the database operation " + container.getDatabaseOperation() + " is currently not supported.");
    }
}
Also used : INaviOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) TypeManager(com.google.security.zynamics.binnavi.disassembly.types.TypeManager) RawTypeSubstitution(com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Example 4 with RawTypeSubstitution

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

the class CCodeNodeParser method createNewOperand.

/**
   * Creates a new operand tree node object from data from a code node provider.
   *
   * @param module The module the loaded function belongs to.
   * @param dataset Provides the operand data.
   *
   * @return The created operand tree node object.
   *
   * @throws ParserException Thrown if not all data for the operand tree node object could be read.
   */
private static OperandTreeNode createNewOperand(final INaviModule module, final ICodeNodeProvider dataset) throws ParserException {
    final int expressionId = dataset.getExpressionTreeId();
    final int type = dataset.getExpressionTreeType();
    final String value = getValue(dataset, type);
    final Integer parentId = dataset.getParentId();
    final String replacementString = dataset.getReplacement();
    final IAddress functionAddress = dataset.getFunctionAddress();
    final Integer typeId = dataset.getSubstitutionTypeId();
    RawTypeSubstitution substitution = null;
    if (typeId != null) {
        substitution = new RawTypeSubstitution(dataset.getInstructionAddress(), dataset.getSubstitutionPosition(), expressionId, typeId, dataset.getSubstitutionPath(), dataset.getSubstitutionOffset());
    }
    final Integer instanceId = dataset.getTypeInstanceId() == null ? null : dataset.getTypeInstanceId();
    final int operandPosition = dataset.getOperandPosition();
    final IAddress address = dataset.getInstructionAddress();
    // The function parse references moves the dataset around quite heavily therefore all direct
    // access to the dataset must be done before.
    final List<CReference> references = parseReferences(expressionId, dataset);
    final INaviReplacement replacement = lookupReplacement(replacementString, module, functionAddress);
    return new OperandTreeNode(expressionId, type, value, getParentId(parentId), replacement, references, substitution, instanceId, operandPosition, address);
}
Also used : INaviReplacement(com.google.security.zynamics.binnavi.disassembly.INaviReplacement) RawTypeSubstitution(com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution) INaviOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode) CReference(com.google.security.zynamics.binnavi.disassembly.CReference) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 5 with RawTypeSubstitution

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

the class PostgreSQLTypeFunctions method loadRawTypeSubstitution.

/**
   * Loads a single type substitution from the database.
   *
   * @param provider The {@link SQLProvider} used to access the database.
   * @param module The {@link INaviModule} this {@link RawTypeSubstitution} is associated to.
   * @param address of the {@link INaviInstruction instruction} where the
   *        {@link RawTypeSubstitution} is associated to.
   * @param position of the {@link INaviOperandTree operand tree} where the
   *        {@link RawTypeSubstitution} is associated to.
   * @param expressionId of the {@link INaviOperandTreeNode operand tree node} in the
   *        {@link INaviOperandTree operand tree} where the {@link RawTypeSubstitution type
   *        substitution} is associated to.
   *
   * @return The {@link RawTypeSubstitution type substitution} from the database which matches the
   *         given arguments.
   * @throws CouldntLoadDataException if the single {@link RawTypeSubstitution type substitution}
   *         could not be loaded from the database.
   */
public static RawTypeSubstitution loadRawTypeSubstitution(final SQLProvider provider, final INaviModule module, final BigInteger address, final Integer position, final Integer expressionId) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
    Preconditions.checkNotNull(module, "Error: module argument can not be null");
    Preconditions.checkNotNull(address, "Error: address argument can not be null");
    Preconditions.checkNotNull(position, "Error: position argument can not be null");
    Preconditions.checkNotNull(expressionId, "Error: expressionId argument can not be null");
    final String query = " SELECT * FROM load_type_substitution(?, ?, ?, ?) ";
    try {
        final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        statement.setInt(1, module.getConfiguration().getId());
        statement.setObject(2, address, Types.BIGINT);
        statement.setInt(3, position);
        statement.setInt(4, expressionId);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                if (resultSet.first()) {
                    final int baseTypeId = resultSet.getInt("base_type_id");
                    final Array arr = resultSet.getArray("path");
                    final Integer[] path = resultSet.wasNull() ? new Integer[0] : (Integer[]) arr.getArray();
                    Integer offset = resultSet.getInt("offset");
                    if (resultSet.wasNull()) {
                        offset = null;
                    }
                    return new RawTypeSubstitution(new CAddress(address), position, expressionId, baseTypeId, path, offset);
                }
            }
        } finally {
            resultSet.close();
            statement.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    throw new CouldntLoadDataException("Error: could not load single type substitution from the database.");
}
Also used : Array(java.sql.Array) BigInteger(java.math.BigInteger) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RawTypeSubstitution(com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Aggregations

RawTypeSubstitution (com.google.security.zynamics.binnavi.disassembly.types.RawTypeSubstitution)5 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)3 BigInteger (java.math.BigInteger)3 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)2 INaviOperandTreeNode (com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode)2 Array (java.sql.Array)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 CReference (com.google.security.zynamics.binnavi.disassembly.CReference)1 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)1 INaviReplacement (com.google.security.zynamics.binnavi.disassembly.INaviReplacement)1 TypeManager (com.google.security.zynamics.binnavi.disassembly.types.TypeManager)1 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)1 ArrayList (java.util.ArrayList)1