You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LocalVariables.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package org.aspectj.apache.bcel.verifier.utility;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import org.aspectj.apache.bcel.generic.Type;
  56. import org.aspectj.apache.bcel.generic.ReferenceType;
  57. import org.aspectj.apache.bcel.verifier.exc.*;
  58. /**
  59. * This class implements an array of local variables used for symbolic JVM simulation.
  60. *
  61. * @version $Id$
  62. * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
  63. */
  64. public class LocalVariables {
  65. // The Type[] containing the local variable slots
  66. private Type[] locals;
  67. // Creates a new LocalVariables object
  68. public LocalVariables(int maxLocals){
  69. locals = new Type[maxLocals];
  70. for (int i=0; i<maxLocals; i++) locals[i] = Type.TOP;
  71. }
  72. /** Returns the type of the local variable slot i */
  73. public Type get(int i) {
  74. return locals[i];
  75. }
  76. /**
  77. * Returns a (correctly typed) clone of this object.
  78. * This is equivalent to ((LocalVariables) this.clone()).
  79. */
  80. public LocalVariables getClone(){
  81. return (LocalVariables) this.clone();
  82. }
  83. /**
  84. * Returns the number of local variable slots this
  85. * LocalVariables instance has.
  86. */
  87. public int maxLocals(){
  88. return locals.length;
  89. }
  90. /**
  91. * Sets a new Type for the given local variable slot.
  92. */
  93. public void set(int i, Type type){
  94. if (type == Type.BYTE || type == Type.SHORT || type == Type.BOOLEAN || type == Type.CHAR){
  95. throw new AssertionViolatedException("LocalVariables do not know about '"+type+"'. Use Type.INT instead.");
  96. }
  97. locals[i] = type;
  98. // if (type!=Type.UNKNOWN) numberThatHaveBeenSet=i;
  99. }
  100. /*
  101. * Fulfills the general contract of Object.equals().
  102. */
  103. public boolean equals(Object o){
  104. if (!(o instanceof LocalVariables)) return false;
  105. LocalVariables lv = (LocalVariables) o;
  106. if (this.locals.length != lv.locals.length) return false;
  107. for (int i=0; i<this.locals.length; i++){
  108. if (!this.locals[i].equals(lv.locals[i])){
  109. //System.out.println(this.locals[i]+" is not "+lv.locals[i]);
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * Merges two local variables sets as described in the Java Virtual Machine Specification,
  117. * Second Edition, section 4.9.2, page 146.
  118. */
  119. public void merge(LocalVariables lv){
  120. if (this.locals.length != lv.locals.length){
  121. throw new AssertionViolatedException("Merging LocalVariables of different size?!? From different methods or what?!?");
  122. }
  123. for (int i=0; i<locals.length; i++){
  124. merge(lv, i);
  125. }
  126. }
  127. /**
  128. * Merges a single local variable.
  129. *
  130. * @see #merge(LocalVariables)
  131. */
  132. private void merge(LocalVariables lv, int i){
  133. // We won't accept an unitialized object if we know it was initialized;
  134. // compare vmspec2, 4.9.4, last paragraph.
  135. if ( (!(locals[i] instanceof UninitializedObjectType)) && (lv.locals[i] instanceof UninitializedObjectType) ){
  136. throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected.");
  137. }
  138. // Even harder, what about _different_ uninitialized object types?!
  139. if ( (!(locals[i].equals(lv.locals[i]))) && (locals[i] instanceof UninitializedObjectType) && (lv.locals[i] instanceof UninitializedObjectType) ){
  140. throw new StructuralCodeConstraintException("Backwards branch with an uninitialized object in the local variables detected.");
  141. }
  142. // If we just didn't know that it was initialized, we have now learned.
  143. if (locals[i] instanceof UninitializedObjectType){
  144. if (! (lv.locals[i] instanceof UninitializedObjectType)){
  145. locals[i] = ((UninitializedObjectType) locals[i]).getInitialized();
  146. }
  147. }
  148. if ((locals[i] instanceof ReferenceType) && (lv.locals[i] instanceof ReferenceType)){
  149. if (! locals[i].equals(lv.locals[i])){ // needed in case of two UninitializedObjectType instances
  150. Type sup = ((ReferenceType) locals[i]).getFirstCommonSuperclass((ReferenceType) (lv.locals[i]));
  151. if (sup != null){
  152. locals[i] = sup;
  153. }
  154. else{
  155. // We should have checked this in Pass2!
  156. throw new AssertionViolatedException("Could not load all the super classes of '"+locals[i]+"' and '"+lv.locals[i]+"'.");
  157. }
  158. }
  159. }
  160. else{
  161. if (! (locals[i].equals(lv.locals[i])) ){
  162. /*TODO
  163. if ((locals[i] instanceof org.aspectj.apache.bcel.generic.ReturnaddressType) && (lv.locals[i] instanceof org.aspectj.apache.bcel.generic.ReturnaddressType)){
  164. //System.err.println("merging "+locals[i]+" and "+lv.locals[i]);
  165. throw new AssertionViolatedException("Merging different ReturnAddresses: '"+locals[i]+"' and '"+lv.locals[i]+"'.");
  166. }
  167. */
  168. locals[i] = Type.TOP;
  169. }
  170. }
  171. }
  172. /**
  173. * Returns a String representation of this object.
  174. */
  175. public String toString(){
  176. String s = new String();
  177. for (int i=0; i<locals.length; i++){
  178. s += Integer.toString(i)+": "+locals[i]+"\n";
  179. }
  180. return s;
  181. }
  182. /**
  183. * Return a more compact string than toString()
  184. */
  185. public String toCompactString() {
  186. StringBuffer sb = new StringBuffer();
  187. sb.append("[").append(maxLocals()).append(":");
  188. for (int i=0;i<locals.length;i++) sb.append(i).append("=").append(compactName(locals[i])).append(" ");
  189. sb.append("]");
  190. return sb.toString();
  191. }
  192. public String compactName(Type type) {
  193. if (type==Type.UNKNOWN) return "<Unknown>";
  194. String s = type.toString();
  195. int pos = s.lastIndexOf(".");
  196. if (pos==-1) return s;
  197. return s.substring(pos+1);
  198. }
  199. /**
  200. * Replaces all occurences of u in this local variables set
  201. * with an "initialized" ObjectType.
  202. */
  203. public void initializeObject(UninitializedObjectType u){
  204. for (int i=0; i<locals.length; i++){
  205. if (locals[i] == u){
  206. locals[i] = u.getInitialized();
  207. }
  208. }
  209. }
  210. /**
  211. * Returns a deep copy of this object; i.e. the clone
  212. * operates on a new local variable array.
  213. * However, the Type objects in the array are shared.
  214. */
  215. protected Object clone(){
  216. LocalVariables lvs = new LocalVariables(locals.length);
  217. for (int i=0; i<locals.length; i++){
  218. lvs.locals[i] = this.locals[i];
  219. }
  220. return lvs;
  221. }
  222. public void flush() {
  223. // reset them all back to unknown!
  224. for (int i=0; i<locals.length; i++) locals[i] = Type.TOP;
  225. }
  226. public int getNextUnset() {
  227. return numberThatHaveBeenSet;
  228. }
  229. public void haveSet(int i) {
  230. // Called so we can remember where the end is
  231. numberThatHaveBeenSet=i;
  232. }
  233. private int numberThatHaveBeenSet;
  234. // public int getFirstUnknown() {
  235. // for (int i = 0; i < locals.length; i++) {
  236. // if (locals[i].equals(Type.UNKNOWN)) return i;
  237. // }
  238. // return -1;
  239. // }
  240. }