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.
19 package gov.nasa.jpf.listener;
21 import java.util.ArrayList;
22 import java.util.List;
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;
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
46 * Since our only action is to inject OutOfMemoryErrors, we don't need
47 * to implement a Property interface
49 public class OOMEInjector extends ListenerAdapter {
52 static OOME throwOOME = new OOME(); // we can reuse the same object as an attribute
54 List<LocationSpec> locations = new ArrayList<LocationSpec>();
55 List<TypeSpec> types = new ArrayList<TypeSpec>();
57 public OOMEInjector (Config config, JPF jpf){
58 String[] spec = config.getStringArray("oome.locations");
60 for (String s : spec){
61 LocationSpec locSpec = LocationSpec.createLocationSpec(s);
63 locations.add(locSpec);
68 spec = config.getStringArray("oome.types");
70 for (String s : spec){
71 TypeSpec typeSpec = TypeSpec.createTypeSpec(s);
72 if (typeSpec != null){
79 protected void markMatchingInstructions (MethodInfo mi, LocationSpec locSpec){
80 int first = locSpec.getFromLine();
81 int[] lineNumbers = mi.getLineNumbers();
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]){
90 Instruction insn = mi.getInstruction(i);
91 insn.addAttr(throwOOME);
98 public void classLoaded (VM vm, ClassInfo loadedClass){
99 String fname = loadedClass.getSourceFileName();
101 for (TypeSpec typeSpec : types){
102 if (typeSpec.matches(loadedClass)){
103 loadedClass.addAttr(throwOOME);
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);
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));
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);
137 public void instructionExecuted (VM vm, ThreadInfo ti, Instruction insn, Instruction executedInsn){
139 if (executedInsn instanceof JVMInvokeInstruction){
140 StackFrame frame = ti.getTopFrame();
142 if (frame.getPC() != executedInsn){ // means the call did succeed
143 if (checkCallerForOOM(frame.getPrevious(), executedInsn)){
144 frame.addFrameAttr(throwOOME); // propagate caller OOME context
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);