Thu, 30 Oct 2008 15:48:59 -0400
6756528: Bytecodes::special_length_at reads past end of code buffer
Summary: Add end-of-buffer indicator for paths used by the verifier
Reviewed-by: acorn, coleenp
duke@435 | 1 | /* |
duke@435 | 2 | * Copyright 1997-2002 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | // Base class for different kinds of abstractions working |
duke@435 | 26 | // relative to an objects 'this' pointer. |
duke@435 | 27 | |
duke@435 | 28 | class ThisRelativeObj VALUE_OBJ_CLASS_SPEC { |
duke@435 | 29 | private: |
duke@435 | 30 | int sign_extend (int x, int size) const { const int s = (BytesPerInt - size)*BitsPerByte; return (x << s) >> s; } |
duke@435 | 31 | |
duke@435 | 32 | public: |
duke@435 | 33 | // Address computation |
duke@435 | 34 | address addr_at (int offset) const { return (address)this + offset; } |
duke@435 | 35 | address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); } |
duke@435 | 36 | int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); } |
duke@435 | 37 | |
duke@435 | 38 | // Java unsigned accessors (using Java spec byte ordering) |
duke@435 | 39 | int java_byte_at (int offset) const { return *(jubyte*)addr_at(offset); } |
duke@435 | 40 | int java_hwrd_at (int offset) const { return java_byte_at(offset) << (1 * BitsPerByte) | java_byte_at(offset + 1); } |
duke@435 | 41 | int java_word_at (int offset) const { return java_hwrd_at(offset) << (2 * BitsPerByte) | java_hwrd_at(offset + 2); } |
duke@435 | 42 | |
duke@435 | 43 | // Java signed accessors (using Java spec byte ordering) |
duke@435 | 44 | int java_signed_byte_at(int offset) const { return sign_extend(java_byte_at(offset), 1); } |
duke@435 | 45 | int java_signed_hwrd_at(int offset) const { return sign_extend(java_hwrd_at(offset), 2); } |
duke@435 | 46 | int java_signed_word_at(int offset) const { return java_word_at(offset) ; } |
duke@435 | 47 | |
duke@435 | 48 | // Fast accessors (using the machine's natural byte ordering) |
duke@435 | 49 | int fast_byte_at (int offset) const { return *(jubyte *)addr_at(offset); } |
duke@435 | 50 | int fast_hwrd_at (int offset) const { return *(jushort*)addr_at(offset); } |
duke@435 | 51 | int fast_word_at (int offset) const { return *(juint *)addr_at(offset); } |
duke@435 | 52 | |
duke@435 | 53 | // Fast signed accessors (using the machine's natural byte ordering) |
duke@435 | 54 | int fast_signed_byte_at(int offset) const { return *(jbyte *)addr_at(offset); } |
duke@435 | 55 | int fast_signed_hwrd_at(int offset) const { return *(jshort*)addr_at(offset); } |
duke@435 | 56 | int fast_signed_word_at(int offset) const { return *(jint *)addr_at(offset); } |
duke@435 | 57 | |
duke@435 | 58 | // Fast manipulators (using the machine's natural byte ordering) |
duke@435 | 59 | void set_fast_byte_at (int offset, int x) const { *(jbyte *)addr_at(offset) = (jbyte )x; } |
duke@435 | 60 | void set_fast_hwrd_at (int offset, int x) const { *(jshort*)addr_at(offset) = (jshort)x; } |
duke@435 | 61 | void set_fast_word_at (int offset, int x) const { *(jint *)addr_at(offset) = (jint )x; } |
duke@435 | 62 | }; |
duke@435 | 63 | |
duke@435 | 64 | |
duke@435 | 65 | // The base class for different kinds of bytecode abstractions. |
duke@435 | 66 | // Provides the primitive operations to manipulate code relative |
duke@435 | 67 | // to an objects 'this' pointer. |
duke@435 | 68 | // |
duke@435 | 69 | // Note: Even though it seems that the fast_index & set_fast_index |
duke@435 | 70 | // functions are machine specific, they're not. They only use |
duke@435 | 71 | // the natural way to store a 16bit index on a given machine, |
duke@435 | 72 | // independent of the particular byte ordering. Since all other |
duke@435 | 73 | // places in the system that refer to these indices use the |
duke@435 | 74 | // same method (the natural byte ordering on the platform) |
duke@435 | 75 | // this will always work and be machine-independent). |
duke@435 | 76 | |
duke@435 | 77 | class Bytecode: public ThisRelativeObj { |
duke@435 | 78 | protected: |
duke@435 | 79 | u_char byte_at(int offset) const { return *addr_at(offset); } |
duke@435 | 80 | bool check_must_rewrite() const; |
duke@435 | 81 | |
duke@435 | 82 | public: |
duke@435 | 83 | // Attributes |
duke@435 | 84 | address bcp() const { return addr_at(0); } |
duke@435 | 85 | address next_bcp() const { return addr_at(0) + Bytecodes::length_at(bcp()); } |
duke@435 | 86 | |
duke@435 | 87 | Bytecodes::Code code() const { return Bytecodes::code_at(addr_at(0)); } |
duke@435 | 88 | Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } |
duke@435 | 89 | bool must_rewrite() const { return Bytecodes::can_rewrite(code()) && check_must_rewrite(); } |
duke@435 | 90 | bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); } |
duke@435 | 91 | |
duke@435 | 92 | int one_byte_index() const { return byte_at(1); } |
duke@435 | 93 | int two_byte_index() const { return (byte_at(1) << 8) + byte_at(2); } |
duke@435 | 94 | int offset() const { return (two_byte_index() << 16) >> 16; } |
duke@435 | 95 | address destination() const { return bcp() + offset(); } |
duke@435 | 96 | int fast_index() const { return Bytes::get_native_u2(addr_at(1)); } |
duke@435 | 97 | |
duke@435 | 98 | // Attribute modification |
duke@435 | 99 | void set_code(Bytecodes::Code code); |
duke@435 | 100 | void set_fast_index(int i); |
duke@435 | 101 | |
duke@435 | 102 | // Creation |
duke@435 | 103 | inline friend Bytecode* Bytecode_at(address bcp); |
duke@435 | 104 | }; |
duke@435 | 105 | |
duke@435 | 106 | inline Bytecode* Bytecode_at(address bcp) { |
duke@435 | 107 | return (Bytecode*)bcp; |
duke@435 | 108 | } |
duke@435 | 109 | |
duke@435 | 110 | |
duke@435 | 111 | // Abstractions for lookupswitch bytecode |
duke@435 | 112 | |
duke@435 | 113 | class LookupswitchPair: ThisRelativeObj { |
duke@435 | 114 | private: |
duke@435 | 115 | int _match; |
duke@435 | 116 | int _offset; |
duke@435 | 117 | |
duke@435 | 118 | public: |
duke@435 | 119 | int match() const { return java_signed_word_at(0 * jintSize); } |
duke@435 | 120 | int offset() const { return java_signed_word_at(1 * jintSize); } |
duke@435 | 121 | }; |
duke@435 | 122 | |
duke@435 | 123 | |
duke@435 | 124 | class Bytecode_lookupswitch: public Bytecode { |
duke@435 | 125 | public: |
duke@435 | 126 | void verify() const PRODUCT_RETURN; |
duke@435 | 127 | |
duke@435 | 128 | // Attributes |
duke@435 | 129 | int default_offset() const { return java_signed_word_at(aligned_offset(1 + 0*jintSize)); } |
duke@435 | 130 | int number_of_pairs() const { return java_signed_word_at(aligned_offset(1 + 1*jintSize)); } |
duke@435 | 131 | LookupswitchPair* pair_at(int i) const { assert(0 <= i && i < number_of_pairs(), "pair index out of bounds"); |
duke@435 | 132 | return (LookupswitchPair*)aligned_addr_at(1 + (1 + i)*2*jintSize); } |
duke@435 | 133 | // Creation |
duke@435 | 134 | inline friend Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp); |
duke@435 | 135 | }; |
duke@435 | 136 | |
duke@435 | 137 | inline Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp) { |
duke@435 | 138 | Bytecode_lookupswitch* b = (Bytecode_lookupswitch*)bcp; |
duke@435 | 139 | debug_only(b->verify()); |
duke@435 | 140 | return b; |
duke@435 | 141 | } |
duke@435 | 142 | |
duke@435 | 143 | |
duke@435 | 144 | class Bytecode_tableswitch: public Bytecode { |
duke@435 | 145 | public: |
duke@435 | 146 | void verify() const PRODUCT_RETURN; |
duke@435 | 147 | |
duke@435 | 148 | // Attributes |
duke@435 | 149 | int default_offset() const { return java_signed_word_at(aligned_offset(1 + 0*jintSize)); } |
duke@435 | 150 | int low_key() const { return java_signed_word_at(aligned_offset(1 + 1*jintSize)); } |
duke@435 | 151 | int high_key() const { return java_signed_word_at(aligned_offset(1 + 2*jintSize)); } |
duke@435 | 152 | int dest_offset_at(int i) const; |
duke@435 | 153 | int length() { return high_key()-low_key()+1; } |
duke@435 | 154 | |
duke@435 | 155 | // Creation |
duke@435 | 156 | inline friend Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp); |
duke@435 | 157 | }; |
duke@435 | 158 | |
duke@435 | 159 | inline Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp) { |
duke@435 | 160 | Bytecode_tableswitch* b = (Bytecode_tableswitch*)bcp; |
duke@435 | 161 | debug_only(b->verify()); |
duke@435 | 162 | return b; |
duke@435 | 163 | } |
duke@435 | 164 | |
duke@435 | 165 | |
duke@435 | 166 | // Abstraction for invoke_{virtual, static, interface, special} |
duke@435 | 167 | |
duke@435 | 168 | class Bytecode_invoke: public ResourceObj { |
duke@435 | 169 | protected: |
duke@435 | 170 | methodHandle _method; // method containing the bytecode |
duke@435 | 171 | int _bci; // position of the bytecode |
duke@435 | 172 | |
duke@435 | 173 | Bytecode_invoke(methodHandle method, int bci) : _method(method), _bci(bci) {} |
duke@435 | 174 | |
duke@435 | 175 | public: |
duke@435 | 176 | void verify() const; |
duke@435 | 177 | |
duke@435 | 178 | // Attributes |
duke@435 | 179 | methodHandle method() const { return _method; } |
duke@435 | 180 | int bci() const { return _bci; } |
duke@435 | 181 | address bcp() const { return _method->bcp_from(bci()); } |
duke@435 | 182 | |
duke@435 | 183 | int index() const; // the constant pool index for the invoke |
duke@435 | 184 | symbolOop name() const; // returns the name of the invoked method |
duke@435 | 185 | symbolOop signature() const; // returns the signature of the invoked method |
duke@435 | 186 | BasicType result_type(Thread *thread) const; // returns the result type of the invoke |
duke@435 | 187 | |
duke@435 | 188 | Bytecodes::Code code() const { return Bytecodes::code_at(bcp(), _method()); } |
duke@435 | 189 | Bytecodes::Code adjusted_invoke_code() const { return Bytecodes::java_code(code()); } |
duke@435 | 190 | |
duke@435 | 191 | methodHandle static_target(TRAPS); // "specified" method (from constant pool) |
duke@435 | 192 | |
duke@435 | 193 | // Testers |
duke@435 | 194 | bool is_invokeinterface() const { return adjusted_invoke_code() == Bytecodes::_invokeinterface; } |
duke@435 | 195 | bool is_invokevirtual() const { return adjusted_invoke_code() == Bytecodes::_invokevirtual; } |
duke@435 | 196 | bool is_invokestatic() const { return adjusted_invoke_code() == Bytecodes::_invokestatic; } |
duke@435 | 197 | bool is_invokespecial() const { return adjusted_invoke_code() == Bytecodes::_invokespecial; } |
duke@435 | 198 | |
duke@435 | 199 | bool is_valid() const { return is_invokeinterface() || |
duke@435 | 200 | is_invokevirtual() || |
duke@435 | 201 | is_invokestatic() || |
duke@435 | 202 | is_invokespecial(); } |
duke@435 | 203 | |
duke@435 | 204 | // Creation |
duke@435 | 205 | inline friend Bytecode_invoke* Bytecode_invoke_at(methodHandle method, int bci); |
duke@435 | 206 | |
duke@435 | 207 | // Like Bytecode_invoke_at. Instead it returns NULL if the bci is not at an invoke. |
duke@435 | 208 | inline friend Bytecode_invoke* Bytecode_invoke_at_check(methodHandle method, int bci); |
duke@435 | 209 | }; |
duke@435 | 210 | |
duke@435 | 211 | inline Bytecode_invoke* Bytecode_invoke_at(methodHandle method, int bci) { |
duke@435 | 212 | Bytecode_invoke* b = new Bytecode_invoke(method, bci); |
duke@435 | 213 | debug_only(b->verify()); |
duke@435 | 214 | return b; |
duke@435 | 215 | } |
duke@435 | 216 | |
duke@435 | 217 | inline Bytecode_invoke* Bytecode_invoke_at_check(methodHandle method, int bci) { |
duke@435 | 218 | Bytecode_invoke* b = new Bytecode_invoke(method, bci); |
duke@435 | 219 | return b->is_valid() ? b : NULL; |
duke@435 | 220 | } |
duke@435 | 221 | |
duke@435 | 222 | |
duke@435 | 223 | // Abstraction for all field accesses (put/get field/static_ |
duke@435 | 224 | class Bytecode_field: public Bytecode { |
duke@435 | 225 | public: |
duke@435 | 226 | void verify() const; |
duke@435 | 227 | |
duke@435 | 228 | int index() const; |
duke@435 | 229 | bool is_static() const; |
duke@435 | 230 | |
duke@435 | 231 | // Creation |
duke@435 | 232 | inline friend Bytecode_field* Bytecode_field_at(const methodOop method, address bcp); |
duke@435 | 233 | }; |
duke@435 | 234 | |
duke@435 | 235 | inline Bytecode_field* Bytecode_field_at(const methodOop method, address bcp) { |
duke@435 | 236 | Bytecode_field* b = (Bytecode_field*)bcp; |
duke@435 | 237 | debug_only(b->verify()); |
duke@435 | 238 | return b; |
duke@435 | 239 | } |
duke@435 | 240 | |
duke@435 | 241 | |
duke@435 | 242 | // Abstraction for {get,put}static |
duke@435 | 243 | |
duke@435 | 244 | class Bytecode_static: public Bytecode { |
duke@435 | 245 | public: |
duke@435 | 246 | void verify() const; |
duke@435 | 247 | |
duke@435 | 248 | // Returns the result type of the send by inspecting the field ref |
duke@435 | 249 | BasicType result_type(methodOop method) const; |
duke@435 | 250 | |
duke@435 | 251 | // Creation |
duke@435 | 252 | inline friend Bytecode_static* Bytecode_static_at(const methodOop method, address bcp); |
duke@435 | 253 | }; |
duke@435 | 254 | |
duke@435 | 255 | inline Bytecode_static* Bytecode_static_at(const methodOop method, address bcp) { |
duke@435 | 256 | Bytecode_static* b = (Bytecode_static*)bcp; |
duke@435 | 257 | debug_only(b->verify()); |
duke@435 | 258 | return b; |
duke@435 | 259 | } |
duke@435 | 260 | |
duke@435 | 261 | |
duke@435 | 262 | // Abstraction for checkcast |
duke@435 | 263 | |
duke@435 | 264 | class Bytecode_checkcast: public Bytecode { |
duke@435 | 265 | public: |
duke@435 | 266 | void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); } |
duke@435 | 267 | |
duke@435 | 268 | // Returns index |
duke@435 | 269 | long index() const { return java_hwrd_at(1); }; |
duke@435 | 270 | |
duke@435 | 271 | // Creation |
duke@435 | 272 | inline friend Bytecode_checkcast* Bytecode_checkcast_at(address bcp); |
duke@435 | 273 | }; |
duke@435 | 274 | |
duke@435 | 275 | inline Bytecode_checkcast* Bytecode_checkcast_at(address bcp) { |
duke@435 | 276 | Bytecode_checkcast* b = (Bytecode_checkcast*)bcp; |
duke@435 | 277 | debug_only(b->verify()); |
duke@435 | 278 | return b; |
duke@435 | 279 | } |
duke@435 | 280 | |
duke@435 | 281 | |
duke@435 | 282 | // Abstraction for instanceof |
duke@435 | 283 | |
duke@435 | 284 | class Bytecode_instanceof: public Bytecode { |
duke@435 | 285 | public: |
duke@435 | 286 | void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); } |
duke@435 | 287 | |
duke@435 | 288 | // Returns index |
duke@435 | 289 | long index() const { return java_hwrd_at(1); }; |
duke@435 | 290 | |
duke@435 | 291 | // Creation |
duke@435 | 292 | inline friend Bytecode_instanceof* Bytecode_instanceof_at(address bcp); |
duke@435 | 293 | }; |
duke@435 | 294 | |
duke@435 | 295 | inline Bytecode_instanceof* Bytecode_instanceof_at(address bcp) { |
duke@435 | 296 | Bytecode_instanceof* b = (Bytecode_instanceof*)bcp; |
duke@435 | 297 | debug_only(b->verify()); |
duke@435 | 298 | return b; |
duke@435 | 299 | } |
duke@435 | 300 | |
duke@435 | 301 | |
duke@435 | 302 | class Bytecode_new: public Bytecode { |
duke@435 | 303 | public: |
duke@435 | 304 | void verify() const { assert(java_code() == Bytecodes::_new, "check new"); } |
duke@435 | 305 | |
duke@435 | 306 | // Returns index |
duke@435 | 307 | long index() const { return java_hwrd_at(1); }; |
duke@435 | 308 | |
duke@435 | 309 | // Creation |
duke@435 | 310 | inline friend Bytecode_new* Bytecode_new_at(address bcp); |
duke@435 | 311 | }; |
duke@435 | 312 | |
duke@435 | 313 | inline Bytecode_new* Bytecode_new_at(address bcp) { |
duke@435 | 314 | Bytecode_new* b = (Bytecode_new*)bcp; |
duke@435 | 315 | debug_only(b->verify()); |
duke@435 | 316 | return b; |
duke@435 | 317 | } |
duke@435 | 318 | |
duke@435 | 319 | |
duke@435 | 320 | class Bytecode_multianewarray: public Bytecode { |
duke@435 | 321 | public: |
duke@435 | 322 | void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); } |
duke@435 | 323 | |
duke@435 | 324 | // Returns index |
duke@435 | 325 | long index() const { return java_hwrd_at(1); }; |
duke@435 | 326 | |
duke@435 | 327 | // Creation |
duke@435 | 328 | inline friend Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp); |
duke@435 | 329 | }; |
duke@435 | 330 | |
duke@435 | 331 | inline Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp) { |
duke@435 | 332 | Bytecode_multianewarray* b = (Bytecode_multianewarray*)bcp; |
duke@435 | 333 | debug_only(b->verify()); |
duke@435 | 334 | return b; |
duke@435 | 335 | } |
duke@435 | 336 | |
duke@435 | 337 | |
duke@435 | 338 | class Bytecode_anewarray: public Bytecode { |
duke@435 | 339 | public: |
duke@435 | 340 | void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); } |
duke@435 | 341 | |
duke@435 | 342 | // Returns index |
duke@435 | 343 | long index() const { return java_hwrd_at(1); }; |
duke@435 | 344 | |
duke@435 | 345 | // Creation |
duke@435 | 346 | inline friend Bytecode_anewarray* Bytecode_anewarray_at(address bcp); |
duke@435 | 347 | }; |
duke@435 | 348 | |
duke@435 | 349 | inline Bytecode_anewarray* Bytecode_anewarray_at(address bcp) { |
duke@435 | 350 | Bytecode_anewarray* b = (Bytecode_anewarray*)bcp; |
duke@435 | 351 | debug_only(b->verify()); |
duke@435 | 352 | return b; |
duke@435 | 353 | } |
duke@435 | 354 | |
duke@435 | 355 | |
duke@435 | 356 | // Abstraction for ldc, ldc_w and ldc2_w |
duke@435 | 357 | |
duke@435 | 358 | class Bytecode_loadconstant: public Bytecode { |
duke@435 | 359 | public: |
duke@435 | 360 | void verify() const { |
duke@435 | 361 | Bytecodes::Code stdc = Bytecodes::java_code(code()); |
duke@435 | 362 | assert(stdc == Bytecodes::_ldc || |
duke@435 | 363 | stdc == Bytecodes::_ldc_w || |
duke@435 | 364 | stdc == Bytecodes::_ldc2_w, "load constant"); |
duke@435 | 365 | } |
duke@435 | 366 | |
duke@435 | 367 | int index() const; |
duke@435 | 368 | |
duke@435 | 369 | inline friend Bytecode_loadconstant* Bytecode_loadconstant_at(const methodOop method, address bcp); |
duke@435 | 370 | }; |
duke@435 | 371 | |
duke@435 | 372 | inline Bytecode_loadconstant* Bytecode_loadconstant_at(const methodOop method, address bcp) { |
duke@435 | 373 | Bytecode_loadconstant* b = (Bytecode_loadconstant*)bcp; |
duke@435 | 374 | debug_only(b->verify()); |
duke@435 | 375 | return b; |
duke@435 | 376 | } |