1 /* 2 * Copyright (c) 1998, 2017, 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 #ifndef SHARE_VM_CLASSFILE_VERIFIER_HPP 26 #define SHARE_VM_CLASSFILE_VERIFIER_HPP 27 28 #include "classfile/verificationType.hpp" 29 #include "gc/shared/gcLocker.hpp" 30 #include "oops/klass.hpp" 31 #include "oops/method.hpp" 32 #include "runtime/handles.hpp" 33 #include "utilities/exceptions.hpp" 34 #include "utilities/growableArray.hpp" 35 36 // For bit tests involving JVM_CONSTANT_Value during verification, 37 // define a "safe" number to avoid integer overflow of types 38 #define SAFE_JVM_CONSTANT_Value JVM_CONSTANT_ExternalMax 39 40 // The verifier class 41 class Verifier : AllStatic { 42 public: 43 enum { 44 STRICTER_ACCESS_CTRL_CHECK_VERSION = 49, 45 STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50, 46 INVOKEDYNAMIC_MAJOR_VERSION = 51, 47 NO_RELAX_ACCESS_CTRL_CHECK_VERSION = 52, 48 DYNAMICCONSTANT_MAJOR_VERSION = 55, 49 VALUETYPE_MAJOR_VERSION = 53, 50 VALUETYPE_MINOR_VERSION = 1 51 }; 52 typedef enum { ThrowException, NoException } Mode; 53 54 /** 55 * Verify the bytecodes for a class. If 'throw_exception' is true 56 * then the appropriate VerifyError or ClassFormatError will be thrown. 57 * Otherwise, no exception is thrown and the return indicates the 58 * error. 59 */ 60 static void log_end_verification(outputStream* st, const char* klassName, Symbol* exception_name, TRAPS); 61 static bool verify(InstanceKlass* klass, Mode mode, bool should_verify_class, TRAPS); 62 63 // Return false if the class is loaded by the bootstrap loader, 64 // or if defineClass was called requesting skipping verification 65 // -Xverify:all/none override this value 66 static bool should_verify_for(oop class_loader, bool should_verify_class); 67 68 // Relax certain access checks to enable some broken 1.1 apps to run on 1.2. 69 static bool relax_access_for(oop class_loader); 70 71 // Print output for class+resolve 72 static void trace_class_resolution(Klass* resolve_class, InstanceKlass* verify_class); 73 74 private: 75 static bool is_eligible_for_verification(InstanceKlass* klass, bool should_verify_class); 76 static Symbol* inference_verify( 77 InstanceKlass* klass, char* msg, size_t msg_len, TRAPS); 78 }; 79 80 class RawBytecodeStream; 81 class StackMapFrame; 82 class StackMapTable; 83 84 // Summary of verifier's memory usage: 85 // StackMapTable is stack allocated. 86 // StackMapFrame are resource allocated. There is only one ResourceMark 87 // for each class verification, which is created at the top level. 88 // There is one mutable StackMapFrame (current_frame) which is updated 89 // by abstract bytecode interpretation. frame_in_exception_handler() returns 90 // a frame that has a mutable one-item stack (ready for pushing the 91 // catch type exception object). All the other StackMapFrame's 92 // are immutable (including their locals and stack arrays) after 93 // their constructions. 94 // locals/stack arrays in StackMapFrame are resource allocated. 95 // locals/stack arrays can be shared between StackMapFrame's, except 96 // the mutable StackMapFrame (current_frame). 97 98 // These macros are used similarly to CHECK macros but also check 99 // the status of the verifier and return if that has an error. 100 #define CHECK_VERIFY(verifier) \ 101 CHECK); if ((verifier)->has_error()) return; ((void)0 102 #define CHECK_VERIFY_(verifier, result) \ 103 CHECK_(result)); if ((verifier)->has_error()) return (result); ((void)0 104 105 class TypeOrigin VALUE_OBJ_CLASS_SPEC { 106 private: 107 typedef enum { 108 CF_LOCALS, // Comes from the current frame locals 109 CF_STACK, // Comes from the current frame expression stack 110 SM_LOCALS, // Comes from stackmap locals 111 SM_STACK, // Comes from stackmap expression stack 112 CONST_POOL, // Comes from the constant pool 113 SIG, // Comes from method signature 114 IMPLICIT, // Comes implicitly from code or context 115 BAD_INDEX, // No type, but the index is bad 116 FRAME_ONLY, // No type, context just contains the frame 117 NONE 118 } Origin; 119 120 Origin _origin; 121 u2 _index; // local, stack, or constant pool index 122 StackMapFrame* _frame; // source frame if CF or SM 123 VerificationType _type; // The actual type 124 125 TypeOrigin( 126 Origin origin, u2 index, StackMapFrame* frame, VerificationType type) 127 : _origin(origin), _index(index), _frame(frame), _type(type) {} 128 129 public: 130 TypeOrigin() : _origin(NONE), _index(0), _frame(NULL) {} 131 132 static TypeOrigin null(); 133 static TypeOrigin local(u2 index, StackMapFrame* frame); 134 static TypeOrigin stack(u2 index, StackMapFrame* frame); 135 static TypeOrigin sm_local(u2 index, StackMapFrame* frame); 136 static TypeOrigin sm_stack(u2 index, StackMapFrame* frame); 137 static TypeOrigin cp(u2 index, VerificationType vt); 138 static TypeOrigin signature(VerificationType vt); 139 static TypeOrigin bad_index(u2 index); 140 static TypeOrigin implicit(VerificationType t); 141 static TypeOrigin frame(StackMapFrame* frame); 142 143 void reset_frame(); 144 void details(outputStream* ss) const; 145 void print_frame(outputStream* ss) const; 146 const StackMapFrame* frame() const { return _frame; } 147 bool is_valid() const { return _origin != NONE; } 148 u2 index() const { return _index; } 149 150 #ifdef ASSERT 151 void print_on(outputStream* str) const; 152 #endif 153 }; 154 155 class ErrorContext VALUE_OBJ_CLASS_SPEC { 156 private: 157 typedef enum { 158 INVALID_BYTECODE, // There was a problem with the bytecode 159 WRONG_TYPE, // Type value was not as expected 160 FLAGS_MISMATCH, // Frame flags are not assignable 161 BAD_CP_INDEX, // Invalid constant pool index 162 BAD_LOCAL_INDEX, // Invalid local index 163 LOCALS_SIZE_MISMATCH, // Frames have differing local counts 164 STACK_SIZE_MISMATCH, // Frames have different stack sizes 165 STACK_OVERFLOW, // Attempt to push onto a full expression stack 166 STACK_UNDERFLOW, // Attempt to pop and empty expression stack 167 MISSING_STACKMAP, // No stackmap for this location and there should be 168 BAD_STACKMAP, // Format error in stackmap 169 NO_FAULT, // No error 170 UNKNOWN 171 } FaultType; 172 173 int _bci; 174 FaultType _fault; 175 TypeOrigin _type; 176 TypeOrigin _expected; 177 178 ErrorContext(int bci, FaultType fault) : 179 _bci(bci), _fault(fault) {} 180 ErrorContext(int bci, FaultType fault, TypeOrigin type) : 181 _bci(bci), _fault(fault), _type(type) {} 182 ErrorContext(int bci, FaultType fault, TypeOrigin type, TypeOrigin exp) : 183 _bci(bci), _fault(fault), _type(type), _expected(exp) {} 184 185 public: 186 ErrorContext() : _bci(-1), _fault(NO_FAULT) {} 187 188 static ErrorContext bad_code(u2 bci) { 189 return ErrorContext(bci, INVALID_BYTECODE); 190 } 191 static ErrorContext bad_type(u2 bci, TypeOrigin type) { 192 return ErrorContext(bci, WRONG_TYPE, type); 193 } 194 static ErrorContext bad_type(u2 bci, TypeOrigin type, TypeOrigin exp) { 195 return ErrorContext(bci, WRONG_TYPE, type, exp); 196 } 197 static ErrorContext bad_flags(u2 bci, StackMapFrame* frame) { 198 return ErrorContext(bci, FLAGS_MISMATCH, TypeOrigin::frame(frame)); 199 } 200 static ErrorContext bad_flags(u2 bci, StackMapFrame* cur, StackMapFrame* sm) { 201 return ErrorContext(bci, FLAGS_MISMATCH, 202 TypeOrigin::frame(cur), TypeOrigin::frame(sm)); 203 } 204 static ErrorContext bad_cp_index(u2 bci, u2 index) { 205 return ErrorContext(bci, BAD_CP_INDEX, TypeOrigin::bad_index(index)); 206 } 207 static ErrorContext bad_local_index(u2 bci, u2 index) { 208 return ErrorContext(bci, BAD_LOCAL_INDEX, TypeOrigin::bad_index(index)); 209 } 210 static ErrorContext locals_size_mismatch( 211 u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) { 212 return ErrorContext(bci, LOCALS_SIZE_MISMATCH, 213 TypeOrigin::frame(frame0), TypeOrigin::frame(frame1)); 214 } 215 static ErrorContext stack_size_mismatch( 216 u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) { 217 return ErrorContext(bci, STACK_SIZE_MISMATCH, 218 TypeOrigin::frame(frame0), TypeOrigin::frame(frame1)); 219 } 220 static ErrorContext stack_overflow(u2 bci, StackMapFrame* frame) { 221 return ErrorContext(bci, STACK_OVERFLOW, TypeOrigin::frame(frame)); 222 } 223 static ErrorContext stack_underflow(u2 bci, StackMapFrame* frame) { 224 return ErrorContext(bci, STACK_UNDERFLOW, TypeOrigin::frame(frame)); 225 } 226 static ErrorContext missing_stackmap(u2 bci) { 227 return ErrorContext(bci, MISSING_STACKMAP); 228 } 229 static ErrorContext bad_stackmap(int index, StackMapFrame* frame) { 230 return ErrorContext(0, BAD_STACKMAP, TypeOrigin::frame(frame)); 231 } 232 233 bool is_valid() const { return _fault != NO_FAULT; } 234 int bci() const { return _bci; } 235 236 void reset_frames() { 237 _type.reset_frame(); 238 _expected.reset_frame(); 239 } 240 241 void details(outputStream* ss, const Method* method) const; 242 243 #ifdef ASSERT 244 void print_on(outputStream* str) const { 245 str->print("error_context(%d, %d,", _bci, _fault); 246 _type.print_on(str); 247 str->print(","); 248 _expected.print_on(str); 249 str->print(")"); 250 } 251 #endif 252 253 private: 254 void location_details(outputStream* ss, const Method* method) const; 255 void reason_details(outputStream* ss) const; 256 void frame_details(outputStream* ss) const; 257 void bytecode_details(outputStream* ss, const Method* method) const; 258 void handler_details(outputStream* ss, const Method* method) const; 259 void stackmap_details(outputStream* ss, const Method* method) const; 260 }; 261 262 // A new instance of this class is created for each class being verified 263 class ClassVerifier : public StackObj { 264 private: 265 Thread* _thread; 266 GrowableArray<Symbol*>* _symbols; // keep a list of symbols created 267 268 Symbol* _exception_type; 269 char* _message; 270 271 ErrorContext _error_context; // contains information about an error 272 273 void verify_method(const methodHandle& method, TRAPS); 274 char* generate_code_data(const methodHandle& m, u4 code_length, TRAPS); 275 void verify_exception_handler_table(u4 code_length, char* code_data, 276 int& min, int& max, TRAPS); 277 void verify_local_variable_table(u4 code_length, char* code_data, TRAPS); 278 279 VerificationType cp_ref_index_to_type( 280 int index, const constantPoolHandle& cp, TRAPS) { 281 return cp_index_to_reference_type(cp->klass_ref_index_at(index), cp, THREAD); 282 } 283 284 VerificationType cp_value_index_to_type( 285 int index, const constantPoolHandle& cp, TRAPS) { 286 return cp_index_to_valuetype(cp->klass_ref_index_at(index), cp, THREAD); 287 } 288 289 bool is_protected_access( 290 InstanceKlass* this_class, Klass* target_class, 291 Symbol* field_name, Symbol* field_sig, bool is_method); 292 293 void verify_cp_index(u2 bci, const constantPoolHandle& cp, int index, TRAPS); 294 void verify_cp_type(u2 bci, int index, const constantPoolHandle& cp, 295 unsigned int types, TRAPS); 296 void verify_cp_class_type(u2 bci, int index, const constantPoolHandle& cp, TRAPS); 297 void verify_cp_value_type(u2 bci, int index, const constantPoolHandle& cp, TRAPS); 298 void verify_cp_class_or_value_type(u2 bci, int index, const constantPoolHandle& cp, TRAPS); 299 300 u2 verify_stackmap_table( 301 u2 stackmap_index, u2 bci, StackMapFrame* current_frame, 302 StackMapTable* stackmap_table, bool no_control_flow, TRAPS); 303 304 void verify_exception_handler_targets( 305 u2 bci, bool this_uninit, StackMapFrame* current_frame, 306 StackMapTable* stackmap_table, TRAPS); 307 308 void verify_ldc( 309 int opcode, u2 index, StackMapFrame *current_frame, 310 const constantPoolHandle& cp, u2 bci, TRAPS); 311 312 void verify_switch( 313 RawBytecodeStream* bcs, u4 code_length, char* code_data, 314 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS); 315 316 void verify_field_instructions( 317 RawBytecodeStream* bcs, StackMapFrame* current_frame, 318 const constantPoolHandle& cp, bool allow_arrays, TRAPS); 319 320 void verify_vwithfield( 321 RawBytecodeStream* bcs, StackMapFrame* current_frame, 322 const constantPoolHandle& cp, TRAPS); 323 324 void verify_invoke_init( 325 RawBytecodeStream* bcs, u2 ref_index, VerificationType ref_class_type, 326 StackMapFrame* current_frame, u4 code_length, bool in_try_block, 327 bool* this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table, 328 TRAPS); 329 330 // Used by ends_in_athrow() to push all handlers that contain bci onto the 331 // handler_stack, if the handler has not already been pushed on the stack. 332 void push_handlers(ExceptionTable* exhandlers, 333 GrowableArray<u4>* handler_list, 334 GrowableArray<u4>* handler_stack, 335 u4 bci); 336 337 // Returns true if all paths starting with start_bc_offset end in athrow 338 // bytecode or loop. 339 bool ends_in_athrow(u4 start_bc_offset); 340 341 void verify_invoke_instructions( 342 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame, 343 bool in_try_block, bool* this_uninit, VerificationType return_type, 344 const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS); 345 346 VerificationType get_newarray_type(u2 index, u2 bci, TRAPS); 347 void verify_anewarray(u2 bci, u2 index, const constantPoolHandle& cp, 348 StackMapFrame* current_frame, TRAPS); 349 void verify_return_value( 350 VerificationType return_type, VerificationType type, u2 offset, 351 StackMapFrame* current_frame, TRAPS); 352 353 void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS); 354 void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS); 355 void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS); 356 void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS); 357 void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS); 358 void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS); 359 void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS); 360 void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS); 361 void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS); 362 void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS); 363 void verify_iinc (u2 index, StackMapFrame* current_frame, TRAPS); 364 365 bool name_in_supers(Symbol* ref_name, InstanceKlass* current); 366 367 VerificationType object_type() const; 368 VerificationType __value_type() const; 369 370 InstanceKlass* _klass; // the class being verified 371 methodHandle _method; // current method being verified 372 VerificationType _this_type; // the verification type of the current class 373 374 // Some recursive calls from the verifier to the name resolver 375 // can cause the current class to be re-verified and rewritten. 376 // If this happens, the original verification should not continue, 377 // because constant pool indexes will have changed. 378 // The rewriter is preceded by the verifier. If the verifier throws 379 // an error, rewriting is prevented. Also, rewriting always precedes 380 // bytecode execution or compilation. Thus, is_rewritten implies 381 // that a class has been verified and prepared for execution. 382 bool was_recursively_verified() { return _klass->is_rewritten(); } 383 384 bool is_same_or_direct_interface(InstanceKlass* klass, 385 VerificationType klass_type, VerificationType ref_class_type); 386 387 public: 388 enum { 389 BYTECODE_OFFSET = 1, 390 NEW_OFFSET = 2 391 }; 392 393 // constructor 394 ClassVerifier(InstanceKlass* klass, TRAPS); 395 396 // destructor 397 ~ClassVerifier(); 398 399 Thread* thread() { return _thread; } 400 const methodHandle& method() { return _method; } 401 InstanceKlass* current_class() const { return _klass; } 402 VerificationType current_type() const { return _this_type; } 403 404 // Verifies the class. If a verify or class file format error occurs, 405 // the '_exception_name' symbols will set to the exception name and 406 // the message_buffer will be filled in with the exception message. 407 void verify_class(TRAPS); 408 409 // Return status modes 410 Symbol* result() const { return _exception_type; } 411 bool has_error() const { return result() != NULL; } 412 char* exception_message() { 413 stringStream ss; 414 ss.print("%s", _message); 415 _error_context.details(&ss, _method()); 416 return ss.as_string(); 417 } 418 419 // Called when verify or class format errors are encountered. 420 // May throw an exception based upon the mode. 421 void verify_error(ErrorContext ctx, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4); 422 void class_format_error(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3); 423 424 Klass* load_class(Symbol* name, TRAPS); 425 426 int change_sig_to_verificationType( 427 SignatureStream* sig_type, VerificationType* inference_type, TRAPS); 428 429 VerificationType cp_index_to_reference_type(int index, const constantPoolHandle& cp, TRAPS) { 430 return VerificationType::reference_type(cp->klass_name_at(index)); 431 } 432 433 VerificationType cp_index_to_valuetype(int index, const constantPoolHandle& cp, TRAPS) { 434 return VerificationType::valuetype_type(cp->klass_name_at(index)); 435 } 436 437 // Keep a list of temporary symbols created during verification because 438 // their reference counts need to be decremented when the verifier object 439 // goes out of scope. Since these symbols escape the scope in which they're 440 // created, we can't use a TempNewSymbol. 441 Symbol* create_temporary_symbol(const Symbol* s, int begin, int end, TRAPS); 442 Symbol* create_temporary_symbol(const char *s, int length, TRAPS); 443 444 Symbol* create_temporary_symbol(Symbol* s) { 445 // This version just updates the reference count and saves the symbol to be 446 // dereferenced later. 447 s->increment_refcount(); 448 _symbols->push(s); 449 return s; 450 } 451 452 TypeOrigin ref_ctx(const char* str, TRAPS); 453 TypeOrigin valuetype_ctx(const char* str, TRAPS); 454 455 }; 456 457 inline int ClassVerifier::change_sig_to_verificationType( 458 SignatureStream* sig_type, VerificationType* inference_type, TRAPS) { 459 BasicType bt = sig_type->type(); 460 switch (bt) { 461 case T_OBJECT: 462 case T_ARRAY: 463 case T_VALUETYPE: 464 { 465 Symbol* name = sig_type->as_symbol(CHECK_0); 466 // Create another symbol to save as signature stream unreferences this symbol. 467 Symbol* name_copy = create_temporary_symbol(name); 468 assert(name_copy == name, "symbols don't match"); 469 *inference_type = ((bt == T_VALUETYPE) ? VerificationType::valuetype_type(name_copy) : 470 VerificationType::reference_type(name_copy)); 471 return 1; 472 } 473 case T_LONG: 474 *inference_type = VerificationType::long_type(); 475 *++inference_type = VerificationType::long2_type(); 476 return 2; 477 case T_DOUBLE: 478 *inference_type = VerificationType::double_type(); 479 *++inference_type = VerificationType::double2_type(); 480 return 2; 481 case T_INT: 482 case T_BOOLEAN: 483 case T_BYTE: 484 case T_CHAR: 485 case T_SHORT: 486 *inference_type = VerificationType::integer_type(); 487 return 1; 488 case T_FLOAT: 489 *inference_type = VerificationType::float_type(); 490 return 1; 491 default: 492 ShouldNotReachHere(); 493 return 1; 494 } 495 } 496 497 #endif // SHARE_VM_CLASSFILE_VERIFIER_HPP