Skip to content

Commit

Permalink
Merge pull request #733 from soot-oss/validator_changes
Browse files Browse the repository at this point in the history
NewValidator Changes
  • Loading branch information
kadirayk authored Jan 2, 2024
2 parents 23e5a3e + 3dd6d09 commit bc6f0bc
Show file tree
Hide file tree
Showing 27 changed files with 513 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -57,7 +58,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) {
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;
}

Expand Down
25 changes: 8 additions & 17 deletions sootup.core/src/main/java/sootup/core/model/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,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.
Expand All @@ -89,7 +88,7 @@ private Body(
this.graph = /* FIXME: [ms] make immutable when availabe */
new MutableBlockStmtGraph(stmtGraph).unmodifiableStmtGraph();
this.position = position;
checkInit();
// checkInit();
}

/**
Expand Down Expand Up @@ -122,14 +121,6 @@ public int getLocalCount() {
return locals.size();
}

private void runValidation(BodyValidator validator) {
final List<ValidationException> 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());}
Expand All @@ -141,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<Local> getLocals() {
Expand Down Expand Up @@ -279,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@

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 {
/**
* 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<ValidationException> exceptions);
List<ValidationException> validate(Body body, View<?> view);

/**
* Basic validators run essential checks and are run always if validate is called.<br>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationException> exception) {
public List<ValidationException> validate(Body body, View<?> view) {

// TODO: #535 implement validator
// check code copied from old soot
Expand All @@ -41,6 +42,7 @@ public void validate(Body body, List<ValidationException> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationException> exception) {
public List<ValidationException> 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();
Expand Down Expand Up @@ -88,6 +89,7 @@ public void validate(Body body, List<ValidationException> exception) {
* body.getMethod())); } } } return; } exception.add(new ValidationException(stmt, "Warning: Bad types" + errorSuffix +
* " in " + body.getMethod()));
*/
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationException> exception) {
public List<ValidationException> 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
Expand Down
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 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
Expand All @@ -22,14 +22,19 @@
* #L%
*/

import java.util.ArrayList;
import java.util.List;
import sootup.core.model.Body;
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<ValidationException> exceptions) {
public List<ValidationException> validate(Body body, View<?> view) {

List<ValidationException> validationException = new ArrayList<>();

// TODO: check copied code from old soot
/*
* SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; }
Expand All @@ -54,6 +59,8 @@ public void validate(Body body, List<ValidationException> exceptions) {
* "Trying to get an instance field which is static: " + v)); } } else { throw new RuntimeException("unknown field ref");
* } }
*/

return validationException;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.List;
import sootup.core.model.Body;
import sootup.core.views.View;

public class IdentityStatementsValidator implements BodyValidator {

Expand All @@ -36,9 +37,11 @@ public class IdentityStatementsValidator implements BodyValidator {
* <li>param-references must precede all statements that are not themselves param-references or
* this-references, if they occur at all
* </ol>
*
* @return
*/
@Override
public void validate(Body body, List<ValidationException> exceptions) {
public List<ValidationException> validate(Body body, View<?> view) {
// TODO: check copied code from old soot
/*
* SootMethod methodRef = body.getMethod(); if (methodRef.isAbstract()) { return; }
Expand All @@ -57,6 +60,7 @@ public void validate(Body body, List<ValidationException> exceptions) {
* // @caughtexception statement foundNonThisOrParamIdentityStatement = true; } } else { // non-identity statement
* foundNonThisOrParamIdentityStatement = true; } firstStatement = false; }
*/
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<ValidationException> exceptions) {
public List<ValidationException> validate(Body body, View<?> view) {
// TODO: check copied code from old soot
/*
* boolean hasThisLocal = false; int paramCount = body.getMethod().getParameterCount(); boolean[] parameterRefs = new
Expand All @@ -57,6 +62,7 @@ public void validate(Body body, List<ValidationException> exceptions) {
* String.format("There is no parameter local for parameter number %d", i))); } }
*
*/
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,13 +35,14 @@
public class InvokeArgumentValidator implements BodyValidator {

@Override
public void validate(Body body, List<ValidationException> exceptions) {
public List<ValidationException> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationException> exceptions) {
public List<ValidationException> validate(Body body, View<?> view) {
// TODO: check copied code from old soot
/*
* Set<Unit> caughtUnits = new HashSet<Unit>(); for (Trap trap : body.getTraps()) {
Expand All @@ -52,6 +57,7 @@ public void validate(Body body, List<ValidationException> exceptions) {
* body.getMethod().getSignature() + " contains a caught exception reference," +
* "but not a corresponding trap using this statement as handler")); } } } }
*/
return null;
}

@Override
Expand Down
Loading

0 comments on commit bc6f0bc

Please sign in to comment.