2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.listener;
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.ListenerAdapter;
23 import gov.nasa.jpf.search.Search;
24 import gov.nasa.jpf.jvm.bytecode.*;
25 import gov.nasa.jpf.vm.*;
26 import gov.nasa.jpf.vm.bytecode.LocalVariableInstruction;
27 import gov.nasa.jpf.vm.bytecode.ReadInstruction;
28 import gov.nasa.jpf.vm.bytecode.StoreInstruction;
29 import gov.nasa.jpf.vm.bytecode.WriteInstruction;
31 import java.io.PrintWriter;
36 * Listener using data flow analysis to find conflicts between smartApps.
39 public class ConflictTracker extends ListenerAdapter {
41 private final PrintWriter out;
42 private final HashSet<String> conflictSet = new HashSet<String>(); // Variables we want to track
43 private final HashSet<String> appSet = new HashSet<String>(); // Apps we want to find their conflicts
44 private final HashSet<String> manualSet = new HashSet<String>(); // Writer classes with manual inputs to detect direct-direct(No Conflict) interactions
45 private final HashMap<Integer, Node> nodes = new HashMap<Integer, Node>(); // Nodes of a graph
46 private HashSet<NameValuePair> tempSetSet = new HashSet<NameValuePair>();
48 private long startTime;
49 private Node parentNode = new Node(-2);
50 private String operation;
51 private String detail;
52 private String errorMessage;
55 private boolean conflictFound = false;
56 private boolean isSet = false;
57 private boolean manual = false;
60 public ConflictTracker(Config config, JPF jpf) {
61 out = new PrintWriter(System.out, true);
63 String[] conflictVars = config.getStringArray("variables");
64 // We are not tracking anything if it is null
65 if (conflictVars != null) {
66 for (String var : conflictVars) {
70 String[] apps = config.getStringArray("apps");
71 // We are not tracking anything if it is null
73 for (String var : apps) {
77 String[] manualClasses = config.getStringArray("manualClasses");
78 // We are not tracking anything if it is null
79 if (manualClasses != null) {
80 for (String var : manualClasses) {
85 // Timeout input from config is in minutes, so we need to convert into millis
86 timeout = config.getInt("timeout", 0) * 60 * 1000;
87 startTime = System.currentTimeMillis();
90 boolean propagateTheChange(Node currentNode) {
91 HashSet<Node> changed = new HashSet<Node>(currentNode.getSuccessors());
93 while(!changed.isEmpty()) {
94 // Get the first element of HashSet and remove it from the changed set
95 Node nodeToProcess = changed.iterator().next();
96 changed.remove(nodeToProcess);
98 // Update the sets, store the outSet to temp before its changes
99 boolean isChanged = updateSets(nodeToProcess);
101 // Check for a conflict
102 if (checkForConflict(nodeToProcess))
105 // Checking if the out set has changed or not(Add its successors to the change list!)
107 for (Node i : nodeToProcess.getSuccessors()) {
108 if (!changed.contains(i))
117 boolean setOutSet(Node currentNode) {
118 Integer prevSize = currentNode.getOutSet().size();
120 // Update based on setSet
121 for (NameValuePair i : currentNode.getSetSet()) {
122 if (currentNode.getOutSet().contains(i))
123 currentNode.getOutSet().remove(i);
124 currentNode.getOutSet().add(i);
128 currentNode.getOutSet().addAll(currentNode.getInSet());
130 // Check if the outSet is changed
131 if (!prevSize.equals(currentNode.getOutSet().size()))
137 void setInSet(Node currentNode) {
138 for (Node i : currentNode.getPredecessors()) {
139 currentNode.getInSet().addAll(i.getOutSet());
143 boolean checkForConflict(Node currentNode) {
144 HashMap<String, String> valueMap = new HashMap<String, String>(); // HashMap from varName to value
145 HashMap<String, Integer> writerMap = new HashMap<String, Integer>(); // HashMap from varName to appNum
147 for (NameValuePair i : currentNode.getSetSet()) {
148 if (i.getIsManual()) // Manual input: we have no conflict
151 valueMap.put(i.getVarName(), i.getValue());
152 if (writerMap.containsKey(i.getVarName()))
153 writerMap.put(i.getVarName(), i.getAppNum()+writerMap.get(i.getVarName())); // We have two writers?
155 writerMap.put(i.getVarName(), i.getAppNum());
158 // Comparing the inSet and setSet to find the conflict
159 for (NameValuePair i : currentNode.getInSet()) {
160 if (valueMap.containsKey(i.getVarName())) {
161 if (!(valueMap.get(i.getVarName()).equals(i.getValue()))) // We have different values
162 if (!(writerMap.get(i.getVarName()).equals(i.getAppNum()))) {// We have different writers
163 errorMessage = "Conflict found between the two apps. App"+i.getAppNum()+" has written the value: "+i.getValue()+
164 " to the variable: "+i.getVarName()+" while App"+writerMap.get(i.getVarName())+" is overwriting the value: "
165 +valueMap.get(i.getVarName())+" to the same variable!";
174 boolean updateSets(Node currentNode) {
175 // Set input set according to output set of pred states of current state
176 setInSet(currentNode);
178 // Set outSet according to inSet, and setSet of current node, check if there is a change
179 return setOutSet(currentNode);
184 HashSet<Node> predecessors = new HashSet<Node>();
185 HashSet<Node> successors = new HashSet<Node>();
186 HashSet<NameValuePair> inSet = new HashSet<NameValuePair>();
187 HashSet<NameValuePair> setSet = new HashSet<NameValuePair>();
188 HashSet<NameValuePair> outSet = new HashSet<NameValuePair>();
194 void addPredecessor(Node node) {
195 predecessors.add(node);
198 void addSuccessor(Node node) {
199 successors.add(node);
202 void setSetSet(HashSet<NameValuePair> setSet, boolean isManual) {
204 this.setSet = new HashSet<NameValuePair>();
205 for (NameValuePair i : setSet) {
206 this.setSet.add(new NameValuePair(i.getAppNum(), i.getValue(), i.getVarName(), i.getIsManual()));
214 HashSet<Node> getPredecessors() {
218 HashSet<Node> getSuccessors() {
222 HashSet<NameValuePair> getInSet() {
226 HashSet<NameValuePair> getSetSet() {
230 HashSet<NameValuePair> getOutSet() {
235 static class NameValuePair {
241 NameValuePair(Integer appNum, String value, String varName, boolean isManual) {
242 this.appNum = appNum;
244 this.varName = varName;
245 this.isManual = isManual;
248 void setAppNum(Integer appNum) {
249 this.appNum = appNum;
252 void setValue(String value) {
256 void setVarName(String varName) {
257 this.varName = varName;
260 void setIsManual(String varName) {
261 this.isManual = isManual;
264 Integer getAppNum() {
272 String getVarName() {
276 boolean getIsManual() {
281 public boolean equals(Object o) {
282 if (o instanceof NameValuePair) {
283 NameValuePair other = (NameValuePair) o;
284 if (varName.equals(other.getVarName()))
285 return appNum.equals(other.getAppNum());
291 public int hashCode() {
292 return appNum.hashCode() * 31 + varName.hashCode();
297 public void stateRestored(Search search) {
298 id = search.getStateId();
299 depth = search.getDepth();
300 operation = "restored";
303 out.println("The state is restored to state with id: "+id+", depth: "+depth);
305 // Update the parent node
306 if (nodes.containsKey(id)) {
307 parentNode = nodes.get(id);
309 parentNode = new Node(id);
314 public void searchStarted(Search search) {
315 out.println("----------------------------------- search started");
320 public void stateAdvanced(Search search) {
321 String theEnd = null;
322 id = search.getStateId();
323 depth = search.getDepth();
324 operation = "forward";
326 // Add the node to the list of nodes
327 nodes.put(id, new Node(id));
328 Node currentNode = nodes.get(id);
330 // Update the setSet for this new node
332 currentNode.setSetSet(tempSetSet, manual);
333 tempSetSet = new HashSet<NameValuePair>();
338 if (search.isNewState()) {
344 if (search.isEndState()) {
345 out.println("This is the last state!");
349 out.println("The state is forwarded to state with id: "+id+", depth: "+depth+" which is "+detail+" state: "+"% "+theEnd);
351 // Updating the predecessors for this node
352 // Check if parent node is already in successors of the current node or not
353 if (!(currentNode.getPredecessors().contains(parentNode)))
354 currentNode.addPredecessor(parentNode);
356 // Update the successors for this node
357 // Check if current node is already in successors of the parent node or not
358 if (!(parentNode.getSuccessors().contains(currentNode)))
359 parentNode.addSuccessor(currentNode);
361 // Update the sets, check if the outSet is changed or not
362 boolean isChanged = updateSets(currentNode);
364 // Check for a conflict
365 conflictFound = checkForConflict(currentNode);
367 // Check if the outSet of this state has changed, update all of its successors' sets if any
369 conflictFound = conflictFound || propagateTheChange(currentNode);
371 // Update the parent node
372 if (nodes.containsKey(id)) {
373 parentNode = nodes.get(id);
375 parentNode = new Node(id);
380 public void stateBacktracked(Search search) {
381 id = search.getStateId();
382 depth = search.getDepth();
383 operation = "backtrack";
386 out.println("The state is backtracked to state with id: "+id+", depth: "+depth);
388 // Update the parent node
389 if (nodes.containsKey(id)) {
390 parentNode = nodes.get(id);
392 parentNode = new Node(id);
397 public void searchFinished(Search search) {
398 out.println("----------------------------------- search finished");
401 private String getValue(ThreadInfo ti, Instruction inst, byte type) {
405 frame = ti.getTopFrame();
407 if ((inst instanceof JVMLocalVariableInstruction) ||
408 (inst instanceof JVMFieldInstruction))
410 if (frame.getTopPos() < 0)
414 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
416 return(decodeValue(type, lo, hi));
419 if (inst instanceof JVMArrayElementInstruction)
420 return(getArrayValue(ti, type));
425 private final static String decodeValue(byte type, int lo, int hi) {
427 case Types.T_ARRAY: return(null);
428 case Types.T_VOID: return(null);
430 case Types.T_BOOLEAN: return(String.valueOf(Types.intToBoolean(lo)));
431 case Types.T_BYTE: return(String.valueOf(lo));
432 case Types.T_CHAR: return(String.valueOf((char) lo));
433 case Types.T_DOUBLE: return(String.valueOf(Types.intsToDouble(lo, hi)));
434 case Types.T_FLOAT: return(String.valueOf(Types.intToFloat(lo)));
435 case Types.T_INT: return(String.valueOf(lo));
436 case Types.T_LONG: return(String.valueOf(Types.intsToLong(lo, hi)));
437 case Types.T_SHORT: return(String.valueOf(lo));
439 case Types.T_REFERENCE:
440 ElementInfo ei = VM.getVM().getHeap().get(lo);
444 ClassInfo ci = ei.getClassInfo();
448 if (ci.getName().equals("java.lang.String"))
449 return('"' + ei.asString() + '"');
451 return(ei.toString());
454 System.err.println("Unknown type: " + type);
459 private String getArrayValue(ThreadInfo ti, byte type) {
463 frame = ti.getTopFrame();
465 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
467 return(decodeValue(type, lo, hi));
470 private byte getType(ThreadInfo ti, Instruction inst) {
475 frame = ti.getTopFrame();
476 if ((frame.getTopPos() >= 0) && (frame.isOperandRef())) {
477 return (Types.T_REFERENCE);
482 if (inst instanceof JVMLocalVariableInstruction) {
483 type = ((JVMLocalVariableInstruction) inst).getLocalVariableType();
484 } else if (inst instanceof JVMFieldInstruction){
485 fi = ((JVMFieldInstruction) inst).getFieldInfo();
489 if (inst instanceof JVMArrayElementInstruction) {
490 return (getTypeFromInstruction(inst));
494 return (Types.T_VOID);
497 return (decodeType(type));
500 private final static byte getTypeFromInstruction(Instruction inst) {
501 if (inst instanceof JVMArrayElementInstruction)
502 return(getTypeFromInstruction((JVMArrayElementInstruction) inst));
504 return(Types.T_VOID);
507 private final static byte decodeType(String type) {
508 if (type.charAt(0) == '?'){
509 return(Types.T_REFERENCE);
511 return Types.getBuiltinType(type);
515 // Find the variable writer
516 // It should be one of the apps listed in the .jpf file
517 private String getWriter(List<StackFrame> sfList, HashSet<String> writerSet) {
518 // Start looking from the top of the stack backward
519 for(int i=sfList.size()-1; i>=0; i--) {
520 MethodInfo mi = sfList.get(i).getMethodInfo();
521 if(!mi.isJPFInternal()) {
522 String method = mi.getStackTraceName();
523 // Check against the writers in the writerSet
524 for(String writer : writerSet) {
525 if (method.contains(writer)) {
536 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
537 // Instantiate timeoutTimer
539 if (System.currentTimeMillis() - startTime > timeout) {
540 StringBuilder sbTimeOut = new StringBuilder();
541 sbTimeOut.append("Execution timeout: " + (timeout / (60 * 1000)) + " minutes have passed!");
542 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sbTimeOut.toString());
543 ti.setNextPC(nextIns);
548 StringBuilder sb = new StringBuilder();
549 sb.append(errorMessage);
550 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
551 ti.setNextPC(nextIns);
552 } else if (executedInsn instanceof WriteInstruction) {
553 String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
554 for(String var : conflictSet) {
556 if (varId.contains(var)) {
558 byte type = getType(ti, executedInsn);
559 String value = getValue(ti, executedInsn, type);
560 String writer = getWriter(ti.getStack(), appSet);
561 // Just return if the writer is not one of the listed apps in the .jpf file
565 if (getWriter(ti.getStack(), manualSet) != null)
568 // Update the temporary Set set.
569 if (writer.equals("App1"))
570 tempSetSet.add(new NameValuePair(1, value, var, manual));
571 else if (writer.equals("App2"))
572 tempSetSet.add(new NameValuePair(2, value, var, manual));