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

This page was automatically generated by Maven