1. package com.sun.org.apache.bcel.internal.verifier.statics;
  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 com.sun.org.apache.bcel.internal.classfile.*;
  56. import com.sun.org.apache.bcel.internal.Repository;
  57. import com.sun.org.apache.bcel.internal.verifier.*;
  58. import com.sun.org.apache.bcel.internal.verifier.exc.*;
  59. import com.sun.org.apache.bcel.internal.verifier.exc.Utility;
  60. import java.util.ArrayList;
  61. /**
  62. * This PassVerifier verifies a class file according to pass 1 as
  63. * described in The Java Virtual Machine Specification, 2nd edition.
  64. * More detailed information is to be found at the do_verify() method's
  65. * documentation.
  66. *
  67. * @version $Id: Pass1Verifier.java,v 1.1.1.1 2001/10/29 20:00:35 jvanzyl Exp $
  68. * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
  69. * @see #do_verify()
  70. */
  71. public final class Pass1Verifier extends PassVerifier{
  72. /**
  73. * DON'T USE THIS EVEN PRIVATELY! USE getJavaClass() INSTEAD.
  74. * @see #getJavaClass()
  75. */
  76. private JavaClass jc;
  77. /**
  78. * The Verifier that created this.
  79. */
  80. private Verifier myOwner;
  81. /** Used to load in and return the myOwner-matching JavaClass object when needed. Avoids loading in a class file when it's not really needed! */
  82. private JavaClass getJavaClass(){
  83. if (jc == null){
  84. jc = Repository.lookupClass(myOwner.getClassName());
  85. }
  86. return jc;
  87. }
  88. /**
  89. * Should only be instantiated by a Verifier.
  90. *
  91. * @see com.sun.org.apache.bcel.internal.verifier.Verifier
  92. */
  93. public Pass1Verifier(Verifier owner){
  94. myOwner = owner;
  95. }
  96. /**
  97. * Pass-one verification basically means loading in a class file.
  98. * The Java Virtual Machine Specification is not too precise about
  99. * what makes the difference between passes one and two.
  100. * The answer is that only pass one is performed on a class file as
  101. * long as its resolution is not requested; whereas pass two and
  102. * pass three are performed during the resolution process.
  103. * Only four constraints to be checked are explicitely stated by
  104. * The Java Virtual Machine Specification, 2nd edition:
  105. * <UL>
  106. * <LI>The first four bytes must contain the right magic number (0xCAFEBABE).
  107. * <LI>All recognized attributes must be of the proper length.
  108. * <LI>The class file must not be truncated or have extra bytes at the end.
  109. * <LI>The constant pool must not contain any superficially unrecognizable information.
  110. * </UL>
  111. * A more in-depth documentation of what pass one should do was written by
  112. * <A HREF=mailto:pwfong@cs.sfu.ca>Philip W. L. Fong</A>:
  113. * <UL>
  114. * <LI> the file should not be truncated.
  115. * <LI> the file should not have extra bytes at the end.
  116. * <LI> all variable-length structures should be well-formatted:
  117. * <UL>
  118. * <LI> there should only be constant_pool_count-1 many entries in the constant pool.
  119. * <LI> all constant pool entries should have size the same as indicated by their type tag.
  120. * <LI> there are exactly interfaces_count many entries in the interfaces array of the class file.
  121. * <LI> there are exactly fields_count many entries in the fields array of the class file.
  122. * <LI> there are exactly methods_count many entries in the methods array of the class file.
  123. * <LI> there are exactly attributes_count many entries in the attributes array of the class file, fields, methods, and code attribute.
  124. * <LI> there should be exactly attribute_length many bytes in each attribute. Inconsistency between attribute_length and the actually size of the attribute content should be uncovered. For example, in an Exceptions attribute, the actual number of exceptions as required by the number_of_exceptions field might yeild an attribute size that doesn't match the attribute_length. Such an anomaly should be detected.
  125. * <LI> all attributes should have proper length. In particular, under certain context (e.g. while parsing method_info), recognizable attributes (e.g. "Code" attribute) should have correct format (e.g. attribute_length is 2).
  126. * </UL>
  127. * <LI> Also, certain constant values are checked for validity:
  128. * <UL>
  129. * <LI> The magic number should be 0xCAFEBABE.
  130. * <LI> The major and minor version numbers are valid.
  131. * <LI> All the constant pool type tags are recognizable.
  132. * <LI> All undocumented access flags are masked off before use. Strictly speaking, this is not really a check.
  133. * <LI> The field this_class should point to a string that represents a legal non-array class name, and this name should be the same as the class file being loaded.
  134. * <LI> the field super_class should point to a string that represents a legal non-array class name.
  135. * <LI> Because some of the above checks require cross referencing the constant pool entries, guards are set up to make sure that the referenced entries are of the right type and the indices are within the legal range (0 < index < constant_pool_count).
  136. * </UL>
  137. * <LI> Extra checks done in pass 1:
  138. * <UL>
  139. * <LI> the constant values of static fields should have the same type as the fields.
  140. * <LI> the number of words in a parameter list does not exceed 255 and locals_max.
  141. * <LI> the name and signature of fields and methods are verified to be of legal format.
  142. * </UL>
  143. * </UL>
  144. * (From the Paper <A HREF=http://www.cs.sfu.ca/people/GradStudents/pwfong/personal/JVM/pass1/>The Mysterious Pass One, first draft, September 2, 1997</A>.)
  145. * </BR>
  146. * However, most of this is done by parsing a class file or generating a class file into BCEL's internal data structure.
  147. * <B>Therefore, all that is really done here is look up the class file from BCEL's repository.</B>
  148. * This is also motivated by the fact that some omitted things
  149. * (like the check for extra bytes at the end of the class file) are handy when actually using BCEL to repair a class file (otherwise you would not be
  150. * able to load it into BCEL).
  151. *
  152. * @see com.sun.org.apache.bcel.internal.Repository
  153. */
  154. public VerificationResult do_verify(){
  155. JavaClass jc;
  156. try{
  157. jc = getJavaClass(); //loads in the class file if not already done.
  158. if (jc != null){
  159. /* If we find more constraints to check, we should do this in an own method. */
  160. if (! myOwner.getClassName().equals(jc.getClassName())){
  161. // This should maybe caught by BCEL: In case of renamed .class files we get wrong
  162. // JavaClass objects here.
  163. throw new LoadingException("Wrong name: the internal name of the .class file '"+jc.getClassName()+"' does not match the file's name '"+myOwner.getClassName()+"'.");
  164. }
  165. }
  166. }
  167. catch(LoadingException e){
  168. return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
  169. }
  170. catch(ClassFormatError e){
  171. // BCEL sometimes is a little harsh describing exceptual situations.
  172. return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
  173. }
  174. catch(RuntimeException e){
  175. // BCEL does not catch every possible RuntimeException; e.g. if
  176. // a constant pool index is referenced that does not exist.
  177. return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Parsing via BCEL did not succeed. "+e.getClass().getName()+" occured:\n"+Utility.getStackTrace(e));
  178. }
  179. if (jc != null){
  180. return VerificationResult.VR_OK;
  181. }
  182. else{
  183. //TODO: Maybe change Repository's behaviour to throw a LoadingException instead of just returning "null"
  184. // if a class file cannot be found or in another way be looked up.
  185. return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Repository.lookup() failed. FILE NOT FOUND?");
  186. }
  187. }
  188. /**
  189. * Currently this returns an empty array of String.
  190. * One could parse the error messages of BCEL
  191. * (written to java.lang.System.err) when loading
  192. * a class file such as detecting unknown attributes
  193. * or trailing garbage at the end of a class file.
  194. * However, Markus Dahm does not like the idea so this
  195. * method is currently useless and therefore marked as
  196. * <B>TODO</B>.
  197. */
  198. public String[] getMessages(){
  199. // This method is only here to override the javadoc-comment.
  200. return super.getMessages();
  201. }
  202. }