From 1fb79eb208e81a558bb0b361160191d3317fea86 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Mon, 30 Oct 2023 17:40:49 +0100 Subject: [PATCH 01/22] Refactored NewValidator.java to work with Sootup. Deleting CheckEscapingValidator.java and ReturnStatementsValidator.java. --- .../binary/NewKeywordValidator.class | Bin 0 -> 463 bytes .../source/NewKeywordValidator.java | 9 + .../java/sootup/core/jimple/basic/Local.java | 11 +- .../src/main/java/sootup/core/model/Body.java | 3 +- .../validation/CheckEscapingValidator.java | 49 ---- .../sootup/core/validation/NewValidator.java | 239 ++++++++++-------- .../validation/ReturnStatementsValidator.java | 63 ----- .../NewKeywordValidatorTest.java | 32 +++ 8 files changed, 190 insertions(+), 216 deletions(-) create mode 100644 shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class create mode 100644 shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java delete mode 100644 sootup.core/src/main/java/sootup/core/validation/CheckEscapingValidator.java delete mode 100644 sootup.core/src/main/java/sootup/core/validation/ReturnStatementsValidator.java create mode 100644 sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java diff --git a/shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class b/shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class new file mode 100644 index 0000000000000000000000000000000000000000..9531b60cb6c7d97534c810bbaf5686b5ef71fe56 GIT binary patch literal 463 zcmZuuT}uK%6g{JxtJ|iPY4%+&p$7R-Z$>XcAc+Nr5Iv1L$jE-gwr2jUq7n-F0sW}x ztOX+UFn8wOd(OS*-ue1|{{(Q1T?;0bQ-~vLV#VT3Cx^U+RTNCD35Z9Ynn2=2dpbN7 zh!;yO0prZ?DuGO0d#W*T+bX!0ZHE!VmD&@?7jNr*`5-Hf^m>(M7-+9ojfhs$9|RqB zt|M@vp@tV~H1vaROFFtMLq9l-Xf`a&+ejiMko^Z_V%^3DHcf2V*v5{);ldp(9P0$Op7IP8PV2u>g$oz4R7>_d955#ZAFzU~kJ9t6t4T&i^ RO%jk`Q~nt(C!`<4(hnq|S-SuL literal 0 HcmV?d00001 diff --git a/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java b/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java new file mode 100644 index 00000000000..d3057c84b9a --- /dev/null +++ b/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java @@ -0,0 +1,9 @@ +public class NewKeywordValidator { + + int x = 5; + + public static void main(String[] args) { + NewKeywordValidator myObj = new NewKeywordValidator(); + System.out.println(myObj.x); + } +} diff --git a/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java b/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java index 07cf93cf5eb..88a98571301 100644 --- a/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java +++ b/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java @@ -33,6 +33,7 @@ import sootup.core.model.Body; import sootup.core.model.Position; import sootup.core.types.Type; +import sootup.core.types.VoidType; import sootup.core.util.Copyable; import sootup.core.util.printer.StmtPrinter; @@ -55,9 +56,15 @@ public Local(@Nonnull String name, @Nonnull Type type) { } /** Constructs a JimpleLocal of the given name and type. */ - public Local(@Nonnull String name, @Nonnull Type type, @Nonnull Position position) { + public Local(@Nonnull String name, @Nonnull Type type, @Nonnull Position position) { this.name = name; - this.type = type; + if(type instanceof VoidType) + { + throw new RuntimeException("Type should not be VoidType"); + } + else { + this.type = type; + } this.position = position; } diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index 706d8e7e8d8..dfc9dba4d6f 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -68,8 +68,7 @@ public class Body implements Copyable { new ValuesValidator(), new CheckInitValidator(), new CheckTypesValidator(), - new CheckVoidLocalesValidator(), - new CheckEscapingValidator()); + new CheckVoidLocalesValidator()); /** * Creates an body which is not associated to any method. diff --git a/sootup.core/src/main/java/sootup/core/validation/CheckEscapingValidator.java b/sootup.core/src/main/java/sootup/core/validation/CheckEscapingValidator.java deleted file mode 100644 index 1973d840c07..00000000000 --- a/sootup.core/src/main/java/sootup/core/validation/CheckEscapingValidator.java +++ /dev/null @@ -1,49 +0,0 @@ -package sootup.core.validation; - -/*- - * #%L - * Soot - a J*va Optimization Framework - * %% - * Copyright (C) 1997-2020 Raja Vallée-Rai, Linghui Luo - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * . - * #L% - */ - -import java.util.List; -import sootup.core.model.Body; - -public class CheckEscapingValidator implements BodyValidator { - - @Override - public void validate(Body body, List exception) { - - // TODO: check code from old soot below - - /* - * for (Unit u : body.getUnits()) { if (u instanceof Stmt) { Stmt stmt = (Stmt) u; if (stmt.containsInvokeExpr()) { - * InvokeExpr iexpr = stmt.getInvokeExpr(); SootMethodRef ref = iexpr.getMethodRef(); if (ref.name().contains("'") || - * ref.declaringClass().getName().contains("'")) { throw new ValidationException(stmt, - * "Escaped name in signature found"); } for (Type paramType : ref.parameterTypes()) { if - * (paramType.toString().contains("'")) { throw new ValidationException(stmt, "Escaped name in signature found"); } } } } - * } - */ - } - - @Override - public boolean isBasicValidator() { - return false; - } -} diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index 6d7f94904e6..dc9ce7a80e3 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -1,37 +1,19 @@ package sootup.core.validation; -/*- - * #%L - * Soot - a J*va Optimization Framework - * %% - * Copyright (C) 1997-2020 Raja Vallée-Rai, Christian Brüggemann, Markus Schmidt and others - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * . - * #L% - */ - -import java.util.List; +import java.util.*; +import sootup.core.graph.StmtGraph; +import sootup.core.jimple.basic.Local; +import sootup.core.jimple.basic.Value; +import sootup.core.jimple.common.expr.AbstractInvokeExpr; +import sootup.core.jimple.common.expr.JNewExpr; +import sootup.core.jimple.common.expr.JSpecialInvokeExpr; +import sootup.core.jimple.common.stmt.JAssignStmt; +import sootup.core.jimple.common.stmt.JInvokeStmt; +import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; +import sootup.core.types.ReferenceType; +import sootup.core.types.UnknownType; -/** - * A relatively simple validator. It tries to check whether after each new-expression-statement - * there is a corresponding call to the <init> method before a use or the end of the method. - * - * @author Marc Miltenberger - * @author Steven Arzt - */ public class NewValidator implements BodyValidator { private static final String errorMsg = @@ -39,78 +21,135 @@ public class NewValidator implements BodyValidator { public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; - /** Checks whether after each new-instruction a constructor call follows. */ + // Checks whether after each new-instruction a constructor call follows. / @Override public void validate(Body body, List exceptions) { - // TODO: check copied code from old soot - /* - * UnitGraph g = new BriefUnitGraph(body); for (Unit u : body.getUnits()) { if (u instanceof JAssignStmt) { JAssignStmt - * assign = (JAssignStmt) u; - * - * // First seek for a JNewExpr. if (assign.getRightOp() instanceof JNewExpr) { if (!(assign.getLeftOp().getType() - * instanceof RefType)) { exceptions.add(new ValidationException(u, - * "A new-expression must be used on reference type locals", - * String.format("Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", - * body.getMethod().getSignature()))); return; } - * - * // We search for a JSpecialInvokeExpr on the local. LinkedHashSet locals = new LinkedHashSet(); - * locals.add((Local) assign.getLeftOp()); - * - * checkForInitializerOnPath(g, assign, exceptions); } } } - * - * } - * - * - *

Checks whether all pathes from start to the end of the methodRef have a call to the <init> methodRef in - * between.

$r0 = new X;
...
specialinvoke $r0.()>; //validator checks whether this - * statement is missing

Regarding aliasingLocals:
The first local in the set is always the local - * on the LHS of the new-expression-assignment (called: original local; in the example $r0).

- * - * @param g the unit graph of the methodRef - * - * @param exception the list of all collected exceptions - * - * @return true if a call to a <init>-Method has been found on this way. - * - * private boolean checkForInitializerOnPath(UnitGraph g, AssignStmt newStmt, List exception) { - * List workList = new ArrayList(); Set doneSet = new HashSet(); workList.add(newStmt); - * - * Set aliasingLocals = new HashSet(); aliasingLocals.add((Local) newStmt.getLeftOp()); - * - * while (!workList.isEmpty()) { Stmt curStmt = (Stmt) workList.remove(0); if (!doneSet.add(curStmt)) { continue; } if - * (!newStmt.equals(curStmt)) { if (curStmt.containsInvokeExpr()) { InvokeExpr expr = curStmt.getInvokeExpr(); if - * (expr.getMethod().isConstructor()) { if (!(expr instanceof SpecialInvokeExpr)) { exception.add(new - * ValidationException(curStmt, " methodRef calls may only be used with specialinvoke.")); // At least we found an - * initializer, so we return true... return true; } if (!(curStmt instanceof InvokeStmt)) { exception.add(new - * ValidationException(curStmt, " methods may only be called with invoke statements.")); // At least we found an - * initializer, so we return true... return true; } - * - * SpecialInvokeExpr invoke = (SpecialInvokeExpr) expr; if (aliasingLocals.contains(invoke.getBase())) { // We are happy - * now, continue the loop and check other paths continue; } } } - * - * // We are still in the loop, so this was not the constructor call we // were looking for boolean creatingAlias = - * false; if (curStmt instanceof AssignStmt) { AssignStmt assignCheck = (AssignStmt) curStmt; if - * (aliasingLocals.contains(assignCheck.getRightOp())) { if (assignCheck.getLeftOp() instanceof Local) { // A new alias - * is created. aliasingLocals.add((Local) assignCheck.getLeftOp()); creatingAlias = true; } } Local originalLocal = - * aliasingLocals.iterator().next(); if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: - * - * // Handles cases like // $r0 = new x; // $r0 = null; - * - * // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check for the original local - * continue; } else { // Since the local on the left hand side gets overwritten // even if it was aliasing with our - * original local, // now it does not any more... aliasingLocals.remove(assignCheck.getLeftOp()); } } - * - * if (!creatingAlias) { for (ValueBox box : curStmt.getUseBoxes()) { Value used = box.getValue(); if - * (aliasingLocals.contains(used)) { // The current unit uses one of the aliasing locals, but // there was no initializer - * in between. // However, when creating such an alias, the use is okay. exception.add(new ValidationException(newStmt, - * String.format(errorMsg, newStmt, curStmt))); return false; } } } } // Enqueue the successors List successors = - * g.getSuccsOf(curStmt); if (successors.isEmpty() && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { // This means that we are - * e.g. at the end of the methodRef // There was no call on our way... exception.add(new - * ValidationException(newStmt, String.format(errorMsg, newStmt, curStmt))); return false; } workList.addAll(successors); - * } return true; - * - * - */ + + StmtGraph g = body.getStmtGraph(); + for (Stmt u : body.getStmts()) { + if (u instanceof JAssignStmt) { + JAssignStmt assign = (JAssignStmt) u; + + // First seek for a JNewExpr. + if (assign.getRightOp() instanceof JNewExpr) { + if (!((assign.getLeftOp().getType() instanceof ReferenceType) + || assign.getLeftOp().getType() instanceof UnknownType)) { + exceptions.add( + new ValidationException( + assign.getLeftOp(), + String.format( + "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", + body.getMethodSignature()))); + return; + } + + // We search for a JSpecialInvokeExpr on the local. + LinkedHashSet locals = new LinkedHashSet(); + locals.add((Local) assign.getLeftOp()); + + checkForInitializerOnPath(g, assign, exceptions); + } + } + } + } + + private boolean checkForInitializerOnPath( + StmtGraph g, JAssignStmt newStmt, List exception) { + List workList = new ArrayList(); + Set doneSet = new HashSet(); + workList.add(newStmt); + + Set aliasingLocals = new HashSet(); + aliasingLocals.add((Local) newStmt.getLeftOp()); + + while (!workList.isEmpty()) { + Stmt curStmt = (Stmt) workList.remove(0); + if (!doneSet.add(curStmt)) { + continue; + } + if (!newStmt.equals(curStmt)) { + if (curStmt.containsInvokeExpr()) { + AbstractInvokeExpr expr = curStmt.getInvokeExpr(); + if (!(expr instanceof JSpecialInvokeExpr)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methodRef calls may only be used with specialinvoke.")); // At least we + // found an + // initializer, + // so we return + // true...return true; + } + if (!(curStmt instanceof JInvokeStmt)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methods may only be called with invoke statements.")); // At least we + // found an + // initializer, + // so we return + // true...return + // true; + } + + JSpecialInvokeExpr invoke = (JSpecialInvokeExpr) expr; + if (aliasingLocals.contains( + invoke.getBase())) { // We are happy now,continue the loop and check other paths + // continue; + } + } + + // We are still in the loop, so this was not the constructor call we // were looking for + boolean creatingAlias = false; + if (curStmt instanceof JAssignStmt) { + JAssignStmt assignCheck = (JAssignStmt) curStmt; + if (aliasingLocals.contains(assignCheck.getRightOp())) { + if (assignCheck.getLeftOp() + instanceof Local) { // A new aliasis created.aliasingLocals.add((Local) + // assignCheck.getLeftOp()); + creatingAlias = true; + } + } + Local originalLocal = aliasingLocals.iterator().next(); + if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: + + // Handles cases like // $r0 = new x; // $r0 = null; + + // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check + // for the original local + continue; + } else { // Since the local on the left hand side gets overwritten // even if it was + // aliasing with our original local, // now it does not any more... + // aliasingLocals.remove(assignCheck.getLeftOp()); } } + + if (!creatingAlias) { + for (Value box : curStmt.getUses()) { + if (aliasingLocals.contains( + box)) { // The current unit uses one of the aliasing locals, but // there was no + // initializer in between. // However, when creating such an alias, the + // use is okay. exception.add(new ValidationException(newStmt, + String.format(errorMsg, newStmt, curStmt); + return false; + } + } + } + } + // Enqueue the successors + List successors = g.successors(curStmt); + if (successors.isEmpty() + && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { // This means that we are e.g.at the end of + // the methodRef // There was no call + // on our way... + exception.add( + new ValidationException( + newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + return false; + } + workList.addAll(successors); + } + } + } + return true; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/ReturnStatementsValidator.java b/sootup.core/src/main/java/sootup/core/validation/ReturnStatementsValidator.java deleted file mode 100644 index e76f5922c58..00000000000 --- a/sootup.core/src/main/java/sootup/core/validation/ReturnStatementsValidator.java +++ /dev/null @@ -1,63 +0,0 @@ -package sootup.core.validation; - -/*- - * #%L - * Soot - a J*va Optimization Framework - * %% - * Copyright (C) 1997-2020 Raja Vallée-Rai, Markus Schmidt and others - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * . - * #L% - */ - -import java.util.List; -import sootup.core.model.Body; - -public class ReturnStatementsValidator implements BodyValidator { - - /** - * Checks the following invariants on this Jimple body: - * - *
    - *
  1. this-references may only occur in instance methods - *
  2. this-references may only occur as the first statement in a method, if they occur at all - *
  3. param-references must precede all statements that are not themselves param-references or - * this-references, if they occur at all - *
- */ - @Override - public void validate(Body body, List exceptions) { - - // TODO: check copied code from old soot - /* - * // Checks that this Jimple body actually contains a return statement for (Unit u : body.getUnits()) { if ((u - * instanceof JReturnStmt) || (u instanceof JReturnVoidStmt) || (u instanceof JRetStmt) || (u instanceof JThrowStmt)) { - * return; } } - * - * // A methodRef can have an infinite loop // and no return statement: // // public class Infinite { // public static - * void main(String[] args) { // int i = 0; while (true) {i += 1;} } } // // Only check that the execution cannot fall - * off the code. Unit last = body.getUnits().getLast(); if (last instanceof JGotoStmt|| last instanceof JThrowStmt) { - * return; } - * - * exceptions.add(new ValidationException(body.getMethod(), "The methodRef does not contain a return statement", - * "Body of methodRef " + body.getMethod().getSignature() + " does not contain a return statement")); - */ - } - - @Override - public boolean isBasicValidator() { - return true; - } -} diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java new file mode 100644 index 00000000000..4ab027d1dbd --- /dev/null +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java @@ -0,0 +1,32 @@ +package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Test; +import sootup.core.validation.NewValidator; +import sootup.core.validation.ValidationException; +import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; +import sootup.java.core.JavaSootClass; +import sootup.java.core.JavaSootMethod; + +public class NewKeywordValidatorTest extends MinimalBytecodeTestSuiteBase { + + @Test + public void testNewValidator() { + JavaSootClass sootClass = loadClass(getDeclaredClassSignature()); + + List main = + sootClass.getMethods().stream() + .filter(javaSootMethod -> javaSootMethod.getName().contains("main")) + .collect(Collectors.toList()); + + List validationExceptions = new ArrayList<>(); + + new NewValidator().validate(main.get(0).getBody(), validationExceptions); + + assertEquals(0, validationExceptions.size()); + } +} From dc0277ba701b68ffcabae29947612f8d3938a534 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Mon, 30 Oct 2023 18:33:50 +0100 Subject: [PATCH 02/22] Changes in NewValidator.java --- .../java/sootup/core/jimple/basic/Local.java | 8 +- .../sootup/core/validation/NewValidator.java | 262 +++++++++--------- 2 files changed, 134 insertions(+), 136 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java b/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java index 88a98571301..88edf8a8e8e 100644 --- a/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java +++ b/sootup.core/src/main/java/sootup/core/jimple/basic/Local.java @@ -56,13 +56,11 @@ public Local(@Nonnull String name, @Nonnull Type type) { } /** Constructs a JimpleLocal of the given name and type. */ - public Local(@Nonnull String name, @Nonnull Type type, @Nonnull Position position) { + public Local(@Nonnull String name, @Nonnull Type type, @Nonnull Position position) { this.name = name; - if(type instanceof VoidType) - { + if (type instanceof VoidType) { throw new RuntimeException("Type should not be VoidType"); - } - else { + } else { this.type = type; } this.position = position; diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index dc9ce7a80e3..5c3eff61ce3 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -1,6 +1,7 @@ package sootup.core.validation; import java.util.*; + import sootup.core.graph.StmtGraph; import sootup.core.jimple.basic.Local; import sootup.core.jimple.basic.Value; @@ -16,144 +17,143 @@ public class NewValidator implements BodyValidator { - private static final String errorMsg = - "There is a path from '%s' to the usage '%s' where does not get called in between."; - - public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; - - // Checks whether after each new-instruction a constructor call follows. / - @Override - public void validate(Body body, List exceptions) { - - StmtGraph g = body.getStmtGraph(); - for (Stmt u : body.getStmts()) { - if (u instanceof JAssignStmt) { - JAssignStmt assign = (JAssignStmt) u; - - // First seek for a JNewExpr. - if (assign.getRightOp() instanceof JNewExpr) { - if (!((assign.getLeftOp().getType() instanceof ReferenceType) - || assign.getLeftOp().getType() instanceof UnknownType)) { - exceptions.add( - new ValidationException( - assign.getLeftOp(), - String.format( - "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", - body.getMethodSignature()))); - return; - } - - // We search for a JSpecialInvokeExpr on the local. - LinkedHashSet locals = new LinkedHashSet(); - locals.add((Local) assign.getLeftOp()); - - checkForInitializerOnPath(g, assign, exceptions); + private static final String errorMsg = + "There is a path from '%s' to the usage '%s' where does not get called in between."; + + public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; + + // Checks whether after each new-instruction a constructor call follows. + @Override + public void validate(Body body, List exceptions) { + + StmtGraph g = body.getStmtGraph(); + for (Stmt u : body.getStmts()) { + if (u instanceof JAssignStmt) { + JAssignStmt assign = (JAssignStmt) u; + + // First seek for a JNewExpr. + if (assign.getRightOp() instanceof JNewExpr) { + if (!((assign.getLeftOp().getType() instanceof ReferenceType) + || assign.getLeftOp().getType() instanceof UnknownType)) { + exceptions.add( + new ValidationException( + assign.getLeftOp(), + String.format( + "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", + body.getMethodSignature()))); + return; + } + + // We search for a JSpecialInvokeExpr on the local. + LinkedHashSet locals = new LinkedHashSet(); + locals.add((Local) assign.getLeftOp()); + + checkForInitializerOnPath(g, assign, exceptions); + } + } } - } } - } - - private boolean checkForInitializerOnPath( - StmtGraph g, JAssignStmt newStmt, List exception) { - List workList = new ArrayList(); - Set doneSet = new HashSet(); - workList.add(newStmt); - - Set aliasingLocals = new HashSet(); - aliasingLocals.add((Local) newStmt.getLeftOp()); - - while (!workList.isEmpty()) { - Stmt curStmt = (Stmt) workList.remove(0); - if (!doneSet.add(curStmt)) { - continue; - } - if (!newStmt.equals(curStmt)) { - if (curStmt.containsInvokeExpr()) { - AbstractInvokeExpr expr = curStmt.getInvokeExpr(); - if (!(expr instanceof JSpecialInvokeExpr)) { - exception.add( - new ValidationException( - curStmt.getInvokeExpr(), - " methodRef calls may only be used with specialinvoke.")); // At least we - // found an - // initializer, - // so we return - // true...return true; - } - if (!(curStmt instanceof JInvokeStmt)) { - exception.add( - new ValidationException( - curStmt.getInvokeExpr(), - " methods may only be called with invoke statements.")); // At least we - // found an - // initializer, - // so we return - // true...return - // true; - } - - JSpecialInvokeExpr invoke = (JSpecialInvokeExpr) expr; - if (aliasingLocals.contains( - invoke.getBase())) { // We are happy now,continue the loop and check other paths - // continue; - } - } - // We are still in the loop, so this was not the constructor call we // were looking for - boolean creatingAlias = false; - if (curStmt instanceof JAssignStmt) { - JAssignStmt assignCheck = (JAssignStmt) curStmt; - if (aliasingLocals.contains(assignCheck.getRightOp())) { - if (assignCheck.getLeftOp() - instanceof Local) { // A new aliasis created.aliasingLocals.add((Local) - // assignCheck.getLeftOp()); - creatingAlias = true; + private boolean checkForInitializerOnPath( + StmtGraph g, JAssignStmt newStmt, List exception) { + List workList = new ArrayList(); + Set doneSet = new HashSet(); + workList.add(newStmt); + + Set aliasingLocals = new HashSet(); + aliasingLocals.add((Local) newStmt.getLeftOp()); + + while (!workList.isEmpty()) { + Stmt curStmt = (Stmt) workList.remove(0); + if (!doneSet.add(curStmt)) { + continue; } - } - Local originalLocal = aliasingLocals.iterator().next(); - if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: - - // Handles cases like // $r0 = new x; // $r0 = null; - - // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check - // for the original local - continue; - } else { // Since the local on the left hand side gets overwritten // even if it was - // aliasing with our original local, // now it does not any more... - // aliasingLocals.remove(assignCheck.getLeftOp()); } } - - if (!creatingAlias) { - for (Value box : curStmt.getUses()) { - if (aliasingLocals.contains( - box)) { // The current unit uses one of the aliasing locals, but // there was no - // initializer in between. // However, when creating such an alias, the - // use is okay. exception.add(new ValidationException(newStmt, - String.format(errorMsg, newStmt, curStmt); - return false; + if (!newStmt.equals(curStmt)) { + if (curStmt.containsInvokeExpr()) { + AbstractInvokeExpr expr = curStmt.getInvokeExpr(); + if (!(expr instanceof JSpecialInvokeExpr)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methodRef calls may only be used with specialinvoke.")); // At least we + // found an initializer, so we return true... + return true; + } + if (!(curStmt instanceof JInvokeStmt)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methods may only be called with invoke statements.")); // At least we + // found an initializer, so we return true... + return true; + } + + JSpecialInvokeExpr invoke = (JSpecialInvokeExpr) expr; + if (aliasingLocals.contains(invoke.getBase())) { + // We are happy now,continue the loop and check other paths + continue; + } } - } + + // We are still in the loop, so this was not the constructor call we were looking for + boolean creatingAlias = false; + if (curStmt instanceof JAssignStmt) { + JAssignStmt assignCheck = (JAssignStmt) curStmt; + if (aliasingLocals.contains(assignCheck.getRightOp())) { + if (assignCheck.getLeftOp() instanceof Local) { + // A new alias is created. + aliasingLocals.add((Local) assignCheck.getLeftOp()); + creatingAlias = true; + } + } + Local originalLocal = aliasingLocals.iterator().next(); + if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: + + // Handles cases like // $r0 = new x; // $r0 = null; + + // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check + // for the original local + continue; + } else { + // Since the local on the left hand side gets overwritten + // even if it was aliasing with our original local, + // now it does not any more... + aliasingLocals.remove(assignCheck.getLeftOp()); + } + } + + if (!creatingAlias) { + for (Value box : curStmt.getUses()) { + if (aliasingLocals.contains(box)) { + // The current unit uses one of the aliasing locals, but + // there was no initializer in between. + // However, when creating such an alias, the use is okay. + exception.add( + new ValidationException( + newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + return false; + } + } + } + } + // Enqueue the successors + List successors = g.successors(curStmt); + if (successors.isEmpty() && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { + // This means that we are e.g.at the end of + // the methodRef // There was no call + // on our way... + exception.add( + new ValidationException( + newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + return false; } - } - // Enqueue the successors - List successors = g.successors(curStmt); - if (successors.isEmpty() - && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { // This means that we are e.g.at the end of - // the methodRef // There was no call - // on our way... - exception.add( - new ValidationException( - newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); - return false; - } - workList.addAll(successors); + workList.addAll(successors); } - } + return true; } - return true; - } - @Override - public boolean isBasicValidator() { - return false; - } + @Override + public boolean isBasicValidator() { + return false; + } } From be5facec8c0e346bc3c558b3a1907fe03129f212 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 2 Nov 2023 12:36:33 +0100 Subject: [PATCH 03/22] Formatting changes after executing fmt maven plugin. --- .../sootup/core/validation/NewValidator.java | 263 +++++++++--------- 1 file changed, 131 insertions(+), 132 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index 5c3eff61ce3..6fe3e4e3e3f 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -1,7 +1,6 @@ package sootup.core.validation; import java.util.*; - import sootup.core.graph.StmtGraph; import sootup.core.jimple.basic.Local; import sootup.core.jimple.basic.Value; @@ -17,143 +16,143 @@ public class NewValidator implements BodyValidator { - private static final String errorMsg = - "There is a path from '%s' to the usage '%s' where does not get called in between."; - - public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; - - // Checks whether after each new-instruction a constructor call follows. - @Override - public void validate(Body body, List exceptions) { - - StmtGraph g = body.getStmtGraph(); - for (Stmt u : body.getStmts()) { - if (u instanceof JAssignStmt) { - JAssignStmt assign = (JAssignStmt) u; - - // First seek for a JNewExpr. - if (assign.getRightOp() instanceof JNewExpr) { - if (!((assign.getLeftOp().getType() instanceof ReferenceType) - || assign.getLeftOp().getType() instanceof UnknownType)) { - exceptions.add( - new ValidationException( - assign.getLeftOp(), - String.format( - "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", - body.getMethodSignature()))); - return; - } - - // We search for a JSpecialInvokeExpr on the local. - LinkedHashSet locals = new LinkedHashSet(); - locals.add((Local) assign.getLeftOp()); - - checkForInitializerOnPath(g, assign, exceptions); - } - } + private static final String errorMsg = + "There is a path from '%s' to the usage '%s' where does not get called in between."; + + public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; + + // Checks whether after each new-instruction a constructor call follows. + @Override + public void validate(Body body, List exceptions) { + + StmtGraph g = body.getStmtGraph(); + for (Stmt u : body.getStmts()) { + if (u instanceof JAssignStmt) { + JAssignStmt assign = (JAssignStmt) u; + + // First seek for a JNewExpr. + if (assign.getRightOp() instanceof JNewExpr) { + if (!((assign.getLeftOp().getType() instanceof ReferenceType) + || assign.getLeftOp().getType() instanceof UnknownType)) { + exceptions.add( + new ValidationException( + assign.getLeftOp(), + String.format( + "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", + body.getMethodSignature()))); + return; + } + + // We search for a JSpecialInvokeExpr on the local. + LinkedHashSet locals = new LinkedHashSet(); + locals.add((Local) assign.getLeftOp()); + + checkForInitializerOnPath(g, assign, exceptions); } + } } + } + + private boolean checkForInitializerOnPath( + StmtGraph g, JAssignStmt newStmt, List exception) { + List workList = new ArrayList(); + Set doneSet = new HashSet(); + workList.add(newStmt); + + Set aliasingLocals = new HashSet(); + aliasingLocals.add((Local) newStmt.getLeftOp()); + + while (!workList.isEmpty()) { + Stmt curStmt = (Stmt) workList.remove(0); + if (!doneSet.add(curStmt)) { + continue; + } + if (!newStmt.equals(curStmt)) { + if (curStmt.containsInvokeExpr()) { + AbstractInvokeExpr expr = curStmt.getInvokeExpr(); + if (!(expr instanceof JSpecialInvokeExpr)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methodRef calls may only be used with specialinvoke.")); // At least we + // found an initializer, so we return true... + return true; + } + if (!(curStmt instanceof JInvokeStmt)) { + exception.add( + new ValidationException( + curStmt.getInvokeExpr(), + " methods may only be called with invoke statements.")); // At least we + // found an initializer, so we return true... + return true; + } + + JSpecialInvokeExpr invoke = (JSpecialInvokeExpr) expr; + if (aliasingLocals.contains(invoke.getBase())) { + // We are happy now,continue the loop and check other paths + continue; + } + } - private boolean checkForInitializerOnPath( - StmtGraph g, JAssignStmt newStmt, List exception) { - List workList = new ArrayList(); - Set doneSet = new HashSet(); - workList.add(newStmt); - - Set aliasingLocals = new HashSet(); - aliasingLocals.add((Local) newStmt.getLeftOp()); - - while (!workList.isEmpty()) { - Stmt curStmt = (Stmt) workList.remove(0); - if (!doneSet.add(curStmt)) { - continue; - } - if (!newStmt.equals(curStmt)) { - if (curStmt.containsInvokeExpr()) { - AbstractInvokeExpr expr = curStmt.getInvokeExpr(); - if (!(expr instanceof JSpecialInvokeExpr)) { - exception.add( - new ValidationException( - curStmt.getInvokeExpr(), - " methodRef calls may only be used with specialinvoke.")); // At least we - // found an initializer, so we return true... - return true; - } - if (!(curStmt instanceof JInvokeStmt)) { - exception.add( - new ValidationException( - curStmt.getInvokeExpr(), - " methods may only be called with invoke statements.")); // At least we - // found an initializer, so we return true... - return true; - } - - JSpecialInvokeExpr invoke = (JSpecialInvokeExpr) expr; - if (aliasingLocals.contains(invoke.getBase())) { - // We are happy now,continue the loop and check other paths - continue; - } - } - - // We are still in the loop, so this was not the constructor call we were looking for - boolean creatingAlias = false; - if (curStmt instanceof JAssignStmt) { - JAssignStmt assignCheck = (JAssignStmt) curStmt; - if (aliasingLocals.contains(assignCheck.getRightOp())) { - if (assignCheck.getLeftOp() instanceof Local) { - // A new alias is created. - aliasingLocals.add((Local) assignCheck.getLeftOp()); - creatingAlias = true; - } - } - Local originalLocal = aliasingLocals.iterator().next(); - if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: - - // Handles cases like // $r0 = new x; // $r0 = null; - - // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check - // for the original local - continue; - } else { - // Since the local on the left hand side gets overwritten - // even if it was aliasing with our original local, - // now it does not any more... - aliasingLocals.remove(assignCheck.getLeftOp()); - } - } - - if (!creatingAlias) { - for (Value box : curStmt.getUses()) { - if (aliasingLocals.contains(box)) { - // The current unit uses one of the aliasing locals, but - // there was no initializer in between. - // However, when creating such an alias, the use is okay. - exception.add( - new ValidationException( - newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); - return false; - } - } - } + // We are still in the loop, so this was not the constructor call we were looking for + boolean creatingAlias = false; + if (curStmt instanceof JAssignStmt) { + JAssignStmt assignCheck = (JAssignStmt) curStmt; + if (aliasingLocals.contains(assignCheck.getRightOp())) { + if (assignCheck.getLeftOp() instanceof Local) { + // A new alias is created. + aliasingLocals.add((Local) assignCheck.getLeftOp()); + creatingAlias = true; } - // Enqueue the successors - List successors = g.successors(curStmt); - if (successors.isEmpty() && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { - // This means that we are e.g.at the end of - // the methodRef // There was no call - // on our way... - exception.add( - new ValidationException( - newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); - return false; - } - workList.addAll(successors); + } + Local originalLocal = aliasingLocals.iterator().next(); + if (originalLocal.equals(assignCheck.getLeftOp())) { // In case of dead assignments: + + // Handles cases like // $r0 = new x; // $r0 = null; + + // But not cases like // $r0 = new x; // $r1 = $r0; // $r1 = null; // Because we check + // for the original local + continue; + } else { + // Since the local on the left hand side gets overwritten + // even if it was aliasing with our original local, + // now it does not any more... + aliasingLocals.remove(assignCheck.getLeftOp()); + } } - return true; - } - @Override - public boolean isBasicValidator() { + if (!creatingAlias) { + for (Value box : curStmt.getUses()) { + if (aliasingLocals.contains(box)) { + // The current unit uses one of the aliasing locals, but + // there was no initializer in between. + // However, when creating such an alias, the use is okay. + exception.add( + new ValidationException( + newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + return false; + } + } + } + } + // Enqueue the successors + List successors = g.successors(curStmt); + if (successors.isEmpty() && MUST_CALL_CONSTRUCTOR_BEFORE_RETURN) { + // This means that we are e.g.at the end of + // the methodRef // There was no call + // on our way... + exception.add( + new ValidationException( + newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); return false; + } + workList.addAll(successors); } + return true; + } + + @Override + public boolean isBasicValidator() { + return false; + } } From bbf8513c84fff69851a56cebc3f1dd9b8965485d Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Fri, 10 Nov 2023 15:34:23 +0100 Subject: [PATCH 04/22] add Validator Interceptor draft --- .../interceptors/JimpleSemanticsChecker.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java new file mode 100644 index 00000000000..4624035e5b4 --- /dev/null +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java @@ -0,0 +1,49 @@ +package sootup.java.bytecode.interceptors; + +import java.util.List; +import javax.annotation.Nonnull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sootup.core.model.Body; +import sootup.core.transform.BodyInterceptor; +import sootup.core.validation.ValidationException; +import sootup.core.views.View; + +/** + * This Interceptor executes validations on Jimple semantics + * + *

e.g. like a Local must be assigned before its use + */ +public abstract class JimpleSemanticsChecker implements BodyInterceptor { + + private static final Logger logger = LoggerFactory.getLogger(JimpleSemanticsChecker.class); + + protected List validate( + @Nonnull Body.BodyBuilder builder, @Nonnull View view) { + // FIXME: implement + throw new UnsupportedOperationException("List of Validators is not incorporated yet."); + } + + @Override + public abstract void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view); + + public static class LoggingJimpleChecker extends JimpleSemanticsChecker { + + @Override + public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) { + final List exceptions = validate(builder, view); + for (ValidationException validationException : exceptions) { + logger.warn(validationException.getMessage()); + } + } + } + + public static class ThrowingJimpleChecker extends LoggingJimpleChecker { + + @Override + public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) { + super.interceptBody(builder, view); + throw new IllegalStateException("There are semantic errors in the Jimple - see warn log."); + } + } +} From 81034eb1ed4d632c518e4e698421db4f1415e55f Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Sat, 11 Nov 2023 12:07:27 +0100 Subject: [PATCH 05/22] add licenseheader --- .../interceptors/JimpleSemanticsChecker.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java index 4624035e5b4..92dc217e60e 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/interceptors/JimpleSemanticsChecker.java @@ -1,5 +1,25 @@ package sootup.java.bytecode.interceptors; - +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 2023 Markus Schmidt + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ import java.util.List; import javax.annotation.Nonnull; import org.slf4j.Logger; From 32d3e8dbd234aa2cf0ff536383021dd4d3455088 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 15 Nov 2023 12:13:51 +0100 Subject: [PATCH 06/22] Changes to increase test coverage. --- .../sootup/core/validation/NewValidator.java | 44 ++++-- sootup.java.bytecode/pom.xml | 8 +- .../java/resources/jimple/NewValidator.jimple | 18 +++ .../resources/jimple/NewValidator_fail.jimple | 19 +++ .../NewKeywordValidatorTest.java | 32 ----- .../ValidatorTests/NewValidatorTest.java | 133 ++++++++++++++++++ 6 files changed, 210 insertions(+), 44 deletions(-) create mode 100644 sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple create mode 100644 sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple delete mode 100644 sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java create mode 100644 sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index 6fe3e4e3e3f..c96318c8aef 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -1,6 +1,27 @@ package sootup.core.validation; -import java.util.*; +/*- + * #%L + * Soot - a J*va Optimization Framework + * %% + * Copyright (C) 1997-2020 Raja Vallée-Rai, Christian Brüggemann, Markus Schmidt, Akshita Dubey and others + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + import sootup.core.graph.StmtGraph; import sootup.core.jimple.basic.Local; import sootup.core.jimple.basic.Value; @@ -13,6 +34,11 @@ import sootup.core.model.Body; import sootup.core.types.ReferenceType; import sootup.core.types.UnknownType; +import java.util.LinkedHashSet; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; public class NewValidator implements BodyValidator { @@ -33,20 +59,16 @@ public void validate(Body body, List exceptions) { // First seek for a JNewExpr. if (assign.getRightOp() instanceof JNewExpr) { if (!((assign.getLeftOp().getType() instanceof ReferenceType) - || assign.getLeftOp().getType() instanceof UnknownType)) { + || assign.getLeftOp().getType() instanceof UnknownType)) { exceptions.add( - new ValidationException( - assign.getLeftOp(), - String.format( - "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", - body.getMethodSignature()))); + new ValidationException( + assign.getLeftOp(), + String.format( + "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", + body.getMethodSignature()))); return; } - // We search for a JSpecialInvokeExpr on the local. - LinkedHashSet locals = new LinkedHashSet(); - locals.add((Local) assign.getLeftOp()); - checkForInitializerOnPath(g, assign, exceptions); } } diff --git a/sootup.java.bytecode/pom.xml b/sootup.java.bytecode/pom.xml index bcc304d5982..baf73df1195 100644 --- a/sootup.java.bytecode/pom.xml +++ b/sootup.java.bytecode/pom.xml @@ -38,7 +38,13 @@ dex-tools v64 - + + org.soot-oss + sootup.jimple.parser + 1.1.2-SNAPSHOT + test + + diff --git a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple new file mode 100644 index 00000000000..21c5b06cae2 --- /dev/null +++ b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple @@ -0,0 +1,18 @@ +public super class NewValidator extends java.lang.Object +{ + public void newValidator() + { + java.lang.String[] $l0; + unknown $l1, $stack2, $stack3, $stack4; + + $l0 := @parameter0: java.lang.String[]; + $stack2 = new NewValidator; + specialinvoke $stack2.()>(); + $l1 = $stack2; + $stack4 = ; + $stack3 = $l1.; + virtualinvoke $stack4.($stack3); + + return; + } +} \ No newline at end of file diff --git a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple new file mode 100644 index 00000000000..39c75fbbd58 --- /dev/null +++ b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple @@ -0,0 +1,19 @@ +public super class NewValidator_fail extends java.lang.Object +{ + public void newValidator_fail() + { + java.lang.String[] $l0; + unknown $l1, $stack2, $stack3, $stack4; + + $l0 := @parameter0: java.lang.String[]; + $stack2 = new NewValidator_fail; + + $l1 = $stack2; + $stack4 = ; + $stack3 = $l1.; + virtualinvoke $stack4.($stack3); + + return; + } + +} \ No newline at end of file diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java deleted file mode 100644 index 4ab027d1dbd..00000000000 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewKeywordValidatorTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; - -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import org.junit.Test; -import sootup.core.validation.NewValidator; -import sootup.core.validation.ValidationException; -import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; -import sootup.java.core.JavaSootClass; -import sootup.java.core.JavaSootMethod; - -public class NewKeywordValidatorTest extends MinimalBytecodeTestSuiteBase { - - @Test - public void testNewValidator() { - JavaSootClass sootClass = loadClass(getDeclaredClassSignature()); - - List main = - sootClass.getMethods().stream() - .filter(javaSootMethod -> javaSootMethod.getName().contains("main")) - .collect(Collectors.toList()); - - List validationExceptions = new ArrayList<>(); - - new NewValidator().validate(main.get(0).getBody(), validationExceptions); - - assertEquals(0, validationExceptions.size()); - } -} diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java new file mode 100644 index 00000000000..07ec8337c9b --- /dev/null +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -0,0 +1,133 @@ +package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; + +import java.nio.file.Paths; +import java.util.*; + +import org.junit.Before; +import org.junit.Test; + +import sootup.core.model.Body; + +import sootup.core.model.SootClass; +import sootup.core.model.SourceType; +import sootup.core.signatures.PackageName; +import sootup.core.types.ClassType; +import sootup.core.validation.NewValidator; +import sootup.core.validation.ValidationException; + +import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; +import sootup.core.inputlocation.AnalysisInputLocation; +import sootup.java.core.JavaSootClass; + +import sootup.java.core.views.JavaView; +import sootup.jimple.parser.JimpleAnalysisInputLocation; +import sootup.jimple.parser.JimpleProject; +import sootup.jimple.parser.JimpleView; + + +import static org.junit.Assert.*; + +public class NewValidatorTest extends MinimalBytecodeTestSuiteBase { + + NewValidator validator; + Body body; + JavaView view; + + @Before + public void Setup() { + + validator = new NewValidator(); + + ClassType classTypeNewValidator = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.NewValidator"; + } + + @Override + public String getClassName() { + return "NewValidator"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + final ClassType classTypeNewValidator_fail = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.NewValidator_fail"; + } + + @Override + public String getClassName() { + return "NewValidator_fail"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + final String resourceDir = "src/test/java/resources/"; + + // files direct in dir + final JimpleAnalysisInputLocation inputLocation1 = + new JimpleAnalysisInputLocation(Paths.get(resourceDir + "/jimple/")); + JimpleView jv1 = new JimpleProject(inputLocation1).createView(); + final Optional> classSource1 = jv1.getClass(classTypeNewValidator); + assertFalse(classSource1.isPresent()); + final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); + assertFalse(classSource2.isPresent()); + } + + @Test + public void testNewValidator() { + List validationExceptions_success = new ArrayList<>(); + List validationExceptions_fail = new ArrayList<>(); + + String classPath = "src/test/java/resources/jimple"; + AnalysisInputLocation jimpleInputLocation = + new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application + + ); + JimpleView view = new JimpleProject(jimpleInputLocation).createOnDemandView(); + + Collection> classes = new HashSet<>(); // Set to track the classes to check + + for (SootClass aClass : view.getClasses()) { + if (!aClass.isLibraryClass()) { + classes.add(aClass); + } + } + + validator.validate(classes.stream().filter(c -> c.getType().getClassName().contains("NewValidator")).findFirst() + .get().getMethods().stream().filter(m -> m.getName().contains("newValidator")) + .map(m -> m.getBody()).findFirst().get(),validationExceptions_success); + + assertEquals(0, validationExceptions_success.size()); + + validator.validate(classes.stream().filter(c -> c.getType().getClassName().contains("NewValidator_fail")).findFirst() + .get().getMethods().stream().filter(m -> m.getName().contains("newValidator_fail")) + .map(m -> m.getBody()).findFirst().get(),validationExceptions_fail); + + assertEquals(1, validationExceptions_fail.size()); + } +} + + From 4961fe232c43e88a2ca77c4cc2c09b7e8cdf3c5c Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 16 Nov 2023 12:30:57 +0100 Subject: [PATCH 07/22] Formatting changes --- .../sootup/core/validation/NewValidator.java | 21 +- .../ValidatorTests/NewValidatorTest.java | 225 +++++++++--------- 2 files changed, 127 insertions(+), 119 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index c96318c8aef..420ddb009ca 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -22,6 +22,10 @@ * #L% */ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import sootup.core.graph.StmtGraph; import sootup.core.jimple.basic.Local; import sootup.core.jimple.basic.Value; @@ -34,11 +38,6 @@ import sootup.core.model.Body; import sootup.core.types.ReferenceType; import sootup.core.types.UnknownType; -import java.util.LinkedHashSet; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; public class NewValidator implements BodyValidator { @@ -59,13 +58,13 @@ public void validate(Body body, List exceptions) { // First seek for a JNewExpr. if (assign.getRightOp() instanceof JNewExpr) { if (!((assign.getLeftOp().getType() instanceof ReferenceType) - || assign.getLeftOp().getType() instanceof UnknownType)) { + || assign.getLeftOp().getType() instanceof UnknownType)) { exceptions.add( - new ValidationException( - assign.getLeftOp(), - String.format( - "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", - body.getMethodSignature()))); + new ValidationException( + assign.getLeftOp(), + String.format( + "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", + body.getMethodSignature()))); return; } diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index 07ec8337c9b..f2d7798c698 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -1,133 +1,142 @@ package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; +import static org.junit.Assert.*; + import java.nio.file.Paths; import java.util.*; - import org.junit.Before; import org.junit.Test; - +import sootup.core.inputlocation.AnalysisInputLocation; import sootup.core.model.Body; - import sootup.core.model.SootClass; import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; import sootup.core.validation.NewValidator; import sootup.core.validation.ValidationException; - import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; -import sootup.core.inputlocation.AnalysisInputLocation; import sootup.java.core.JavaSootClass; - import sootup.java.core.views.JavaView; import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleProject; import sootup.jimple.parser.JimpleView; - -import static org.junit.Assert.*; - public class NewValidatorTest extends MinimalBytecodeTestSuiteBase { - NewValidator validator; - Body body; - JavaView view; - - @Before - public void Setup() { - - validator = new NewValidator(); - - ClassType classTypeNewValidator = - new ClassType() { - @Override - public boolean isBuiltInClass() { - return false; - } - - @Override - public String getFullyQualifiedName() { - return "jimple.NewValidator"; - } - - @Override - public String getClassName() { - return "NewValidator"; - } - - @Override - public PackageName getPackageName() { - return new PackageName("jimple"); - } - }; - - final ClassType classTypeNewValidator_fail = - new ClassType() { - @Override - public boolean isBuiltInClass() { - return false; - } - - @Override - public String getFullyQualifiedName() { - return "jimple.NewValidator_fail"; - } - - @Override - public String getClassName() { - return "NewValidator_fail"; - } - - @Override - public PackageName getPackageName() { - return new PackageName("jimple"); - } - }; - - final String resourceDir = "src/test/java/resources/"; - - // files direct in dir - final JimpleAnalysisInputLocation inputLocation1 = - new JimpleAnalysisInputLocation(Paths.get(resourceDir + "/jimple/")); - JimpleView jv1 = new JimpleProject(inputLocation1).createView(); - final Optional> classSource1 = jv1.getClass(classTypeNewValidator); - assertFalse(classSource1.isPresent()); - final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); - assertFalse(classSource2.isPresent()); + NewValidator validator; + Body body; + JavaView view; + + @Before + public void Setup() { + + validator = new NewValidator(); + + ClassType classTypeNewValidator = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.NewValidator"; + } + + @Override + public String getClassName() { + return "NewValidator"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + final ClassType classTypeNewValidator_fail = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.NewValidator_fail"; + } + + @Override + public String getClassName() { + return "NewValidator_fail"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + final String resourceDir = "src/test/java/resources/"; + + // files direct in dir + final JimpleAnalysisInputLocation inputLocation1 = + new JimpleAnalysisInputLocation(Paths.get(resourceDir + "/jimple/")); + JimpleView jv1 = new JimpleProject(inputLocation1).createView(); + final Optional> classSource1 = jv1.getClass(classTypeNewValidator); + assertFalse(classSource1.isPresent()); + final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); + assertFalse(classSource2.isPresent()); + } + + @Test + public void testNewValidator() { + List validationExceptions_success = new ArrayList<>(); + List validationExceptions_fail = new ArrayList<>(); + + String classPath = "src/test/java/resources/jimple"; + AnalysisInputLocation jimpleInputLocation = + new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); + + JimpleView view = new JimpleProject(jimpleInputLocation).createOnDemandView(); + + Collection> classes = new HashSet<>(); // Set to track the classes to check + + for (SootClass aClass : view.getClasses()) { + if (!aClass.isLibraryClass()) { + classes.add(aClass); + } } - @Test - public void testNewValidator() { - List validationExceptions_success = new ArrayList<>(); - List validationExceptions_fail = new ArrayList<>(); - - String classPath = "src/test/java/resources/jimple"; - AnalysisInputLocation jimpleInputLocation = - new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application - - ); - JimpleView view = new JimpleProject(jimpleInputLocation).createOnDemandView(); - - Collection> classes = new HashSet<>(); // Set to track the classes to check - - for (SootClass aClass : view.getClasses()) { - if (!aClass.isLibraryClass()) { - classes.add(aClass); - } - } - - validator.validate(classes.stream().filter(c -> c.getType().getClassName().contains("NewValidator")).findFirst() - .get().getMethods().stream().filter(m -> m.getName().contains("newValidator")) - .map(m -> m.getBody()).findFirst().get(),validationExceptions_success); - - assertEquals(0, validationExceptions_success.size()); - - validator.validate(classes.stream().filter(c -> c.getType().getClassName().contains("NewValidator_fail")).findFirst() - .get().getMethods().stream().filter(m -> m.getName().contains("newValidator_fail")) - .map(m -> m.getBody()).findFirst().get(),validationExceptions_fail); - - assertEquals(1, validationExceptions_fail.size()); - } + validator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().contains("NewValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().contains("newValidator")) + .map(m -> m.getBody()) + .findFirst() + .get(), + validationExceptions_success); + + assertEquals(0, validationExceptions_success.size()); + + validator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().contains("NewValidator_fail")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().contains("newValidator_fail")) + .map(m -> m.getBody()) + .findFirst() + .get(), + validationExceptions_fail); + + assertEquals(1, validationExceptions_fail.size()); + } } - - From 4d3022cd542ac81ef01e820dd3e8fff98132b92f Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Wed, 22 Nov 2023 11:44:01 +0100 Subject: [PATCH 08/22] minor --- .../java/sootup/core/validation/NewValidator.java | 14 +++++++------- .../java6/ValidatorTests/NewValidatorTest.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index 420ddb009ca..fa826acfa92 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -41,7 +41,7 @@ public class NewValidator implements BodyValidator { - private static final String errorMsg = + private static final String ERROR_MSG = "There is a path from '%s' to the usage '%s' where does not get called in between."; public static boolean MUST_CALL_CONSTRUCTOR_BEFORE_RETURN = false; @@ -76,15 +76,15 @@ public void validate(Body body, List exceptions) { private boolean checkForInitializerOnPath( StmtGraph g, JAssignStmt newStmt, List exception) { - List workList = new ArrayList(); - Set doneSet = new HashSet(); + List workList = new ArrayList<>(); + Set doneSet = new HashSet<>(); workList.add(newStmt); - Set aliasingLocals = new HashSet(); + Set aliasingLocals = new HashSet<>(); aliasingLocals.add((Local) newStmt.getLeftOp()); while (!workList.isEmpty()) { - Stmt curStmt = (Stmt) workList.remove(0); + Stmt curStmt = workList.remove(0); if (!doneSet.add(curStmt)) { continue; } @@ -150,7 +150,7 @@ private boolean checkForInitializerOnPath( // However, when creating such an alias, the use is okay. exception.add( new ValidationException( - newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + newStmt.getLeftOp(), String.format(ERROR_MSG, newStmt, curStmt))); return false; } } @@ -164,7 +164,7 @@ private boolean checkForInitializerOnPath( // on our way... exception.add( new ValidationException( - newStmt.getLeftOp(), String.format(errorMsg, newStmt, curStmt))); + newStmt.getLeftOp(), String.format(ERROR_MSG, newStmt, curStmt))); return false; } workList.addAll(successors); diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index f2d7798c698..f4769125645 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -99,7 +99,7 @@ public void testNewValidator() { AnalysisInputLocation jimpleInputLocation = new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); - JimpleView view = new JimpleProject(jimpleInputLocation).createOnDemandView(); + JimpleView view = new JimpleProject(jimpleInputLocation).createView(); Collection> classes = new HashSet<>(); // Set to track the classes to check From 6868bb4c3cfe106a9aa606be47e8aa88613efa5e Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 6 Dec 2023 14:31:31 +0100 Subject: [PATCH 09/22] validate method signature change --- .../src/main/java/sootup/core/model/Body.java | 22 ++--- .../sootup/core/validation/BodyValidator.java | 5 +- .../core/validation/CheckInitValidator.java | 4 +- .../core/validation/CheckTypesValidator.java | 4 +- .../validation/CheckVoidLocalesValidator.java | 4 +- .../core/validation/FieldRefValidator.java | 96 +++++++++++++------ .../IdentityStatementsValidator.java | 6 +- .../core/validation/IdentityValidator.java | 10 +- .../validation/InvokeArgumentValidator.java | 4 +- .../core/validation/JimpleTrapValidator.java | 12 ++- .../core/validation/LocalsValidator.java | 15 ++- .../core/validation/MethodValidator.java | 6 +- .../sootup/core/validation/NewValidator.java | 8 +- .../core/validation/StmtsValidator.java | 11 ++- .../core/validation/TrapsValidator.java | 10 +- .../core/validation/TypesValidator.java | 4 +- .../sootup/core/validation/UsesValidator.java | 10 +- .../core/validation/ValuesValidator.java | 10 +- .../ValidatorTests/NewValidatorTest.java | 54 ++++++----- 19 files changed, 201 insertions(+), 94 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index 12c4220a6a7..3792b887b60 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -88,7 +88,7 @@ private Body( this.graph = /* FIXME: [ms] make immutable when availabe */ new MutableBlockStmtGraph(stmtGraph).unmodifiableStmtGraph(); this.position = position; - checkInit(); + // checkInit(); } /** @@ -121,14 +121,6 @@ public int getLocalCount() { return locals.size(); } - private void runValidation(BodyValidator validator) { - final List exceptionList = new ArrayList<>(); - validator.validate(this, exceptionList); - if (!exceptionList.isEmpty()) { - throw exceptionList.get(0); - } - } - /** Verifies that a Value is not used in more than one place. */ // TODO: #535 implement validator public void validateValues() { runValidation(new // ValuesValidator());} @@ -140,9 +132,9 @@ private void runValidation(BodyValidator validator) { /** Verifies that each use in this Body has a def. */ // TODO: #535 implement validator public void validateUses() { runValidation(new // UsesValidator()); } - private void checkInit() { - runValidation(new CheckInitValidator()); - } + // private void checkInit() { + // runValidation(new CheckInitValidator(),); + // } /** Returns a backed chain of the locals declared in this Body. */ public Set getLocals() { @@ -278,9 +270,9 @@ public boolean isStmtBranchTarget(@Nonnull Stmt targetStmt) { return getStmtGraph().isStmtBranchTarget(targetStmt); } - public void validateIdentityStatements() { - runValidation(new IdentityStatementsValidator()); - } + // public void validateIdentityStatements() { + // runValidation(new IdentityStatementsValidator()); + // } /** Returns the first non-identity stmt in this body. */ @Nonnull diff --git a/sootup.core/src/main/java/sootup/core/validation/BodyValidator.java b/sootup.core/src/main/java/sootup/core/validation/BodyValidator.java index 30d1f2c4ef1..3c50827e75c 100644 --- a/sootup.core/src/main/java/sootup/core/validation/BodyValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/BodyValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; /** Implement this interface if you want to provide your own body Validator */ public interface BodyValidator { @@ -31,9 +32,9 @@ public interface BodyValidator { * Validates the given body and saves all validation errors in the given list. * * @param body the body to check - * @param exceptions the list of exceptions + * @param view the view */ - void validate(Body body, List exceptions); + List validate(Body body, View view); /** * Basic validators run essential checks and are run always if validate is called.
diff --git a/sootup.core/src/main/java/sootup/core/validation/CheckInitValidator.java b/sootup.core/src/main/java/sootup/core/validation/CheckInitValidator.java index d801585b227..b68a9ced297 100644 --- a/sootup.core/src/main/java/sootup/core/validation/CheckInitValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/CheckInitValidator.java @@ -24,11 +24,12 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class CheckInitValidator implements BodyValidator { @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: #535 implement validator // check code copied from old soot @@ -41,6 +42,7 @@ public void validate(Body body, List exception) { * "Local variable $1 is not definitively defined at this point".replace("$1", l.getName()), "Warning: Local variable " + * l + " not definitely defined at " + s + " in " + body.getMethod(), false); } } } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/CheckTypesValidator.java b/sootup.core/src/main/java/sootup/core/validation/CheckTypesValidator.java index efef2e6e2c6..1a8a301eb54 100644 --- a/sootup.core/src/main/java/sootup/core/validation/CheckTypesValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/CheckTypesValidator.java @@ -24,11 +24,12 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class CheckTypesValidator implements BodyValidator { @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: check code from old soot in the comment below /* * for (Unit u : body.getUnits()) { String errorSuffix = " at " + u + " in " + body.getMethod(); @@ -88,6 +89,7 @@ public void validate(Body body, List exception) { * body.getMethod())); } } } return; } exception.add(new ValidationException(stmt, "Warning: Bad types" + errorSuffix + * " in " + body.getMethod())); */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/CheckVoidLocalesValidator.java b/sootup.core/src/main/java/sootup/core/validation/CheckVoidLocalesValidator.java index 34885ec3d17..02c546ce28d 100644 --- a/sootup.core/src/main/java/sootup/core/validation/CheckVoidLocalesValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/CheckVoidLocalesValidator.java @@ -24,16 +24,18 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class CheckVoidLocalesValidator implements BodyValidator { @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * for (Local l : body.getLocals()) { if (l.getType() instanceof VoidType) { exception.add(new ValidationException(l, * "Local " + l + " in " + body.getMethod() + " defined with void type")); } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java index 2e6f8e57cc1..d4a015c65b4 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -4,7 +4,7 @@ * #%L * Soot - a J*va Optimization Framework * %% - * Copyright (C) 1997-2020 Raja Vallée-Rai, Linghui Luo, Markus Schmidt and others + * Copyright (C) 1997-2020 Raja Vallée-Rai, Christian Brüggemann, Markus Schmidt, Akshita Dubey and others * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -22,38 +22,80 @@ * #L% */ +import java.util.ArrayList; import java.util.List; +import sootup.core.graph.MutableStmtGraph; +import sootup.core.jimple.common.ref.JFieldRef; +import sootup.core.jimple.common.ref.JInstanceFieldRef; +import sootup.core.jimple.common.ref.JStaticFieldRef; +import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; +import sootup.core.model.MethodModifier; +import sootup.core.model.SootField; +import sootup.core.views.View; public class FieldRefValidator implements BodyValidator { - /** Checks the consistency of field references. */ + // Checks the consistency of field references. @Override - public void validate(Body body, List exceptions) { - // TODO: check copied code from old soot - /* - * SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; } - * - * Chain units = body.getUnits().getNonPatchingChain(); - * - * for (Unit unit : units) { Stmt s = (Stmt) unit; if (!s.containsFieldRef()) { continue; } JFieldRef fr = - * s.getFieldRef(); - * - * if (fr instanceof JStaticFieldRef) { JStaticFieldRef v = (JStaticFieldRef) fr; try { SootField field = v.getField(); - * if (field == null) { exceptions.add(new UnitValidationException(unit, body, "Resolved field is null: " + - * fr.toString())); } else if (!field.isStatic() && !field.isPhantom()) { exceptions .add(new - * UnitValidationException(unit, body, "Trying to get a static field which is non-static: " + v)); } } catch - * (ResolutionFailedException e) { exceptions.add(new UnitValidationException(unit, body, - * "Trying to get a static field which is non-static: " + v)); } } else if (fr instanceof InstanceFieldRef) { - * InstanceFieldRef v = (InstanceFieldRef) fr; - * - * try { SootField field = v.getField(); if (field == null) { exceptions.add(new UnitValidationException(unit, body, - * "Resolved field is null: " + fr.toString())); } else if (field.isStatic() && !field.isPhantom()) { exceptions.add(new - * UnitValidationException(unit, body, "Trying to get an instance field which is static: " + v)); } } catch - * (ResolutionFailedException e) { exceptions.add(new UnitValidationException(unit, body, - * "Trying to get an instance field which is static: " + v)); } } else { throw new RuntimeException("unknown field ref"); - * } } - */ + public List validate(Body body, View view) { + + List validationException = new ArrayList<>(); + + Body.BodyBuilder bodyBuilder = Body.builder((MutableStmtGraph) body.getStmtGraph()); + if (bodyBuilder.getModifiers().contains(MethodModifier.ABSTRACT)) { + return validationException; + } + + List stmts = body.getStmts(); + + for (Stmt stmt : stmts) { + if (!stmt.containsFieldRef()) { + continue; + } + JFieldRef fr = stmt.getFieldRef(); + + if (fr instanceof JStaticFieldRef) { + JStaticFieldRef v = (JStaticFieldRef) fr; + try { + SootField field = view.getField(v.getFieldSignature()).get(); + if (field == null) { + validationException.add(new ValidationException(v, "Resolved field is null: ")); + } else if (!field.isStatic()) { + validationException.add( + new ValidationException( + v, "Trying to get a static field which is non-static: " + v)); + } + } catch (Exception e) { + validationException.add( + new ValidationException( + body.getStmts().get(0).getFieldRef(), + "Trying to get a static field which is non-static: " + v)); + } + } else if (fr instanceof JInstanceFieldRef) { + JInstanceFieldRef v = (JInstanceFieldRef) fr; + + try { + SootField field = view.getField(v.getFieldSignature()).get(); + if (field == null) { + validationException.add( + new ValidationException( + body.getStmts().get(0).getFieldRef(), + "Resolved field is null: " + fr.toString())); + } else if (field.isStatic()) { + validationException.add( + new ValidationException( + v, "Trying to get an instance field which is static: " + v)); + } + } catch (Exception e) { + validationException.add( + new ValidationException(v, "Trying to get an instance field which is static: " + v)); + } + } else { + throw new RuntimeException("unknown field ref"); + } + } + return validationException; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/IdentityStatementsValidator.java b/sootup.core/src/main/java/sootup/core/validation/IdentityStatementsValidator.java index 6d1e821f1da..4fb5d657285 100644 --- a/sootup.core/src/main/java/sootup/core/validation/IdentityStatementsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/IdentityStatementsValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class IdentityStatementsValidator implements BodyValidator { @@ -36,9 +37,11 @@ public class IdentityStatementsValidator implements BodyValidator { *

  • param-references must precede all statements that are not themselves param-references or * this-references, if they occur at all * + * + * @return */ @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; } @@ -57,6 +60,7 @@ public void validate(Body body, List exceptions) { * // @caughtexception statement foundNonThisOrParamIdentityStatement = true; } } else { // non-identity statement * foundNonThisOrParamIdentityStatement = true; } firstStatement = false; } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/IdentityValidator.java b/sootup.core/src/main/java/sootup/core/validation/IdentityValidator.java index c76bb0494ee..20090c42283 100644 --- a/sootup.core/src/main/java/sootup/core/validation/IdentityValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/IdentityValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; /** * This validator checks whether each ParameterRef and ThisRef is used exactly once. @@ -32,9 +33,13 @@ */ public class IdentityValidator implements BodyValidator { - /** Checks whether each ParameterRef and ThisRef is used exactly once. */ + /** + * Checks whether each ParameterRef and ThisRef is used exactly once. + * + * @return + */ @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * boolean hasThisLocal = false; int paramCount = body.getMethod().getParameterCount(); boolean[] parameterRefs = new @@ -57,6 +62,7 @@ public void validate(Body body, List exceptions) { * String.format("There is no parameter local for parameter number %d", i))); } } * */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/InvokeArgumentValidator.java b/sootup.core/src/main/java/sootup/core/validation/InvokeArgumentValidator.java index 478cd30a9df..ba7317dd0a8 100644 --- a/sootup.core/src/main/java/sootup/core/validation/InvokeArgumentValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/InvokeArgumentValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; /** * A basic validator that checks whether the length of the invoke statement's argument list matches @@ -34,13 +35,14 @@ public class InvokeArgumentValidator implements BodyValidator { @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * for (Unit u : body.getUnits()) { Stmt s = (Stmt) u; if (s.containsInvokeExpr()) { InvokeExpr iinvExpr = * s.getInvokeExpr(); SootMethod callee = iinvExpr.getMethod(); if (callee != null && iinvExpr.getArgCount() != * callee.getParameterCount()) { exceptions.add(new ValidationException(s, "Invalid number of arguments")); } } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java b/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java index 37d325af093..63bbe30bc52 100644 --- a/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java @@ -24,19 +24,24 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; /** * This validator checks whether the jimple traps are correct. It does not perform the same checks * as {link sootup.validation.TrapsValidator} * - * @see JimpleTrapValidator#validate(Body, List) + * @see BodyValidator#validate(Body, View) * @author Marc Miltenberger */ public class JimpleTrapValidator implements BodyValidator { - /** Checks whether all Caught-Exception-References are associated to traps. */ + /** + * Checks whether all Caught-Exception-References are associated to traps. + * + * @return + */ @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * Set caughtUnits = new HashSet(); for (Trap trap : body.getTraps()) { @@ -52,6 +57,7 @@ public void validate(Body body, List exceptions) { * body.getMethod().getSignature() + " contains a caught exception reference," + * "but not a corresponding trap using this statement as handler")); } } } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java index dc70ca2eb01..eaf39c38f89 100644 --- a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java @@ -22,17 +22,26 @@ * #L% */ +import java.util.ArrayList; import java.util.List; import java.util.Set; import javax.annotation.Nonnull; import sootup.core.jimple.basic.Local; import sootup.core.model.Body; +import sootup.core.views.View; public class LocalsValidator implements BodyValidator { - /** Verifies that each Local of getUses() and getDefs() belongs to this body's locals. */ + /** + * Verifies that each Local of getUses() and getDefs() belongs to this body's locals. + * + * @return + */ @Override - public void validate(@Nonnull Body body, @Nonnull List exception) { + public List validate(@Nonnull Body body, @Nonnull View view) { + + List exception = new ArrayList<>(); + final Set locals = body.getLocals(); body.getUses() @@ -54,6 +63,8 @@ public void validate(@Nonnull Body body, @Nonnull List exce new ValidationException( value, "Local not in chain : " + value + " in " + body.getMethodSignature()))); + + return exception; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/MethodValidator.java b/sootup.core/src/main/java/sootup/core/validation/MethodValidator.java index bee729f7378..67e556c48dd 100644 --- a/sootup.core/src/main/java/sootup/core/validation/MethodValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/MethodValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class MethodValidator implements BodyValidator { @@ -33,9 +34,11 @@ public class MethodValidator implements BodyValidator { *
      *
    1. static initializer should have 'static' modifier *
    + * + * @return */ @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { // TODO: check copied code from old soot /* * SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; } if (methodRef.isStaticInitializer() @@ -43,6 +46,7 @@ public void validate(Body body, List exceptions) { * " should be static! Static initializer without 'static'('0x8') modifier" + * " will cause problem when running on android platform: " + "\" is not flagged correctly wrt/ static\"!")); } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java index fa826acfa92..36c524f7270 100644 --- a/sootup.core/src/main/java/sootup/core/validation/NewValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/NewValidator.java @@ -38,6 +38,7 @@ import sootup.core.model.Body; import sootup.core.types.ReferenceType; import sootup.core.types.UnknownType; +import sootup.core.views.View; public class NewValidator implements BodyValidator { @@ -48,7 +49,9 @@ public class NewValidator implements BodyValidator { // Checks whether after each new-instruction a constructor call follows. @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { + + List exceptions = new ArrayList<>(); StmtGraph g = body.getStmtGraph(); for (Stmt u : body.getStmts()) { @@ -65,13 +68,14 @@ public void validate(Body body, List exceptions) { String.format( "Body of methodRef %s contains a new-expression, which is assigned to a non-reference local", body.getMethodSignature()))); - return; + return exceptions; } checkForInitializerOnPath(g, assign, exceptions); } } } + return exceptions; } private boolean checkForInitializerOnPath( diff --git a/sootup.core/src/main/java/sootup/core/validation/StmtsValidator.java b/sootup.core/src/main/java/sootup/core/validation/StmtsValidator.java index 2e88a1608d9..a80c86c109d 100644 --- a/sootup.core/src/main/java/sootup/core/validation/StmtsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/StmtsValidator.java @@ -24,12 +24,19 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class StmtsValidator implements BodyValidator { - /** Verifies that the Units of this Body all point to a Unit contained within this body. */ + /** + * Verifies that the Units of this Body all point to a Unit contained within this body. + * + * @return + */ @Override - public void validate(Body body, List exception) {} + public List validate(Body body, View view) { + return null; + } @Override public boolean isBasicValidator() { diff --git a/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java b/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java index 5e9093a4fd4..d13e9a4e189 100644 --- a/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java @@ -24,12 +24,17 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class TrapsValidator implements BodyValidator { - /** Verifies that the begin, end and handler units of each trap are in this body. */ + /** + * Verifies that the begin, end and handler units of each trap are in this body. + * + * @return + */ @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: check code from old soot below /* @@ -44,6 +49,7 @@ public void validate(Body body, List exception) { * if (!units.contains(t.getHandlerUnit())) { exception.add(new ValidationException(t.getHandlerUnit(), * "handler not in chain" + " in " + body.getMethod())); } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/TypesValidator.java b/sootup.core/src/main/java/sootup/core/validation/TypesValidator.java index 4da9a64818b..d7559fc6f90 100644 --- a/sootup.core/src/main/java/sootup/core/validation/TypesValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/TypesValidator.java @@ -24,6 +24,7 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; /** * Checks whether the types used for locals, method parameters, and method return values are allowed @@ -32,7 +33,7 @@ public class TypesValidator implements BodyValidator { @Override - public void validate(Body body, List exceptions) { + public List validate(Body body, View view) { /* * SootMethod methodRef = body.getMethod(); * @@ -45,6 +46,7 @@ public void validate(Body body, List exceptions) { * exceptions.add(new ValidationException(l, "Local type not allowed in final code: " + t, "(" + methodRef + * ") local type not allowed in final code: " + t + " local: " + l)); } } */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/UsesValidator.java b/sootup.core/src/main/java/sootup/core/validation/UsesValidator.java index 9d1f2e32008..849beb763a0 100644 --- a/sootup.core/src/main/java/sootup/core/validation/UsesValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/UsesValidator.java @@ -24,12 +24,17 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class UsesValidator implements BodyValidator { - /** Verifies that each use in this Body has a def. */ + /** + * Verifies that each use in this Body has a def. + * + * @return + */ @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: auto generated stub /* * // Conservative validation of uses: add edges to exception handlers // even if they are not reachable. // // class C { @@ -61,6 +66,7 @@ public void validate(Body body, List exception) { * private boolean graphEdgesAreValid(UnitGraph g, Unit u) { /* for (Unit p : g.getPredsOf(u)) { if * (!g.getSuccsOf(p).contains(u)) { return false; } } return true; */ + return null; } @Override diff --git a/sootup.core/src/main/java/sootup/core/validation/ValuesValidator.java b/sootup.core/src/main/java/sootup/core/validation/ValuesValidator.java index 5595e0cbebc..2787482fb04 100644 --- a/sootup.core/src/main/java/sootup/core/validation/ValuesValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/ValuesValidator.java @@ -24,12 +24,17 @@ import java.util.List; import sootup.core.model.Body; +import sootup.core.views.View; public class ValuesValidator implements BodyValidator { - /** Verifies that a Value is not used in more than one place. */ + /** + * Verifies that a Value is not used in more than one place. + * + * @return + */ @Override - public void validate(Body body, List exception) { + public List validate(Body body, View view) { // TODO: check code from old soot below /* * Set set = newSetFromMap(new IdentityHashMap()); @@ -40,6 +45,7 @@ public void validate(Body body, List exception) { * * for (Unit u : body.getUnits()) { System.err.println(u); } } */ + return null; } @Override diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index f4769125645..0505d4b4296 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -92,8 +92,8 @@ public PackageName getPackageName() { @Test public void testNewValidator() { - List validationExceptions_success = new ArrayList<>(); - List validationExceptions_fail = new ArrayList<>(); + List validationExceptions_success; + List validationExceptions_fail; String classPath = "src/test/java/resources/jimple"; AnalysisInputLocation jimpleInputLocation = @@ -109,33 +109,35 @@ public void testNewValidator() { } } - validator.validate( - classes.stream() - .filter(c -> c.getType().getClassName().contains("NewValidator")) - .findFirst() - .get() - .getMethods() - .stream() - .filter(m -> m.getName().contains("newValidator")) - .map(m -> m.getBody()) - .findFirst() - .get(), - validationExceptions_success); + validationExceptions_success = + validator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().contains("NewValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().contains("newValidator")) + .map(m -> m.getBody()) + .findFirst() + .get(), + view); assertEquals(0, validationExceptions_success.size()); - validator.validate( - classes.stream() - .filter(c -> c.getType().getClassName().contains("NewValidator_fail")) - .findFirst() - .get() - .getMethods() - .stream() - .filter(m -> m.getName().contains("newValidator_fail")) - .map(m -> m.getBody()) - .findFirst() - .get(), - validationExceptions_fail); + validationExceptions_fail = + validator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().contains("NewValidator_fail")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().contains("newValidator_fail")) + .map(m -> m.getBody()) + .findFirst() + .get(), + view); assertEquals(1, validationExceptions_fail.size()); } From 30bde646fa309e78a7fc26841a04ef6e68af3ed2 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 6 Dec 2023 18:10:21 +0100 Subject: [PATCH 10/22] Deleting files, jimple file used for testing --- .../binary/NewKeywordValidator.class | Bin 463 -> 0 bytes .../source/NewKeywordValidator.java | 9 --------- 2 files changed, 9 deletions(-) delete mode 100644 shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class delete mode 100644 shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java diff --git a/shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class b/shared-test-resources/miniTestSuite/ValidatorTests/binary/NewKeywordValidator.class deleted file mode 100644 index 9531b60cb6c7d97534c810bbaf5686b5ef71fe56..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 463 zcmZuuT}uK%6g{JxtJ|iPY4%+&p$7R-Z$>XcAc+Nr5Iv1L$jE-gwr2jUq7n-F0sW}x ztOX+UFn8wOd(OS*-ue1|{{(Q1T?;0bQ-~vLV#VT3Cx^U+RTNCD35Z9Ynn2=2dpbN7 zh!;yO0prZ?DuGO0d#W*T+bX!0ZHE!VmD&@?7jNr*`5-Hf^m>(M7-+9ojfhs$9|RqB zt|M@vp@tV~H1vaROFFtMLq9l-Xf`a&+ejiMko^Z_V%^3DHcf2V*v5{);ldp(9P0$Op7IP8PV2u>g$oz4R7>_d955#ZAFzU~kJ9t6t4T&i^ RO%jk`Q~nt(C!`<4(hnq|S-SuL diff --git a/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java b/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java deleted file mode 100644 index d3057c84b9a..00000000000 --- a/shared-test-resources/miniTestSuite/ValidatorTests/source/NewKeywordValidator.java +++ /dev/null @@ -1,9 +0,0 @@ -public class NewKeywordValidator { - - int x = 5; - - public static void main(String[] args) { - NewKeywordValidator myObj = new NewKeywordValidator(); - System.out.println(myObj.x); - } -} From e7eae506b7c4fe8d9323c121c7755a5acca66ebf Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Mon, 11 Dec 2023 16:48:43 +0100 Subject: [PATCH 11/22] Renaming to handle ambiguity --- sootup.java.bytecode/pom.xml | 2 +- .../{NewValidator.jimple => NewValidator_pass.jimple} | 10 +++++----- .../java6/ValidatorTests/NewValidatorTest.java | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) rename sootup.java.bytecode/src/test/java/resources/jimple/{NewValidator.jimple => NewValidator_pass.jimple} (56%) diff --git a/sootup.java.bytecode/pom.xml b/sootup.java.bytecode/pom.xml index 2dac745118e..3f1e9e33101 100644 --- a/sootup.java.bytecode/pom.xml +++ b/sootup.java.bytecode/pom.xml @@ -41,7 +41,7 @@ org.soot-oss sootup.jimple.parser - 1.1.2-SNAPSHOT + ${project.version} test diff --git a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_pass.jimple similarity index 56% rename from sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple rename to sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_pass.jimple index 21c5b06cae2..deb137d1503 100644 --- a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple +++ b/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_pass.jimple @@ -1,16 +1,16 @@ -public super class NewValidator extends java.lang.Object +public super class NewValidator_pass extends java.lang.Object { - public void newValidator() + public void newValidator_pass() { java.lang.String[] $l0; unknown $l1, $stack2, $stack3, $stack4; $l0 := @parameter0: java.lang.String[]; - $stack2 = new NewValidator; - specialinvoke $stack2.()>(); + $stack2 = new NewValidator_pass; + specialinvoke $stack2.()>(); $l1 = $stack2; $stack4 = ; - $stack3 = $l1.; + $stack3 = $l1.; virtualinvoke $stack4.($stack3); return; diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index 0505d4b4296..7d7956c6655 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -112,12 +112,12 @@ public void testNewValidator() { validationExceptions_success = validator.validate( classes.stream() - .filter(c -> c.getType().getClassName().contains("NewValidator")) + .filter(c -> c.getType().getClassName().contains("NewValidator_pass")) .findFirst() .get() .getMethods() .stream() - .filter(m -> m.getName().contains("newValidator")) + .filter(m -> m.getName().contains("newValidator_pass")) .map(m -> m.getBody()) .findFirst() .get(), From d8e357a7d95d82b62461db2743548fa85deec61e Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Tue, 19 Dec 2023 13:00:59 +0100 Subject: [PATCH 12/22] use equals instead of contains --- .../ValidatorTests/NewValidatorTest.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index 0505d4b4296..0348531f174 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -14,18 +14,18 @@ import sootup.core.types.ClassType; import sootup.core.validation.NewValidator; import sootup.core.validation.ValidationException; -import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleProject; import sootup.jimple.parser.JimpleView; -public class NewValidatorTest extends MinimalBytecodeTestSuiteBase { +public class NewValidatorTest { NewValidator validator; Body body; JavaView view; + Collection> classes; @Before public void Setup() { @@ -88,12 +88,6 @@ public PackageName getPackageName() { assertFalse(classSource1.isPresent()); final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); assertFalse(classSource2.isPresent()); - } - - @Test - public void testNewValidator() { - List validationExceptions_success; - List validationExceptions_fail; String classPath = "src/test/java/resources/jimple"; AnalysisInputLocation jimpleInputLocation = @@ -101,39 +95,49 @@ public void testNewValidator() { JimpleView view = new JimpleProject(jimpleInputLocation).createView(); - Collection> classes = new HashSet<>(); // Set to track the classes to check + classes = new HashSet<>(); // Set to track the classes to check for (SootClass aClass : view.getClasses()) { if (!aClass.isLibraryClass()) { classes.add(aClass); } } + } + + @Test + public void testNewValidatorSuccess() { + List validationExceptions_success; validationExceptions_success = validator.validate( classes.stream() - .filter(c -> c.getType().getClassName().contains("NewValidator")) + .filter(c -> c.getType().getClassName().equals("NewValidator")) .findFirst() .get() .getMethods() .stream() - .filter(m -> m.getName().contains("newValidator")) + .filter(m -> m.getName().equals("newValidator")) .map(m -> m.getBody()) .findFirst() .get(), view); assertEquals(0, validationExceptions_success.size()); + } + + @Test + public void testNewValidatorFailure() { + List validationExceptions_fail; validationExceptions_fail = validator.validate( classes.stream() - .filter(c -> c.getType().getClassName().contains("NewValidator_fail")) + .filter(c -> c.getType().getClassName().equals("NewValidator_fail")) .findFirst() .get() .getMethods() .stream() - .filter(m -> m.getName().contains("newValidator_fail")) + .filter(m -> m.getName().equals("newValidator_fail")) .map(m -> m.getBody()) .findFirst() .get(), From 8085ac9de1fae6bd72b11a3011a858c3cc451ad9 Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Tue, 19 Dec 2023 14:28:07 +0100 Subject: [PATCH 13/22] move test to integration --- sootup.java.bytecode/pom.xml | 6 ------ sootup.tests/pom.xml | 5 +++++ .../java/sootup/tests/validator}/NewValidatorTest.java | 8 +++----- .../test/resources/validator}/jimple/NewValidator.jimple | 0 .../resources/validator}/jimple/NewValidator_fail.jimple | 0 5 files changed, 8 insertions(+), 11 deletions(-) rename {sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests => sootup.tests/src/test/java/sootup/tests/validator}/NewValidatorTest.java (94%) rename {sootup.java.bytecode/src/test/java/resources => sootup.tests/src/test/resources/validator}/jimple/NewValidator.jimple (100%) rename {sootup.java.bytecode/src/test/java/resources => sootup.tests/src/test/resources/validator}/jimple/NewValidator_fail.jimple (100%) diff --git a/sootup.java.bytecode/pom.xml b/sootup.java.bytecode/pom.xml index 2dac745118e..815fc01ad47 100644 --- a/sootup.java.bytecode/pom.xml +++ b/sootup.java.bytecode/pom.xml @@ -38,12 +38,6 @@ dex2jar 2.4.6 - - org.soot-oss - sootup.jimple.parser - 1.1.2-SNAPSHOT - test - diff --git a/sootup.tests/pom.xml b/sootup.tests/pom.xml index fdf3c29f384..5c0e32b5d5b 100644 --- a/sootup.tests/pom.xml +++ b/sootup.tests/pom.xml @@ -34,6 +34,11 @@ sootup.java.sourcecode ${project.version} + + org.soot-oss + sootup.jimple.parser + ${project.version} + org.soot-oss sootup.callgraph diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java similarity index 94% rename from sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java rename to sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java index 0348531f174..4f8f56a5712 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -1,4 +1,4 @@ -package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; +package sootup.tests.validator; import static org.junit.Assert.*; @@ -7,7 +7,6 @@ import org.junit.Before; import org.junit.Test; import sootup.core.inputlocation.AnalysisInputLocation; -import sootup.core.model.Body; import sootup.core.model.SootClass; import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; @@ -23,7 +22,6 @@ public class NewValidatorTest { NewValidator validator; - Body body; JavaView view; Collection> classes; @@ -78,7 +76,7 @@ public PackageName getPackageName() { } }; - final String resourceDir = "src/test/java/resources/"; + final String resourceDir = "src/test/resources/validator"; // files direct in dir final JimpleAnalysisInputLocation inputLocation1 = @@ -89,7 +87,7 @@ public PackageName getPackageName() { final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); assertFalse(classSource2.isPresent()); - String classPath = "src/test/java/resources/jimple"; + String classPath = "src/test/resources/validator/jimple"; AnalysisInputLocation jimpleInputLocation = new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); diff --git a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple similarity index 100% rename from sootup.java.bytecode/src/test/java/resources/jimple/NewValidator.jimple rename to sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple diff --git a/sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple b/sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple similarity index 100% rename from sootup.java.bytecode/src/test/java/resources/jimple/NewValidator_fail.jimple rename to sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple From 732bcd35a07e85c3711bbeb8fdac0bc202022c14 Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Tue, 19 Dec 2023 14:56:51 +0100 Subject: [PATCH 14/22] remove JimpleProject --- .../tests/validator/NewValidatorTest.java | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java index 4f8f56a5712..f682c39cb56 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -1,28 +1,28 @@ package sootup.tests.validator; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import java.nio.file.Paths; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Test; -import sootup.core.inputlocation.AnalysisInputLocation; import sootup.core.model.SootClass; import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; import sootup.core.types.ClassType; import sootup.core.validation.NewValidator; import sootup.core.validation.ValidationException; -import sootup.java.core.JavaSootClass; -import sootup.java.core.views.JavaView; import sootup.jimple.parser.JimpleAnalysisInputLocation; -import sootup.jimple.parser.JimpleProject; import sootup.jimple.parser.JimpleView; public class NewValidatorTest { NewValidator validator; - JavaView view; + JimpleView view; Collection> classes; @Before @@ -76,22 +76,15 @@ public PackageName getPackageName() { } }; - final String resourceDir = "src/test/resources/validator"; - - // files direct in dir - final JimpleAnalysisInputLocation inputLocation1 = - new JimpleAnalysisInputLocation(Paths.get(resourceDir + "/jimple/")); - JimpleView jv1 = new JimpleProject(inputLocation1).createView(); - final Optional> classSource1 = jv1.getClass(classTypeNewValidator); - assertFalse(classSource1.isPresent()); - final Optional> classSource2 = jv1.getClass(classTypeNewValidator_fail); - assertFalse(classSource2.isPresent()); - String classPath = "src/test/resources/validator/jimple"; - AnalysisInputLocation jimpleInputLocation = + JimpleAnalysisInputLocation jimpleInputLocation = new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); - JimpleView view = new JimpleProject(jimpleInputLocation).createView(); + view = new JimpleView(jimpleInputLocation); + final Optional> classSource1 = view.getClass(classTypeNewValidator); + assertFalse(classSource1.isPresent()); + final Optional> classSource2 = view.getClass(classTypeNewValidator_fail); + assertFalse(classSource2.isPresent()); classes = new HashSet<>(); // Set to track the classes to check From 8bde134566f7e5659cc48baf6de55db033c417f9 Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Tue, 19 Dec 2023 15:09:10 +0100 Subject: [PATCH 15/22] remove hardcoded version --- sootup.java.bytecode/pom.xml | 4 ++-- sootup.java.sourcecode/pom.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sootup.java.bytecode/pom.xml b/sootup.java.bytecode/pom.xml index 815fc01ad47..e3a1e875d14 100644 --- a/sootup.java.bytecode/pom.xml +++ b/sootup.java.bytecode/pom.xml @@ -16,12 +16,12 @@ org.soot-oss sootup.core - 1.1.2-SNAPSHOT + ${project.version} org.soot-oss sootup.java.core - 1.1.2-SNAPSHOT + ${project.version} org.ow2.asm diff --git a/sootup.java.sourcecode/pom.xml b/sootup.java.sourcecode/pom.xml index 56f40ea86d3..669c7f133f4 100644 --- a/sootup.java.sourcecode/pom.xml +++ b/sootup.java.sourcecode/pom.xml @@ -69,12 +69,12 @@ org.soot-oss sootup.core - 1.1.2-SNAPSHOT + ${project.version} org.soot-oss sootup.java.core - 1.1.2-SNAPSHOT + ${project.version} From 331d0598bdca307f3600e248cf86cec0c21f820a Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Tue, 19 Dec 2023 17:40:04 +0100 Subject: [PATCH 16/22] Fixing Tests --- .../java6/ValidatorTests/NewValidatorTest.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java index 7d7956c6655..bda63d6ade2 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/minimaltestsuite/java6/ValidatorTests/NewValidatorTest.java @@ -1,9 +1,12 @@ package sootup.java.bytecode.minimaltestsuite.java6.ValidatorTests; -import static org.junit.Assert.*; import java.nio.file.Paths; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; import sootup.core.inputlocation.AnalysisInputLocation; @@ -14,14 +17,16 @@ import sootup.core.types.ClassType; import sootup.core.validation.NewValidator; import sootup.core.validation.ValidationException; -import sootup.java.bytecode.minimaltestsuite.MinimalBytecodeTestSuiteBase; import sootup.java.core.JavaSootClass; import sootup.java.core.views.JavaView; import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleProject; import sootup.jimple.parser.JimpleView; -public class NewValidatorTest extends MinimalBytecodeTestSuiteBase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class NewValidatorTest { NewValidator validator; Body body; @@ -41,12 +46,12 @@ public boolean isBuiltInClass() { @Override public String getFullyQualifiedName() { - return "jimple.NewValidator"; + return "jimple.NewValidator_pass"; } @Override public String getClassName() { - return "NewValidator"; + return "NewValidator_pass"; } @Override From 3bcdb992e2ec32e811d3e26aab8c3530a6175aa6 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Tue, 19 Dec 2023 18:58:00 +0100 Subject: [PATCH 17/22] Refactoring NewValidator tests --- sootup.tests/pom.xml | 8 ++++- .../tests/validator/NewValidatorTest.java | 29 ++----------------- .../validator/jimple/NewValidator.jimple | 18 +++++++++++- .../validator/jimple/NewValidator_fail.jimple | 19 ------------ .../validator/jimple/NewValidator_pass.jimple | 18 ------------ 5 files changed, 26 insertions(+), 66 deletions(-) delete mode 100644 sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple delete mode 100644 sootup.tests/src/test/resources/validator/jimple/NewValidator_pass.jimple diff --git a/sootup.tests/pom.xml b/sootup.tests/pom.xml index 5c0e32b5d5b..fd266c24d67 100644 --- a/sootup.tests/pom.xml +++ b/sootup.tests/pom.xml @@ -44,7 +44,13 @@ sootup.callgraph ${project.version} - + + org.soot-oss + sootup.jimple.parser + 1.1.2-SNAPSHOT + test + + diff --git a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java index f682c39cb56..ebd12db02f3 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -53,29 +53,6 @@ public PackageName getPackageName() { } }; - final ClassType classTypeNewValidator_fail = - new ClassType() { - @Override - public boolean isBuiltInClass() { - return false; - } - - @Override - public String getFullyQualifiedName() { - return "jimple.NewValidator_fail"; - } - - @Override - public String getClassName() { - return "NewValidator_fail"; - } - - @Override - public PackageName getPackageName() { - return new PackageName("jimple"); - } - }; - String classPath = "src/test/resources/validator/jimple"; JimpleAnalysisInputLocation jimpleInputLocation = new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); @@ -83,8 +60,6 @@ public PackageName getPackageName() { view = new JimpleView(jimpleInputLocation); final Optional> classSource1 = view.getClass(classTypeNewValidator); assertFalse(classSource1.isPresent()); - final Optional> classSource2 = view.getClass(classTypeNewValidator_fail); - assertFalse(classSource2.isPresent()); classes = new HashSet<>(); // Set to track the classes to check @@ -107,7 +82,7 @@ public void testNewValidatorSuccess() { .get() .getMethods() .stream() - .filter(m -> m.getName().equals("newValidator")) + .filter(m -> m.getName().equals("newValidator_pass")) .map(m -> m.getBody()) .findFirst() .get(), @@ -123,7 +98,7 @@ public void testNewValidatorFailure() { validationExceptions_fail = validator.validate( classes.stream() - .filter(c -> c.getType().getClassName().equals("NewValidator_fail")) + .filter(c -> c.getType().getClassName().equals("NewValidator")) .findFirst() .get() .getMethods() diff --git a/sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple index deb137d1503..f8aea86b66a 100644 --- a/sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple +++ b/sootup.tests/src/test/resources/validator/jimple/NewValidator.jimple @@ -1,4 +1,4 @@ -public super class NewValidator_pass extends java.lang.Object +public super class NewValidator extends java.lang.Object { public void newValidator_pass() { @@ -15,4 +15,20 @@ public super class NewValidator_pass extends java.lang.Object return; } + + public void newValidator_fail() + { + java.lang.String[] $l0; + unknown $l1, $stack2, $stack3, $stack4; + + $l0 := @parameter0: java.lang.String[]; + $stack2 = new NewValidator_fail; + + $l1 = $stack2; + $stack4 = ; + $stack3 = $l1.; + virtualinvoke $stack4.($stack3); + + return; + } } \ No newline at end of file diff --git a/sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple b/sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple deleted file mode 100644 index 39c75fbbd58..00000000000 --- a/sootup.tests/src/test/resources/validator/jimple/NewValidator_fail.jimple +++ /dev/null @@ -1,19 +0,0 @@ -public super class NewValidator_fail extends java.lang.Object -{ - public void newValidator_fail() - { - java.lang.String[] $l0; - unknown $l1, $stack2, $stack3, $stack4; - - $l0 := @parameter0: java.lang.String[]; - $stack2 = new NewValidator_fail; - - $l1 = $stack2; - $stack4 = ; - $stack3 = $l1.; - virtualinvoke $stack4.($stack3); - - return; - } - -} \ No newline at end of file diff --git a/sootup.tests/src/test/resources/validator/jimple/NewValidator_pass.jimple b/sootup.tests/src/test/resources/validator/jimple/NewValidator_pass.jimple deleted file mode 100644 index deb137d1503..00000000000 --- a/sootup.tests/src/test/resources/validator/jimple/NewValidator_pass.jimple +++ /dev/null @@ -1,18 +0,0 @@ -public super class NewValidator_pass extends java.lang.Object -{ - public void newValidator_pass() - { - java.lang.String[] $l0; - unknown $l1, $stack2, $stack3, $stack4; - - $l0 := @parameter0: java.lang.String[]; - $stack2 = new NewValidator_pass; - specialinvoke $stack2.()>(); - $l1 = $stack2; - $stack4 = ; - $stack3 = $l1.; - virtualinvoke $stack4.($stack3); - - return; - } -} \ No newline at end of file From 64f5d8dc7274e0d3f4b79db8200b3f0f230cd17d Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 20 Dec 2023 15:54:06 +0100 Subject: [PATCH 18/22] Commenting FieldRefValidator changes. --- .../core/validation/FieldRefValidator.java | 122 +++++++++--------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java index d4a015c65b4..9dea14ca66a 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -24,14 +24,14 @@ import java.util.ArrayList; import java.util.List; -import sootup.core.graph.MutableStmtGraph; -import sootup.core.jimple.common.ref.JFieldRef; -import sootup.core.jimple.common.ref.JInstanceFieldRef; -import sootup.core.jimple.common.ref.JStaticFieldRef; -import sootup.core.jimple.common.stmt.Stmt; +//import sootup.core.graph.MutableStmtGraph; +//import sootup.core.jimple.common.ref.JFieldRef; +//import sootup.core.jimple.common.ref.JInstanceFieldRef; +//import sootup.core.jimple.common.ref.JStaticFieldRef; +//import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; -import sootup.core.model.MethodModifier; -import sootup.core.model.SootField; +//import sootup.core.model.MethodModifier; +//import sootup.core.model.SootField; import sootup.core.views.View; public class FieldRefValidator implements BodyValidator { @@ -41,60 +41,60 @@ public class FieldRefValidator implements BodyValidator { public List validate(Body body, View view) { List validationException = new ArrayList<>(); - - Body.BodyBuilder bodyBuilder = Body.builder((MutableStmtGraph) body.getStmtGraph()); - if (bodyBuilder.getModifiers().contains(MethodModifier.ABSTRACT)) { - return validationException; - } - - List stmts = body.getStmts(); - - for (Stmt stmt : stmts) { - if (!stmt.containsFieldRef()) { - continue; - } - JFieldRef fr = stmt.getFieldRef(); - - if (fr instanceof JStaticFieldRef) { - JStaticFieldRef v = (JStaticFieldRef) fr; - try { - SootField field = view.getField(v.getFieldSignature()).get(); - if (field == null) { - validationException.add(new ValidationException(v, "Resolved field is null: ")); - } else if (!field.isStatic()) { - validationException.add( - new ValidationException( - v, "Trying to get a static field which is non-static: " + v)); - } - } catch (Exception e) { - validationException.add( - new ValidationException( - body.getStmts().get(0).getFieldRef(), - "Trying to get a static field which is non-static: " + v)); - } - } else if (fr instanceof JInstanceFieldRef) { - JInstanceFieldRef v = (JInstanceFieldRef) fr; - - try { - SootField field = view.getField(v.getFieldSignature()).get(); - if (field == null) { - validationException.add( - new ValidationException( - body.getStmts().get(0).getFieldRef(), - "Resolved field is null: " + fr.toString())); - } else if (field.isStatic()) { - validationException.add( - new ValidationException( - v, "Trying to get an instance field which is static: " + v)); - } - } catch (Exception e) { - validationException.add( - new ValidationException(v, "Trying to get an instance field which is static: " + v)); - } - } else { - throw new RuntimeException("unknown field ref"); - } - } +// +// Body.BodyBuilder bodyBuilder = Body.builder((MutableStmtGraph) body.getStmtGraph()); +// if (bodyBuilder.getModifiers().contains(MethodModifier.ABSTRACT)) { +// return validationException; +// } +// +// List stmts = body.getStmts(); +// +// for (Stmt stmt : stmts) { +// if (!stmt.containsFieldRef()) { +// continue; +// } +// JFieldRef fr = stmt.getFieldRef(); +// +// if (fr instanceof JStaticFieldRef) { +// JStaticFieldRef v = (JStaticFieldRef) fr; +// try { +// SootField field = view.getField(v.getFieldSignature()).get(); +// if (field == null) { +// validationException.add(new ValidationException(v, "Resolved field is null: ")); +// } else if (!field.isStatic()) { +// validationException.add( +// new ValidationException( +// v, "Trying to get a static field which is non-static: " + v)); +// } +// } catch (Exception e) { +// validationException.add( +// new ValidationException( +// body.getStmts().get(0).getFieldRef(), +// "Trying to get a static field which is non-static: " + v)); +// } +// } else if (fr instanceof JInstanceFieldRef) { +// JInstanceFieldRef v = (JInstanceFieldRef) fr; +// +// try { +// SootField field = view.getField(v.getFieldSignature()).get(); +// if (field == null) { +// validationException.add( +// new ValidationException( +// body.getStmts().get(0).getFieldRef(), +// "Resolved field is null: " + fr.toString())); +// } else if (field.isStatic()) { +// validationException.add( +// new ValidationException( +// v, "Trying to get an instance field which is static: " + v)); +// } +// } catch (Exception e) { +// validationException.add( +// new ValidationException(v, "Trying to get an instance field which is static: " + v)); +// } +// } else { +// throw new RuntimeException("unknown field ref"); +// } +// } return validationException; } From 57209fbf16c8636f6f623d8da7536bf58508736c Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 21 Dec 2023 13:10:53 +0100 Subject: [PATCH 19/22] Rollback changes for FieldRefValidator --- .../core/validation/FieldRefValidator.java | 80 ++++++------------- 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java index 9dea14ca66a..780588c4584 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -41,60 +41,32 @@ public class FieldRefValidator implements BodyValidator { public List validate(Body body, View view) { List validationException = new ArrayList<>(); -// -// Body.BodyBuilder bodyBuilder = Body.builder((MutableStmtGraph) body.getStmtGraph()); -// if (bodyBuilder.getModifiers().contains(MethodModifier.ABSTRACT)) { -// return validationException; -// } -// -// List stmts = body.getStmts(); -// -// for (Stmt stmt : stmts) { -// if (!stmt.containsFieldRef()) { -// continue; -// } -// JFieldRef fr = stmt.getFieldRef(); -// -// if (fr instanceof JStaticFieldRef) { -// JStaticFieldRef v = (JStaticFieldRef) fr; -// try { -// SootField field = view.getField(v.getFieldSignature()).get(); -// if (field == null) { -// validationException.add(new ValidationException(v, "Resolved field is null: ")); -// } else if (!field.isStatic()) { -// validationException.add( -// new ValidationException( -// v, "Trying to get a static field which is non-static: " + v)); -// } -// } catch (Exception e) { -// validationException.add( -// new ValidationException( -// body.getStmts().get(0).getFieldRef(), -// "Trying to get a static field which is non-static: " + v)); -// } -// } else if (fr instanceof JInstanceFieldRef) { -// JInstanceFieldRef v = (JInstanceFieldRef) fr; -// -// try { -// SootField field = view.getField(v.getFieldSignature()).get(); -// if (field == null) { -// validationException.add( -// new ValidationException( -// body.getStmts().get(0).getFieldRef(), -// "Resolved field is null: " + fr.toString())); -// } else if (field.isStatic()) { -// validationException.add( -// new ValidationException( -// v, "Trying to get an instance field which is static: " + v)); -// } -// } catch (Exception e) { -// validationException.add( -// new ValidationException(v, "Trying to get an instance field which is static: " + v)); -// } -// } else { -// throw new RuntimeException("unknown field ref"); -// } -// } + + // TODO: check copied code from old soot + /* + * SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; } + * + * Chain units = body.getUnits().getNonPatchingChain(); + * + * for (Unit unit : units) { Stmt s = (Stmt) unit; if (!s.containsFieldRef()) { continue; } JFieldRef fr = + * s.getFieldRef(); + * + * if (fr instanceof JStaticFieldRef) { JStaticFieldRef v = (JStaticFieldRef) fr; try { SootField field = v.getField(); + * if (field == null) { exceptions.add(new UnitValidationException(unit, body, "Resolved field is null: " + + * fr.toString())); } else if (!field.isStatic() && !field.isPhantom()) { exceptions .add(new + * UnitValidationException(unit, body, "Trying to get a static field which is non-static: " + v)); } } catch + * (ResolutionFailedException e) { exceptions.add(new UnitValidationException(unit, body, + * "Trying to get a static field which is non-static: " + v)); } } else if (fr instanceof InstanceFieldRef) { + * InstanceFieldRef v = (InstanceFieldRef) fr; + * + * try { SootField field = v.getField(); if (field == null) { exceptions.add(new UnitValidationException(unit, body, + * "Resolved field is null: " + fr.toString())); } else if (field.isStatic() && !field.isPhantom()) { exceptions.add(new + * UnitValidationException(unit, body, "Trying to get an instance field which is static: " + v)); } } catch + * (ResolutionFailedException e) { exceptions.add(new UnitValidationException(unit, body, + * "Trying to get an instance field which is static: " + v)); } } else { throw new RuntimeException("unknown field ref"); + * } } + */ + return validationException; } From e2a0747dbe9c67c11e08ae06d68f74111630802d Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 21 Dec 2023 13:14:41 +0100 Subject: [PATCH 20/22] Rollback changes for FieldRefValidator --- .../java/sootup/core/validation/FieldRefValidator.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java index 780588c4584..f7a2c07a04a 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -4,7 +4,7 @@ * #%L * Soot - a J*va Optimization Framework * %% - * Copyright (C) 1997-2020 Raja Vallée-Rai, Christian Brüggemann, Markus Schmidt, Akshita Dubey and others + * Copyright (C) 1997-2020 Raja Vallée-Rai, Christian Brüggemann, Markus Schmidt and others * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -24,14 +24,7 @@ import java.util.ArrayList; import java.util.List; -//import sootup.core.graph.MutableStmtGraph; -//import sootup.core.jimple.common.ref.JFieldRef; -//import sootup.core.jimple.common.ref.JInstanceFieldRef; -//import sootup.core.jimple.common.ref.JStaticFieldRef; -//import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; -//import sootup.core.model.MethodModifier; -//import sootup.core.model.SootField; import sootup.core.views.View; public class FieldRefValidator implements BodyValidator { From ae8ede56dca48a24514ee204602e53f8ad17c7c2 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 21 Dec 2023 15:05:22 +0100 Subject: [PATCH 21/22] Rollback LocalsValidator changes --- .../core/validation/LocalsValidator.java | 44 +++++++++---------- sootup.java.bytecode/pom.xml | 6 --- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java index eaf39c38f89..17bedda222f 100644 --- a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java @@ -39,30 +39,30 @@ public class LocalsValidator implements BodyValidator { */ @Override public List validate(@Nonnull Body body, @Nonnull View view) { - +//TODO : Write tests List exception = new ArrayList<>(); - final Set locals = body.getLocals(); - - body.getUses() - .parallelStream() - .filter(value -> value instanceof Local && !locals.contains(value)) - .forEach( - value -> - exception.add( - new ValidationException( - value, - "Local not in chain : " + value + " in " + body.getMethodSignature()))); - - body.getDefs() - .parallelStream() - .filter(value -> value instanceof Local && !locals.contains(value)) - .forEach( - value -> - exception.add( - new ValidationException( - value, - "Local not in chain : " + value + " in " + body.getMethodSignature()))); +// final Set locals = body.getLocals(); +// +// body.getUses() +// .parallelStream() +// .filter(value -> value instanceof Local && !locals.contains(value)) +// .forEach( +// value -> +// exception.add( +// new ValidationException( +// value, +// "Local not in chain : " + value + " in " + body.getMethodSignature()))); +// +// body.getDefs() +// .parallelStream() +// .filter(value -> value instanceof Local && !locals.contains(value)) +// .forEach( +// value -> +// exception.add( +// new ValidationException( +// value, +// "Local not in chain : " + value + " in " + body.getMethodSignature()))); return exception; } diff --git a/sootup.java.bytecode/pom.xml b/sootup.java.bytecode/pom.xml index f61f7538893..e3a1e875d14 100644 --- a/sootup.java.bytecode/pom.xml +++ b/sootup.java.bytecode/pom.xml @@ -38,12 +38,6 @@ dex2jar 2.4.6 - - org.soot-oss - sootup.jimple.parser - 1.1.2-SNAPSHOT - test - From 3dd6d09989fcf7acd68fcd3c224c27429df44265 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Mon, 25 Dec 2023 21:26:45 +0100 Subject: [PATCH 22/22] Formatting changes --- .../core/validation/LocalsValidator.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java index 17bedda222f..57808e72bf0 100644 --- a/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/LocalsValidator.java @@ -24,9 +24,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Set; import javax.annotation.Nonnull; -import sootup.core.jimple.basic.Local; import sootup.core.model.Body; import sootup.core.views.View; @@ -39,30 +37,32 @@ public class LocalsValidator implements BodyValidator { */ @Override public List validate(@Nonnull Body body, @Nonnull View view) { -//TODO : Write tests + // TODO : Write tests List exception = new ArrayList<>(); -// final Set locals = body.getLocals(); -// -// body.getUses() -// .parallelStream() -// .filter(value -> value instanceof Local && !locals.contains(value)) -// .forEach( -// value -> -// exception.add( -// new ValidationException( -// value, -// "Local not in chain : " + value + " in " + body.getMethodSignature()))); -// -// body.getDefs() -// .parallelStream() -// .filter(value -> value instanceof Local && !locals.contains(value)) -// .forEach( -// value -> -// exception.add( -// new ValidationException( -// value, -// "Local not in chain : " + value + " in " + body.getMethodSignature()))); + // final Set locals = body.getLocals(); + // + // body.getUses() + // .parallelStream() + // .filter(value -> value instanceof Local && !locals.contains(value)) + // .forEach( + // value -> + // exception.add( + // new ValidationException( + // value, + // "Local not in chain : " + value + " in " + + // body.getMethodSignature()))); + // + // body.getDefs() + // .parallelStream() + // .filter(value -> value instanceof Local && !locals.contains(value)) + // .forEach( + // value -> + // exception.add( + // new ValidationException( + // value, + // "Local not in chain : " + value + " in " + + // body.getMethodSignature()))); return exception; }