Skip to content

Commit ac66700

Browse files
authored
Improve thread-safety of KeY (#3335)
This PR prepares KeY for more parallelisation in an upcoming PR The changes are relatively low risk and improve code quality in general - Make strategies stateless - Move static cache to instance cache managed by ServiceCaches No introduction of synchronization primitives in this PR.
2 parents e978aed + 6d635c8 commit ac66700

File tree

167 files changed

+901
-599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+901
-599
lines changed

key.core.symbolic_execution/src/main/java/de/uka/ilkd/key/symbolic_execution/strategy/CutHeapObjectsFeature.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import de.uka.ilkd.key.proof.Goal;
1616
import de.uka.ilkd.key.rule.RuleApp;
1717
import de.uka.ilkd.key.strategy.feature.BinaryFeature;
18+
import de.uka.ilkd.key.strategy.feature.MutableState;
1819
import de.uka.ilkd.key.strategy.termProjection.SVInstantiationProjection;
1920

2021
/**
@@ -35,9 +36,10 @@ public class CutHeapObjectsFeature extends BinaryFeature {
3536
* {@inheritDoc}
3637
*/
3738
@Override
38-
protected boolean filter(RuleApp app, PosInOccurrence pos, Goal goal) {
39+
protected boolean filter(RuleApp app, PosInOccurrence pos, Goal goal, MutableState mState) {
3940
Term cutFormula =
40-
SVInstantiationProjection.create(new Name("cutFormula"), false).toTerm(app, pos, goal);
41+
SVInstantiationProjection.create(new Name("cutFormula"), false).toTerm(app, pos, goal,
42+
mState);
4143
if (cutFormula != null) {
4244
if (cutFormula.op() == Junctor.NOT) {
4345
cutFormula = cutFormula.sub(0);

key.core.symbolic_execution/src/main/java/de/uka/ilkd/key/symbolic_execution/strategy/CutHeapObjectsTermGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import de.uka.ilkd.key.logic.op.Function;
1818
import de.uka.ilkd.key.proof.Goal;
1919
import de.uka.ilkd.key.rule.RuleApp;
20+
import de.uka.ilkd.key.strategy.feature.MutableState;
2021
import de.uka.ilkd.key.strategy.termgenerator.TermGenerator;
2122

2223
/**
@@ -31,7 +32,8 @@ public class CutHeapObjectsTermGenerator implements TermGenerator {
3132
* {@inheritDoc}
3233
*/
3334
@Override
34-
public Iterator<Term> generate(RuleApp app, PosInOccurrence pos, Goal goal) {
35+
public Iterator<Term> generate(RuleApp app, PosInOccurrence pos, Goal goal,
36+
MutableState mState) {
3537
// Compute collect terms of sequent formulas
3638
Sequent sequent = goal.sequent();
3739
Set<Term> topTerms = new LinkedHashSet<>();

key.core.symbolic_execution/src/main/java/de/uka/ilkd/key/symbolic_execution/strategy/SimplifyTermStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Name name() {
4949
@Override
5050
protected Feature setupApprovalF() {
5151
Feature superFeature = super.setupApprovalF();
52-
Feature labelFeature = (app, pos, goal) -> {
52+
Feature labelFeature = (app, pos, goal, mState) -> {
5353
boolean hasLabel = false;
5454
if (pos != null && app instanceof TacletApp) {
5555
Term findTerm = pos.subTerm();

key.core.symbolic_execution/src/main/java/de/uka/ilkd/key/symbolic_execution/strategy/SymbolicExecutionStrategy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ protected Feature setupGlobalF(Feature dispatcher) {
117117
// body branches)
118118
globalF = add(globalF, ifZero(not(new BinaryFeature() {
119119
@Override
120-
protected boolean filter(RuleApp app, PosInOccurrence pos, Goal goal) {
120+
protected boolean filter(RuleApp app, PosInOccurrence pos, Goal goal,
121+
MutableState mState) {
121122
return pos != null
122123
&& SymbolicExecutionUtil.hasSymbolicExecutionLabel(pos.subTerm());
123124
}

key.core.testgen/src/main/java/de/uka/ilkd/key/macros/TestGenMacro.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import de.uka.ilkd.key.strategy.NumberRuleAppCost;
1818
import de.uka.ilkd.key.strategy.RuleAppCost;
1919
import de.uka.ilkd.key.strategy.Strategy;
20+
import de.uka.ilkd.key.strategy.feature.MutableState;
2021

2122
public class TestGenMacro extends StrategyProofMacro {
2223
@Override
@@ -78,11 +79,12 @@ public TestGenStrategy(Strategy delegate) {
7879
}
7980

8081
@Override
81-
public RuleAppCost computeCost(RuleApp app, PosInOccurrence pio, Goal goal) {
82+
public RuleAppCost computeCost(RuleApp app, PosInOccurrence pio, Goal goal,
83+
MutableState mState) {
8284
if (TestGenStrategy.isUnwindRule(app.rule())) {
8385
return NumberRuleAppCost.create(TestGenStrategy.UNWIND_COST);
8486
}
85-
return super.computeCost(app, pio, goal);
87+
return super.computeCost(app, pio, goal, mState);
8688
}
8789

8890
private int computeUnwindRules(Goal goal) {

key.core/src/main/java/de/uka/ilkd/key/api/Matcher.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import de.uka.ilkd.key.nparser.ParsingFacade;
1616
import de.uka.ilkd.key.rule.*;
1717
import de.uka.ilkd.key.rule.inst.SVInstantiations;
18-
import de.uka.ilkd.key.rule.match.legacy.LegacyTacletMatcher;
18+
import de.uka.ilkd.key.rule.match.vm.VMTacletMatcher;
1919

20+
import org.key_project.util.collection.ImmutableArray;
2021
import org.key_project.util.collection.ImmutableList;
2122

2223
import org.antlr.v4.runtime.CharStreams;
@@ -67,7 +68,7 @@ public List<VariableAssignments> matchPattern(String pattern, Sequent currentSeq
6768
Taclet t = parseTaclet(patternString, copyServices);
6869

6970
// Build Matcher for Matchpattern
70-
LegacyTacletMatcher ltm = new LegacyTacletMatcher(t);
71+
VMTacletMatcher tacletMatcher = new VMTacletMatcher(t);
7172

7273
// patternSequent should not be null, as we have created it
7374
assert t.ifSequent() != null;
@@ -79,9 +80,9 @@ public List<VariableAssignments> matchPattern(String pattern, Sequent currentSeq
7980
List<SearchNode> finalCandidates = new ArrayList<>(100);
8081
if (size > 0) {
8182
// Iteratoren durch die Sequent
82-
ImmutableList<IfFormulaInstantiation> antecCand =
83+
ImmutableArray<IfFormulaInstantiation> antecCand =
8384
IfFormulaInstSeq.createList(currentSeq, true, copyServices);
84-
ImmutableList<IfFormulaInstantiation> succCand =
85+
ImmutableArray<IfFormulaInstantiation> succCand =
8586
IfFormulaInstSeq.createList(currentSeq, false, copyServices);
8687

8788
SequentFormula[] patternArray = new SequentFormula[patternSeq.size()];
@@ -93,16 +94,16 @@ public List<VariableAssignments> matchPattern(String pattern, Sequent currentSeq
9394

9495
Queue<SearchNode> queue = new LinkedList<>();
9596
// init
96-
queue.add(new SearchNode(patternArray, asize, antecCand, succCand));
97+
queue.add(new SearchNode(patternArray, asize));
9798

9899

99100
while (!queue.isEmpty()) {
100101
SearchNode node = queue.remove();
101102
boolean inAntecedent = node.isAntecedent();
102103
LOGGER.debug(inAntecedent ? "In Antec: " : "In Succ");
103104

104-
IfMatchResult ma = ltm.matchIf((inAntecedent ? antecCand : succCand),
105-
node.getPatternTerm(), node.mc, copyServices);
105+
IfMatchResult ma = tacletMatcher.matchIf((inAntecedent ? antecCand : succCand),
106+
node.getPatternTerm(), node.getMatchConditions(), copyServices);
106107

107108
if (!ma.getMatchConditions().isEmpty()) {
108109
ImmutableList<MatchConditions> testma = ma.getMatchConditions();
@@ -138,7 +139,7 @@ public List<VariableAssignments> matchPattern(String pattern, Sequent currentSeq
138139
*/
139140
private VariableAssignments extractAssignments(SearchNode sn, VariableAssignments assignments) {
140141
VariableAssignments va = new VariableAssignments();
141-
SVInstantiations insts = sn.mc.getInstantiations();
142+
SVInstantiations insts = sn.getInstantiations();
142143
Set<String> varNames = assignments.getTypeMap().keySet();
143144
for (String varName : varNames) {
144145
SchemaVariable sv = insts.lookupVar(new Name(varName));

key.core/src/main/java/de/uka/ilkd/key/api/SearchNode.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,23 @@
55

66
import de.uka.ilkd.key.logic.SequentFormula;
77
import de.uka.ilkd.key.logic.Term;
8-
import de.uka.ilkd.key.rule.IfFormulaInstantiation;
98
import de.uka.ilkd.key.rule.MatchConditions;
10-
11-
import org.key_project.util.collection.ImmutableList;
9+
import de.uka.ilkd.key.rule.inst.SVInstantiations;
1210

1311
/**
1412
* Created by sarah on 5/2/17.
1513
*/
16-
public class SearchNode {
17-
final SequentFormula[] pattern;
18-
int pos = 0;
19-
int succAntPos = 0;
20-
public final MatchConditions mc;
21-
final ImmutableList<IfFormulaInstantiation> antec;
22-
final ImmutableList<IfFormulaInstantiation> succ;
23-
14+
public final class SearchNode {
15+
private final SequentFormula[] pattern;
16+
private final int pos;
17+
private final int succAntPos;
18+
private final MatchConditions mc;
2419

2520

26-
public SearchNode(SequentFormula[] pattern, int succAntPos,
27-
ImmutableList<IfFormulaInstantiation> antec,
28-
ImmutableList<IfFormulaInstantiation> succ) {
21+
public SearchNode(SequentFormula[] pattern, int succAntPos) {
2922
this.pattern = pattern;
23+
this.pos = 0;
3024
this.succAntPos = succAntPos;
31-
this.antec = antec;
32-
this.succ = succ;
3325
this.mc = MatchConditions.EMPTY_MATCHCONDITIONS;
3426
}
3527

@@ -38,8 +30,6 @@ public SearchNode(SearchNode parent, MatchConditions cond) {
3830
this.succAntPos = parent.succAntPos;
3931
int parentPos = parent.pos;
4032
this.pos = parentPos + 1;
41-
antec = parent.antec;
42-
succ = parent.succ;
4333
mc = cond;
4434
}
4535

@@ -55,4 +45,11 @@ public boolean isFinished() {
5545
return pos >= pattern.length;
5646
}
5747

48+
public SVInstantiations getInstantiations() {
49+
return mc == null ? null : mc.getInstantiations();
50+
}
51+
52+
public MatchConditions getMatchConditions() {
53+
return mc;
54+
}
5855
}

key.core/src/main/java/de/uka/ilkd/key/control/instantiation_model/TacletInstantiationModel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import de.uka.ilkd.key.rule.*;
1616
import de.uka.ilkd.key.rule.inst.SortException;
1717

18+
import org.key_project.util.collection.ImmutableArray;
1819
import org.key_project.util.collection.ImmutableList;
1920
import org.key_project.util.collection.ImmutableSLList;
2021

@@ -119,9 +120,9 @@ private void initIfChoiceModels() {
119120
int size = asize + ifseq.succedent().size();
120121

121122
if (size > 0) {
122-
ImmutableList<IfFormulaInstantiation> antecCand =
123+
ImmutableArray<IfFormulaInstantiation> antecCand =
123124
IfFormulaInstSeq.createList(seq, true, services);
124-
ImmutableList<IfFormulaInstantiation> succCand =
125+
ImmutableArray<IfFormulaInstantiation> succCand =
125126
IfFormulaInstSeq.createList(seq, false, services);
126127

127128
Iterator<SequentFormula> it = ifseq.iterator();

key.core/src/main/java/de/uka/ilkd/key/informationflow/macros/SelfcompositionStateExpansionMacro.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import de.uka.ilkd.key.strategy.Strategy;
2323
import de.uka.ilkd.key.strategy.StrategyProperties;
2424
import de.uka.ilkd.key.strategy.TopRuleAppCost;
25+
import de.uka.ilkd.key.strategy.feature.MutableState;
2526

2627
import org.key_project.util.collection.ImmutableList;
2728

@@ -123,14 +124,15 @@ public Name name() {
123124
}
124125

125126
@Override
126-
public RuleAppCost computeCost(RuleApp ruleApp, PosInOccurrence pio, Goal goal) {
127+
public RuleAppCost computeCost(RuleApp ruleApp, PosInOccurrence pio, Goal goal,
128+
MutableState mState) {
127129
String name = ruleApp.rule().name().toString();
128130
if ((admittedRuleNames.contains(name) || name.startsWith(INF_FLOW_UNFOLD_PREFIX))
129131
&& ruleApplicationInContextAllowed(ruleApp, pio, goal)) {
130132
JavaCardDLStrategyFactory strategyFactory = new JavaCardDLStrategyFactory();
131133
Strategy javaDlStrategy =
132134
strategyFactory.create(goal.proof(), new StrategyProperties());
133-
RuleAppCost costs = javaDlStrategy.computeCost(ruleApp, pio, goal);
135+
RuleAppCost costs = javaDlStrategy.computeCost(ruleApp, pio, goal, mState);
134136
if ("orLeft".equals(name)) {
135137
costs = costs.add(NumberRuleAppCost.create(100));
136138
}

key.core/src/main/java/de/uka/ilkd/key/informationflow/macros/UseInformationFlowContractMacro.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import de.uka.ilkd.key.strategy.TopRuleAppCost;
2323
import de.uka.ilkd.key.strategy.feature.FocusIsSubFormulaOfInfFlowContractAppFeature;
2424
import de.uka.ilkd.key.strategy.feature.InfFlowContractAppFeature;
25+
import de.uka.ilkd.key.strategy.feature.MutableState;
2526

2627
import org.key_project.util.collection.DefaultImmutableSet;
2728
import org.key_project.util.collection.ImmutableList;
@@ -181,7 +182,8 @@ public Name name() {
181182

182183

183184
@Override
184-
public RuleAppCost computeCost(RuleApp ruleApp, PosInOccurrence pio, Goal goal) {
185+
public RuleAppCost computeCost(RuleApp ruleApp, PosInOccurrence pio, Goal goal,
186+
MutableState mState) {
185187
// first try to apply
186188
// - impLeft on previous information flow contract application
187189
// formula, else
@@ -190,14 +192,14 @@ public RuleAppCost computeCost(RuleApp ruleApp, PosInOccurrence pio, Goal goal)
190192
String name = ruleApp.rule().name().toString();
191193
if (name.startsWith(INF_FLOW_RULENAME_PREFIX)
192194
&& ruleApplicationInContextAllowed(ruleApp, pio, goal)) {
193-
return InfFlowContractAppFeature.INSTANCE.computeCost(ruleApp, pio, goal);
195+
return InfFlowContractAppFeature.INSTANCE.computeCost(ruleApp, pio, goal, mState);
194196
} else if (name.equals(DOUBLE_IMP_LEFT_RULENAME)) {
195197
RuleAppCost impLeftCost = FocusIsSubFormulaOfInfFlowContractAppFeature.INSTANCE
196-
.computeCost(ruleApp, pio, goal);
198+
.computeCost(ruleApp, pio, goal, mState);
197199
return impLeftCost.add(NumberRuleAppCost.create(-10010));
198200
} else if (name.equals(IMP_LEFT_RULENAME)) {
199201
RuleAppCost impLeftCost = FocusIsSubFormulaOfInfFlowContractAppFeature.INSTANCE
200-
.computeCost(ruleApp, pio, goal);
202+
.computeCost(ruleApp, pio, goal, mState);
201203
return impLeftCost.add(NumberRuleAppCost.create(-10000));
202204
} else if (admittedRuleNames.contains(name)
203205
&& ruleApplicationInContextAllowed(ruleApp, pio, goal)) {

0 commit comments

Comments
 (0)