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.ListenerAdapter;
22 import gov.nasa.jpf.jvm.bytecode.*;
23 import gov.nasa.jpf.vm.*;
24 import gov.nasa.jpf.vm.bytecode.LocalVariableInstruction;
25 import gov.nasa.jpf.vm.bytecode.ReadInstruction;
26 import gov.nasa.jpf.vm.bytecode.StoreInstruction;
27 import gov.nasa.jpf.vm.bytecode.WriteInstruction;
31 // TODO: Fix for Groovy's model-checking
32 // TODO: This is a listener created to detect device conflicts and global variable conflicts
34 * Simple listener tool to track conflicts between 2 apps.
35 * A conflict is defined as one app trying to change the state of a variable
36 * into its opposite value after being set by the other app,
37 * e.g., app1 attempts to change variable A to false after A has been set by app2 to true earlier.
39 public class VariableConflictTracker extends ListenerAdapter {
41 private final HashMap<String, VarChange> writeMap = new HashMap<>();
42 private final HashMap<String, VarChange> readMap = new HashMap<>();
43 private final HashSet<String> conflictSet = new HashSet<>();
44 private final HashSet<String> appSet = new HashSet<>();
45 private boolean trackLocationVar;
47 private long startTime;
49 private final String SET_LOCATION_METHOD = "setLocationMode";
50 private final String LOCATION_VAR = "location.mode";
52 public VariableConflictTracker(Config config) {
53 String[] conflictVars = config.getStringArray("variables");
54 // We are not tracking anything if it is null
55 if (conflictVars != null) {
56 for (String var : conflictVars) {
60 String[] apps = config.getStringArray("apps");
61 // We are not tracking anything if it is null
63 for (String var : apps) {
67 trackLocationVar = config.getBoolean("track_location_var_conflict", false);
68 // Timeout input from config is in minutes, so we need to convert into millis
69 timeout = config.getInt("timeout", 0) * 60 * 1000;
70 startTime = System.currentTimeMillis();
74 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
75 // Instantiate timeoutTimer
77 if (System.currentTimeMillis() - startTime > timeout) {
78 StringBuilder sb = new StringBuilder();
79 sb.append("Execution timeout: " + (timeout / (60 * 1000)) + " minutes have passed!");
80 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
81 ti.setNextPC(nextIns);
85 // CASE #1: Detecting variable write-after-write conflict
86 if (executedInsn instanceof WriteInstruction) {
87 // Check for write-after-write conflict
88 String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
89 for(String var : conflictSet) {
91 if (varId.contains(var)) {
93 byte type = getType(ti, executedInsn);
94 String value = getValue(ti, executedInsn, type);
95 //System.out.println("\n\n" + ti.getStackTrace() + "\n\n");
96 String writer = getWriter(ti.getStack());
97 // Just return if the writer is not one of the listed apps in the .jpf file
101 // Check and throw error if conflict is detected
102 checkWriteMapAndThrowError(var, value, writer, ti);
107 // CASE #2: Detecting global variable location.mode write-after-write conflict
108 if (trackLocationVar) {
109 MethodInfo mi = executedInsn.getMethodInfo();
110 // Find the last load before return and get the value here
111 if (mi.getName().equals(SET_LOCATION_METHOD) &&
112 executedInsn instanceof ALOAD && nextInsn instanceof ARETURN) {
113 byte type = getType(ti, executedInsn);
114 String value = getValue(ti, executedInsn, type);
116 // Extract the writer app name
117 ClassInfo ci = mi.getClassInfo();
118 String writer = ci.getName();
120 // Check and throw error if conflict is detected
121 checkWriteMapAndThrowError(LOCATION_VAR, value, writer, ti);
126 private void checkWriteMapAndThrowError(String var, String value, String writer, ThreadInfo ti) {
128 if (writeMap.containsKey(var)) {
129 // Subsequent writes to the variable
130 VarChange current = writeMap.get(var);
131 if (current.writer != writer) {
132 // Conflict is declared when:
133 // 1) Current writer != previous writer, e.g., App1 vs. App2
134 // 2) Current value != previous value, e.g., "locked" vs. "unlocked"
135 if (!current.value.equals(value)) {
137 StringBuilder sb = new StringBuilder();
138 sb.append("Conflict between apps " + current.writer + " and " + writer + ": ");
139 sb.append(writer + " has attempted to write the value " + value + " into ");
140 sb.append("variable " + var + " that had already had the value " + current.value);
141 sb.append(" previously written by " + current.writer);
142 Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
143 ti.setNextPC(nextIns);
146 // No conflict is declared if this variable is written subsequently by the same writer
147 current.writer = writer;
148 current.value = value;
151 // First write to the variable only if it is not null
153 VarChange change = new VarChange(writer, value);
154 writeMap.put(var, change);
163 VarChange(String writer, String value) {
164 this.writer = writer;
169 // Find the variable writer
170 // It should be one of the apps listed in the .jpf file
171 private String getWriter(List<StackFrame> sfList) {
172 // Start looking from the top of the stack backward
173 for(int i=sfList.size()-1; i>=0; i--) {
174 MethodInfo mi = sfList.get(i).getMethodInfo();
175 if(!mi.isJPFInternal()) {
176 String method = mi.getStackTraceName();
177 // Check against the apps in the appSet
178 for(String app : appSet) {
179 // There is only one writer at a time but we need to always
180 // check all the potential writers in the list
181 if (method.contains(app)) {
191 private byte getType(ThreadInfo ti, Instruction inst) {
196 frame = ti.getTopFrame();
197 if ((frame.getTopPos() >= 0) && (frame.isOperandRef())) {
198 return (Types.T_REFERENCE);
203 if (inst instanceof JVMLocalVariableInstruction) {
204 type = ((JVMLocalVariableInstruction) inst).getLocalVariableType();
205 } else if (inst instanceof JVMFieldInstruction){
206 fi = ((JVMFieldInstruction) inst).getFieldInfo();
210 if (inst instanceof JVMArrayElementInstruction) {
211 return (getTypeFromInstruction(inst));
215 return (Types.T_VOID);
218 return (decodeType(type));
221 private final static byte getTypeFromInstruction(Instruction inst) {
222 if (inst instanceof JVMArrayElementInstruction)
223 return(getTypeFromInstruction((JVMArrayElementInstruction) inst));
225 return(Types.T_VOID);
228 private final static byte getTypeFromInstruction(JVMArrayElementInstruction inst) {
231 name = inst.getClass().getName();
232 name = name.substring(name.lastIndexOf('.') + 1);
234 switch (name.charAt(0)) {
235 case 'A': return(Types.T_REFERENCE);
236 case 'B': return(Types.T_BYTE); // Could be a boolean but it is better to assume a byte.
237 case 'C': return(Types.T_CHAR);
238 case 'F': return(Types.T_FLOAT);
239 case 'I': return(Types.T_INT);
240 case 'S': return(Types.T_SHORT);
241 case 'D': return(Types.T_DOUBLE);
242 case 'L': return(Types.T_LONG);
245 return(Types.T_VOID);
248 private final static String encodeType(byte type) {
250 case Types.T_BYTE: return("B");
251 case Types.T_CHAR: return("C");
252 case Types.T_DOUBLE: return("D");
253 case Types.T_FLOAT: return("F");
254 case Types.T_INT: return("I");
255 case Types.T_LONG: return("J");
256 case Types.T_REFERENCE: return("L");
257 case Types.T_SHORT: return("S");
258 case Types.T_VOID: return("V");
259 case Types.T_BOOLEAN: return("Z");
260 case Types.T_ARRAY: return("[");
266 private final static byte decodeType(String type) {
267 if (type.charAt(0) == '?'){
268 return(Types.T_REFERENCE);
270 return Types.getBuiltinType(type);
274 private String getName(ThreadInfo ti, Instruction inst, byte type) {
279 if ((inst instanceof JVMLocalVariableInstruction) ||
280 (inst instanceof JVMFieldInstruction)) {
281 name = ((LocalVariableInstruction) inst).getVariableId();
282 name = name.substring(name.lastIndexOf('.') + 1);
287 if (inst instanceof JVMArrayElementInstruction) {
288 store = inst instanceof StoreInstruction;
289 name = getArrayName(ti, type, store);
290 index = getArrayIndex(ti, type, store);
291 return(name + '[' + index + ']');
297 private String getValue(ThreadInfo ti, Instruction inst, byte type) {
301 frame = ti.getTopFrame();
303 if ((inst instanceof JVMLocalVariableInstruction) ||
304 (inst instanceof JVMFieldInstruction))
306 if (frame.getTopPos() < 0)
310 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
312 return(decodeValue(type, lo, hi));
315 if (inst instanceof JVMArrayElementInstruction)
316 return(getArrayValue(ti, type));
321 private String getArrayName(ThreadInfo ti, byte type, boolean store) {
325 offset = calcOffset(type, store) + 1;
326 // <2do> String is really not a good attribute type to retrieve!
327 StackFrame frame = ti.getTopFrame();
328 attr = frame.getOperandAttr( offset, String.class);
337 private int getArrayIndex(ThreadInfo ti, byte type, boolean store) {
340 offset = calcOffset(type, store);
342 return(ti.getTopFrame().peek(offset));
345 private final static int calcOffset(byte type, boolean store) {
349 return(Types.getTypeSize(type));
352 private String getArrayValue(ThreadInfo ti, byte type) {
356 frame = ti.getTopFrame();
358 hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
360 return(decodeValue(type, lo, hi));
363 private final static String decodeValue(byte type, int lo, int hi) {
365 case Types.T_ARRAY: return(null);
366 case Types.T_VOID: return(null);
368 case Types.T_BOOLEAN: return(String.valueOf(Types.intToBoolean(lo)));
369 case Types.T_BYTE: return(String.valueOf(lo));
370 case Types.T_CHAR: return(String.valueOf((char) lo));
371 case Types.T_DOUBLE: return(String.valueOf(Types.intsToDouble(lo, hi)));
372 case Types.T_FLOAT: return(String.valueOf(Types.intToFloat(lo)));
373 case Types.T_INT: return(String.valueOf(lo));
374 case Types.T_LONG: return(String.valueOf(Types.intsToLong(lo, hi)));
375 case Types.T_SHORT: return(String.valueOf(lo));
377 case Types.T_REFERENCE:
378 ElementInfo ei = VM.getVM().getHeap().get(lo);
382 ClassInfo ci = ei.getClassInfo();
386 if (ci.getName().equals("java.lang.String"))
387 return('"' + ei.asString() + '"');
389 return(ei.toString());
392 System.err.println("Unknown type: " + type);