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