Пример #1
0
  /** Conveniently create a class deriving from Code.testClass */
  public Pair<SootClass, SignatureTable<Level>> makeDerivedClass(
      String name, SootClass superClass, List<MethodWithSignature<Level>> methods) {
    SootClass result = makeFreshClass(name);
    result.setSuperclass(superClass);
    SignatureTable<Level> newSignatures = this.signatures;
    for (MethodWithSignature<Level> m : methods) {
      result.addMethod(m.method);
      newSignatures =
          newSignatures.extendWith(
              m.method,
              Interop.asJavaStream(m.signature.constraints.stream()).collect(Collectors.toList()),
              m.signature.effects);
    }

    return Pair.of(result, newSignatures);
  }
Пример #2
0
  /**
   * Create a method conveniently. The method is added to the class "TestClass". Parameters can be
   * given as an (positional) array of local variables (the "identity statements", required by Soot
   * to map parameters to local variables, are inserted automatically)
   */
  public SootMethod makeMethod(
      int modifier, String name, List<Local> params, soot.Type retType, List<Unit> bodyStmts) {
    SootMethod m =
        new SootMethod(
            name, params.stream().map(Local::getType).collect(toList()), retType, modifier);
    this.testClass.addMethod(m);
    Body body = Jimple.v().newBody(m);
    m.setActiveBody(body);

    // set the statements for the body.. first the identity statements, then the bodyStmts
    if (!m.isStatic()) {
      body.getLocals().add(localThis);
      body.getUnits()
          .add(Jimple.v().newIdentityStmt(localThis, Jimple.v().newThisRef(testClass.getType())));
    }
    IntStream.range(0, params.size())
        .forEach(
            pos -> {
              Local l = params.get(pos);
              ParameterRef pr = Jimple.v().newParameterRef(l.getType(), pos);
              body.getUnits().add(Jimple.v().newIdentityStmt(l, pr));
            });
    body.getUnits().addAll(bodyStmts);

    // set the locals for the body
    Set<Local> locals =
        Stream.concat(
                params.stream(),
                body.getUseAndDefBoxes()
                    .stream()
                    .filter(b -> b.getValue() instanceof Local)
                    .map(b -> (Local) b.getValue()))
            .collect(toSet());
    locals.removeAll(body.getLocals());
    body.getLocals().addAll(locals);

    return m;
  }
Пример #3
0
  public Code(TypeVars tvars) {

    this.j = Jimple.v();

    this.localX = j.newLocal("x", IntType.v());
    this.localY = j.newLocal("y", IntType.v());
    this.localZ = j.newLocal("z", IntType.v());
    this.localO = j.newLocal("o", RefType.v("java.lang.Object"));
    this.localT = j.newLocal("t", RefType.v("TestClass"));
    this.localThis = j.newLocal("this", RefType.v("TestClass"));
    this.varX = Var.fromLocal(localX);
    this.varY = Var.fromLocal(localY);
    this.varZ = Var.fromLocal(localZ);
    this.varO = Var.fromLocal(localO);
    this.varT = Var.fromLocal(localT);
    this.varThis = Var.fromLocal(localThis);
    this.tvarX = tvars.testParam(varX, "");
    this.tvarY = tvars.testParam(varY, "");
    this.tvarZ = tvars.testParam(varZ, "");
    this.tvarO = tvars.testParam(varO, "");
    this.tvarT = tvars.testParam(varT, "");
    this.tvarThis = tvars.testParam(varThis, "");

    this.init =
        Environments.makeEmpty()
            .add(varX, tvarX)
            .add(varY, tvarY)
            .add(varZ, tvarZ)
            .add(varO, tvarO)
            .add(varT, tvarT)
            .add(varThis, tvarThis);

    this.testClass = makeFreshClass("TestClass");

    Map<SootField, TypeDomain.Type<Level>> fieldMap = new HashMap<>();

    this.testLowField_int = new SootField("testLowField", IntType.v(), 0);
    testClass.addField(this.testLowField_int);
    fieldMap.put(testLowField_int, TLOW);
    this.testHighField_int = new SootField("testHighField", IntType.v(), 0);
    testClass.addField(this.testHighField_int);
    fieldMap.put(testHighField_int, THIGH);
    this.testStaticDynField_int = new SootField("testStaticDynField", IntType.v(), Modifier.STATIC);
    testClass.addField(this.testStaticDynField_int);
    fieldMap.put(testStaticDynField_int, DYN);

    // freeze field map
    this.fields = new FieldTable<>(fieldMap);

    Map<SootMethod, Signature<Level>> sigMap = new HashMap<>();
    Symbol.Param<LowHigh.Level> param_x = param(0);
    Symbol.Param<LowHigh.Level> param_y = param(1);

    // Method:
    // int testCallee()
    //    with {} effect {}
    this.testCallee__int =
        new SootMethod("testCallee", Collections.emptyList(), IntType.v(), Modifier.ABSTRACT);
    this.testClass.addMethod(testCallee__int);
    sigMap.put(
        this.testCallee__int,
        makeSignature(
            this.testCallee__int.getParameterCount(),
            Stream.of(leS(Symbol.literal(PUB), ret())).collect(toList()),
            Effects.emptyEffect()));

    // Method:
    // int testCallee_int_int__int (int, int)
    //   with {@param1 <= @ret, @param2 <= @ret} effect {}
    this.testCallee_int_int__int =
        new SootMethod(
            "testCallee", asList(IntType.v(), IntType.v()), IntType.v(), Modifier.ABSTRACT);
    this.testClass.addMethod(testCallee_int_int__int);
    Stream<SigConstraint<Level>> sigCstrs = Stream.of(leS(param_x, ret()), leS(param_y, ret()));
    sigMap.put(
        this.testCallee_int_int__int,
        makeSignature(
            this.testCallee_int_int__int.getParameterCount(),
            sigCstrs.collect(toList()),
            Effects.emptyEffect()));

    // Method:
    // int ignoreSnd(int, int)
    //   with { @param1 <= @ret } effect {}
    this.ignoreSnd_int_int__int =
        new SootMethod(
            "ignoreSnd", asList(IntType.v(), IntType.v()), IntType.v(), Modifier.ABSTRACT);
    this.testClass.addMethod(ignoreSnd_int_int__int);
    sigCstrs = Stream.of(leS(param_x, ret()), leS(param_y, param_y));
    sigMap.put(
        this.ignoreSnd_int_int__int,
        makeSignature(
            this.ignoreSnd_int_int__int.getParameterCount(),
            sigCstrs.collect(toList()),
            Effects.emptyEffect()));

    // Method:
    // int writeToLowReturn0(int)
    //   with { @param0 <= LOW } effect { LOW }
    this.writeToLowReturn0_int__int =
        new SootMethod("writeToLow", singletonList(IntType.v()), IntType.v(), Modifier.ABSTRACT);
    this.testClass.addMethod(this.writeToLowReturn0_int__int);
    sigCstrs = Stream.of((leS(param_x, literal(TLOW))), leS(ret(), ret()));
    sigMap.put(
        this.writeToLowReturn0_int__int,
        makeSignature(
            this.writeToLowReturn0_int__int.getParameterCount(),
            sigCstrs.collect(toList()),
            Effects.makeEffects(TLOW)));

    // Method:
    // int ignore0Low1ReturnHigh(int, int)
    //   with { @param1 <= LOW, HIGH <= ret } effect {}
    this.ignore0Low1ReturnHigh =
        new SootMethod(
            "ignore0Low1ReturnHigh",
            asList(IntType.v(), IntType.v()),
            IntType.v(),
            Modifier.ABSTRACT);
    this.testClass.addMethod(this.ignore0Low1ReturnHigh);
    sigCstrs = Stream.of(leS(param(1), literal(TLOW)), leS(literal(THIGH), ret()));
    sigMap.put(
        this.ignore0Low1ReturnHigh,
        makeSignature(
            this.ignore0Low1ReturnHigh.getParameterCount(),
            sigCstrs.collect(toList()),
            Effects.emptyEffect()));

    // freeze signatures
    this.signatures = makeTable(sigMap);
  }