KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > verifier > statics > Pass1Verifier


1 package com.sun.org.apache.bcel.internal.verifier.statics;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.classfile.*;
58 import com.sun.org.apache.bcel.internal.Repository;
59 import com.sun.org.apache.bcel.internal.verifier.*;
60 import com.sun.org.apache.bcel.internal.verifier.exc.*;
61 import com.sun.org.apache.bcel.internal.verifier.exc.Utility;
62 import java.util.ArrayList JavaDoc;
63
64 /**
65  * This PassVerifier verifies a class file according to pass 1 as
66  * described in The Java Virtual Machine Specification, 2nd edition.
67  * More detailed information is to be found at the do_verify() method's
68  * documentation.
69  *
70  * @version $Id: Pass1Verifier.java,v 1.1.1.1 2001/10/29 20:00:35 jvanzyl Exp $
71  * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
72  * @see #do_verify()
73  */

74 public final class Pass1Verifier extends PassVerifier{
75     /**
76      * DON'T USE THIS EVEN PRIVATELY! USE getJavaClass() INSTEAD.
77      * @see #getJavaClass()
78      */

79     private JavaClass jc;
80
81     /**
82      * The Verifier that created this.
83      */

84     private Verifier myOwner;
85
86     /** 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! */
87     private JavaClass getJavaClass(){
88         if (jc == null){
89             jc = Repository.lookupClass(myOwner.getClassName());
90         }
91         return jc;
92     }
93     
94     /**
95      * Should only be instantiated by a Verifier.
96      *
97      * @see com.sun.org.apache.bcel.internal.verifier.Verifier
98      */

99     public Pass1Verifier(Verifier owner){
100         myOwner = owner;
101     }
102
103     /**
104      * Pass-one verification basically means loading in a class file.
105      * The Java Virtual Machine Specification is not too precise about
106      * what makes the difference between passes one and two.
107      * The answer is that only pass one is performed on a class file as
108      * long as its resolution is not requested; whereas pass two and
109      * pass three are performed during the resolution process.
110      * Only four constraints to be checked are explicitely stated by
111      * The Java Virtual Machine Specification, 2nd edition:
112      * <UL>
113      * <LI>The first four bytes must contain the right magic number (0xCAFEBABE).
114      * <LI>All recognized attributes must be of the proper length.
115      * <LI>The class file must not be truncated or have extra bytes at the end.
116      * <LI>The constant pool must not contain any superficially unrecognizable information.
117      * </UL>
118      * A more in-depth documentation of what pass one should do was written by
119      * <A HREF=mailto:pwfong@cs.sfu.ca>Philip W. L. Fong</A>:
120      * <UL>
121      * <LI> the file should not be truncated.
122      * <LI> the file should not have extra bytes at the end.
123      * <LI> all variable-length structures should be well-formatted:
124      * <UL>
125      * <LI> there should only be constant_pool_count-1 many entries in the constant pool.
126      * <LI> all constant pool entries should have size the same as indicated by their type tag.
127      * <LI> there are exactly interfaces_count many entries in the interfaces array of the class file.
128      * <LI> there are exactly fields_count many entries in the fields array of the class file.
129      * <LI> there are exactly methods_count many entries in the methods array of the class file.
130      * <LI> there are exactly attributes_count many entries in the attributes array of the class file, fields, methods, and code attribute.
131      * <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.
132      * <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).
133      * </UL>
134      * <LI> Also, certain constant values are checked for validity:
135      * <UL>
136      * <LI> The magic number should be 0xCAFEBABE.
137      * <LI> The major and minor version numbers are valid.
138      * <LI> All the constant pool type tags are recognizable.
139      * <LI> All undocumented access flags are masked off before use. Strictly speaking, this is not really a check.
140      * <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.
141      * <LI> the field super_class should point to a string that represents a legal non-array class name.
142      * <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).
143      * </UL>
144      * <LI> Extra checks done in pass 1:
145      * <UL>
146      * <LI> the constant values of static fields should have the same type as the fields.
147      * <LI> the number of words in a parameter list does not exceed 255 and locals_max.
148      * <LI> the name and signature of fields and methods are verified to be of legal format.
149      * </UL>
150      * </UL>
151      * (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>.)
152      * </BR>
153      * However, most of this is done by parsing a class file or generating a class file into BCEL's internal data structure.
154      * <B>Therefore, all that is really done here is look up the class file from BCEL's repository.</B>
155      * This is also motivated by the fact that some omitted things
156      * (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
157      * able to load it into BCEL).
158      *
159      * @see com.sun.org.apache.bcel.internal.Repository
160      */

161     public VerificationResult do_verify(){
162         JavaClass jc;
163         try{
164             jc = getJavaClass(); //loads in the class file if not already done.
165

166             if (jc != null){
167                 /* If we find more constraints to check, we should do this in an own method. */
168                 if (! myOwner.getClassName().equals(jc.getClassName())){
169                     // This should maybe caught by BCEL: In case of renamed .class files we get wrong
170
// JavaClass objects here.
171
throw new LoadingException("Wrong name: the internal name of the .class file '"+jc.getClassName()+"' does not match the file's name '"+myOwner.getClassName()+"'.");
172                 }
173             }
174             
175         }
176         catch(LoadingException e){
177             return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
178         }
179         catch(ClassFormatError JavaDoc e){
180             // BCEL sometimes is a little harsh describing exceptual situations.
181
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
182         }
183         catch(RuntimeException JavaDoc e){
184             // BCEL does not catch every possible RuntimeException; e.g. if
185
// a constant pool index is referenced that does not exist.
186
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Parsing via BCEL did not succeed. "+e.getClass().getName()+" occured:\n"+Utility.getStackTrace(e));
187         }
188
189         if (jc != null){
190             return VerificationResult.VR_OK;
191         }
192         else{
193             //TODO: Maybe change Repository's behaviour to throw a LoadingException instead of just returning "null"
194
// if a class file cannot be found or in another way be looked up.
195
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Repository.lookup() failed. FILE NOT FOUND?");
196         }
197     }
198
199     /**
200      * Currently this returns an empty array of String.
201      * One could parse the error messages of BCEL
202      * (written to java.lang.System.err) when loading
203      * a class file such as detecting unknown attributes
204      * or trailing garbage at the end of a class file.
205      * However, Markus Dahm does not like the idea so this
206      * method is currently useless and therefore marked as
207      * <B>TODO</B>.
208      */

209     public String JavaDoc[] getMessages(){
210         // This method is only here to override the javadoc-comment.
211
return super.getMessages();
212     }
213
214 }
215
Popular Tags