1 /* 2 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 // The verifier class 26 class Verifier : AllStatic { 27 public: 28 enum { 29 STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50, 30 INVOKEDYNAMIC_MAJOR_VERSION = 51 31 }; 32 typedef enum { ThrowException, NoException } Mode; 33 34 /** 35 * Verify the bytecodes for a class. If 'throw_exception' is true 36 * then the appropriate VerifyError or ClassFormatError will be thrown. 37 * Otherwise, no exception is thrown and the return indicates the 38 * error. 39 */ 40 static bool verify(instanceKlassHandle klass, Mode mode, bool should_verify_class, TRAPS); 41 42 // Return false if the class is loaded by the bootstrap loader, 43 // or if defineClass was called requesting skipping verification 44 // -Xverify:all/none override this value 45 static bool should_verify_for(oop class_loader, bool should_verify_class); 46 47 // Relax certain verifier checks to enable some broken 1.1 apps to run on 1.2. 48 static bool relax_verify_for(oop class_loader); 49 50 private: 51 static bool is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class); 52 static symbolHandle inference_verify( 53 instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS); 54 }; 55 56 class RawBytecodeStream; 57 class StackMapFrame; 58 class StackMapTable; 59 60 // Summary of verifier's memory usage: 61 // StackMapTable is stack allocated. 62 // StackMapFrame are resource allocated. There is one ResourceMark 63 // for each method. 64 // There is one mutable StackMapFrame (current_frame) which is updated 65 // by abstract bytecode interpretation. frame_in_exception_handler() returns 66 // a frame that has a mutable one-item stack (ready for pushing the 67 // catch type exception object). All the other StackMapFrame's 68 // are immutable (including their locals and stack arrays) after 69 // their constructions. 70 // locals/stack arrays in StackMapFrame are resource allocated. 71 // locals/stack arrays can be shared between StackMapFrame's, except 72 // the mutable StackMapFrame (current_frame). 73 // Care needs to be taken to make sure resource objects don't outlive 74 // the lifetime of their ResourceMark. 75 76 // These macros are used similarly to CHECK macros but also check 77 // the status of the verifier and return if that has an error. 78 #define CHECK_VERIFY(verifier) \ 79 CHECK); if ((verifier)->has_error()) return; (0 80 #define CHECK_VERIFY_(verifier, result) \ 81 CHECK_(result)); if ((verifier)->has_error()) return (result); (0 82 83 // A new instance of this class is created for each class being verified 84 class ClassVerifier : public StackObj { 85 private: 86 Thread* _thread; 87 symbolHandle _exception_type; 88 char* _message; 89 size_t _message_buffer_len; 90 91 void verify_method(methodHandle method, TRAPS); 92 char* generate_code_data(methodHandle m, u4 code_length, TRAPS); 93 void verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS); 94 void verify_local_variable_table(u4 code_length, char* code_data, TRAPS); 95 96 VerificationType cp_ref_index_to_type( 97 int index, constantPoolHandle cp, TRAPS) { 98 return cp_index_to_type(cp->klass_ref_index_at(index), cp, THREAD); 99 } 100 101 bool is_protected_access( 102 instanceKlassHandle this_class, klassOop target_class, 103 symbolOop field_name, symbolOop field_sig, bool is_method); 104 105 void verify_cp_index(constantPoolHandle cp, int index, TRAPS); 106 void verify_cp_type( 107 int index, constantPoolHandle cp, unsigned int types, TRAPS); 108 void verify_cp_class_type(int index, constantPoolHandle cp, TRAPS); 109 110 u2 verify_stackmap_table( 111 u2 stackmap_index, u2 bci, StackMapFrame* current_frame, 112 StackMapTable* stackmap_table, bool no_control_flow, TRAPS); 113 114 void verify_exception_handler_targets( 115 u2 bci, bool this_uninit, StackMapFrame* current_frame, 116 StackMapTable* stackmap_table, TRAPS); 117 118 void verify_ldc( 119 int opcode, u2 index, StackMapFrame *current_frame, 120 constantPoolHandle cp, u2 bci, TRAPS); 121 122 void verify_switch( 123 RawBytecodeStream* bcs, u4 code_length, char* code_data, 124 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS); 125 126 void verify_field_instructions( 127 RawBytecodeStream* bcs, StackMapFrame* current_frame, 128 constantPoolHandle cp, TRAPS); 129 130 void verify_invoke_init( 131 RawBytecodeStream* bcs, VerificationType ref_class_type, 132 StackMapFrame* current_frame, u4 code_length, bool* this_uninit, 133 constantPoolHandle cp, TRAPS); 134 135 void verify_invoke_instructions( 136 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame, 137 bool* this_uninit, VerificationType return_type, 138 constantPoolHandle cp, TRAPS); 139 140 VerificationType get_newarray_type(u2 index, u2 bci, TRAPS); 141 void verify_anewarray( 142 u2 index, constantPoolHandle cp, StackMapFrame* current_frame, TRAPS); 143 void verify_return_value( 144 VerificationType return_type, VerificationType type, u2 offset, TRAPS); 145 146 void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS); 147 void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS); 148 void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS); 149 void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS); 150 void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS); 151 void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS); 152 void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS); 153 void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS); 154 void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS); 155 void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS); 156 void verify_iinc (u2 index, StackMapFrame* current_frame, TRAPS); 157 158 bool name_in_supers(symbolOop ref_name, instanceKlassHandle current); 159 160 instanceKlassHandle _klass; // the class being verified 161 methodHandle _method; // current method being verified 162 VerificationType _this_type; // the verification type of the current class 163 164 // Some recursive calls from the verifier to the name resolver 165 // can cause the current class to be re-verified and rewritten. 166 // If this happens, the original verification should not continue, 167 // because constant pool indexes will have changed. 168 // The rewriter is preceded by the verifier. If the verifier throws 169 // an error, rewriting is prevented. Also, rewriting always precedes 170 // bytecode execution or compilation. Thus, is_rewritten implies 171 // that a class has been verified and prepared for execution. 172 bool was_recursively_verified() { return _klass->is_rewritten(); } 173 174 public: 175 enum { 176 BYTECODE_OFFSET = 1, 177 NEW_OFFSET = 2 178 }; 179 180 // constructor 181 ClassVerifier(instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS); 182 183 // destructor 184 ~ClassVerifier(); 185 186 Thread* thread() { return _thread; } 187 methodHandle method() { return _method; } 188 instanceKlassHandle current_class() const { return _klass; } 189 VerificationType current_type() const { return _this_type; } 190 191 // Verifies the class. If a verify or class file format error occurs, 192 // the '_exception_name' symbols will set to the exception name and 193 // the message_buffer will be filled in with the exception message. 194 void verify_class(TRAPS); 195 196 // Return status modes 197 symbolHandle result() const { return _exception_type; } 198 bool has_error() const { return !(result().is_null()); } 199 200 // Called when verify or class format errors are encountered. 201 // May throw an exception based upon the mode. 202 void verify_error(u2 offset, const char* fmt, ...); 203 void verify_error(const char* fmt, ...); 204 void class_format_error(const char* fmt, ...); 205 void format_error_message(const char* fmt, int offset, va_list args); 206 207 klassOop load_class(symbolHandle name, TRAPS); 208 209 int change_sig_to_verificationType( 210 SignatureStream* sig_type, VerificationType* inference_type, TRAPS); 211 212 VerificationType cp_index_to_type(int index, constantPoolHandle cp, TRAPS) { 213 return VerificationType::reference_type( 214 symbolHandle(THREAD, cp->klass_name_at(index))); 215 } 216 217 static bool _verify_verbose; // for debugging 218 }; 219 220 inline int ClassVerifier::change_sig_to_verificationType( 221 SignatureStream* sig_type, VerificationType* inference_type, TRAPS) { 222 BasicType bt = sig_type->type(); 223 switch (bt) { 224 case T_OBJECT: 225 case T_ARRAY: 226 { 227 symbolOop name = sig_type->as_symbol(CHECK_0); 228 *inference_type = 229 VerificationType::reference_type(symbolHandle(THREAD, name)); 230 return 1; 231 } 232 case T_LONG: 233 *inference_type = VerificationType::long_type(); 234 *++inference_type = VerificationType::long2_type(); 235 return 2; 236 case T_DOUBLE: 237 *inference_type = VerificationType::double_type(); 238 *++inference_type = VerificationType::double2_type(); 239 return 2; 240 case T_INT: 241 case T_BOOLEAN: 242 case T_BYTE: 243 case T_CHAR: 244 case T_SHORT: 245 *inference_type = VerificationType::integer_type(); 246 return 1; 247 case T_FLOAT: 248 *inference_type = VerificationType::float_type(); 249 return 1; 250 default: 251 ShouldNotReachHere(); 252 return 1; 253 } 254 }