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);
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 NO_FAULT, // No error
157 UNKNOWN
158 } FaultType;
159
160 int _bci;
161 FaultType _fault;
162 TypeOrigin _type;
163 TypeOrigin _expected;
164
165 ErrorContext(int bci, FaultType fault) :
166 _bci(bci), _fault(fault) {}
167 ErrorContext(int bci, FaultType fault, TypeOrigin type) :
168 _bci(bci), _fault(fault), _type(type) {}
169 ErrorContext(int bci, FaultType fault, TypeOrigin type, TypeOrigin exp) :
170 _bci(bci), _fault(fault), _type(type), _expected(exp) {}
171
172 public:
173 ErrorContext() : _bci(-1), _fault(NO_FAULT) {}
174
175 static ErrorContext bad_code(u2 bci) {
199 return ErrorContext(bci, LOCALS_SIZE_MISMATCH,
200 TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
201 }
202 static ErrorContext stack_size_mismatch(
203 u2 bci, StackMapFrame* frame0, StackMapFrame* frame1) {
204 return ErrorContext(bci, STACK_SIZE_MISMATCH,
205 TypeOrigin::frame(frame0), TypeOrigin::frame(frame1));
206 }
207 static ErrorContext stack_overflow(u2 bci, StackMapFrame* frame) {
208 return ErrorContext(bci, STACK_OVERFLOW, TypeOrigin::frame(frame));
209 }
210 static ErrorContext stack_underflow(u2 bci, StackMapFrame* frame) {
211 return ErrorContext(bci, STACK_UNDERFLOW, TypeOrigin::frame(frame));
212 }
213 static ErrorContext missing_stackmap(u2 bci) {
214 return ErrorContext(bci, MISSING_STACKMAP);
215 }
216 static ErrorContext bad_stackmap(int index, StackMapFrame* frame) {
217 return ErrorContext(0, BAD_STACKMAP, TypeOrigin::frame(frame));
218 }
219
220 bool is_valid() const { return _fault != NO_FAULT; }
221 int bci() const { return _bci; }
222
223 void reset_frames() {
224 _type.reset_frame();
225 _expected.reset_frame();
226 }
227
228 void details(outputStream* ss, const Method* method) const;
229
230 #ifdef ASSERT
231 void print_on(outputStream* str) const {
232 str->print("error_context(%d, %d,", _bci, _fault);
233 _type.print_on(str);
234 str->print(",");
235 _expected.print_on(str);
236 str->print(")");
237 }
238 #endif
385 Symbol* result() const { return _exception_type; }
386 bool has_error() const { return result() != NULL; }
387 char* exception_message() {
388 stringStream ss;
389 ss.print("%s", _message);
390 _error_context.details(&ss, _method());
391 return ss.as_string();
392 }
393
394 // Called when verify or class format errors are encountered.
395 // May throw an exception based upon the mode.
396 void verify_error(ErrorContext ctx, const char* fmt, ...) ATTRIBUTE_PRINTF(3, 4);
397 void class_format_error(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3);
398
399 Klass* load_class(Symbol* name, TRAPS);
400
401 int change_sig_to_verificationType(
402 SignatureStream* sig_type, VerificationType* inference_type, TRAPS);
403
404 VerificationType cp_index_to_type(int index, const constantPoolHandle& cp, TRAPS) {
405 return VerificationType::reference_type(cp->klass_name_at(index));
406 }
407
408 // Keep a list of temporary symbols created during verification because
409 // their reference counts need to be decremented when the verifier object
410 // goes out of scope. Since these symbols escape the scope in which they're
411 // created, we can't use a TempNewSymbol.
412 Symbol* create_temporary_symbol(const Symbol* s, int begin, int end, TRAPS);
413 Symbol* create_temporary_symbol(const char *s, int length, TRAPS);
414
415 Symbol* create_temporary_symbol(Symbol* s) {
416 // This version just updates the reference count and saves the symbol to be
417 // dereferenced later.
418 s->increment_refcount();
419 _symbols->push(s);
420 return s;
421 }
422
423 TypeOrigin ref_ctx(const char* str, TRAPS);
424
425 };
426
427 inline int ClassVerifier::change_sig_to_verificationType(
428 SignatureStream* sig_type, VerificationType* inference_type, TRAPS) {
429 BasicType bt = sig_type->type();
430 switch (bt) {
431 case T_OBJECT:
432 case T_ARRAY:
433 {
434 Symbol* name = sig_type->as_symbol(CHECK_0);
435 // Create another symbol to save as signature stream unreferences this symbol.
436 Symbol* name_copy = create_temporary_symbol(name);
437 assert(name_copy == name, "symbols don't match");
438 *inference_type =
439 VerificationType::reference_type(name_copy);
440 return 1;
441 }
442 case T_LONG:
443 *inference_type = VerificationType::long_type();
444 *++inference_type = VerificationType::long2_type();
445 return 2;
446 case T_DOUBLE:
447 *inference_type = VerificationType::double_type();
448 *++inference_type = VerificationType::double2_type();
449 return 2;
450 case T_INT:
451 case T_BOOLEAN:
452 case T_BYTE:
453 case T_CHAR:
454 case T_SHORT:
455 *inference_type = VerificationType::integer_type();
456 return 1;
457 case T_FLOAT:
458 *inference_type = VerificationType::float_type();
459 return 1;
|
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);
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) {
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
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;
|