< prev index next >

src/hotspot/share/classfile/verifier.hpp

Print this page

 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 #include "utilities/resourceHash.hpp"
 35 
 36 // The verifier class
 37 class Verifier : AllStatic {
 38  public:
 39   enum {
 40     STACKMAP_ATTRIBUTE_MAJOR_VERSION    = 50,
 41     INVOKEDYNAMIC_MAJOR_VERSION         = 51,
 42     NO_RELAX_ACCESS_CTRL_CHECK_VERSION  = 52,
 43     DYNAMICCONSTANT_MAJOR_VERSION       = 55
 44   };
 45 
 46   // Verify the bytecodes for a class.
 47   static bool verify(InstanceKlass* klass, bool should_verify_class, TRAPS);
 48 
 49   static void log_end_verification(outputStream* st, const char* klassName, Symbol* exception_name,
 50                                     oop pending_exception);
 51 
 52   // Return false if the class is loaded by the bootstrap loader,
 53   // or if defineClass was called requesting skipping verification
 54   // -Xverify:all overrides this value
 55   static bool should_verify_for(oop class_loader, bool should_verify_class);
 56 
 57   // Relax certain access checks to enable some broken 1.1 apps to run on 1.2.
 58   static bool relax_access_for(oop class_loader);
 59 
 60   // Print output for class+resolve
 61   static void trace_class_resolution(Klass* resolve_class, InstanceKlass* verify_class);
 62 
 63  private:

138 
139 #ifdef ASSERT
140   void print_on(outputStream* str) const;
141 #endif
142 };
143 
144 class ErrorContext {
145  private:
146   typedef enum {
147     INVALID_BYTECODE,     // There was a problem with the bytecode
148     WRONG_TYPE,           // Type value was not as expected
149     FLAGS_MISMATCH,       // Frame flags are not assignable
150     BAD_CP_INDEX,         // Invalid constant pool index
151     BAD_LOCAL_INDEX,      // Invalid local index
152     LOCALS_SIZE_MISMATCH, // Frames have differing local counts
153     STACK_SIZE_MISMATCH,  // Frames have different stack sizes
154     STACK_OVERFLOW,       // Attempt to push onto a full expression stack
155     STACK_UNDERFLOW,      // Attempt to pop and empty expression stack
156     MISSING_STACKMAP,     // No stackmap for this location and there should be
157     BAD_STACKMAP,         // Format error in stackmap

158     NO_FAULT,             // No error
159     UNKNOWN
160   } FaultType;
161 
162   int _bci;
163   FaultType _fault;
164   TypeOrigin _type;
165   TypeOrigin _expected;
166 
167   ErrorContext(int bci, FaultType fault) :
168       _bci(bci), _fault(fault)  {}
169   ErrorContext(int bci, FaultType fault, TypeOrigin type) :
170       _bci(bci), _fault(fault), _type(type)  {}
171   ErrorContext(int bci, FaultType fault, TypeOrigin type, TypeOrigin exp) :
172       _bci(bci), _fault(fault), _type(type), _expected(exp)  {}
173 
174  public:
175   ErrorContext() : _bci(-1), _fault(NO_FAULT) {}
176 
177   static ErrorContext bad_code(u2 bci) {

201     return ErrorContext(bci, LOCALS_SIZE_MISMATCH,
202         TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
203   }
204   static ErrorContext stack_size_mismatch(
205       u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) {
206     return ErrorContext(bci, STACK_SIZE_MISMATCH,
207         TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
208   }
209   static ErrorContext stack_overflow(u2 bci, StackMapFrame* frame) {
210     return ErrorContext(bci, STACK_OVERFLOW, TypeOrigin::frame(frame));
211   }
212   static ErrorContext stack_underflow(u2 bci, StackMapFrame* frame) {
213     return ErrorContext(bci, STACK_UNDERFLOW, TypeOrigin::frame(frame));
214   }
215   static ErrorContext missing_stackmap(u2 bci) {
216     return ErrorContext(bci, MISSING_STACKMAP);
217   }
218   static ErrorContext bad_stackmap(int index, StackMapFrame* frame) {
219     return ErrorContext(0, BAD_STACKMAP, TypeOrigin::frame(frame));
220   }



221 
222   bool is_valid() const { return _fault != NO_FAULT; }
223   int bci() const { return _bci; }
224 
225   void reset_frames() {
226     _type.reset_frame();
227     _expected.reset_frame();
228   }
229 
230   void details(outputStream* ss, const Method* method) const;
231 
232 #ifdef ASSERT
233   void print_on(outputStream* str) const {
234     str->print("error_context(%d, %d,", _bci, _fault);
235     _type.print_on(str);
236     str->print(",");
237     _expected.print_on(str);
238     str->print(")");
239   }
240 #endif

331 
332   void verify_invoke_init(
333     RawBytecodeStream* bcs, u2 ref_index, VerificationType ref_class_type,
334     StackMapFrame* current_frame, u4 code_length, bool in_try_block,
335     bool* this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
336     TRAPS);
337 
338   // Used by ends_in_athrow() to push all handlers that contain bci onto the
339   // handler_stack, if the handler has not already been pushed on the stack.
340   void push_handlers(ExceptionTable* exhandlers,
341                      GrowableArray<u4>* handler_list,
342                      GrowableArray<u4>* handler_stack,
343                      u4 bci);
344 
345   // Returns true if all paths starting with start_bc_offset end in athrow
346   // bytecode or loop.
347   bool ends_in_athrow(u4 start_bc_offset);
348 
349   void verify_invoke_instructions(
350     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
351     bool in_try_block, bool* this_uninit, VerificationType return_type,
352     const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS);
353 
354   VerificationType get_newarray_type(u2 index, u2 bci, TRAPS);
355   void verify_anewarray(u2 bci, u2 index, const constantPoolHandle& cp,
356       StackMapFrame* current_frame, TRAPS);
357   void verify_return_value(
358       VerificationType return_type, VerificationType type, u2 offset,
359       StackMapFrame* current_frame, TRAPS);
360 
361   void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS);
362   void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS);
363   void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS);
364   void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS);
365   void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS);
366   void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS);
367   void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS);
368   void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS);
369   void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS);
370   void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS);
371   void verify_iinc  (u2 index, StackMapFrame* current_frame, TRAPS);

432 
433   // Called when verify or class format errors are encountered.
434   // May throw an exception based upon the mode.
435   void verify_error(ErrorContext ctx, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);
436   void class_format_error(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3);
437 
438   Klass* load_class(Symbol* name, TRAPS);
439 
440   method_signatures_table_type* method_signatures_table() const {
441     return _method_signatures_table;
442   }
443 
444   void set_method_signatures_table(method_signatures_table_type* method_signatures_table) {
445     _method_signatures_table = method_signatures_table;
446   }
447 
448   int change_sig_to_verificationType(
449     SignatureStream* sig_type, VerificationType* inference_type);
450 
451   VerificationType cp_index_to_type(int index, const constantPoolHandle& cp, TRAPS) {
452     return VerificationType::reference_type(cp->klass_name_at(index));







453   }
454 
455   // Keep a list of temporary symbols created during verification because
456   // their reference counts need to be decremented when the verifier object
457   // goes out of scope.  Since these symbols escape the scope in which they're
458   // created, we can't use a TempNewSymbol.
459   Symbol* create_temporary_symbol(const char *s, int length);
460   Symbol* create_temporary_symbol(Symbol* s) {
461     if (s == _previous_symbol) {
462       return s;
463     }
464     if (!s->is_permanent()) {
465       s->increment_refcount();
466       if (_symbols == NULL) {
467         _symbols = new GrowableArray<Symbol*>(50, 0, NULL);
468       }
469       _symbols->push(s);
470     }
471     _previous_symbol = s;
472     return s;
473   }
474 
475   TypeOrigin ref_ctx(const char* str);
476 
477 };
478 
479 inline int ClassVerifier::change_sig_to_verificationType(
480     SignatureStream* sig_type, VerificationType* inference_type) {
481   BasicType bt = sig_type->type();
482   switch (bt) {
483     case T_OBJECT:
484     case T_ARRAY:
485       {
486         Symbol* name = sig_type->as_symbol();
487         // Create another symbol to save as signature stream unreferences this symbol.
488         Symbol* name_copy = create_temporary_symbol(name);
489         assert(name_copy == name, "symbols don't match");
490         *inference_type =
491           VerificationType::reference_type(name_copy);








492         return 1;
493       }
494     case T_LONG:
495       *inference_type = VerificationType::long_type();
496       *++inference_type = VerificationType::long2_type();
497       return 2;
498     case T_DOUBLE:
499       *inference_type = VerificationType::double_type();
500       *++inference_type = VerificationType::double2_type();
501       return 2;
502     case T_INT:
503     case T_BOOLEAN:
504     case T_BYTE:
505     case T_CHAR:
506     case T_SHORT:
507       *inference_type = VerificationType::integer_type();
508       return 1;
509     case T_FLOAT:
510       *inference_type = VerificationType::float_type();
511       return 1;

 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 #include "utilities/resourceHash.hpp"
 35 
 36 // The verifier class
 37 class Verifier : AllStatic {
 38  public:
 39   enum {
 40     STACKMAP_ATTRIBUTE_MAJOR_VERSION    = 50,
 41     INVOKEDYNAMIC_MAJOR_VERSION         = 51,
 42     NO_RELAX_ACCESS_CTRL_CHECK_VERSION  = 52,
 43     DYNAMICCONSTANT_MAJOR_VERSION       = 55,
 44   };
 45 
 46   // Verify the bytecodes for a class.
 47   static bool verify(InstanceKlass* klass, bool should_verify_class, TRAPS);
 48 
 49   static void log_end_verification(outputStream* st, const char* klassName, Symbol* exception_name,
 50                                     oop pending_exception);
 51 
 52   // Return false if the class is loaded by the bootstrap loader,
 53   // or if defineClass was called requesting skipping verification
 54   // -Xverify:all overrides this value
 55   static bool should_verify_for(oop class_loader, bool should_verify_class);
 56 
 57   // Relax certain access checks to enable some broken 1.1 apps to run on 1.2.
 58   static bool relax_access_for(oop class_loader);
 59 
 60   // Print output for class+resolve
 61   static void trace_class_resolution(Klass* resolve_class, InstanceKlass* verify_class);
 62 
 63  private:

138 
139 #ifdef ASSERT
140   void print_on(outputStream* str) const;
141 #endif
142 };
143 
144 class ErrorContext {
145  private:
146   typedef enum {
147     INVALID_BYTECODE,     // There was a problem with the bytecode
148     WRONG_TYPE,           // Type value was not as expected
149     FLAGS_MISMATCH,       // Frame flags are not assignable
150     BAD_CP_INDEX,         // Invalid constant pool index
151     BAD_LOCAL_INDEX,      // Invalid local index
152     LOCALS_SIZE_MISMATCH, // Frames have differing local counts
153     STACK_SIZE_MISMATCH,  // Frames have different stack sizes
154     STACK_OVERFLOW,       // Attempt to push onto a full expression stack
155     STACK_UNDERFLOW,      // Attempt to pop and empty expression stack
156     MISSING_STACKMAP,     // No stackmap for this location and there should be
157     BAD_STACKMAP,         // Format error in stackmap
158     WRONG_INLINE_TYPE,    // Mismatched inline type
159     NO_FAULT,             // No error
160     UNKNOWN
161   } FaultType;
162 
163   int _bci;
164   FaultType _fault;
165   TypeOrigin _type;
166   TypeOrigin _expected;
167 
168   ErrorContext(int bci, FaultType fault) :
169       _bci(bci), _fault(fault)  {}
170   ErrorContext(int bci, FaultType fault, TypeOrigin type) :
171       _bci(bci), _fault(fault), _type(type)  {}
172   ErrorContext(int bci, FaultType fault, TypeOrigin type, TypeOrigin exp) :
173       _bci(bci), _fault(fault), _type(type), _expected(exp)  {}
174 
175  public:
176   ErrorContext() : _bci(-1), _fault(NO_FAULT) {}
177 
178   static ErrorContext bad_code(u2 bci) {

202     return ErrorContext(bci, LOCALS_SIZE_MISMATCH,
203         TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
204   }
205   static ErrorContext stack_size_mismatch(
206       u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) {
207     return ErrorContext(bci, STACK_SIZE_MISMATCH,
208         TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
209   }
210   static ErrorContext stack_overflow(u2 bci, StackMapFrame* frame) {
211     return ErrorContext(bci, STACK_OVERFLOW, TypeOrigin::frame(frame));
212   }
213   static ErrorContext stack_underflow(u2 bci, StackMapFrame* frame) {
214     return ErrorContext(bci, STACK_UNDERFLOW, TypeOrigin::frame(frame));
215   }
216   static ErrorContext missing_stackmap(u2 bci) {
217     return ErrorContext(bci, MISSING_STACKMAP);
218   }
219   static ErrorContext bad_stackmap(int index, StackMapFrame* frame) {
220     return ErrorContext(0, BAD_STACKMAP, TypeOrigin::frame(frame));
221   }
222   static ErrorContext bad_inline_type(u2 bci, TypeOrigin type, TypeOrigin exp) {
223     return ErrorContext(bci, WRONG_INLINE_TYPE, type, exp);
224   }
225 
226   bool is_valid() const { return _fault != NO_FAULT; }
227   int bci() const { return _bci; }
228 
229   void reset_frames() {
230     _type.reset_frame();
231     _expected.reset_frame();
232   }
233 
234   void details(outputStream* ss, const Method* method) const;
235 
236 #ifdef ASSERT
237   void print_on(outputStream* str) const {
238     str->print("error_context(%d, %d,", _bci, _fault);
239     _type.print_on(str);
240     str->print(",");
241     _expected.print_on(str);
242     str->print(")");
243   }
244 #endif

335 
336   void verify_invoke_init(
337     RawBytecodeStream* bcs, u2 ref_index, VerificationType ref_class_type,
338     StackMapFrame* current_frame, u4 code_length, bool in_try_block,
339     bool* this_uninit, const constantPoolHandle& cp, StackMapTable* stackmap_table,
340     TRAPS);
341 
342   // Used by ends_in_athrow() to push all handlers that contain bci onto the
343   // handler_stack, if the handler has not already been pushed on the stack.
344   void push_handlers(ExceptionTable* exhandlers,
345                      GrowableArray<u4>* handler_list,
346                      GrowableArray<u4>* handler_stack,
347                      u4 bci);
348 
349   // Returns true if all paths starting with start_bc_offset end in athrow
350   // bytecode or loop.
351   bool ends_in_athrow(u4 start_bc_offset);
352 
353   void verify_invoke_instructions(
354     RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
355     bool in_try_block, bool* this_uninit,
356     const constantPoolHandle& cp, StackMapTable* stackmap_table, TRAPS);
357 
358   VerificationType get_newarray_type(u2 index, u2 bci, TRAPS);
359   void verify_anewarray(u2 bci, u2 index, const constantPoolHandle& cp,
360       StackMapFrame* current_frame, TRAPS);
361   void verify_return_value(
362       VerificationType return_type, VerificationType type, u2 offset,
363       StackMapFrame* current_frame, TRAPS);
364 
365   void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS);
366   void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS);
367   void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS);
368   void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS);
369   void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS);
370   void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS);
371   void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS);
372   void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS);
373   void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS);
374   void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS);
375   void verify_iinc  (u2 index, StackMapFrame* current_frame, TRAPS);

436 
437   // Called when verify or class format errors are encountered.
438   // May throw an exception based upon the mode.
439   void verify_error(ErrorContext ctx, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);
440   void class_format_error(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3);
441 
442   Klass* load_class(Symbol* name, TRAPS);
443 
444   method_signatures_table_type* method_signatures_table() const {
445     return _method_signatures_table;
446   }
447 
448   void set_method_signatures_table(method_signatures_table_type* method_signatures_table) {
449     _method_signatures_table = method_signatures_table;
450   }
451 
452   int change_sig_to_verificationType(
453     SignatureStream* sig_type, VerificationType* inference_type);
454 
455   VerificationType cp_index_to_type(int index, const constantPoolHandle& cp, TRAPS) {
456     Symbol* name = cp->klass_name_at(index);
457     if (name->is_Q_signature()) {
458       // Remove the Q and ;
459       // TBD need error msg if fundamental_name() returns NULL?
460       Symbol* fund_name = name->fundamental_name(CHECK_(VerificationType::bogus_type()));
461       return VerificationType::inline_type(fund_name);
462     }
463     return VerificationType::reference_type(name);
464   }
465 
466   // Keep a list of temporary symbols created during verification because
467   // their reference counts need to be decremented when the verifier object
468   // goes out of scope.  Since these symbols escape the scope in which they're
469   // created, we can't use a TempNewSymbol.
470   Symbol* create_temporary_symbol(const char *s, int length);
471   Symbol* create_temporary_symbol(Symbol* s) {
472     if (s == _previous_symbol) {
473       return s;
474     }
475     if (!s->is_permanent()) {
476       s->increment_refcount();
477       if (_symbols == NULL) {
478         _symbols = new GrowableArray<Symbol*>(50, 0, NULL);
479       }
480       _symbols->push(s);
481     }
482     _previous_symbol = s;
483     return s;
484   }
485 
486   TypeOrigin ref_ctx(const char* str);
487 
488 };
489 
490 inline int ClassVerifier::change_sig_to_verificationType(
491     SignatureStream* sig_type, VerificationType* inference_type) {
492   BasicType bt = sig_type->type();
493   switch (bt) {
494     case T_OBJECT:
495     case T_ARRAY:
496       {
497         Symbol* name = sig_type->as_symbol();
498         // Create another symbol to save as signature stream unreferences this symbol.
499         Symbol* name_copy = create_temporary_symbol(name);
500         assert(name_copy == name, "symbols don't match");
501         *inference_type = VerificationType::reference_type(name_copy);
502         return 1;
503       }
504     case T_INLINE_TYPE:
505       {
506         Symbol* vname = sig_type->as_symbol();
507         // Create another symbol to save as signature stream unreferences this symbol.
508         Symbol* vname_copy = create_temporary_symbol(vname);
509         assert(vname_copy == vname, "symbols don't match");
510         *inference_type = VerificationType::inline_type(vname_copy);
511         return 1;
512       }
513     case T_LONG:
514       *inference_type = VerificationType::long_type();
515       *++inference_type = VerificationType::long2_type();
516       return 2;
517     case T_DOUBLE:
518       *inference_type = VerificationType::double_type();
519       *++inference_type = VerificationType::double2_type();
520       return 2;
521     case T_INT:
522     case T_BOOLEAN:
523     case T_BYTE:
524     case T_CHAR:
525     case T_SHORT:
526       *inference_type = VerificationType::integer_type();
527       return 1;
528     case T_FLOAT:
529       *inference_type = VerificationType::float_type();
530       return 1;
< prev index next >