Change in Analysis
[jpf-core.git] / src / main / gov / nasa / jpf / listener / OOMEInjector.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18
19 package gov.nasa.jpf.listener;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import gov.nasa.jpf.Config;
25 import gov.nasa.jpf.JPF;
26 import gov.nasa.jpf.ListenerAdapter;
27 import gov.nasa.jpf.jvm.bytecode.JVMInvokeInstruction;
28 import gov.nasa.jpf.jvm.bytecode.NEW;
29 import gov.nasa.jpf.util.LocationSpec;
30 import gov.nasa.jpf.util.TypeSpec;
31 import gov.nasa.jpf.vm.bytecode.NewInstruction;
32 import gov.nasa.jpf.vm.ClassInfo;
33 import gov.nasa.jpf.vm.Instruction;
34 import gov.nasa.jpf.vm.VM;
35 import gov.nasa.jpf.vm.MJIEnv;
36 import gov.nasa.jpf.vm.MethodInfo;
37 import gov.nasa.jpf.vm.StackFrame;
38 import gov.nasa.jpf.vm.ThreadInfo;
39
40 /**
41  * simulator for OutOfMemoryErrors. This can be configured to either
42  * fire for a specified location range (file:line) or specified types.
43  * Ranges are transitive, i.e. everything called from within it should also
44  * trigger.  
45  * 
46  * Since our only action is to inject OutOfMemoryErrors, we don't need
47  * to implement a Property interface
48  */
49 public class OOMEInjector extends ListenerAdapter {
50
51   static class OOME {}
52   static OOME throwOOME = new OOME(); // we can reuse the same object as an attribute
53   
54   List<LocationSpec> locations = new ArrayList<LocationSpec>();
55   List<TypeSpec> types = new ArrayList<TypeSpec>();
56   
57   public OOMEInjector (Config config, JPF jpf){
58     String[] spec = config.getStringArray("oome.locations");
59     if (spec != null){
60       for (String s : spec){
61         LocationSpec locSpec = LocationSpec.createLocationSpec(s);
62         if (locSpec != null){
63           locations.add(locSpec);
64         }
65       }
66     }
67     
68     spec = config.getStringArray("oome.types");
69     if (spec != null){
70       for (String s : spec){
71         TypeSpec typeSpec = TypeSpec.createTypeSpec(s);
72         if (typeSpec != null){
73           types.add(typeSpec);
74         }
75       }      
76     }
77   }
78   
79   protected void markMatchingInstructions (MethodInfo mi, LocationSpec locSpec){
80     int first = locSpec.getFromLine();
81     int[] lineNumbers = mi.getLineNumbers();
82               
83     if (lineNumbers != null && first >= lineNumbers[0]){
84       int last = locSpec.getToLine();
85       for (int i=0; i<lineNumbers.length; i++){
86         int l = lineNumbers[i];
87         if (last < lineNumbers[i]){
88           return;
89         } else {
90           Instruction insn = mi.getInstruction(i);
91           insn.addAttr(throwOOME);                
92         }
93       }
94     }    
95   }
96   
97   @Override
98   public void classLoaded (VM vm, ClassInfo loadedClass){
99     String fname = loadedClass.getSourceFileName();
100     
101     for (TypeSpec typeSpec : types){
102       if (typeSpec.matches(loadedClass)){
103         loadedClass.addAttr(throwOOME);
104       }
105     }
106
107     // if we have a matching typespec this could be skipped, but maybe
108     // we also want to cover statis methods of this class
109     for (LocationSpec locSpec : locations){
110       if (locSpec.matchesFile(fname)){
111         for (MethodInfo mi : loadedClass.getDeclaredMethodInfos()){
112           markMatchingInstructions(mi, locSpec);
113         }
114       }
115     }
116   }
117   
118   protected boolean checkCallerForOOM (StackFrame frame, Instruction insn){
119     // these refer to the calling code
120     return (insn.hasAttr(OOME.class) || frame.hasFrameAttr(OOME.class));
121   }
122   
123   @Override
124   public void executeInstruction (VM vm, ThreadInfo ti, Instruction insnToExecute){
125     if (insnToExecute instanceof NewInstruction){
126       if (checkCallerForOOM(ti.getTopFrame(), insnToExecute)){
127         // we could use Heap.setOutOfMemory(true), but then we would have to reset
128         // if the app handles it so that it doesn't throw outside the specified locations.
129         // This would require more effort than throwing explicitly
130         Instruction nextInsn = ti.createAndThrowException("java.lang.OutOfMemoryError");
131         ti.skipInstruction(nextInsn);
132       }
133     }
134   }
135   
136   @Override
137   public void instructionExecuted (VM vm, ThreadInfo ti, Instruction insn, Instruction executedInsn){
138     
139     if (executedInsn instanceof JVMInvokeInstruction){
140       StackFrame frame = ti.getTopFrame();
141       
142       if (frame.getPC() != executedInsn){ // means the call did succeed
143         if (checkCallerForOOM(frame.getPrevious(), executedInsn)){
144           frame.addFrameAttr(throwOOME); // propagate caller OOME context
145         }
146       }
147       
148     } else if (executedInsn instanceof NEW){
149       if (!types.isEmpty()){
150         int objRef = ((NEW) executedInsn).getNewObjectRef();
151         if (objRef != MJIEnv.NULL) {
152           ClassInfo ci = vm.getClassInfo(objRef);
153           if (ci.hasAttr(OOME.class)) {
154             Instruction nextInsn = ti.createAndThrowException("java.lang.OutOfMemoryError");
155             ti.setNextPC(nextInsn);
156           }
157         }
158       }
159     }
160   }
161 }