From b924f211241bdc1196712585ad1d551e7183fd3b Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 20 Dec 2023 22:59:31 +0100 Subject: [PATCH 1/9] FieldRefValidator changes + First Draft of test --- .../core/validation/FieldRefValidator.java | 123 +++++++++--------- .../validator/FieldRefValidatorTest.java | 95 ++++++++++++++ .../validator/jimple/FieldRefValidator.jimple | 37 ++++++ 3 files changed, 194 insertions(+), 61 deletions(-) create mode 100644 sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java create mode 100644 sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple 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..e4d440c637f 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,15 @@ 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.model.SootMethod; import sootup.core.views.View; public class FieldRefValidator implements BodyValidator { @@ -41,60 +42,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"); -// } -// } + + SootMethod sootMethod = view.getMethod(body.getMethodSignature()).get(); + if(sootMethod.isAbstract()){ + 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( + fr, "Trying to get a static field which is non-static: " + v)); + } + } catch (Exception e) { + validationException.add( + new ValidationException( + fr, + "Trying to get a static field which is non-static: " + v+" "+e.getMessage())); + } + } else if (fr instanceof JInstanceFieldRef) { + JInstanceFieldRef v = (JInstanceFieldRef) fr; + + try { + SootField field = view.getField(v.getFieldSignature()).get(); + if (field == null) { + validationException.add( + new ValidationException( + fr, + "Resolved field is null: " + fr.toString())); + } else if (field.isStatic()) { + validationException.add( + new ValidationException( + fr, "Trying to get an instance field which is static: " + v)); + } + } catch (Exception e) { + validationException.add( + new ValidationException(fr, "Trying to get an instance field which is static: " + v+" "+e.getMessage())); + } + } else { + throw new RuntimeException("unknown field ref"); + } + } return validationException; } diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java new file mode 100644 index 00000000000..3ea6ee915b1 --- /dev/null +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -0,0 +1,95 @@ +package sootup.tests.validator; + +import org.junit.Before; +import org.junit.Test; +import sootup.core.model.SootClass; +import sootup.core.model.SourceType; +import sootup.core.signatures.PackageName; +import sootup.core.types.ClassType; +import sootup.core.validation.FieldRefValidator; +import sootup.core.validation.ValidationException; +import sootup.jimple.parser.JimpleAnalysisInputLocation; +import sootup.jimple.parser.JimpleView; + +import java.nio.file.Paths; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class FieldRefValidatorTest { + + FieldRefValidator fieldRefValidator; + JimpleView jimpleView; + Collection> classes; + + @Before + public void Setup() { + + fieldRefValidator = new FieldRefValidator(); + + ClassType classTypeNewValidator = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.FieldRefValidator"; + } + + @Override + public String getClassName() { + return "FieldRefValidator"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + String classPath = "src/test/resources/validator/jimple"; + JimpleAnalysisInputLocation jimpleInputLocation = + new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); + + jimpleView = new JimpleView(jimpleInputLocation); + final Optional> classSource1 = jimpleView.getClass(classTypeNewValidator); + assertFalse(classSource1.isPresent()); + + classes = new HashSet<>(); // Set to track the classes to check + + for (SootClass aClass : jimpleView.getClasses()) { + if (!aClass.isLibraryClass()) { + classes.add(aClass); + } + } + } + + @Test + public void testFieldRefValidatorSuccess() { + List validationExceptions_success; + + validationExceptions_success = + fieldRefValidator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().equals("fieldRefVariable")) + .map(m -> m.getBody()) + .findFirst() + .get(), + jimpleView); + + assertEquals(1, validationExceptions_success.size()); +// System.out.println(validationExceptions_success.get(0).getMessage()); + } +} diff --git a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple new file mode 100644 index 00000000000..f1cb60d7365 --- /dev/null +++ b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple @@ -0,0 +1,37 @@ +public super class FieldRefValidator extends java.lang.Object +{ + static int num; + + static void () + { + + = 6; + + return; + } + + public void () + { + unknown l0; + + l0 := @this: FieldRefValidator; + + specialinvoke l0.()>(); + + return; + } + + public static void fieldRefVariable() + { + unknown $stack1, $stack0; + + $stack1 = ; + + $stack0 = ; + + virtualinvoke $stack1.($stack0); + + return; + } +} + From dc924a407984e4835640568430728c4fe9d83680 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 21 Dec 2023 13:07:53 +0100 Subject: [PATCH 2/9] success test --- .../validator/FieldRefValidatorTest.java | 26 ++++++++++++++++--- .../validator/jimple/FieldRefValidator.jimple | 16 ++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index 3ea6ee915b1..c5bae70f23c 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -83,13 +83,33 @@ public void testFieldRefValidatorSuccess() { .get() .getMethods() .stream() - .filter(m -> m.getName().equals("fieldRefVariable")) + .filter(m -> m.getName().equals("fieldRefVariable_pass")) .map(m -> m.getBody()) .findFirst() .get(), jimpleView); - assertEquals(1, validationExceptions_success.size()); -// System.out.println(validationExceptions_success.get(0).getMessage()); + assertEquals(0, validationExceptions_success.size()); + } + + @Test + public void testFieldRefValidatorFailure() { + List validationExceptions_fail; + + validationExceptions_fail = + fieldRefValidator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().equals("fieldRefVariable_fail")) + .map(m -> m.getBody()) + .findFirst() + .get(), + jimpleView); + + assertEquals(0, validationExceptions_fail.size()); } } diff --git a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple index f1cb60d7365..f6b52d47f97 100644 --- a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple +++ b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple @@ -21,17 +21,23 @@ public super class FieldRefValidator extends java.lang.Object return; } - public static void fieldRefVariable() + public static void fieldRefVariable_pass() { - unknown $stack1, $stack0; - - $stack1 = ; + unknown $stack0; $stack0 = ; - virtualinvoke $stack1.($stack0); + return; + } + + public static void fieldRefVariable_fail(){ + + int nonStaticVar; + + nonStaticVar = ; return; + } } From fface8fa422bcdf8acedd931c6fc130bdf29c057 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 21 Dec 2023 15:02:03 +0100 Subject: [PATCH 3/9] jhsfsj --- .../core/validation/FieldRefValidator.java | 34 +++++++++++-------- .../validator/jimple/FieldRefValidator.jimple | 14 +++----- 2 files changed, 24 insertions(+), 24 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 e4d440c637f..e812dbc7e2d 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Optional; + import sootup.core.graph.MutableStmtGraph; import sootup.core.jimple.common.ref.JFieldRef; import sootup.core.jimple.common.ref.JInstanceFieldRef; @@ -59,13 +61,17 @@ public List validate(Body body, View view) { 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( - fr, "Trying to get a static field which is non-static: " + v)); + Optional fieldOpt = view.getField(v.getFieldSignature()); + if (!fieldOpt.isPresent()) { + validationException.add(new ValidationException(v, "Resolved field is empty: ")); + } else { + SootField field = fieldOpt.get(); + if (!field.isStatic()) { + validationException.add( + new ValidationException( + fr, "Trying to get a static field which is non-static: " + v)); + } + } } catch (Exception e) { validationException.add( @@ -77,16 +83,16 @@ public List validate(Body body, View view) { JInstanceFieldRef v = (JInstanceFieldRef) fr; try { - SootField field = view.getField(v.getFieldSignature()).get(); - if (field == null) { + Optional fieldOpt = view.getField(v.getFieldSignature()); + if (!fieldOpt.isPresent()) { validationException.add( new ValidationException( - fr, - "Resolved field is null: " + fr.toString())); - } else if (field.isStatic()) { + fr, "Resolved field is null: " + fr.toString())); + } else{ if (field.isStatic()) { validationException.add( - new ValidationException( - fr, "Trying to get an instance field which is static: " + v)); + new ValidationException( + fr, "Trying to get an instance field which is static: " + v)); + } } } catch (Exception e) { validationException.add( diff --git a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple index f6b52d47f97..c7bdd0db022 100644 --- a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple +++ b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple @@ -1,11 +1,13 @@ public super class FieldRefValidator extends java.lang.Object { static int num; + double pi; static void () { = 6; + = 3.14; return; } @@ -23,21 +25,13 @@ public super class FieldRefValidator extends java.lang.Object public static void fieldRefVariable_pass() { - unknown $stack0; + unknown $stack0, $stack1; $stack0 = ; - return; - } - - public static void fieldRefVariable_fail(){ - - int nonStaticVar; - - nonStaticVar = ; + $stack1 = ; return; - } } From 259bad32f3fd71a81b3faec1196b3a5bdb6b9ff9 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Wed, 27 Dec 2023 22:27:03 +0100 Subject: [PATCH 4/9] FieldRefValidator changes and test --- .../core/validation/FieldRefValidator.java | 33 ++- .../validator/FieldRefValidatorTest.java | 189 +++++++++--------- .../validator/jimple/FieldRefValidator.jimple | 43 ++-- 3 files changed, 138 insertions(+), 127 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 e812dbc7e2d..9211190cc6c 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -25,14 +25,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; - -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.SootMethod; import sootup.core.views.View; @@ -46,7 +43,7 @@ public List validate(Body body, View view) { List validationException = new ArrayList<>(); SootMethod sootMethod = view.getMethod(body.getMethodSignature()).get(); - if(sootMethod.isAbstract()){ + if (sootMethod.isAbstract()) { return validationException; } @@ -68,16 +65,15 @@ public List validate(Body body, View view) { SootField field = fieldOpt.get(); if (!field.isStatic()) { validationException.add( - new ValidationException( - fr, "Trying to get a static field which is non-static: " + v)); + new ValidationException( + fr, "Trying to get a static field which is non-static: " + v)); } - } } catch (Exception e) { validationException.add( new ValidationException( - fr, - "Trying to get a static field which is non-static: " + v+" "+e.getMessage())); + fr, + "Trying to get a static field which is non-static: " + v + " " + e.getMessage())); } } else if (fr instanceof JInstanceFieldRef) { JInstanceFieldRef v = (JInstanceFieldRef) fr; @@ -86,17 +82,20 @@ public List validate(Body body, View view) { Optional fieldOpt = view.getField(v.getFieldSignature()); if (!fieldOpt.isPresent()) { validationException.add( - new ValidationException( - fr, "Resolved field is null: " + fr.toString())); - } else{ if (field.isStatic()) { - validationException.add( - new ValidationException( - fr, "Trying to get an instance field which is static: " + v)); - } + new ValidationException(fr, "Resolved field is null: " + fr.toString())); + } else { + SootField field = fieldOpt.get(); + if (field.isStatic()) { + validationException.add( + new ValidationException( + fr, "Trying to get an instance field which is static: " + v)); + } } } catch (Exception e) { validationException.add( - new ValidationException(fr, "Trying to get an instance field which is static: " + v+" "+e.getMessage())); + new ValidationException( + fr, + "Trying to get an instance field which is static: " + v + " " + e.getMessage())); } } else { throw new RuntimeException("unknown field ref"); diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index c5bae70f23c..986c6d05930 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -1,5 +1,13 @@ package sootup.tests.validator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.nio.file.Paths; +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.model.SootClass; @@ -11,105 +19,96 @@ import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleView; -import java.nio.file.Paths; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; +public class FieldRefValidatorTest { -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; + FieldRefValidator fieldRefValidator; + JimpleView jimpleView; + Collection> classes; -public class FieldRefValidatorTest { + @Before + public void Setup() { - FieldRefValidator fieldRefValidator; - JimpleView jimpleView; - Collection> classes; - - @Before - public void Setup() { - - fieldRefValidator = new FieldRefValidator(); - - ClassType classTypeNewValidator = - new ClassType() { - @Override - public boolean isBuiltInClass() { - return false; - } - - @Override - public String getFullyQualifiedName() { - return "jimple.FieldRefValidator"; - } - - @Override - public String getClassName() { - return "FieldRefValidator"; - } - - @Override - public PackageName getPackageName() { - return new PackageName("jimple"); - } - }; - - String classPath = "src/test/resources/validator/jimple"; - JimpleAnalysisInputLocation jimpleInputLocation = - new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); - - jimpleView = new JimpleView(jimpleInputLocation); - final Optional> classSource1 = jimpleView.getClass(classTypeNewValidator); - assertFalse(classSource1.isPresent()); - - classes = new HashSet<>(); // Set to track the classes to check - - for (SootClass aClass : jimpleView.getClasses()) { - if (!aClass.isLibraryClass()) { - classes.add(aClass); - } - } - } + fieldRefValidator = new FieldRefValidator(); - @Test - public void testFieldRefValidatorSuccess() { - List validationExceptions_success; - - validationExceptions_success = - fieldRefValidator.validate( - classes.stream() - .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) - .findFirst() - .get() - .getMethods() - .stream() - .filter(m -> m.getName().equals("fieldRefVariable_pass")) - .map(m -> m.getBody()) - .findFirst() - .get(), - jimpleView); - - assertEquals(0, validationExceptions_success.size()); - } + ClassType classTypeNewValidator = + new ClassType() { + @Override + public boolean isBuiltInClass() { + return false; + } + + @Override + public String getFullyQualifiedName() { + return "jimple.FieldRefValidator"; + } + + @Override + public String getClassName() { + return "FieldRefValidator"; + } + + @Override + public PackageName getPackageName() { + return new PackageName("jimple"); + } + }; + + String classPath = "src/test/resources/validator/jimple"; + JimpleAnalysisInputLocation jimpleInputLocation = + new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); + + jimpleView = new JimpleView(jimpleInputLocation); + final Optional> classSource1 = jimpleView.getClass(classTypeNewValidator); + assertFalse(classSource1.isPresent()); + + classes = new HashSet<>(); // Set to track the classes to check - @Test - public void testFieldRefValidatorFailure() { - List validationExceptions_fail; - - validationExceptions_fail = - fieldRefValidator.validate( - classes.stream() - .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) - .findFirst() - .get() - .getMethods() - .stream() - .filter(m -> m.getName().equals("fieldRefVariable_fail")) - .map(m -> m.getBody()) - .findFirst() - .get(), - jimpleView); - - assertEquals(0, validationExceptions_fail.size()); + for (SootClass aClass : jimpleView.getClasses()) { + if (!aClass.isLibraryClass()) { + classes.add(aClass); + } } + } + + @Test + public void testFieldRefValidatorSuccess() { + List validationExceptions_success; + + validationExceptions_success = + fieldRefValidator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().equals("testFieldRefVariable_pass")) + .map(m -> m.getBody()) + .findFirst() + .get(), + jimpleView); + + assertEquals(0, validationExceptions_success.size()); + } + + @Test + public void testFieldRefValidatorFailure() { + List validationExceptions_fail; + + validationExceptions_fail = + fieldRefValidator.validate( + classes.stream() + .filter(c -> c.getType().getClassName().equals("FieldRefValidator")) + .findFirst() + .get() + .getMethods() + .stream() + .filter(m -> m.getName().equals("testFieldRefVariable_fail")) + .map(m -> m.getBody()) + .findFirst() + .get(), + jimpleView); + + assertEquals(2, validationExceptions_fail.size()); + } } diff --git a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple index c7bdd0db022..1ed3945ac3c 100644 --- a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple +++ b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple @@ -1,37 +1,50 @@ public super class FieldRefValidator extends java.lang.Object { - static int num; - double pi; + private int x; - static void () + private static int y; + + public void () { + FieldRefValidator $l0; - = 6; - = 3.14; + + $l0 := @this: FieldRefValidator; + specialinvoke $l0.()>(); return; } - public void () + static void () { - unknown l0; - l0 := @this: FieldRefValidator; - - specialinvoke l0.()>(); + = 5; return; } - public static void fieldRefVariable_pass() + public void testFieldRefVariable_pass() { - unknown $stack0, $stack1; + FieldRefValidator $l0; + unknown $stack1, $stack2; + + $l0 := @this: FieldRefValidator; + $stack1 = $l0.; + $stack2 = ; + + return; + } - $stack0 = ; + public void testFieldRefVariable_fail() + { + FieldRefValidator $l0; + unknown $stack1, $stack2; - $stack1 = ; + $l0 := @this: FieldRefValidator; + $stack1 = ; + $stack2 = $l0.; return; } -} +} \ No newline at end of file From bd33187679c661419d8df3cd712780cea3ef3e15 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Tue, 2 Jan 2024 16:26:10 +0100 Subject: [PATCH 5/9] FieldRefValidator change --- .../java/sootup/tests/validator/FieldRefValidatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index 986c6d05930..a29bb03229a 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -30,7 +30,7 @@ public void Setup() { fieldRefValidator = new FieldRefValidator(); - ClassType classTypeNewValidator = + ClassType classTypeFieldRefValidator = new ClassType() { @Override public boolean isBuiltInClass() { @@ -58,7 +58,7 @@ public PackageName getPackageName() { new JimpleAnalysisInputLocation(Paths.get(classPath), SourceType.Application); jimpleView = new JimpleView(jimpleInputLocation); - final Optional> classSource1 = jimpleView.getClass(classTypeNewValidator); + final Optional> classSource1 = jimpleView.getClass(classTypeFieldRefValidator); assertFalse(classSource1.isPresent()); classes = new HashSet<>(); // Set to track the classes to check From 4cb37853cc679be133120e5cead8c2d0f7121761 Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Wed, 3 Jan 2024 14:30:25 +0100 Subject: [PATCH 6/9] add test category --- .../java/sootup/tests/validator/FieldRefValidatorTest.java | 4 ++++ .../test/java/sootup/tests/validator/NewValidatorTest.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index a29bb03229a..e1234707446 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -8,8 +8,11 @@ import java.util.HashSet; import java.util.List; import java.util.Optional; + +import categories.Java8Test; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.categories.Category; import sootup.core.model.SootClass; import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; @@ -19,6 +22,7 @@ import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleView; +@Category(Java8Test.class) public class FieldRefValidatorTest { FieldRefValidator fieldRefValidator; 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 ebd12db02f3..db729b04cdf 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -8,8 +8,11 @@ import java.util.HashSet; import java.util.List; import java.util.Optional; + +import categories.Java8Test; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.categories.Category; import sootup.core.model.SootClass; import sootup.core.model.SourceType; import sootup.core.signatures.PackageName; @@ -19,6 +22,7 @@ import sootup.jimple.parser.JimpleAnalysisInputLocation; import sootup.jimple.parser.JimpleView; +@Category(Java8Test.class) public class NewValidatorTest { NewValidator validator; From 109b8ce1357a32d74f324928f3874f6e92c7b6c3 Mon Sep 17 00:00:00 2001 From: Kadiray Karakaya Date: Wed, 3 Jan 2024 14:54:06 +0100 Subject: [PATCH 7/9] fmt --- .../java/sootup/tests/validator/FieldRefValidatorTest.java | 3 +-- .../src/test/java/sootup/tests/validator/NewValidatorTest.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index e1234707446..e88f5a136d2 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -3,13 +3,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import categories.Java8Test; import java.nio.file.Paths; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Optional; - -import categories.Java8Test; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; 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 db729b04cdf..d414ac2e789 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/NewValidatorTest.java @@ -3,13 +3,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import categories.Java8Test; import java.nio.file.Paths; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Optional; - -import categories.Java8Test; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; From b47a0205963b960f549ca32677496fc8a4ad8c77 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 4 Jan 2024 13:37:27 +0100 Subject: [PATCH 8/9] FieldRefValidator test case name change --- .../main/java/sootup/core/validation/FieldRefValidator.java | 1 + .../java/sootup/tests/validator/FieldRefValidatorTest.java | 4 ++-- .../resources/validator/jimple/FieldRefValidator.jimple | 6 +++--- 3 files changed, 6 insertions(+), 5 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 9211190cc6c..c92a69d7db7 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -44,6 +44,7 @@ public List validate(Body body, View view) { SootMethod sootMethod = view.getMethod(body.getMethodSignature()).get(); if (sootMethod.isAbstract()) { + validationException.add(new ValidationException(sootMethod, "The method is abstract")); return validationException; } diff --git a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java index e88f5a136d2..289e04003f6 100644 --- a/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java +++ b/sootup.tests/src/test/java/sootup/tests/validator/FieldRefValidatorTest.java @@ -85,7 +85,7 @@ public void testFieldRefValidatorSuccess() { .get() .getMethods() .stream() - .filter(m -> m.getName().equals("testFieldRefVariable_pass")) + .filter(m -> m.getName().equals("testFieldRefValidator_pass")) .map(m -> m.getBody()) .findFirst() .get(), @@ -106,7 +106,7 @@ public void testFieldRefValidatorFailure() { .get() .getMethods() .stream() - .filter(m -> m.getName().equals("testFieldRefVariable_fail")) + .filter(m -> m.getName().equals("testFieldRefValidator_fail")) .map(m -> m.getBody()) .findFirst() .get(), diff --git a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple index 1ed3945ac3c..be5d8c2d15b 100644 --- a/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple +++ b/sootup.tests/src/test/resources/validator/jimple/FieldRefValidator.jimple @@ -23,7 +23,7 @@ public super class FieldRefValidator extends java.lang.Object return; } - public void testFieldRefVariable_pass() + public void testFieldRefValidator_pass() { FieldRefValidator $l0; unknown $stack1, $stack2; @@ -35,7 +35,7 @@ public super class FieldRefValidator extends java.lang.Object return; } - public void testFieldRefVariable_fail() + public void testFieldRefValidator_fail() { FieldRefValidator $l0; unknown $stack1, $stack2; @@ -47,4 +47,4 @@ public super class FieldRefValidator extends java.lang.Object return; } -} \ No newline at end of file +} From 4e952564efec415114bc621bfb94f0260d99c4c2 Mon Sep 17 00:00:00 2001 From: akshitad11 Date: Thu, 4 Jan 2024 15:56:15 +0100 Subject: [PATCH 9/9] FieldRefValidator - remove try-catch, abstract code --- .../core/validation/FieldRefValidator.java | 58 ++++++------------- 1 file changed, 19 insertions(+), 39 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 c92a69d7db7..fee35aa25d8 100644 --- a/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/FieldRefValidator.java @@ -31,7 +31,6 @@ import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; import sootup.core.model.SootField; -import sootup.core.model.SootMethod; import sootup.core.views.View; public class FieldRefValidator implements BodyValidator { @@ -42,12 +41,6 @@ public List validate(Body body, View view) { List validationException = new ArrayList<>(); - SootMethod sootMethod = view.getMethod(body.getMethodSignature()).get(); - if (sootMethod.isAbstract()) { - validationException.add(new ValidationException(sootMethod, "The method is abstract")); - return validationException; - } - List stmts = body.getStmts(); for (Stmt stmt : stmts) { @@ -58,45 +51,32 @@ public List validate(Body body, View view) { if (fr instanceof JStaticFieldRef) { JStaticFieldRef v = (JStaticFieldRef) fr; - try { - Optional fieldOpt = view.getField(v.getFieldSignature()); - if (!fieldOpt.isPresent()) { - validationException.add(new ValidationException(v, "Resolved field is empty: ")); - } else { - SootField field = fieldOpt.get(); - if (!field.isStatic()) { - validationException.add( - new ValidationException( - fr, "Trying to get a static field which is non-static: " + v)); - } + + Optional fieldOpt = view.getField(v.getFieldSignature()); + if (!fieldOpt.isPresent()) { + validationException.add(new ValidationException(v, "Resolved field is empty: ")); + } else { + SootField field = fieldOpt.get(); + if (!field.isStatic()) { + validationException.add( + new ValidationException( + fr, "Trying to get a static field which is non-static: " + v)); } - } catch (Exception e) { - validationException.add( - new ValidationException( - fr, - "Trying to get a static field which is non-static: " + v + " " + e.getMessage())); } } else if (fr instanceof JInstanceFieldRef) { JInstanceFieldRef v = (JInstanceFieldRef) fr; - try { - Optional fieldOpt = view.getField(v.getFieldSignature()); - if (!fieldOpt.isPresent()) { + Optional fieldOpt = view.getField(v.getFieldSignature()); + if (!fieldOpt.isPresent()) { + validationException.add( + new ValidationException(fr, "Resolved field is null: " + fr.toString())); + } else { + SootField field = fieldOpt.get(); + if (field.isStatic()) { validationException.add( - new ValidationException(fr, "Resolved field is null: " + fr.toString())); - } else { - SootField field = fieldOpt.get(); - if (field.isStatic()) { - validationException.add( - new ValidationException( - fr, "Trying to get an instance field which is static: " + v)); - } + new ValidationException( + fr, "Trying to get an instance field which is static: " + v)); } - } catch (Exception e) { - validationException.add( - new ValidationException( - fr, - "Trying to get an instance field which is static: " + v + " " + e.getMessage())); } } else { throw new RuntimeException("unknown field ref");