Examples of RTLExpression


Examples of org.jakstab.rtl.expressions.RTLExpression

    if (expressions.length != 2) return null;
   
    // If not on trace, don't concretize
    if (isBot()) return null;

    RTLExpression condition = expressions[0];
    RTLExpression target = expressions[1];
    RTLNumber cCondition;
    RTLNumber cTarget;
   
    Set<Tuple<RTLNumber>> res = new FastSet<Tuple<RTLNumber>>();
   
    for (AbsoluteAddress successor : getNextPC()) {
      RTLNumber nextPC = successor.toNumericConstant();

      if (target instanceof RTLNumber) {
        // If target is a number, this is a direct jump, and maybe conditional

        cTarget = (RTLNumber)target;

        if (condition instanceof RTLNumber) {
          // Direct, unconditional jump
          cCondition = (RTLNumber)condition;
        } else if (target.equals(nextPC)) {
          // Conditional jump that is taken according to the trace
          cCondition = ExpressionFactory.TRUE;
        } else {
          // Conditional jump that is not taken
          cCondition = ExpressionFactory.FALSE;
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

    return visitor.visit(this);
  }

  @Override
  public RTLStatement evaluate(Context context) {
    RTLExpression evaldDestination = destination.evaluate(context);
    RTLExpression evaldValue = value.evaluate(context);
    RTLExpression evaldCount = count.evaluate(context);
   
    destination = evaldDestination;
    value = evaldValue;
    count = evaldCount;
   
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

      RTLNumber conditionValue = pair.get(0);
      RTLNumber targetValue = pair.get(1);
      Location nextLabel;
      // assume correct condition case
      assert conditionValue != null;
      RTLExpression assumption =
          ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
      if (conditionValue.equals(ExpressionFactory.FALSE)) {
        // assume (condition = false), and set next statement to fallthrough
        nextLabel = stmt.getNextLabel();
      } else {
        if (targetValue == null) {

          if (stmt.getType() == RTLGoto.Type.CALL) {
            // if it's a call TOP, add an unknown call edge if we're allowing unsound analysis
            RTLUnknownProcedureCall unknownCallEdge = new RTLUnknownProcedureCall(stmt);
            unknownCallEdge.setLabel(stmt.getLabel());
            unknownCallEdge.setNextLabel(stmt.getNextLabel());
            results.add(new CFAEdge(stmt.getLabel(), stmt.getNextLabel(), unknownCallEdge));
            logger.info(stmt.getLabel() + ": Cannot resolve target expression " +
                stmt.getTargetExpression() + " of call. Adding unknown call edge.");
            logger.debug("State is: " + a);
          } else {
            // if target could not be resolved, just leave the edge out for now
            logger.info(stmt.getLabel() + ": Cannot resolve target expression " +
                stmt.getTargetExpression() + ". Continuing with unsound underapproximation.");
            logger.debug("State is: " + a);
            unresolvedBranches.add(stmt.getLabel());
          }
          sound = false;

          continue;
        } else {
          // assume (condition = true AND targetExpression = targetValue)
          assumption = ExpressionFactory.createAnd(
              assumption,
              ExpressionFactory.createEqual(
                  stmt.getTargetExpression(),
                  targetValue)
          );
          // set next label to jump target
          nextLabel = new Location(new AbsoluteAddress(targetValue));
        }
      }
      assumption = assumption.evaluate(new Context());
      RTLAssume assume = new RTLAssume(assumption, stmt);
      assume.setLabel(stmt.getLabel());
      assume.setNextLabel(nextLabel);
      results.add(new CFAEdge(stmt.getLabel(), nextLabel, assume));
    }
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

      RTLNumber conditionValue = pair.get(0);
      RTLNumber targetValue = pair.get(1);
      Location nextLabel;
      // assume correct condition case
      assert conditionValue != null;
      RTLExpression assumption =
          ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
      if (conditionValue.equals(ExpressionFactory.FALSE)) {
        // assume (condition = false), and set next statement to fallthrough
        nextLabel = stmt.getNextLabel();
      } else {
        if (targetValue == null) {
          // if target could not be resolved, just leave the edge out for now
          // Can only happen here with indirect jumps
          logger.info(stmt.getLabel() + ": Cannot resolve target expression " +
              stmt.getTargetExpression() + ".");
          logger.debug("State is: " + a);
          sound = false;
          unresolvedBranches.add(stmt.getLabel());
          continue;
        } else {
          // assume (condition = true AND targetExpression = targetValue)
          assumption = ExpressionFactory.createAnd(
              assumption,
              ExpressionFactory.createEqual(
                  stmt.getTargetExpression(),
                  targetValue)
              );
          // set next label to jump target
          nextLabel = new Location(new AbsoluteAddress(targetValue));
        }
      }
      assumption = assumption.evaluate(new Context());
      RTLAssume assume = new RTLAssume(assumption, stmt);
      assume.setLabel(stmt.getLabel());
      assume.setNextLabel(nextLabel);
      results.add(new CFAEdge(assume.getLabel(), assume.getNextLabel(), assume));
    }
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

      RTLNumber conditionValue = pair.get(0);
      RTLNumber targetValue = pair.get(1);
      Location nextLabel;
      // assume correct condition case
      assert conditionValue != null;
      RTLExpression assumption =
          ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
      if (conditionValue.equals(ExpressionFactory.FALSE)) {
        // assume (condition = false), and set next statement to fallthrough
        nextLabel = stmt.getNextLabel();
      } else {
        if (targetValue == null) {
          // if target could not be resolved, just leave the edge out for now
          logger.info(stmt.getLabel() + ": Cannot resolve target expression " +
              stmt.getTargetExpression() + ". Continuing with unsound underapproximation.");
          logger.debug("State is: " + a);
          sound = false;
          unresolvedBranches.add(stmt.getLabel());
          if (Options.debug.getValue())
            throw new ControlFlowException(a, "Unresolvable control flow from " + stmt.getLabel());
          continue;
        } else {
          // assume (condition = true AND targetExpression = targetValue)
          assumption = ExpressionFactory.createAnd(
              assumption,
              ExpressionFactory.createEqual(
                  stmt.getTargetExpression(),
                  targetValue)
              );
          // set next label to jump target
          nextLabel = new Location(new AbsoluteAddress(targetValue));
        }
      }
      assumption = assumption.evaluate(new Context());
      RTLAssume assume = new RTLAssume(assumption, stmt);
      assume.setLabel(stmt.getLabel());
      assume.setNextLabel(nextLabel);
      // Target address sanity check
      if (nextLabel.getAddress().getValue() < 10L) {
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

      RTLNumber conditionValue = pair.get(0);
      RTLNumber targetValue = pair.get(1);
      Location nextLabel;
      // assume correct condition case
      assert conditionValue != null;
      RTLExpression assumption =
          ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
      if (conditionValue.equals(ExpressionFactory.FALSE)) {
        // assume (condition = false), and set next statement to fallthrough
        nextLabel = stmt.getNextLabel();
      } else {
        if (targetValue == null) {

          if (!Options.allEdges.getValue()) {
            // if target could not be resolved, just leave the edge out for now
            logger.info(stmt.getLabel() + ": Cannot resolve target expression " +
                stmt.getTargetExpression() + ". Continuing with unsound underapproximation.");
            logger.debug("State is: " + a);
            sound = false;
            unresolvedBranches.add(stmt.getLabel());
            if (Options.debug.getValue())
              throw new ControlFlowException(a, "Unresolvable control flow from " + stmt.getLabel());
            continue;
          } else {
            // Over-approximate target and add edges to all possible program locations (!)
            logger.warn(stmt.getLabel() + ": Cannot resolve target expression " +
                stmt.getTargetExpression() + ". Adding over-approximate edges to all program locations!");


            for (Iterator<AbsoluteAddress> it = Program.getProgram().codeAddressIterator(); it.hasNext();) {
              targetValue = it.next().toNumericConstant();
              assumption = ExpressionFactory.createEqual(stmt.getTargetExpression(), targetValue);
              // set next label to jump target
              nextLabel = new Location(new AbsoluteAddress(targetValue));
              RTLAssume assume = new RTLAssume(assumption, stmt);
              assume.setLabel(stmt.getLabel());
              assume.setNextLabel(nextLabel);
              results.add(new CFAEdge(assume.getLabel(), assume.getNextLabel(), assume));
            }

            continue;
          }
        } else {
          // assume (condition = true AND targetExpression = targetValue)
          assumption = ExpressionFactory.createAnd(
              assumption,
              ExpressionFactory.createEqual(
                  stmt.getTargetExpression(),
                  targetValue)
              );
          // set next label to jump target
          nextLabel = new Location(new AbsoluteAddress(targetValue));
        }
      }
      assumption = assumption.evaluate(new Context());
      RTLAssume assume = new RTLAssume(assumption, stmt);
      assume.setLabel(stmt.getLabel());
      assume.setNextLabel(nextLabel);
      // Target address sanity check
      if (nextLabel.getAddress().getValue() < 10L) {
View Full Code Here

Examples of org.jakstab.rtl.expressions.RTLExpression

          RTLNumber conditionValue = pair.get(0);
          RTLNumber targetValue = pair.get(1);
          Location nextLabel;
          // Start building the assume expression: assume correct condition case
          assert conditionValue != null;
          RTLExpression assumption =
              ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
          if (conditionValue.equals(ExpressionFactory.FALSE)) {
            // assume (condition = false), and set next statement to fallthrough
            nextLabel = stmt.getNextLabel();
          } else {
            if (targetValue == null) {
              logger.debug("No value from MAY-analysis at " + stmt.getLabel());
              sound = false;
              unresolvedBranches.add(stmt.getLabel());
              continue;
            }
            // assume (condition = true AND targetExpression = targetValue)
            assumption = ExpressionFactory.createAnd(
                assumption,
                ExpressionFactory.createEqual(
                    stmt.getTargetExpression(),
                    targetValue)
                );
            // set next label to jump target
            nextLabel = new Location(new AbsoluteAddress(targetValue));
          }
          assumption = assumption.evaluate(new Context());
          RTLAssume assume = new RTLAssume(assumption, stmt);
          assume.setLabel(stmt.getLabel());
          assume.setNextLabel(nextLabel);
          // Target address sanity check
          if (nextLabel.getAddress().getValue() < 10L) {
            logger.warn("Control flow from " + stmt.getLabel() + " reaches address " + nextLabel.getAddress() + "!");
          }

          results.add(new CFAEdge(assume.getLabel(), assume.getNextLabel(), assume, Kind.MAY));
        }

        // Add all edges from under-approximation

        for (Tuple<RTLNumber> pair : dcs.projection(stmt.getCondition(), stmt.getTargetExpression())) {
          RTLNumber conditionValue = pair.get(0);
          RTLNumber targetValue = pair.get(1);
          Location nextLabel;
          // Start building the assume expression: assume correct condition case
          assert conditionValue != null;
          RTLExpression assumption =
              ExpressionFactory.createEqual(stmt.getCondition(), conditionValue);
          if (conditionValue.equals(ExpressionFactory.FALSE)) {
            // assume (condition = false), and set next statement to fallthrough
            nextLabel = stmt.getNextLabel();
          } else {
            assert targetValue != null;

            // Translate real library addresses into stub addresses. Necessary because the static analysis component
            // does not know about concrete import addresses, so it uses a stub system.
            if (!isProgramAddress(targetValue)) {
              logger.debug(dcs.getLocation() + ": Jumping out of module to " + targetValue.toHexString());

              // Attempt to map this out-of-module location to a stub
              if (realToStub.containsKey(targetValue)) {
                // If we saw this concrete address before, replace it by the known stub
                logger.debug("Replacing concrete target " + targetValue.toHexString() + " with stub to " + program.getSymbolFor(new AbsoluteAddress(realToStub.get(targetValue))));
                targetValue = realToStub.get(targetValue);
              } else {

                // Check the statically produced edges for one that is not yet mapped to a concrete address.
                // If the over-approximation resolved an import to a stub, it's going to be contained.
                boolean foundStub = false;
                for (CFAEdge e : results) {
                  RTLNumber staticTarget = e.getTarget().getAddress().toNumericConstant();
                  if (!isProgramAddress(staticTarget) && !stubToReal.containsKey(staticTarget)) {
                    // Take the first one that's neither taken nor in the program
                    // TODO: This could map the wrong addresses in some (hopefully) rare cases depending on analysis order
                    stubToReal.put(staticTarget, targetValue);
                    realToStub.put(targetValue, staticTarget);
                    targetValue = staticTarget;
                    foundStub = true;
                    break;
                  }
                }

                if (!foundStub) {
                  // If we have not found anything suitable, we need to create a new stub
                  // FIXME: The new stub will likely have incorrect stack height adjustment.
                  //        We should extract that information from the trace.

                  logger.info(dcs.getLocation() + ": Creating new stub for unknown function at " + targetValue.toHexString());
                  RTLNumber stubTarget = Program.getProgram().getProcAddress("JAK_UNKNOWN", "proc" + targetValue.toHexString()).toNumericConstant();
                  stubToReal.put(stubTarget, targetValue);
                  realToStub.put(targetValue, stubTarget);
                  targetValue = stubTarget;
                }
              }

            }

            // assume (condition = true AND targetExpression = targetValue)
            assumption = ExpressionFactory.createAnd(
                assumption,
                ExpressionFactory.createEqual(
                    stmt.getTargetExpression(),
                    targetValue)
                );
            // set next label to jump target
            nextLabel = new Location(new AbsoluteAddress(targetValue));
          }
          assumption = assumption.evaluate(new Context());
          RTLAssume assume = new RTLAssume(assumption, stmt);
          assume.setLabel(stmt.getLabel());
          assume.setNextLabel(nextLabel);

          results.add(new CFAEdge(assume.getLabel(), assume.getNextLabel(), assume, Kind.MUST));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.