Skip to content

Commit

Permalink
CheckInitValidator changes and test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
akshitad11 committed Jan 17, 2024
1 parent 570fd8b commit c12bfec
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997-2020 Raja Vallée-Rai, Linghui Luo, Markus Schmidt
* Copyright (C) 1997-2020 Raja Vallée-Rai, Linghui Luo, Markus Schmidt, Akshita Dubey
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -22,7 +22,12 @@
* #L%
*/

import java.util.ArrayList;
import java.util.List;
import sootup.core.graph.StmtGraph;
import sootup.core.jimple.basic.Local;
import sootup.core.jimple.basic.Value;
import sootup.core.jimple.common.stmt.Stmt;
import sootup.core.model.Body;
import sootup.core.views.View;

Expand All @@ -31,18 +36,36 @@ public class CheckInitValidator implements BodyValidator {
@Override
public List<ValidationException> validate(Body body, View view) {

// TODO: #535 implement validator
// check code copied from old soot
/*
* ExceptionalUnitGraph g = new ExceptionalUnitGraph(body, ThrowAnalysisFactory.checkInitThrowAnalysis(), false);
*
* InitAnalysis analysis = new InitAnalysis(g); for (Unit s : body.getUnits()) { FlowSet<Local> init =
* analysis.getFlowBefore(s); for (ValueBox vBox : s.getUseBoxes()) { Value v = vBox.getValue(); if (v instanceof Local)
* { Local l = (Local) v; if (!init.contains(l)) { throw new ValidationException(s,
* "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;
List<ValidationException> validationException = new ArrayList<>();
StmtGraph<?> g = body.getStmtGraph();
List<String> predecessors = new ArrayList<>();
for (Stmt s : body.getStmts()) {
predecessors.add(s.toString());
for (Value v : s.getUses()) {
if (v instanceof Local) {
Local l = (Local) v;
if (!predecessors.contains(getStmtDefinition(l, body))) {
validationException.add(
new ValidationException(
l,
"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.getMethodSignature()));
}
}
}
}
return validationException;
}

private String getStmtDefinition(Local l, Body body) {
String def = l.getDefs(body.getStmts()).toString();
return def.substring(1, def.length() - 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package sootup.tests.validator;

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 org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.model.SourceType;
import sootup.core.signatures.PackageName;
import sootup.core.types.ClassType;
import sootup.core.validation.CheckInitValidator;
import sootup.core.validation.ValidationException;
import sootup.jimple.parser.JimpleAnalysisInputLocation;
import sootup.jimple.parser.JimpleView;

@Category(Java8Test.class)
public class CheckInitValidatorTest {
CheckInitValidator checkInitValidator;
JimpleView jimpleView;

Collection<SootClass> classes;

@Before
public void Setup() {

checkInitValidator = new CheckInitValidator();

ClassType classTypeCheckInitValidator =
new ClassType() {
@Override
public boolean isBuiltInClass() {
return false;
}

@Override
public String getFullyQualifiedName() {
return "jimple.CheckInitValidator";
}

@Override
public String getClassName() {
return "CheckInitValidator";
}

@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<SootClass> classSource1 = jimpleView.getClass(classTypeCheckInitValidator);
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 testCheckInitValidatorSuccess() {
List<ValidationException> validationExceptions_success;

validationExceptions_success =
checkInitValidator.validate(
classes.stream()
.filter(c -> c.getType().getClassName().equals("CheckInitValidator"))
.findFirst()
.get()
.getMethods()
.stream()
.filter(m -> m.getName().equals("checkInitValidator_success"))
.map(SootMethod::getBody)
.findFirst()
.get(),
jimpleView);

assertEquals(0, validationExceptions_success.size());
}

@Test
public void testCheckInitValidatorFail() {
List<ValidationException> validationExceptions_fail;

validationExceptions_fail =
checkInitValidator.validate(
classes.stream()
.filter(c -> c.getType().getClassName().equals("CheckInitValidator"))
.findFirst()
.get()
.getMethods()
.stream()
.filter(m -> m.getName().equals("checkInitValidator_fail"))
.map(SootMethod::getBody)
.findFirst()
.get(),
jimpleView);

assertEquals(1, validationExceptions_fail.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class CheckInitValidator extends java.lang.Object
{
public void <init>()
{
CheckInitValidator $l0;


$l0 := @this: CheckInitValidator;
specialinvoke $l0.<java.lang.Object: void <init>()>();

return;
}

public void checkInitValidator_success()
{
CheckInitValidator $l0;
unknown $l1, $l2, $l3;


$l0 := @this: CheckInitValidator;
$l1 = 2;
$l2 = 3;
$l3 = $l1 + $l2;

return;
}

public void checkInitValidator_fail()
{
CheckInitValidator $l0;
unknown $l1, $l2, $l3;

$l0 := @this: CheckInitValidator;
$l2 = 3;
$l3 = $l1 + $l2;

return;
}
}

0 comments on commit c12bfec

Please sign in to comment.