verifier.hpp revision 1472:c18cbe5936b8
1/* 2 * Copyright (c) 1998, 2006, 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// The verifier class 26class Verifier : AllStatic { 27 public: 28 enum { STACKMAP_ATTRIBUTE_MAJOR_VERSION = 50 }; 29 typedef enum { ThrowException, NoException } Mode; 30 31 /** 32 * Verify the bytecodes for a class. If 'throw_exception' is true 33 * then the appropriate VerifyError or ClassFormatError will be thrown. 34 * Otherwise, no exception is thrown and the return indicates the 35 * error. 36 */ 37 static bool verify(instanceKlassHandle klass, Mode mode, bool should_verify_class, TRAPS); 38 39 // Return false if the class is loaded by the bootstrap loader, 40 // or if defineClass was called requesting skipping verification 41 // -Xverify:all/none override this value 42 static bool should_verify_for(oop class_loader, bool should_verify_class); 43 44 // Relax certain verifier checks to enable some broken 1.1 apps to run on 1.2. 45 static bool relax_verify_for(oop class_loader); 46 47 private: 48 static bool is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class); 49 static symbolHandle inference_verify( 50 instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS); 51}; 52 53class RawBytecodeStream; 54class StackMapFrame; 55class StackMapTable; 56 57// Summary of verifier's memory usage: 58// StackMapTable is stack allocated. 59// StackMapFrame are resource allocated. There is one ResourceMark 60// for each method. 61// There is one mutable StackMapFrame (current_frame) which is updated 62// by abstract bytecode interpretation. frame_in_exception_handler() returns 63// a frame that has a mutable one-item stack (ready for pushing the 64// catch type exception object). All the other StackMapFrame's 65// are immutable (including their locals and stack arrays) after 66// their constructions. 67// locals/stack arrays in StackMapFrame are resource allocated. 68// locals/stack arrays can be shared between StackMapFrame's, except 69// the mutable StackMapFrame (current_frame). 70// Care needs to be taken to make sure resource objects don't outlive 71// the lifetime of their ResourceMark. 72 73// These macros are used similarly to CHECK macros but also check 74// the status of the verifier and return if that has an error. 75#define CHECK_VERIFY(verifier) \ 76 CHECK); if ((verifier)->has_error()) return; (0 77#define CHECK_VERIFY_(verifier, result) \ 78 CHECK_(result)); if ((verifier)->has_error()) return (result); (0 79 80// A new instance of this class is created for each class being verified 81class ClassVerifier : public StackObj { 82 private: 83 Thread* _thread; 84 symbolHandle _exception_type; 85 char* _message; 86 size_t _message_buffer_len; 87 88 void verify_method(methodHandle method, TRAPS); 89 char* generate_code_data(methodHandle m, u4 code_length, TRAPS); 90 void verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS); 91 void verify_local_variable_table(u4 code_length, char* code_data, TRAPS); 92 93 VerificationType cp_ref_index_to_type( 94 int index, constantPoolHandle cp, TRAPS) { 95 return cp_index_to_type(cp->klass_ref_index_at(index), cp, THREAD); 96 } 97 98 bool is_protected_access( 99 instanceKlassHandle this_class, klassOop target_class, 100 symbolOop field_name, symbolOop field_sig, bool is_method); 101 102 void verify_cp_index(constantPoolHandle cp, int index, TRAPS); 103 void verify_cp_type( 104 int index, constantPoolHandle cp, unsigned int types, TRAPS); 105 void verify_cp_class_type(int index, constantPoolHandle cp, TRAPS); 106 107 u2 verify_stackmap_table( 108 u2 stackmap_index, u2 bci, StackMapFrame* current_frame, 109 StackMapTable* stackmap_table, bool no_control_flow, TRAPS); 110 111 void verify_exception_handler_targets( 112 u2 bci, bool this_uninit, StackMapFrame* current_frame, 113 StackMapTable* stackmap_table, TRAPS); 114 115 void verify_ldc( 116 int opcode, u2 index, StackMapFrame *current_frame, 117 constantPoolHandle cp, u2 bci, TRAPS); 118 119 void verify_switch( 120 RawBytecodeStream* bcs, u4 code_length, char* code_data, 121 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS); 122 123 void verify_field_instructions( 124 RawBytecodeStream* bcs, StackMapFrame* current_frame, 125 constantPoolHandle cp, TRAPS); 126 127 void verify_invoke_init( 128 RawBytecodeStream* bcs, VerificationType ref_class_type, 129 StackMapFrame* current_frame, u4 code_length, bool* this_uninit, 130 constantPoolHandle cp, TRAPS); 131 132 void verify_invoke_instructions( 133 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame, 134 bool* this_uninit, VerificationType return_type, 135 constantPoolHandle cp, TRAPS); 136 137 VerificationType get_newarray_type(u2 index, u2 bci, TRAPS); 138 void verify_anewarray( 139 u2 index, constantPoolHandle cp, StackMapFrame* current_frame, TRAPS); 140 void verify_return_value( 141 VerificationType return_type, VerificationType type, u2 offset, TRAPS); 142 143 void verify_iload (u2 index, StackMapFrame* current_frame, TRAPS); 144 void verify_lload (u2 index, StackMapFrame* current_frame, TRAPS); 145 void verify_fload (u2 index, StackMapFrame* current_frame, TRAPS); 146 void verify_dload (u2 index, StackMapFrame* current_frame, TRAPS); 147 void verify_aload (u2 index, StackMapFrame* current_frame, TRAPS); 148 void verify_istore(u2 index, StackMapFrame* current_frame, TRAPS); 149 void verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS); 150 void verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS); 151 void verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS); 152 void verify_astore(u2 index, StackMapFrame* current_frame, TRAPS); 153 void verify_iinc (u2 index, StackMapFrame* current_frame, TRAPS); 154 155 bool name_in_supers(symbolOop ref_name, instanceKlassHandle current); 156 157 instanceKlassHandle _klass; // the class being verified 158 methodHandle _method; // current method being verified 159 VerificationType _this_type; // the verification type of the current class 160 161 public: 162 enum { 163 BYTECODE_OFFSET = 1, 164 NEW_OFFSET = 2 165 }; 166 167 // constructor 168 ClassVerifier(instanceKlassHandle klass, char* msg, size_t msg_len, TRAPS); 169 170 // destructor 171 ~ClassVerifier(); 172 173 Thread* thread() { return _thread; } 174 methodHandle method() { return _method; } 175 instanceKlassHandle current_class() const { return _klass; } 176 VerificationType current_type() const { return _this_type; } 177 178 // Verifies the class. If a verify or class file format error occurs, 179 // the '_exception_name' symbols will set to the exception name and 180 // the message_buffer will be filled in with the exception message. 181 void verify_class(TRAPS); 182 183 // Return status modes 184 symbolHandle result() const { return _exception_type; } 185 bool has_error() const { return !(result().is_null()); } 186 187 // Called when verify or class format errors are encountered. 188 // May throw an exception based upon the mode. 189 void verify_error(u2 offset, const char* fmt, ...); 190 void verify_error(const char* fmt, ...); 191 void class_format_error(const char* fmt, ...); 192 void format_error_message(const char* fmt, int offset, va_list args); 193 194 klassOop load_class(symbolHandle name, TRAPS); 195 196 int change_sig_to_verificationType( 197 SignatureStream* sig_type, VerificationType* inference_type, TRAPS); 198 199 VerificationType cp_index_to_type(int index, constantPoolHandle cp, TRAPS) { 200 return VerificationType::reference_type( 201 symbolHandle(THREAD, cp->klass_name_at(index))); 202 } 203 204 static bool _verify_verbose; // for debugging 205}; 206 207inline int ClassVerifier::change_sig_to_verificationType( 208 SignatureStream* sig_type, VerificationType* inference_type, TRAPS) { 209 BasicType bt = sig_type->type(); 210 switch (bt) { 211 case T_OBJECT: 212 case T_ARRAY: 213 { 214 symbolOop name = sig_type->as_symbol(CHECK_0); 215 *inference_type = 216 VerificationType::reference_type(symbolHandle(THREAD, name)); 217 return 1; 218 } 219 case T_LONG: 220 *inference_type = VerificationType::long_type(); 221 *++inference_type = VerificationType::long2_type(); 222 return 2; 223 case T_DOUBLE: 224 *inference_type = VerificationType::double_type(); 225 *++inference_type = VerificationType::double2_type(); 226 return 2; 227 case T_INT: 228 case T_BOOLEAN: 229 case T_BYTE: 230 case T_CHAR: 231 case T_SHORT: 232 *inference_type = VerificationType::integer_type(); 233 return 1; 234 case T_FLOAT: 235 *inference_type = VerificationType::float_type(); 236 return 1; 237 default: 238 ShouldNotReachHere(); 239 return 1; 240 } 241} 242