Skip to content

Commit 3c809e6

Browse files
committed
Simplify rule manager/make it more extensible
Adding methods to swap out rules and make it possible to reason about class implementations instead of using actual instances of classes.
1 parent d2c934f commit 3c809e6

28 files changed

+267
-318
lines changed

soot-infoflow/src/soot/jimple/infoflow/collections/problems/rules/forward/ArrayWithIndexPropagationRule.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,18 @@
1313
import soot.jimple.LengthExpr;
1414
import soot.jimple.NewArrayExpr;
1515
import soot.jimple.Stmt;
16-
import soot.jimple.infoflow.InfoflowManager;
1716
import soot.jimple.infoflow.aliasing.Aliasing;
1817
import soot.jimple.infoflow.collections.ICollectionsSupport;
1918
import soot.jimple.infoflow.collections.strategies.containers.IContainerStrategy;
2019
import soot.jimple.infoflow.collections.util.Tristate;
2120
import soot.jimple.infoflow.data.Abstraction;
2221
import soot.jimple.infoflow.data.AccessPath;
2322
import soot.jimple.infoflow.data.ContainerContext;
24-
import soot.jimple.infoflow.problems.TaintPropagationResults;
2523
import soot.jimple.infoflow.problems.rules.IArrayContextProvider;
2624
import soot.jimple.infoflow.problems.rules.forward.ArrayPropagationRule;
2725
import soot.jimple.infoflow.util.ByReferenceBoolean;
2826

2927
public class ArrayWithIndexPropagationRule extends ArrayPropagationRule implements IArrayContextProvider {
30-
public ArrayWithIndexPropagationRule(InfoflowManager manager, Abstraction zeroValue,
31-
TaintPropagationResults results) {
32-
super(manager, zeroValue, results);
33-
}
3428

3529
@Override
3630
public Collection<Abstraction> propagateNormalFlow(Abstraction d1, Abstraction source, Stmt stmt, Stmt destStmt,

soot-infoflow/src/soot/jimple/infoflow/collections/problems/rules/forward/CollectionWrapperPropagationRule.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import soot.jimple.InstanceInvokeExpr;
1010
import soot.jimple.Stmt;
1111
import soot.jimple.infoflow.InfoflowConfiguration.StaticFieldTrackingMode;
12-
import soot.jimple.infoflow.InfoflowManager;
1312
import soot.jimple.infoflow.aliasing.Aliasing;
1413
import soot.jimple.infoflow.cfg.FlowDroidSourceStatement;
1514
import soot.jimple.infoflow.collections.ICollectionsSupport;
1615
import soot.jimple.infoflow.data.Abstraction;
1716
import soot.jimple.infoflow.data.AccessPath;
18-
import soot.jimple.infoflow.problems.TaintPropagationResults;
1917
import soot.jimple.infoflow.problems.rules.forward.WrapperPropagationRule;
2018
import soot.jimple.infoflow.typing.TypeUtils;
2119
import soot.jimple.infoflow.util.ByReferenceBoolean;
@@ -27,11 +25,6 @@
2725
*/
2826
public class CollectionWrapperPropagationRule extends WrapperPropagationRule {
2927

30-
public CollectionWrapperPropagationRule(InfoflowManager manager, Abstraction zeroValue,
31-
TaintPropagationResults results) {
32-
super(manager, zeroValue, results);
33-
}
34-
3528
/**
3629
* Computes the taints produced by a taint wrapper object
3730
*

soot-infoflow/src/soot/jimple/infoflow/problems/rules/AbstractTaintPropagationRule.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
*/
1414
public abstract class AbstractTaintPropagationRule implements ITaintPropagationRule {
1515

16-
protected final InfoflowManager manager;
17-
protected final Abstraction zeroValue;
18-
protected final TaintPropagationResults results;
16+
protected InfoflowManager manager;
17+
protected Abstraction zeroValue;
18+
protected TaintPropagationResults results;
1919

20-
public AbstractTaintPropagationRule(InfoflowManager manager, Abstraction zeroValue,
21-
TaintPropagationResults results) {
20+
@Override
21+
public void init(InfoflowManager manager, Abstraction zeroValue, TaintPropagationResults results) {
2222
this.manager = manager;
2323
this.zeroValue = zeroValue;
2424
this.results = results;

soot-infoflow/src/soot/jimple/infoflow/problems/rules/BackwardPropagationRuleManagerFactory.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
import soot.jimple.infoflow.InfoflowManager;
77
import soot.jimple.infoflow.data.Abstraction;
88
import soot.jimple.infoflow.problems.TaintPropagationResults;
9-
import soot.jimple.infoflow.problems.rules.backward.*;
9+
import soot.jimple.infoflow.problems.rules.backward.BackwardsArrayPropagationRule;
10+
import soot.jimple.infoflow.problems.rules.backward.BackwardsClinitRule;
11+
import soot.jimple.infoflow.problems.rules.backward.BackwardsExceptionPropagationRule;
12+
import soot.jimple.infoflow.problems.rules.backward.BackwardsImplicitFlowRule;
13+
import soot.jimple.infoflow.problems.rules.backward.BackwardsSinkPropagationRule;
14+
import soot.jimple.infoflow.problems.rules.backward.BackwardsSourcePropagationRule;
15+
import soot.jimple.infoflow.problems.rules.backward.BackwardsStrongUpdatePropagationRule;
16+
import soot.jimple.infoflow.problems.rules.backward.BackwardsWrapperRule;
1017
import soot.jimple.infoflow.problems.rules.forward.SkipSystemClassRule;
1118
import soot.jimple.infoflow.problems.rules.forward.StopAfterFirstKFlowsPropagationRule;
1219

@@ -21,29 +28,29 @@ public class BackwardPropagationRuleManagerFactory implements IPropagationRuleMa
2128
@Override
2229
public PropagationRuleManager createRuleManager(InfoflowManager manager, Abstraction zeroValue,
2330
TaintPropagationResults results) {
24-
List<ITaintPropagationRule> ruleList = new ArrayList<>();
31+
List<Class<? extends ITaintPropagationRule>> ruleList = new ArrayList<>();
2532

2633
// backwards only
27-
ruleList.add(new BackwardsSinkPropagationRule(manager, zeroValue, results));
28-
ruleList.add(new BackwardsSourcePropagationRule(manager, zeroValue, results));
29-
ruleList.add(new BackwardsClinitRule(manager, zeroValue, results));
30-
ruleList.add(new BackwardsStrongUpdatePropagationRule(manager, zeroValue, results));
34+
ruleList.add(BackwardsSinkPropagationRule.class);
35+
ruleList.add(BackwardsSourcePropagationRule.class);
36+
ruleList.add(BackwardsClinitRule.class);
37+
ruleList.add(BackwardsStrongUpdatePropagationRule.class);
3138
if (manager.getConfig().getEnableExceptionTracking())
32-
ruleList.add(new BackwardsExceptionPropagationRule(manager, zeroValue, results));
39+
ruleList.add(BackwardsExceptionPropagationRule.class);
3340
if (manager.getConfig().getEnableArrayTracking())
34-
ruleList.add(new BackwardsArrayPropagationRule(manager, zeroValue, results));
41+
ruleList.add(BackwardsArrayPropagationRule.class);
3542
if (manager.getTaintWrapper() != null)
36-
ruleList.add(new BackwardsWrapperRule(manager, zeroValue, results));
43+
ruleList.add(BackwardsWrapperRule.class);
3744

3845
// shared
39-
ruleList.add(new SkipSystemClassRule(manager, zeroValue, results));
46+
ruleList.add(SkipSystemClassRule.class);
4047
if (manager.getConfig().getStopAfterFirstKFlows() > 0)
41-
ruleList.add(new StopAfterFirstKFlowsPropagationRule(manager, zeroValue, results));
48+
ruleList.add(StopAfterFirstKFlowsPropagationRule.class);
4249

4350
if (manager.getConfig().getImplicitFlowMode().trackControlFlowDependencies())
44-
ruleList.add(new BackwardsImplicitFlowRule(manager, zeroValue, results));
51+
ruleList.add(BackwardsImplicitFlowRule.class);
4552

46-
return new PropagationRuleManager(manager, zeroValue, results, ruleList.toArray(new ITaintPropagationRule[0]));
53+
return new PropagationRuleManager(manager, zeroValue, results, ruleList);
4754
}
4855

4956
}

soot-infoflow/src/soot/jimple/infoflow/problems/rules/DefaultPropagationRuleManagerFactory.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,38 @@ public class DefaultPropagationRuleManagerFactory implements IPropagationRuleMan
3232
@Override
3333
public PropagationRuleManager createRuleManager(InfoflowManager manager, Abstraction zeroValue,
3434
TaintPropagationResults results) {
35-
List<ITaintPropagationRule> ruleList = new ArrayList<>();
35+
List<Class<? extends ITaintPropagationRule>> ruleList = new ArrayList<>();
3636

37-
ruleList.add(new SourcePropagationRule(manager, zeroValue, results));
38-
ruleList.add(new SinkPropagationRule(manager, zeroValue, results));
39-
ruleList.add(new StaticPropagationRule(manager, zeroValue, results));
37+
ruleList.add(SourcePropagationRule.class);
38+
ruleList.add(SinkPropagationRule.class);
39+
ruleList.add(StaticPropagationRule.class);
4040

4141
boolean preciseCollectionTrackingEnabled = manager.getConfig()
4242
.getPreciseCollectionStrategy() != PreciseCollectionStrategy.NONE;
4343
if (manager.getConfig().getEnableArrayTracking()) {
4444
if (preciseCollectionTrackingEnabled)
45-
ruleList.add(new ArrayWithIndexPropagationRule(manager, zeroValue, results));
45+
ruleList.add(ArrayWithIndexPropagationRule.class);
4646
else
47-
ruleList.add(new ArrayPropagationRule(manager, zeroValue, results));
47+
ruleList.add(ArrayPropagationRule.class);
4848
}
4949
if (manager.getConfig().getEnableExceptionTracking())
50-
ruleList.add(new ExceptionPropagationRule(manager, zeroValue, results));
50+
ruleList.add(ExceptionPropagationRule.class);
5151
if (manager.getTaintWrapper() != null) {
5252
if (preciseCollectionTrackingEnabled)
53-
ruleList.add(new CollectionWrapperPropagationRule(manager, zeroValue, results));
53+
ruleList.add(CollectionWrapperPropagationRule.class);
5454
else
55-
ruleList.add(new WrapperPropagationRule(manager, zeroValue, results));
55+
ruleList.add(WrapperPropagationRule.class);
5656
}
5757
if (manager.getConfig().getImplicitFlowMode().trackControlFlowDependencies())
58-
ruleList.add(new ImplicitPropagtionRule(manager, zeroValue, results));
59-
ruleList.add(new StrongUpdatePropagationRule(manager, zeroValue, results));
58+
ruleList.add(ImplicitPropagtionRule.class);
59+
ruleList.add(StrongUpdatePropagationRule.class);
6060
if (manager.getConfig().getEnableTypeChecking())
61-
ruleList.add(new TypingPropagationRule(manager, zeroValue, results));
62-
ruleList.add(new SkipSystemClassRule(manager, zeroValue, results));
61+
ruleList.add(TypingPropagationRule.class);
62+
ruleList.add(SkipSystemClassRule.class);
6363
if (manager.getConfig().getStopAfterFirstKFlows() > 0)
64-
ruleList.add(new StopAfterFirstKFlowsPropagationRule(manager, zeroValue, results));
64+
ruleList.add(StopAfterFirstKFlowsPropagationRule.class);
6565

66-
return new PropagationRuleManager(manager, zeroValue, results, ruleList.toArray(new ITaintPropagationRule[0]));
66+
return new PropagationRuleManager(manager, zeroValue, results, ruleList);
6767
}
6868

6969
public PropagationRuleManager createAliasRuleManager(InfoflowManager manager, Abstraction zeroValue) {

soot-infoflow/src/soot/jimple/infoflow/problems/rules/ITaintPropagationRule.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@
44

55
import soot.SootMethod;
66
import soot.jimple.Stmt;
7+
import soot.jimple.infoflow.InfoflowManager;
78
import soot.jimple.infoflow.data.Abstraction;
9+
import soot.jimple.infoflow.problems.TaintPropagationResults;
810
import soot.jimple.infoflow.util.ByReferenceBoolean;
911

1012
/**
11-
* Common interface for taint propagation rules
13+
* Common interface for taint propagation rules.
14+
* Note that instances of this class are stateful due to the {@link #init() init} method.
15+
* The implementing class should have a public non-argument constructor.
1216
*
1317
* @author Steven Arzt
1418
*
1519
*/
1620
public interface ITaintPropagationRule {
17-
21+
22+
/**
23+
* Initializes the class. Usual implementations of this class save the
24+
* parameters of the class.
25+
* @param manager the infoflow manager
26+
* @param zeroValue the zero value abstraction
27+
* @param results where results should be saved
28+
*/
29+
public void init(InfoflowManager manager, Abstraction zeroValue, TaintPropagationResults results);
30+
1831
/**
1932
* Propagates a flow along a normal statement this is not a call or return
2033
* site
@@ -29,10 +42,8 @@ public interface ITaintPropagationRule {
2942
* killed and nothing shall be propagated onwards
3043
* @return The new abstractions to be propagated to the next statement
3144
*/
32-
public Collection<Abstraction> propagateNormalFlow(Abstraction d1,
33-
Abstraction source, Stmt stmt, Stmt destStmt,
34-
ByReferenceBoolean killSource,
35-
ByReferenceBoolean killAll);
45+
public Collection<Abstraction> propagateNormalFlow(Abstraction d1, Abstraction source, Stmt stmt, Stmt destStmt,
46+
ByReferenceBoolean killSource, ByReferenceBoolean killAll);
3647

3748
/**
3849
* Propagates a flow across a call site
@@ -44,10 +55,9 @@ public Collection<Abstraction> propagateNormalFlow(Abstraction d1,
4455
* all taints shall be killed, i.e., nothing shall be propagated
4556
* @return The new abstractions to be propagated to the next statement
4657
*/
47-
public Collection<Abstraction> propagateCallFlow(Abstraction d1,
48-
Abstraction source, Stmt stmt, SootMethod dest,
58+
public Collection<Abstraction> propagateCallFlow(Abstraction d1, Abstraction source, Stmt stmt, SootMethod dest,
4959
ByReferenceBoolean killAll);
50-
60+
5161
/**
5262
* Propagates a flow along a the call-to-return edge at a call site
5363
* @param d1 The context abstraction
@@ -59,10 +69,9 @@ public Collection<Abstraction> propagateCallFlow(Abstraction d1,
5969
* all taints shall be killed, i.e., nothing shall be propagated
6070
* @return The new abstractions to be propagated to the next statement
6171
*/
62-
public Collection<Abstraction> propagateCallToReturnFlow(Abstraction d1,
63-
Abstraction source, Stmt stmt, ByReferenceBoolean killSource,
64-
ByReferenceBoolean killAll);
65-
72+
public Collection<Abstraction> propagateCallToReturnFlow(Abstraction d1, Abstraction source, Stmt stmt,
73+
ByReferenceBoolean killSource, ByReferenceBoolean killAll);
74+
6675
/**
6776
* Propagates a flow along a the return edge
6877
* @param callerD1s The context abstraction at the caller side
@@ -76,9 +85,7 @@ public Collection<Abstraction> propagateCallToReturnFlow(Abstraction d1,
7685
* all taints shall be killed, i.e., nothing shall be propagated
7786
* @return The new abstractions to be propagated to the next statement
7887
*/
79-
public Collection<Abstraction> propagateReturnFlow(
80-
Collection<Abstraction> callerD1s, Abstraction calleeD1, Abstraction source,
81-
Stmt stmt, Stmt retSite, Stmt callSite,
82-
ByReferenceBoolean killAll);
83-
88+
public Collection<Abstraction> propagateReturnFlow(Collection<Abstraction> callerD1s, Abstraction calleeD1,
89+
Abstraction source, Stmt stmt, Stmt retSite, Stmt callSite, ByReferenceBoolean killAll);
90+
8491
}

soot-infoflow/src/soot/jimple/infoflow/problems/rules/IdentityPropagationRuleManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class IdentityPropagationRuleManager extends PropagationRuleManager {
2020
public static IdentityPropagationRuleManager INSTANCE = new IdentityPropagationRuleManager();
2121

2222
private IdentityPropagationRuleManager() {
23-
super(null, null, null, null);
23+
super(null, null, null, (ITaintPropagationRule[]) null);
2424
}
2525

2626
@Override

soot-infoflow/src/soot/jimple/infoflow/problems/rules/PropagationRuleManager.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package soot.jimple.infoflow.problems.rules;
22

3+
import java.util.Arrays;
34
import java.util.Collection;
45
import java.util.HashSet;
6+
import java.util.List;
57
import java.util.Set;
68

79
import soot.SootMethod;
@@ -34,16 +36,77 @@ public PropagationRuleManager(InfoflowManager manager, Abstraction zeroValue, Ta
3436

3537
if (rules != null) {
3638
for (ITaintPropagationRule rule : rules) {
39+
rule.init(manager, zeroValue, results);
3740
if (rule instanceof IArrayContextProvider) {
3841
arrayRule = (IArrayContextProvider) rule;
39-
break;
4042
}
4143
}
4244
}
4345
if (arrayRule == null)
4446
arrayRule = new DummyArrayContext();
4547
}
4648

49+
public PropagationRuleManager(InfoflowManager manager, Abstraction zeroValue, TaintPropagationResults results,
50+
Class<? extends ITaintPropagationRule>[] rules) {
51+
this(manager, zeroValue, results, instantiate(Arrays.asList(rules)));
52+
}
53+
54+
public PropagationRuleManager(InfoflowManager manager, Abstraction zeroValue, TaintPropagationResults results,
55+
List<Class<? extends ITaintPropagationRule>> rules) {
56+
this(manager, zeroValue, results, instantiate(rules));
57+
}
58+
59+
private static ITaintPropagationRule[] instantiate(List<Class<? extends ITaintPropagationRule>> rules) {
60+
ITaintPropagationRule[] r = new ITaintPropagationRule[rules.size()];
61+
for (int i = 0; i < r.length; i++) {
62+
Class<? extends ITaintPropagationRule> ruleC = rules.get(i);
63+
r[i] = instantiateSingle(ruleC);
64+
}
65+
return r;
66+
}
67+
68+
private static ITaintPropagationRule instantiateSingle(Class<? extends ITaintPropagationRule> ruleClass) {
69+
try {
70+
return ruleClass.getDeclaredConstructor().newInstance();
71+
} catch (Exception e) {
72+
throw new RuntimeException(String.format("Could not instantiate rule %s", ruleClass.getName()), e);
73+
}
74+
}
75+
76+
/**
77+
* Swaps out an existing rule.
78+
* @param oldRule the class of the old rule implementation
79+
* @param newRule the class of the new implementation
80+
*/
81+
public void swapRule(Class<? extends ITaintPropagationRule> oldRule,
82+
Class<? extends ITaintPropagationRule> newRule) {
83+
swapRule(oldRule, instantiateSingle(newRule));
84+
}
85+
86+
/**
87+
* Swaps out an existing rule.
88+
* @param oldRule the class of the old rule implementation
89+
* @param newRule the new implementation
90+
*/
91+
public void swapRule(Class<? extends ITaintPropagationRule> oldRule, ITaintPropagationRule newRule) {
92+
if (rules == null)
93+
throw new IllegalStateException("No rules configured");
94+
95+
for (int i = 0; i < rules.length; i++) {
96+
ITaintPropagationRule r = rules[i];
97+
if (r.getClass() == oldRule) {
98+
r.init(manager, zeroValue, results);
99+
if (r instanceof IArrayContextProvider) {
100+
arrayRule = (IArrayContextProvider) r;
101+
}
102+
rules[i] = newRule;
103+
return;
104+
}
105+
}
106+
throw new IllegalArgumentException(
107+
String.format("Could not find %s in the rules: %s", oldRule.getName(), Arrays.toString(rules)));
108+
}
109+
47110
/**
48111
* Applies all rules to the normal flow function
49112
*

soot-infoflow/src/soot/jimple/infoflow/problems/rules/backward/BackwardsArrayPropagationRule.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
import soot.jimple.LengthExpr;
1414
import soot.jimple.NewArrayExpr;
1515
import soot.jimple.Stmt;
16-
import soot.jimple.infoflow.InfoflowManager;
1716
import soot.jimple.infoflow.aliasing.Aliasing;
1817
import soot.jimple.infoflow.data.Abstraction;
1918
import soot.jimple.infoflow.data.AccessPath;
2019
import soot.jimple.infoflow.data.AccessPath.ArrayTaintType;
2120
import soot.jimple.infoflow.data.ContainerContext;
22-
import soot.jimple.infoflow.problems.TaintPropagationResults;
2321
import soot.jimple.infoflow.problems.rules.AbstractTaintPropagationRule;
2422
import soot.jimple.infoflow.problems.rules.IArrayContextProvider;
2523
import soot.jimple.infoflow.typing.TypeUtils;
@@ -33,11 +31,6 @@
3331
*/
3432
public class BackwardsArrayPropagationRule extends AbstractTaintPropagationRule implements IArrayContextProvider {
3533

36-
public BackwardsArrayPropagationRule(InfoflowManager manager, Abstraction zeroValue,
37-
TaintPropagationResults results) {
38-
super(manager, zeroValue, results);
39-
}
40-
4134
@Override
4235
public Collection<Abstraction> propagateNormalFlow(Abstraction d1, Abstraction source, Stmt stmt, Stmt destStmt,
4336
ByReferenceBoolean killSource, ByReferenceBoolean killAll) {

0 commit comments

Comments
 (0)