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;
30 import gov.nasa.jpf.vm.choice.IntChoiceFromSet;
32 import java.io.PrintWriter;
37 * Listener using data flow analysis to find conflicts between smartApps.
40 public class ConflictTracker extends ListenerAdapter {
42 private final PrintWriter out;
43 private final HashMap<Integer, Node> nodes = new HashMap<Integer, Node>(); // Nodes of a graph
44 private final HashSet<String> conflictSet = new HashSet<String>(); // Variables we want to track
45 private final HashSet<String> appSet = new HashSet<String>(); // Apps we want to find their conflicts
46 private final HashSet<String> manualSet = new HashSet<String>(); // Writer classes with manual inputs to detect direct-direct(No Conflict) interactions
47 private HashSet<Transition> transitions = new HashSet<Transition>();
48 private ArrayList<Update> currUpdates = new ArrayList<Update>();
50 private long startTime;
51 private Node parentNode = new Node(-2);
52 private String operation;
53 private String detail;
54 private String errorMessage;
55 private String varName;
58 private boolean manual = false;
59 private boolean conflictFound = false;
60 private int currentEvent = -1;
61 private boolean debugMode = false;
62 private int popRef = 0;
64 private final String SET_LOCATION_METHOD = "setLocationMode";
65 private final String LOCATION_VAR = "locationMode";
67 public ConflictTracker(Config config, JPF jpf) {
68 out = new PrintWriter(System.out, true);
69 debugMode = config.getBoolean("debug_mode", false);
71 String[] conflictVars = config.getStringArray("variables");
72 // We are not tracking anything if it is null
73 if (conflictVars != null) {
74 for (String var : conflictVars) {
78 String[] apps = config.getStringArray("apps");
79 // We are not tracking anything if it is null
81 for (String var : apps) {
86 // Timeout input from config is in minutes, so we need to convert into millis
87 timeout = config.getInt("timeout", 0) * 60 * 1000;
88 startTime = System.currentTimeMillis();
91 void propagateChange(Edge newEdge) {
92 HashSet<Edge> changed = new HashSet<Edge>();
94 // Add the current node to the changed set
97 while(!changed.isEmpty()) {
98 // Get the first element of the changed set and remove it
99 Edge edgeToProcess = changed.iterator().next();
100 changed.remove(edgeToProcess);
102 //If propagating change on edge causes change, enqueue all the target node's out edges
103 if (propagateEdge(edgeToProcess)) {
104 Node dst = edgeToProcess.getDst();
105 for (Edge e : dst.getOutEdges()) {
112 boolean propagateEdge(Edge e) {
113 HashMap<IndexObject, HashSet<Update>> srcUpdates = e.getSrc().getLastUpdates();
114 HashMap<IndexObject, HashSet<Update>> dstUpdates = e.getDst().getLastUpdates();
115 ArrayList<Update> edgeUpdates = e.getUpdates();
116 HashMap<IndexObject, Update> lastupdate = new HashMap<IndexObject, Update>();
117 boolean changed = false;
119 //Go through each update on the current transition
120 for(int i=0; i<edgeUpdates.size(); i++) {
121 Update u = edgeUpdates.get(i);
122 IndexObject io = u.getIndex();
123 HashSet<Update> confupdates = null;
125 //See if we have already updated this device attribute
126 if (lastupdate.containsKey(io)) {
127 confupdates = new HashSet<Update>();
128 confupdates.add(lastupdate.get(io));
129 } else if (srcUpdates.containsKey(io)){
130 confupdates = srcUpdates.get(io);
133 //Check for conflict with the appropriate update set if we are not a manual transition
134 //If this is debug mode, then we do not report any conflicts
135 if (!debugMode && confupdates != null && !u.isManual()) {
136 for(Update u2: confupdates) {
137 if (conflicts(u, u2)) {
138 //throw new RuntimeException(createErrorMessage(u, u2));
139 conflictFound = true;
140 errorMessage = createErrorMessage(u, u2);
144 lastupdate.put(io, u);
146 for(IndexObject io: srcUpdates.keySet()) {
147 //Only propagate src changes if the transition doesn't update the device attribute
148 if (!lastupdate.containsKey(io)) {
149 //Make sure destination has hashset in map
150 if (!dstUpdates.containsKey(io))
151 dstUpdates.put(io, new HashSet<Update>());
153 changed |= dstUpdates.get(io).addAll(srcUpdates.get(io));
156 for(IndexObject io: lastupdate.keySet()) {
157 //Make sure destination has hashset in map
158 if (!dstUpdates.containsKey(io))
159 dstUpdates.put(io, new HashSet<Update>());
161 changed |= dstUpdates.get(io).add(lastupdate.get(io));
166 //Method to check for conflicts between two updates
167 //Have conflict if same device, same attribute, different app, different vaalue
168 boolean conflicts(Update u, Update u2) {
169 return (!u.getApp().equals(u2.getApp())) &&
170 u.getDeviceId().equals(u2.getDeviceId()) &&
171 u.getAttribute().equals(u2.getAttribute()) &&
172 (!u.getValue().equals(u2.getValue()));
175 String createErrorMessage(Update u, Update u2) {
176 String message = "Conflict found between the two apps. "+u.getApp()+
177 " has written the value: "+u.getValue()+
178 " to the attribute: "+u.getAttribute()+" while "
179 +u2.getApp()+" is writing the value: "
180 +u2.getValue()+" to the same variable!";
181 System.out.println(message);
185 Edge createEdge(Node parent, Node current, Transition transition, int evtNum) {
186 //Check if this transition is explored. If so, just skip everything
187 if (transitions.contains(transition))
191 Edge e = new Edge(parent, current, transition, evtNum, currUpdates);
192 parent.addOutEdge(e);
194 //Mark transition as explored
195 transitions.add(transition);
201 Vector<Edge> outEdges = new Vector<Edge>();
202 HashMap<IndexObject, HashSet<Update>> lastUpdates = new HashMap<IndexObject, HashSet<Update>>();
212 Vector<Edge> getOutEdges() {
216 void addOutEdge(Edge e) {
220 HashMap<IndexObject, HashSet<Update>> getLastUpdates() {
225 //Each Edge corresponds to a transition
227 Node source, destination;
228 Transition transition;
230 ArrayList<Update> updates = new ArrayList<Update>();
232 Edge(Node src, Node dst, Transition t, int evtNum, ArrayList<Update> _updates) {
234 this.destination = dst;
236 this.eventNumber = evtNum;
237 this.updates.addAll(_updates);
248 Transition getTransition() {
252 int getEventNumber() { return eventNumber; }
254 ArrayList<Update> getUpdates() {
259 static class Update {
266 Update(String _appName, String _deviceId, String _attribute, String _value, boolean _ismanual) {
267 this.appName = _appName;
268 this.deviceId = _deviceId;
269 this.attribute = _attribute;
271 this.ismanual = _ismanual;
282 String getDeviceId() {
286 String getAttribute() {
294 //Gets an index object for indexing updates by just device and attribute
295 IndexObject getIndex() {
296 return new IndexObject(this);
299 public boolean equals(Object o) {
300 if (!(o instanceof Update))
303 return (getDeviceId().equals(u.getDeviceId()) &&
304 getAttribute().equals(u.getAttribute()) &&
305 getApp().equals(u.getApp()) &&
306 getValue().equals(u.getValue()));
309 public int hashCode() {
310 return (getDeviceId().hashCode() << 3) ^
311 (getAttribute().hashCode() << 2) ^
312 (getApp().hashCode() << 1) ^
313 getValue().hashCode();
316 public String toString() {
317 return "<"+getAttribute()+", "+getValue()+", "+getApp()+", "+ismanual+">";
321 static class IndexObject {
323 IndexObject(Update _u) {
327 public boolean equals(Object o) {
328 if (!(o instanceof IndexObject))
330 IndexObject io=(IndexObject)o;
331 return (u.getDeviceId().equals(io.u.getDeviceId()) &&
332 u.getAttribute().equals(io.u.getAttribute()));
334 public int hashCode() {
335 return (u.getDeviceId().hashCode() << 1) ^ u.getAttribute().hashCode();
340 public void stateRestored(Search search) {
341 id = search.getStateId();
342 depth = search.getDepth();
343 operation = "restored";
346 out.println("The state is restored to state with id: "+id+", depth: "+depth);
348 // Update the parent node
349 parentNode = getNode(id);
353 public void searchStarted(Search search) {
354 out.println("----------------------------------- search started");
357 private Node getNode(Integer id) {
358 if (!nodes.containsKey(id))
359 nodes.put(id, new Node(id));
360 return nodes.get(id);
363 public void printGraph() {
364 System.out.println("digraph testgraph {");
365 for(Integer i : nodes.keySet()) {
366 Node n = nodes.get(i);
367 System.out.print("N"+i+"[label=\"");
369 for(IndexObject io:n.lastUpdates.keySet()) {
370 for(Update u:n.lastUpdates.get(io)) {
371 System.out.print(u.toString().replace("\"", "\\\"")+", ");
374 System.out.println("\"];");
375 for(Edge e:n.outEdges) {
376 System.out.print("N"+e.getSrc().getId()+"->N"+e.getDst().getId()+"[label=\"");
377 for(Update u:e.getUpdates()) {
378 System.out.print(u.toString().replace("\"", "\\\"")+", ");
380 System.out.println("\"];");
384 System.out.println("}");
388 public void choiceGeneratorAdvanced(VM vm, ChoiceGenerator<?> currentCG) {
390 if (currentCG instanceof IntChoiceFromSet) {
391 IntChoiceFromSet icsCG = (IntChoiceFromSet) currentCG;
392 currentEvent = icsCG.getNextChoice();
397 public void stateAdvanced(Search search) {
398 String theEnd = null;
399 Transition transition = search.getTransition();
400 id = search.getStateId();
401 depth = search.getDepth();
402 operation = "forward";
404 // Add the node to the list of nodes
405 Node currentNode = getNode(id);
407 // Create an edge based on the current transition
408 Edge newEdge = createEdge(parentNode, currentNode, transition, currentEvent);
410 // Reset the temporary variables and flags
414 // If we have a new Edge, check for conflicts
416 propagateChange(newEdge);
418 if (search.isNewState()) {
424 if (search.isEndState()) {
425 out.println("This is the last state!");
429 out.println("The state is forwarded to state with id: "+id+", depth: "+depth+" which is "+detail+" state: "+"% "+theEnd);
431 // Update the parent node
432 parentNode = currentNode;
436 public void stateBacktracked(Search search) {
437 id = search.getStateId();
438 depth = search.getDepth();
439 operation = "backtrack";
442 out.println("The state is backtracked to state with id: "+id+", depth: "+depth);
444 // Update the parent node
445 parentNode = getNode(id);
449 public void searchFinished(Search search) {
450 out.println("----------------------------------- search finished");
452 //Comment out the following line to print the explored graph
456 private String getValue(ThreadInfo ti, Instruction inst, byte type) {
460 frame = ti.getTopFrame();
462 if ((inst instanceof JVMLocalVariableInstruction) ||
463 (inst instanceof JVMFieldInstruction))
465 if (frame.getTopPos() < 0)
469 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
471 // TODO: Fix for integer values (need to dig deeper into the stack frame to find the right value other than 0)
472 // TODO: Seems to be a problem since this is Groovy (not Java)
473 if (type == Types.T_INT || type == Types.T_LONG || type == Types.T_SHORT) {
476 lo = frame.peek(offset);
481 return(decodeValue(type, lo, hi));
484 if (inst instanceof JVMArrayElementInstruction)
485 return(getArrayValue(ti, type));
490 private final static String decodeValue(byte type, int lo, int hi) {
492 case Types.T_ARRAY: return(null);
493 case Types.T_VOID: return(null);
495 case Types.T_BOOLEAN: return(String.valueOf(Types.intToBoolean(lo)));
496 case Types.T_BYTE: return(String.valueOf(lo));
497 case Types.T_CHAR: return(String.valueOf((char) lo));
498 case Types.T_DOUBLE: return(String.valueOf(Types.intsToDouble(lo, hi)));
499 case Types.T_FLOAT: return(String.valueOf(Types.intToFloat(lo)));
500 case Types.T_INT: return(String.valueOf(lo));
501 case Types.T_LONG: return(String.valueOf(Types.intsToLong(lo, hi)));
502 case Types.T_SHORT: return(String.valueOf(lo));
504 case Types.T_REFERENCE:
505 ElementInfo ei = VM.getVM().getHeap().get(lo);
509 ClassInfo ci = ei.getClassInfo();
513 if (ci.getName().equals("java.lang.String"))
514 return('"' + ei.asString() + '"');
516 return(ei.toString());
519 System.err.println("Unknown type: " + type);
524 private String getArrayValue(ThreadInfo ti, byte type) {
528 frame = ti.getTopFrame();
530 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
532 return(decodeValue(type, lo, hi));
535 private byte getType(ThreadInfo ti, Instruction inst) {
540 frame = ti.getTopFrame();
541 if ((frame.getTopPos() >= 0) && (frame.isOperandRef())) {
542 return (Types.T_REFERENCE);
547 if (inst instanceof JVMLocalVariableInstruction) {
548 type = ((JVMLocalVariableInstruction) inst).getLocalVariableType();
549 } else if (inst instanceof JVMFieldInstruction){
550 fi = ((JVMFieldInstruction) inst).getFieldInfo();
554 if (inst instanceof JVMArrayElementInstruction) {
555 return (getTypeFromInstruction(inst));
559 return (Types.T_VOID);
562 return (decodeType(type));
565 private final static byte getTypeFromInstruction(Instruction inst) {
566 if (inst instanceof JVMArrayElementInstruction)
567 return(getTypeFromInstruction((JVMArrayElementInstruction) inst));
569 return(Types.T_VOID);
572 private final static byte decodeType(String type) {
573 if (type.charAt(0) == '?'){
574 return(Types.T_REFERENCE);
576 return Types.getBuiltinType(type);
580 // Find the variable writer
581 // It should be one of the apps listed in the .jpf file
582 private String getWriter(List<StackFrame> sfList, HashSet<String> writerSet) {
583 // Start looking from the top of the stack backward
584 for(int i=sfList.size()-1; i>=0; i--) {
585 MethodInfo mi = sfList.get(i).getMethodInfo();
586 if(!mi.isJPFInternal()) {
587 String method = mi.getStackTraceName();
588 // Check against the writers in the writerSet
589 for(String writer : writerSet) {
590 if (method.contains(writer)) {
600 private void writeWriterAndValue(String writer, String attribute, String value) {
601 Update u = new Update(writer, "DEVICE", attribute, value, manual);
606 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
608 if (System.currentTimeMillis() - startTime > timeout) {
609 StringBuilder sbTimeOut = new StringBuilder();
610 sbTimeOut.append("Execution timeout: " + (timeout / (60 * 1000)) + " minutes have passed!");
611 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sbTimeOut.toString());
612 ti.setNextPC(nextIns);
617 StringBuilder sb = new StringBuilder();
618 sb.append(errorMessage);
619 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
620 ti.setNextPC(nextIns);
622 // Check if we are ready to pop the values
624 byte type = getType(ti, executedInsn);
625 varName = getValue(ti, executedInsn, type);
626 String writer = getWriter(ti.getStack(), appSet);
629 } else if (popRef == 1) {
630 byte type = getType(ti, executedInsn);
631 String value = getValue(ti, executedInsn, type);
632 String writer = getWriter(ti.getStack(), appSet);
634 for (String var: conflictSet) {
635 if (varName.contains(var)) {
637 writeWriterAndValue(writer, varName, value);
645 if (executedInsn.getMnemonic().equals("getfield")) {
646 if (executedInsn.toString().contains("deviceValueSmartThing") ||
647 executedInsn.toString().contains("deviceIntValueSmartThing")) {
648 if (executedInsn.getNext() != null) {
649 if (executedInsn.getNext().getMnemonic().contains("load")) {
651 if (executedInsn.getNext().getNext() != null)
652 if (executedInsn.getNext().getNext().getMnemonic().contains("load"))
659 if (executedInsn instanceof WriteInstruction) {
660 String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
661 // Check if we have an update to isManualTransaction to update manual field
662 if (varId.contains("isManualTransaction")) {
663 byte type = getType(ti, executedInsn);
664 String value = getValue(ti, executedInsn, type);
666 manual = (value.equals("true"))?true:false;