Fixing a subtle bug: the method isParameterWithType() did not consider arbitrary...
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / bytecode / GetHelper.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.jvm.bytecode;
20
21 import gov.nasa.jpf.vm.Instruction;
22 import gov.nasa.jpf.vm.MethodInfo;
23
24 /**
25  * helper class to factor out common GET code
26  * 
27  * <2do> This is going to be moved into a Java 8 interface with default methods
28  */
29 public class GetHelper {
30
31   /**
32    * do a little bytecode pattern analysis on the fly, to find out if a
33    * GETFIELD or GETSTATIC is just part of a "..synchronized (obj) {..} .."
34    * pattern, which usually translates into the following pattern:
35    *   ...
36    *   getfield / getstatic
37    *   dup
38    *   [astore]
39    *   monitorenter
40    *   ...
41    *
42    * If it does, there is no need to break the transition since the object
43    * reference is not used for anything that can cause violations between
44    * the get and the monitorenter.
45    *
46    * <2do> We might want to extend this in the future to also cover sync on
47    * local vars, like "Object o = myField; synchronized(o){..}..", but then
48    * the check becomes more expensive since we get interspersed aload/astore
49    * insns, and some of the locals could be used outside the sync block. Not
50    * sure if it buys much on the bottom line
51    *   
52    * <2do> this relies on javac code patterns. The dup/astore could
53    * lead to subsequent use of the object reference w/o corresponding get/putfield
54    * insns (if it's not a volatile), but this access would be either a call
55    * or a get/putfield on a share object, i.e. would be checked separately 
56    */
57   protected static boolean isMonitorEnterPrologue(MethodInfo mi, int insnIndex){
58     Instruction[] code = mi.getInstructions();
59     int off = insnIndex+1;
60
61     if (off < code.length-3) {
62       // we don't reach out further than 3 instructions
63       if (code[off] instanceof DUP) {
64         off++;
65
66         if (code[off] instanceof ASTORE) {
67           off++;
68         }
69
70         if (code[off] instanceof MONITORENTER) {
71           return true;
72         }
73       }
74     }
75     
76     return false; // if in doubt, we assume it is not part of a monitorenter code pattern
77   }
78 }