KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > verifier > statics > Pass1Verifier


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.verifier.statics;
18
19
20 import org.apache.bcel.Repository;
21 import org.apache.bcel.classfile.ClassFormatException;
22 import org.apache.bcel.classfile.JavaClass;
23 import org.apache.bcel.verifier.PassVerifier;
24 import org.apache.bcel.verifier.VerificationResult;
25 import org.apache.bcel.verifier.Verifier;
26 import org.apache.bcel.verifier.exc.LoadingException;
27 import org.apache.bcel.verifier.exc.Utility;
28
29 /**
30  * This PassVerifier verifies a class file according to pass 1 as
31  * described in The Java Virtual Machine Specification, 2nd edition.
32  * More detailed information is to be found at the do_verify() method's
33  * documentation.
34  *
35  * @version $Id: Pass1Verifier.java 371539 2006-01-23 14:08:00Z tcurdt $
36  * @author Enver Haase
37  * @see #do_verify()
38  */

39 public final class Pass1Verifier extends PassVerifier{
40     /**
41      * DON'T USE THIS EVEN PRIVATELY! USE getJavaClass() INSTEAD.
42      * @see #getJavaClass()
43      */

44     private JavaClass jc;
45
46     /**
47      * The Verifier that created this.
48      */

49     private Verifier myOwner;
50
51     /** 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! */
52     private JavaClass getJavaClass(){
53         if (jc == null){
54             try {
55                 jc = Repository.lookupClass(myOwner.getClassName());
56             } catch (ClassNotFoundException JavaDoc e) {
57                 // FIXME: currently, Pass1Verifier treats jc == null as a special
58
// case, so we don't need to do anything here. A better solution
59
// would be to simply throw the ClassNotFoundException
60
// out of this method.
61
}
62         }
63         return jc;
64     }
65     
66     /**
67      * Should only be instantiated by a Verifier.
68      *
69      * @see org.apache.bcel.verifier.Verifier
70      */

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

133     public VerificationResult do_verify(){
134         JavaClass jc;
135         try{
136             jc = getJavaClass(); //loads in the class file if not already done.
137

138             if (jc != null){
139                 /* If we find more constraints to check, we should do this in an own method. */
140                 if (! myOwner.getClassName().equals(jc.getClassName())){
141                     // This should maybe caught by BCEL: In case of renamed .class files we get wrong
142
// JavaClass objects here.
143
throw new LoadingException("Wrong name: the internal name of the .class file '"+jc.getClassName()+"' does not match the file's name '"+myOwner.getClassName()+"'.");
144                 }
145             }
146             
147         }
148         catch(LoadingException e){
149             return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
150         }
151         catch(ClassFormatException e){
152             return new VerificationResult(VerificationResult.VERIFIED_REJECTED, e.getMessage());
153         }
154         catch(RuntimeException JavaDoc e){
155             // BCEL does not catch every possible RuntimeException; e.g. if
156
// a constant pool index is referenced that does not exist.
157
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Parsing via BCEL did not succeed. "+e.getClass().getName()+" occured:\n"+Utility.getStackTrace(e));
158         }
159
160         if (jc != null){
161             return VerificationResult.VR_OK;
162         }
163         else{
164             //TODO: Maybe change Repository's behaviour to throw a LoadingException instead of just returning "null"
165
// if a class file cannot be found or in another way be looked up.
166
return new VerificationResult(VerificationResult.VERIFIED_REJECTED, "Repository.lookup() failed. FILE NOT FOUND?");
167         }
168     }
169
170     /**
171      * Currently this returns an empty array of String.
172      * One could parse the error messages of BCEL
173      * (written to java.lang.System.err) when loading
174      * a class file such as detecting unknown attributes
175      * or trailing garbage at the end of a class file.
176      * However, Markus Dahm does not like the idea so this
177      * method is currently useless and therefore marked as
178      * <B>TODO</B>.
179      */

180     public String JavaDoc[] getMessages(){
181         // This method is only here to override the javadoc-comment.
182
return super.getMessages();
183     }
184
185 }
186
Popular Tags