src/share/vm/classfile/verifier.hpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2297
1070423b51f3
child 2497
3582bf76420e
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_CLASSFILE_VERIFIER_HPP
stefank@2314 26 #define SHARE_VM_CLASSFILE_VERIFIER_HPP
stefank@2314 27
stefank@2314 28 #include "classfile/verificationType.hpp"
stefank@2314 29 #include "memory/gcLocker.hpp"
stefank@2314 30 #include "oops/klass.hpp"
stefank@2314 31 #include "oops/methodOop.hpp"
stefank@2314 32 #include "runtime/handles.hpp"
stefank@2314 33 #include "utilities/exceptions.hpp"
stefank@2314 34
duke@435 35 // The verifier class
duke@435 36 class Verifier : AllStatic {
duke@435 37 public:
jrose@1957 38 enum {
jrose@1957 39 STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50,
jrose@1957 40 INVOKEDYNAMIC_MAJOR_VERSION = 51
jrose@1957 41 };
duke@435 42 typedef enum { ThrowException, NoException } Mode;
duke@435 43
duke@435 44 /**
duke@435 45 * Verify the bytecodes for a class. If 'throw_exception' is true
duke@435 46 * then the appropriate VerifyError or ClassFormatError will be thrown.
duke@435 47 * Otherwise, no exception is thrown and the return indicates the
duke@435 48 * error.
duke@435 49 */
acorn@1408 50 static bool verify(instanceKlassHandle klass, Mode mode, bool should_verify_class, TRAPS);
duke@435 51
acorn@1408 52 // Return false if the class is loaded by the bootstrap loader,
acorn@1408 53 // or if defineClass was called requesting skipping verification
acorn@1408 54 // -Xverify:all/none override this value
acorn@1408 55 static bool should_verify_for(oop class_loader, bool should_verify_class);
duke@435 56
duke@435 57 // Relax certain verifier checks to enable some broken 1.1 apps to run on 1.2.
duke@435 58 static bool relax_verify_for(oop class_loader);
duke@435 59
duke@435 60 private:
acorn@1408 61 static bool is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class);
duke@435 62 static symbolHandle inference_verify(
duke@435 63 instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS);
duke@435 64 };
duke@435 65
duke@435 66 class RawBytecodeStream;
duke@435 67 class StackMapFrame;
duke@435 68 class StackMapTable;
duke@435 69
duke@435 70 // Summary of verifier's memory usage:
duke@435 71 // StackMapTable is stack allocated.
duke@435 72 // StackMapFrame are resource allocated. There is one ResourceMark
duke@435 73 // for each method.
duke@435 74 // There is one mutable StackMapFrame (current_frame) which is updated
duke@435 75 // by abstract bytecode interpretation. frame_in_exception_handler() returns
duke@435 76 // a frame that has a mutable one-item stack (ready for pushing the
duke@435 77 // catch type exception object). All the other StackMapFrame's
duke@435 78 // are immutable (including their locals and stack arrays) after
duke@435 79 // their constructions.
duke@435 80 // locals/stack arrays in StackMapFrame are resource allocated.
duke@435 81 // locals/stack arrays can be shared between StackMapFrame's, except
duke@435 82 // the mutable StackMapFrame (current_frame).
duke@435 83 // Care needs to be taken to make sure resource objects don't outlive
duke@435 84 // the lifetime of their ResourceMark.
duke@435 85
duke@435 86 // These macros are used similarly to CHECK macros but also check
duke@435 87 // the status of the verifier and return if that has an error.
duke@435 88 #define CHECK_VERIFY(verifier) \
duke@435 89 CHECK); if ((verifier)->has_error()) return; (0
duke@435 90 #define CHECK_VERIFY_(verifier, result) \
duke@435 91 CHECK_(result)); if ((verifier)->has_error()) return (result); (0
duke@435 92
duke@435 93 // A new instance of this class is created for each class being verified
duke@435 94 class ClassVerifier : public StackObj {
duke@435 95 private:
duke@435 96 Thread* _thread;
duke@435 97 symbolHandle _exception_type;
duke@435 98 char* _message;
duke@435 99 size_t _message_buffer_len;
duke@435 100
duke@435 101 void verify_method(methodHandle method, TRAPS);
duke@435 102 char* generate_code_data(methodHandle m, u4 code_length, TRAPS);
duke@435 103 void verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS);
duke@435 104 void verify_local_variable_table(u4 code_length, char* code_data, TRAPS);
duke@435 105
duke@435 106 VerificationType cp_ref_index_to_type(
duke@435 107 int index, constantPoolHandle cp, TRAPS) {
duke@435 108 return cp_index_to_type(cp->klass_ref_index_at(index), cp, THREAD);
duke@435 109 }
duke@435 110
duke@435 111 bool is_protected_access(
duke@435 112 instanceKlassHandle this_class, klassOop target_class,
duke@435 113 symbolOop field_name, symbolOop field_sig, bool is_method);
duke@435 114
duke@435 115 void verify_cp_index(constantPoolHandle cp, int index, TRAPS);
duke@435 116 void verify_cp_type(
duke@435 117 int index, constantPoolHandle cp, unsigned int types, TRAPS);
duke@435 118 void verify_cp_class_type(int index, constantPoolHandle cp, TRAPS);
duke@435 119
duke@435 120 u2 verify_stackmap_table(
duke@435 121 u2 stackmap_index, u2 bci, StackMapFrame* current_frame,
duke@435 122 StackMapTable* stackmap_table, bool no_control_flow, TRAPS);
duke@435 123
duke@435 124 void verify_exception_handler_targets(
duke@435 125 u2 bci, bool this_uninit, StackMapFrame* current_frame,
duke@435 126 StackMapTable* stackmap_table, TRAPS);
duke@435 127
duke@435 128 void verify_ldc(
duke@435 129 int opcode, u2 index, StackMapFrame *current_frame,
duke@435 130 constantPoolHandle cp, u2 bci, TRAPS);
duke@435 131
duke@435 132 void verify_switch(
duke@435 133 RawBytecodeStream* bcs, u4 code_length, char* code_data,
duke@435 134 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS);
duke@435 135
duke@435 136 void verify_field_instructions(
duke@435 137 RawBytecodeStream* bcs, StackMapFrame* current_frame,
duke@435 138 constantPoolHandle cp, TRAPS);
duke@435 139
duke@435 140 void verify_invoke_init(
duke@435 141 RawBytecodeStream* bcs, VerificationType ref_class_type,
duke@435 142 StackMapFrame* current_frame, u4 code_length, bool* this_uninit,
duke@435 143 constantPoolHandle cp, TRAPS);
duke@435 144
duke@435 145 void verify_invoke_instructions(
duke@435 146 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame,
duke@435 147 bool* this_uninit, VerificationType return_type,
duke@435 148 constantPoolHandle cp, TRAPS);
duke@435 149
duke@435 150 VerificationType get_newarray_type(u2 index, u2 bci, TRAPS);
duke@435 151 void verify_anewarray(
duke@435 152 u2 index, constantPoolHandle cp, StackMapFrame* current_frame, TRAPS);
duke@435 153 void verify_return_value(
duke@435 154 VerificationType return_type, VerificationType type, u2 offset, TRAPS);
duke@435 155
duke@435 156 void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 157 void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 158 void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 159 void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 160 void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 161 void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 162 void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 163 void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 164 void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 165 void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 166 void verify_iinc (u2 index, StackMapFrame* current_frame, TRAPS);
duke@435 167
duke@435 168 bool name_in_supers(symbolOop ref_name, instanceKlassHandle current);
duke@435 169
kamg@2297 170 VerificationType object_type() const;
kamg@2297 171
duke@435 172 instanceKlassHandle _klass; // the class being verified
duke@435 173 methodHandle _method; // current method being verified
duke@435 174 VerificationType _this_type; // the verification type of the current class
duke@435 175
jrose@1925 176 // Some recursive calls from the verifier to the name resolver
jrose@1925 177 // can cause the current class to be re-verified and rewritten.
jrose@1925 178 // If this happens, the original verification should not continue,
jrose@1925 179 // because constant pool indexes will have changed.
jrose@1925 180 // The rewriter is preceded by the verifier. If the verifier throws
jrose@1925 181 // an error, rewriting is prevented. Also, rewriting always precedes
jrose@1925 182 // bytecode execution or compilation. Thus, is_rewritten implies
jrose@1925 183 // that a class has been verified and prepared for execution.
jrose@1925 184 bool was_recursively_verified() { return _klass->is_rewritten(); }
jrose@1925 185
duke@435 186 public:
duke@435 187 enum {
duke@435 188 BYTECODE_OFFSET = 1,
duke@435 189 NEW_OFFSET = 2
duke@435 190 };
duke@435 191
duke@435 192 // constructor
duke@435 193 ClassVerifier(instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS);
duke@435 194
duke@435 195 // destructor
duke@435 196 ~ClassVerifier();
duke@435 197
duke@435 198 Thread* thread() { return _thread; }
duke@435 199 methodHandle method() { return _method; }
duke@435 200 instanceKlassHandle current_class() const { return _klass; }
duke@435 201 VerificationType current_type() const { return _this_type; }
duke@435 202
duke@435 203 // Verifies the class. If a verify or class file format error occurs,
duke@435 204 // the '_exception_name' symbols will set to the exception name and
duke@435 205 // the message_buffer will be filled in with the exception message.
duke@435 206 void verify_class(TRAPS);
duke@435 207
duke@435 208 // Return status modes
duke@435 209 symbolHandle result() const { return _exception_type; }
duke@435 210 bool has_error() const { return !(result().is_null()); }
duke@435 211
duke@435 212 // Called when verify or class format errors are encountered.
duke@435 213 // May throw an exception based upon the mode.
duke@435 214 void verify_error(u2 offset, const char* fmt, ...);
duke@435 215 void verify_error(const char* fmt, ...);
duke@435 216 void class_format_error(const char* fmt, ...);
duke@435 217 void format_error_message(const char* fmt, int offset, va_list args);
duke@435 218
duke@435 219 klassOop load_class(symbolHandle name, TRAPS);
duke@435 220
duke@435 221 int change_sig_to_verificationType(
duke@435 222 SignatureStream* sig_type, VerificationType* inference_type, TRAPS);
duke@435 223
duke@435 224 VerificationType cp_index_to_type(int index, constantPoolHandle cp, TRAPS) {
duke@435 225 return VerificationType::reference_type(
duke@435 226 symbolHandle(THREAD, cp->klass_name_at(index)));
duke@435 227 }
duke@435 228
duke@435 229 static bool _verify_verbose; // for debugging
duke@435 230 };
duke@435 231
duke@435 232 inline int ClassVerifier::change_sig_to_verificationType(
duke@435 233 SignatureStream* sig_type, VerificationType* inference_type, TRAPS) {
duke@435 234 BasicType bt = sig_type->type();
duke@435 235 switch (bt) {
duke@435 236 case T_OBJECT:
duke@435 237 case T_ARRAY:
duke@435 238 {
duke@435 239 symbolOop name = sig_type->as_symbol(CHECK_0);
duke@435 240 *inference_type =
duke@435 241 VerificationType::reference_type(symbolHandle(THREAD, name));
duke@435 242 return 1;
duke@435 243 }
duke@435 244 case T_LONG:
duke@435 245 *inference_type = VerificationType::long_type();
duke@435 246 *++inference_type = VerificationType::long2_type();
duke@435 247 return 2;
duke@435 248 case T_DOUBLE:
duke@435 249 *inference_type = VerificationType::double_type();
duke@435 250 *++inference_type = VerificationType::double2_type();
duke@435 251 return 2;
duke@435 252 case T_INT:
duke@435 253 case T_BOOLEAN:
duke@435 254 case T_BYTE:
duke@435 255 case T_CHAR:
duke@435 256 case T_SHORT:
duke@435 257 *inference_type = VerificationType::integer_type();
duke@435 258 return 1;
duke@435 259 case T_FLOAT:
duke@435 260 *inference_type = VerificationType::float_type();
duke@435 261 return 1;
duke@435 262 default:
duke@435 263 ShouldNotReachHere();
duke@435 264 return 1;
duke@435 265 }
duke@435 266 }
stefank@2314 267
stefank@2314 268 #endif // SHARE_VM_CLASSFILE_VERIFIER_HPP

mercurial