Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve validator #884

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sootup.core/src/main/java/sootup/core/model/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
JIdentityStmt idStmt = (JIdentityStmt) u;
if (idStmt.getRightOp() instanceof JParameterRef) {
JParameterRef pr = (JParameterRef) idStmt.getRightOp();
retVal.add(pr.getIndex(), (Local) idStmt.getLeftOp());
retVal.add(pr.getIndex(), idStmt.getLeftOp());

Check warning on line 186 in sootup.core/src/main/java/sootup/core/model/Body.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/model/Body.java#L186

Added line #L186 was not covered by tests
}
}
/* if we restrict/define that IdentityStmts MUST be at the beginnging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,4 @@ public interface BodyValidator {
* @param view the view
*/
List<ValidationException> validate(Body body, View view);

/**
* Basic validators run essential checks and are run always if validate is called.<br>
* If this method returns false and the caller of the validator respects this property,<br>
* the checks will only be run if the debug or validation option is activated.
*
* @return whether this validator is a basic validator
*/
boolean isBasicValidator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,4 @@ public List<ValidationException> validate(Body body, View view) {
}
return validationException;
}

@Override
public boolean isBasicValidator() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,4 @@ public List<ValidationException> validate(Body body, View view) {
}
return validationException;
}

@Override
public boolean isBasicValidator() {
return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @author Marc Miltenberger
*/
public class IdentityValidator implements BodyValidator {
public class IdentityStmtsValidator implements BodyValidator {

/**
* Checks whether each ParameterRef and ThisRef is used exactly once.
Expand All @@ -50,74 +50,74 @@
List<ValidationException> exceptions = new ArrayList<>();

boolean hasThisLocal = false;
Optional<? extends SootMethod> optionalSootMethod = view.getMethod(body.getMethodSignature());
if (!optionalSootMethod.isPresent()) {
exceptions.add(
new ValidationException(
body.getMethodSignature(),
"There is no corresponding SootMethod in the given view for the provided method signature."));
return exceptions;
Optional<? extends SootMethod> sootMethodOpt = view.getMethod(body.getMethodSignature());
if (!sootMethodOpt.isPresent()) {
throw new IllegalStateException(

Check warning on line 55 in sootup.core/src/main/java/sootup/core/validation/IdentityStmtsValidator.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/validation/IdentityStmtsValidator.java#L55

Added line #L55 was not covered by tests
"We should find the given method to the given Body in the View. wrong View or Method?");
}

SootMethod method = optionalSootMethod.get();
SootMethod method = sootMethodOpt.get();
int paramCount = method.getParameterCount();
boolean[] parameterRefs = new boolean[paramCount];

for (Stmt stmt : body.getStmts()) {
// TODO: enforce stmts[thisIdentityStmt?, parameterRefIdentityStmt*, ..., returnStmt], too -> or
// better create a preamble in the graph so that its not possible to insert it differently

for (Stmt stmt : body.getStmtGraph().getNodes()) {
if (stmt instanceof JIdentityStmt) {
JIdentityStmt id = (JIdentityStmt) stmt;
if (id.getRightOp() instanceof JThisRef) {
JIdentityStmt identityStmt = (JIdentityStmt) stmt;
if (identityStmt.getRightOp() instanceof JThisRef) {
if (hasThisLocal) {
exceptions.add(new ValidationException(identityStmt, "@this occures more than once."));

Check warning on line 71 in sootup.core/src/main/java/sootup/core/validation/IdentityStmtsValidator.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/validation/IdentityStmtsValidator.java#L71

Added line #L71 was not covered by tests
}
hasThisLocal = true;
}

if (id.getRightOp() instanceof JParameterRef) {
JParameterRef ref = (JParameterRef) id.getRightOp();
} else if (identityStmt.getRightOp() instanceof JParameterRef) {
JParameterRef ref = (JParameterRef) identityStmt.getRightOp();
if (ref.getIndex() < 0 || ref.getIndex() >= paramCount) {
if (paramCount == 0)
if (paramCount == 0) {
exceptions.add(
new ValidationException(
id,
identityStmt,
"This methodRef has no parameters, so no parameter reference is allowed"));
else
} else {
exceptions.add(
new ValidationException(
id,
identityStmt,
String.format(
"Parameter reference index must be between 0 and %d (inclusive)",
paramCount - 1)));
}
} else {
if (parameterRefs[ref.getIndex()])
if (parameterRefs[ref.getIndex()]) {
exceptions.add(
new ValidationException(
id,
identityStmt,
String.format("Only one local for parameter %d is allowed", ref.getIndex())));
}
parameterRefs[ref.getIndex()] = true;
}
}
}
}

if (!method.isStatic() && !hasThisLocal) {
if (method.isStatic() == hasThisLocal) {
exceptions.add(
new ValidationException(
body,
String.format(
"The methodRef %s is not static, but does not have a this local",
body.getMethodSignature())));
"The method %s is %s static, but does %s have a this local",
body.getMethodSignature(),
(method.isStatic() ? "" : "not"),
(hasThisLocal ? "" : "not"))));
}

for (int i = 0; i < paramCount; i++) {
if (!parameterRefs[i]) {
exceptions.add(
new ValidationException(
body, String.format("There is no parameter local for parameter number %d", i)));
body, String.format("There is no Local assigned for parameter number %d", i)));
}
}
return exceptions;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,4 @@ public List<ValidationException> validate(@Nonnull Body body, @Nonnull View view

return exception;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import sootup.core.views.View;

public class MethodValidator implements BodyValidator {

public static final String staticInitializerName = "<clinit>";

/**
Expand All @@ -51,21 +50,18 @@
MethodSignature methodSignature = body.getMethodSignature();
Optional<? extends SootMethod> optionalSootMethod = view.getMethod(methodSignature);
if (!optionalSootMethod.isPresent()) {
exceptions.add(
new ValidationException(
body.getMethodSignature(),
"There is no corresponding SootMethod in the given view for the provided method signature."));
return exceptions;
throw new IllegalStateException("The Method of this Body should be found in the View.");

Check warning on line 53 in sootup.core/src/main/java/sootup/core/validation/MethodValidator.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/validation/MethodValidator.java#L53

Added line #L53 was not covered by tests
}

SootMethod methodRef = optionalSootMethod.get();
if (methodRef.isAbstract()) { // but an abstract method does not have body
return exceptions;
SootMethod method = optionalSootMethod.get();

Check warning on line 56 in sootup.core/src/main/java/sootup/core/validation/MethodValidator.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/validation/MethodValidator.java#L56

Added line #L56 was not covered by tests
if (method.isAbstract()) {
throw new IllegalStateException("An abstract Method does not have Body.");

Check warning on line 58 in sootup.core/src/main/java/sootup/core/validation/MethodValidator.java

View check run for this annotation

Codecov / codecov/patch

sootup.core/src/main/java/sootup/core/validation/MethodValidator.java#L58

Added line #L58 was not covered by tests
}
if (staticInitializerName.equals(methodRef.getName()) && !methodRef.isStatic()) {

if (staticInitializerName.equals(method.getName()) && !method.isStatic()) {
exceptions.add(
new ValidationException(
methodRef,
method,
staticInitializerName
+ " should be static! Static initializer without 'static'('0x8') modifier"
+ " will cause problem when running on android platform: "
Expand All @@ -74,9 +70,4 @@

return exceptions;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,4 @@ private boolean checkForInitializerOnPath(
}
return true;
}

@Override
public boolean isBasicValidator() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ public class StmtsValidator implements BodyValidator {
public List<ValidationException> validate(Body body, View view) {
return null;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,4 @@ public List<ValidationException> validate(Body body, View view) {
*/
return null;
}

@Override
public boolean isBasicValidator() {
return false;
}
}
Loading
Loading