Skip to content

Commit

Permalink
Adapt ExceptionAwareTransformer to deal with Nullpointer
Browse files Browse the repository at this point in the history
  • Loading branch information
smeyer198 committed Jan 3, 2024
1 parent 6048654 commit a846501
Showing 1 changed file with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package crypto.preanalysis;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -36,29 +37,17 @@
public class ExceptionAwareTransformer extends BodyTransformer {

public static void setup(final List<CrySLRule> rules) {
for (final CrySLRule rule : rules) {
final String phaseName = "jap.etr-" + rule.getClassName();
PackManager.v().getPack("jap").remove(phaseName);
PackManager.v().getPack("jap").add(new Transform(phaseName, new ExceptionAwareTransformer(rule)));
PhaseOptions.v().setPhaseOption(phaseName, "on");
}
final String phaseName = "jap.etr";
PackManager.v().getPack("jap").remove(phaseName);
PackManager.v().getPack("jap").add(new Transform(phaseName, new ExceptionAwareTransformer(rules)));
PhaseOptions.v().setPhaseOption(phaseName, "on");
}

private final SootClass spec;

private final Multimap<SootMethod, SootClass> exceptions;

private final Collection<CrySLRule> rules;
private final Map<SootMethod, SootMethod> lookupCache = new HashMap<>();

public ExceptionAwareTransformer(final CrySLRule rule) {
this.exceptions = HashMultimap.create();
this.spec = Scene.v().getSootClass(rule.getClassName());
rule.getConstraints().stream()
.filter(constraint -> constraint instanceof CrySLExceptionConstraint)
.map(constraint -> (CrySLExceptionConstraint) constraint)
.forEach(constraint -> CrySLMethodToSootMethod.v().convert(constraint.getMethod()).stream()
.forEach(
method -> exceptions.put(method, Scene.v().getSootClass(constraint.getException().getException()))));
public ExceptionAwareTransformer(final Collection<CrySLRule> rules) {
this.rules = rules;
}

protected void internalTransform(final Body body, final String phase, final Map<String, String> options) {
Expand All @@ -75,24 +64,51 @@ protected void internalTransform(final Body body, final String phase, final Map<
return;

final SootMethod called = ((Stmt) unit).getInvokeExpr().getMethod();
SootClass declaringClass = called.getDeclaringClass();
Optional<CrySLRule> rule = getRuleForClass(declaringClass);

if (!called.getDeclaringClass().equals(this.spec))
if (!rule.isPresent()) {
return;
}

lookup(called).ifPresent(declared -> {
Multimap<SootMethod, SootClass> exceptions = getExceptionsForRule(rule.get());

lookup(called, exceptions).ifPresent(declared -> {
for (final SootClass exception : exceptions.get(declared))
ExceptionConstraint.getTrap(body, unit, exception)
.ifPresent(trap -> addBranch(units, unit, trap.getHandlerUnit()));
});
});
}

private Optional<CrySLRule> getRuleForClass(SootClass sootClass) {
for (CrySLRule rule : rules) {
if (rule.getClassName().equals(sootClass.getName())) {
return Optional.of(rule);
}
}
return Optional.empty();
}

private Multimap<SootMethod, SootClass> getExceptionsForRule(CrySLRule rule) {
Multimap<SootMethod, SootClass> exceptions = HashMultimap.create();

rule.getConstraints().stream()
.filter(constraint -> constraint instanceof CrySLExceptionConstraint)
.map(constraint -> (CrySLExceptionConstraint) constraint)
.forEach(constraint -> CrySLMethodToSootMethod.v().convert(constraint.getMethod()).stream()
.forEach(
method -> exceptions.put(method, Scene.v().getSootClass(constraint.getException().getException()))));

return exceptions;
}

private void addBranch(final UnitPatchingChain units, final Unit after, final Unit to) {
final JEqExpr condition = new JEqExpr(NullConstant.v(), NullConstant.v());
units.insertOnEdge(new JIfStmt(condition, to), after, null);
}

private Optional<SootMethod> lookup(final SootMethod called) {
private Optional<SootMethod> lookup(final SootMethod called, Multimap<SootMethod, SootClass> exceptions) {
if (lookupCache.containsKey(called))
return Optional.of(lookupCache.get(called));
for (final SootMethod declared : exceptions.keySet()) {
Expand Down

0 comments on commit a846501

Please sign in to comment.