-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transformer architecture to deal with new features from Soot 4.4.1
- Loading branch information
Showing
9 changed files
with
215 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
CryptoAnalysis/src/main/java/crypto/preanalysis/CastTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package crypto.preanalysis; | ||
|
||
import soot.Body; | ||
import soot.UnitPatchingChain; | ||
import soot.Value; | ||
import soot.jimple.AssignStmt; | ||
import soot.jimple.CastExpr; | ||
import soot.jimple.Constant; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This transformer removes explicit cast expressions from the jimple code. | ||
* Since Soot 4.3.0, it transforms the expression 'int a = 65000' into the jimple | ||
* statement '$v = (int) 65000'. However, Boomerang is not able to extract the | ||
* value 65000 from the statement because of the explicit cast operation (int). | ||
* For the analysis, this operation is irrelevant because the variables always | ||
* have the correct type (i.e. $v is of type int), so we can remove the cast | ||
* operation. | ||
*/ | ||
public class CastTransformer extends PreTransformer { | ||
|
||
private static CastTransformer instance; | ||
|
||
public CastTransformer() { | ||
super(); | ||
} | ||
|
||
@Override | ||
protected void internalTransform(Body body, String phaseName, Map<String, String> options) { | ||
if (!body.getMethod().getDeclaringClass().isApplicationClass()) { | ||
return; | ||
} | ||
|
||
final UnitPatchingChain units = body.getUnits(); | ||
units.snapshotIterator().forEachRemaining(unit -> { | ||
if (!(unit instanceof AssignStmt)) { | ||
return; | ||
} | ||
AssignStmt assignStmt = (AssignStmt) unit; | ||
|
||
Value rightSide = assignStmt.getRightOp(); | ||
if (!(rightSide instanceof CastExpr)) { | ||
return; | ||
} | ||
|
||
CastExpr castExpr = (CastExpr) rightSide; | ||
if (!(castExpr.getOp() instanceof Constant)) { | ||
return; | ||
} | ||
Constant constant = (Constant) castExpr.getOp(); | ||
|
||
assignStmt.setRightOp(constant); | ||
}); | ||
} | ||
|
||
public static CastTransformer v() { | ||
if (instance == null) { | ||
instance = new CastTransformer(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void reset() { | ||
instance = null; | ||
} | ||
|
||
} |
25 changes: 5 additions & 20 deletions
25
CryptoAnalysis/src/main/java/crypto/preanalysis/ExceptionAwareTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
CryptoAnalysis/src/main/java/crypto/preanalysis/PreTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package crypto.preanalysis; | ||
|
||
import soot.BodyTransformer; | ||
import soot.MethodOrMethodContext; | ||
import soot.Scene; | ||
import soot.SootMethod; | ||
import soot.jimple.toolkits.callgraph.ReachableMethods; | ||
import soot.util.queue.QueueReader; | ||
|
||
import java.util.HashMap; | ||
|
||
public abstract class PreTransformer extends BodyTransformer { | ||
|
||
private boolean applied; | ||
|
||
public PreTransformer() { | ||
applied = false; | ||
} | ||
|
||
public final void apply() { | ||
if (isApplied()) { | ||
return; | ||
} | ||
|
||
/* Following the concept of Soot, each transformer is applied to each method | ||
* body individually. It may be more efficient to apply all transformers at once | ||
* to a method body. | ||
*/ | ||
ReachableMethods reachableMethods = Scene.v().getReachableMethods(); | ||
QueueReader<MethodOrMethodContext> listener = reachableMethods.listener(); | ||
while (listener.hasNext()) { | ||
SootMethod method = listener.next().method(); | ||
if (method.hasActiveBody()) { | ||
internalTransform(method.getActiveBody(), "preTrans", new HashMap<>()); | ||
} | ||
} | ||
applied = true; | ||
} | ||
|
||
public boolean isApplied() { | ||
return applied; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
CryptoAnalysis/src/main/java/crypto/preanalysis/TransformerSetup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package crypto.preanalysis; | ||
|
||
import boomerang.preanalysis.BoomerangPretransformer; | ||
import crypto.rules.CrySLRule; | ||
|
||
import java.util.List; | ||
|
||
public class TransformerSetup { | ||
|
||
private static TransformerSetup instance; | ||
|
||
public void setupPreTransformer(List<CrySLRule> rules) { | ||
// Transformer related to the analysis | ||
setupCastTransformer(); | ||
setupExceptionAwareTransformer(rules); | ||
|
||
// Transformer related to Boomerang | ||
setupBoomerangTransformer(); | ||
} | ||
|
||
public void setupCastTransformer() { | ||
CastTransformer.v().reset(); | ||
CastTransformer.v().apply(); | ||
} | ||
|
||
public void setupExceptionAwareTransformer(List<CrySLRule> rules) { | ||
for (CrySLRule rule : rules) { | ||
ExceptionAwareTransformer transformer = new ExceptionAwareTransformer(rule); | ||
transformer.apply(); | ||
} | ||
} | ||
|
||
public void setupBoomerangTransformer() { | ||
BoomerangPretransformer.v().reset(); | ||
BoomerangPretransformer.v().apply(); | ||
} | ||
|
||
public static TransformerSetup v() { | ||
if (instance == null) { | ||
instance = new TransformerSetup(); | ||
} | ||
return instance; | ||
} | ||
|
||
public void reset() { | ||
instance = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.