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