src/share/vm/interpreter/bytecode.hpp

Thu, 13 Jan 2011 22:15:41 -0800

author
never
date
Thu, 13 Jan 2011 22:15:41 -0800
changeset 2462
8012aa3ccede
parent 2314
f95d63e2154a
child 2497
3582bf76420e
permissions
-rw-r--r--

4926272: methodOopDesc::method_from_bcp is unsafe
Reviewed-by: coleenp, jrose, kvn, dcubed

duke@435 1 /*
never@2462 2 * Copyright (c) 1997, 2011, 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_INTERPRETER_BYTECODE_HPP
stefank@2314 26 #define SHARE_VM_INTERPRETER_BYTECODE_HPP
stefank@2314 27
stefank@2314 28 #include "interpreter/bytecodes.hpp"
stefank@2314 29 #include "memory/allocation.hpp"
stefank@2314 30 #include "oops/methodOop.hpp"
stefank@2314 31 #ifdef TARGET_ARCH_x86
stefank@2314 32 # include "bytes_x86.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_ARCH_sparc
stefank@2314 35 # include "bytes_sparc.hpp"
stefank@2314 36 #endif
stefank@2314 37 #ifdef TARGET_ARCH_zero
stefank@2314 38 # include "bytes_zero.hpp"
stefank@2314 39 #endif
stefank@2314 40
never@2462 41 class ciBytecodeStream;
duke@435 42
never@2462 43 // The base class for different kinds of bytecode abstractions.
never@2462 44 // Provides the primitive operations to manipulate code relative
never@2462 45 // to the bcp.
never@2462 46
never@2462 47 class Bytecode: public StackObj {
never@2462 48 protected:
never@2462 49 const address _bcp;
never@2462 50 const Bytecodes::Code _code;
never@2462 51
duke@435 52 // Address computation
never@2462 53 address addr_at (int offset) const { return (address)_bcp + offset; }
never@2462 54 u_char byte_at(int offset) const { return *addr_at(offset); }
duke@435 55 address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); }
duke@435 56 int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); }
duke@435 57
jrose@1920 58 // Word access:
jrose@1920 59 int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); }
jrose@1920 60 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
jrose@1920 61 int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); }
jrose@1920 62 int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); }
duke@435 63
duke@435 64 public:
never@2462 65 Bytecode(methodOop method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {
never@2462 66 assert(method != NULL, "this form requires a valid methodOop");
never@2462 67 }
never@2462 68 // Defined in ciStreams.hpp
never@2462 69 inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);
never@2462 70
duke@435 71 // Attributes
never@2462 72 address bcp() const { return _bcp; }
never@2462 73 int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); }
duke@435 74
never@2462 75 Bytecodes::Code code() const { return _code; }
duke@435 76 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); }
jrose@1161 77
jrose@1920 78 // Static functions for parsing bytecodes in place.
jrose@1920 79 int get_index_u1(Bytecodes::Code bc) const {
jrose@1920 80 assert_same_format_as(bc); assert_index_size(1, bc);
jrose@1920 81 return *(jubyte*)addr_at(1);
jrose@1920 82 }
jrose@1920 83 int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {
jrose@1920 84 assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);
jrose@1920 85 address p = addr_at(is_wide ? 2 : 1);
jrose@1920 86 if (can_use_native_byte_order(bc, is_wide))
never@2462 87 return Bytes::get_native_u2(p);
jrose@1920 88 else return Bytes::get_Java_u2(p);
jrose@1920 89 }
jrose@1957 90 int get_index_u1_cpcache(Bytecodes::Code bc) const {
jrose@1957 91 assert_same_format_as(bc); assert_index_size(1, bc);
jrose@1957 92 return *(jubyte*)addr_at(1) + constantPoolOopDesc::CPCACHE_INDEX_TAG;
jrose@1957 93 }
jrose@1920 94 int get_index_u2_cpcache(Bytecodes::Code bc) const {
jrose@1920 95 assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);
jrose@1957 96 return Bytes::get_native_u2(addr_at(1)) + constantPoolOopDesc::CPCACHE_INDEX_TAG;
jrose@1920 97 }
jrose@1920 98 int get_index_u4(Bytecodes::Code bc) const {
jrose@1920 99 assert_same_format_as(bc); assert_index_size(4, bc);
jrose@1920 100 assert(can_use_native_byte_order(bc), "");
jrose@1920 101 return Bytes::get_native_u4(addr_at(1));
jrose@1920 102 }
jrose@1920 103 bool has_index_u4(Bytecodes::Code bc) const {
jrose@1920 104 return bc == Bytecodes::_invokedynamic;
jrose@1920 105 }
jrose@1920 106
jrose@1920 107 int get_offset_s2(Bytecodes::Code bc) const {
jrose@1920 108 assert_same_format_as(bc); assert_offset_size(2, bc);
jrose@1920 109 return (jshort) Bytes::get_Java_u2(addr_at(1));
jrose@1920 110 }
jrose@1920 111 int get_offset_s4(Bytecodes::Code bc) const {
jrose@1920 112 assert_same_format_as(bc); assert_offset_size(4, bc);
jrose@1920 113 return (jint) Bytes::get_Java_u4(addr_at(1));
jrose@1920 114 }
jrose@1920 115
jrose@1920 116 int get_constant_u1(int offset, Bytecodes::Code bc) const {
jrose@1920 117 assert_same_format_as(bc); assert_constant_size(1, offset, bc);
jrose@1920 118 return *(jbyte*)addr_at(offset);
jrose@1920 119 }
jrose@1920 120 int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {
jrose@1920 121 assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);
jrose@1920 122 return (jshort) Bytes::get_Java_u2(addr_at(offset));
jrose@1920 123 }
jrose@1920 124
jrose@1920 125 // These are used locally and also from bytecode streams.
jrose@1920 126 void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;
jrose@1920 127 static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 128 static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 129 static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 130 static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 131 static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {
jrose@1920 132 return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));
jrose@1161 133 }
duke@435 134 };
duke@435 135
duke@435 136
duke@435 137 // Abstractions for lookupswitch bytecode
never@2462 138 class LookupswitchPair VALUE_OBJ_CLASS_SPEC {
never@2462 139 private:
never@2462 140 const address _bcp;
duke@435 141
never@2462 142 address addr_at (int offset) const { return _bcp + offset; }
never@2462 143 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
duke@435 144
duke@435 145 public:
never@2462 146 LookupswitchPair(address bcp): _bcp(bcp) {}
jrose@1920 147 int match() const { return get_Java_u4_at(0 * jintSize); }
jrose@1920 148 int offset() const { return get_Java_u4_at(1 * jintSize); }
duke@435 149 };
duke@435 150
duke@435 151
duke@435 152 class Bytecode_lookupswitch: public Bytecode {
duke@435 153 public:
never@2462 154 Bytecode_lookupswitch(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
never@2462 155 // Defined in ciStreams.hpp
never@2462 156 inline Bytecode_lookupswitch(const ciBytecodeStream* stream);
duke@435 157 void verify() const PRODUCT_RETURN;
duke@435 158
duke@435 159 // Attributes
jrose@1920 160 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
jrose@1920 161 int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
never@2462 162 LookupswitchPair pair_at(int i) const {
never@2462 163 assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");
never@2462 164 return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));
never@2462 165 }
duke@435 166 };
duke@435 167
duke@435 168 class Bytecode_tableswitch: public Bytecode {
duke@435 169 public:
never@2462 170 Bytecode_tableswitch(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
never@2462 171 // Defined in ciStreams.hpp
never@2462 172 inline Bytecode_tableswitch(const ciBytecodeStream* stream);
duke@435 173 void verify() const PRODUCT_RETURN;
duke@435 174
duke@435 175 // Attributes
jrose@1920 176 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
jrose@1920 177 int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
jrose@1920 178 int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); }
duke@435 179 int dest_offset_at(int i) const;
duke@435 180 int length() { return high_key()-low_key()+1; }
duke@435 181 };
duke@435 182
jrose@1957 183 // Common code for decoding invokes and field references.
duke@435 184
never@2462 185 class Bytecode_member_ref: public Bytecode {
duke@435 186 protected:
never@2462 187 const methodHandle _method; // method containing the bytecode
duke@435 188
never@2462 189 Bytecode_member_ref(methodHandle method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method) {}
never@2462 190
never@2462 191 methodHandle method() const { return _method; }
jrose@1957 192
jrose@1957 193 public:
jrose@1957 194 int index() const; // cache index (loaded from instruction)
jrose@1957 195 int pool_index() const; // constant pool index
jrose@1957 196 symbolOop name() const; // returns the name of the method or field
jrose@1957 197 symbolOop signature() const; // returns the signature of the method or field
jrose@1957 198
jrose@1957 199 BasicType result_type(Thread* thread) const; // returns the result type of the getfield or invoke
jrose@1957 200 };
jrose@1957 201
jrose@1957 202 // Abstraction for invoke_{virtual, static, interface, special}
jrose@1957 203
jrose@1957 204 class Bytecode_invoke: public Bytecode_member_ref {
jrose@1957 205 protected:
never@2462 206 // Constructor that skips verification
never@2462 207 Bytecode_invoke(methodHandle method, int bci, bool unused) : Bytecode_member_ref(method, bci) {}
duke@435 208
duke@435 209 public:
never@2462 210 Bytecode_invoke(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
duke@435 211 void verify() const;
duke@435 212
duke@435 213 // Attributes
duke@435 214 methodHandle static_target(TRAPS); // "specified" method (from constant pool)
duke@435 215
duke@435 216 // Testers
jrose@1957 217 bool is_invokeinterface() const { return java_code() == Bytecodes::_invokeinterface; }
jrose@1957 218 bool is_invokevirtual() const { return java_code() == Bytecodes::_invokevirtual; }
jrose@1957 219 bool is_invokestatic() const { return java_code() == Bytecodes::_invokestatic; }
jrose@1957 220 bool is_invokespecial() const { return java_code() == Bytecodes::_invokespecial; }
jrose@1957 221 bool is_invokedynamic() const { return java_code() == Bytecodes::_invokedynamic; }
jrose@1161 222
twisti@1573 223 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); }
duke@435 224
duke@435 225 bool is_valid() const { return is_invokeinterface() ||
duke@435 226 is_invokevirtual() ||
duke@435 227 is_invokestatic() ||
twisti@1570 228 is_invokespecial() ||
twisti@1570 229 is_invokedynamic(); }
duke@435 230
never@2462 231 // Helper to skip verification. Used is_valid() to check if the result is really an invoke
never@2462 232 inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci);
duke@435 233 };
duke@435 234
never@2462 235 inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) {
never@2462 236 return Bytecode_invoke(method, bci, false);
duke@435 237 }
duke@435 238
duke@435 239
jrose@1957 240 // Abstraction for all field accesses (put/get field/static)
jrose@1957 241 class Bytecode_field: public Bytecode_member_ref {
never@2462 242 public:
never@2462 243 Bytecode_field(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
jrose@1957 244
jrose@1957 245 // Testers
jrose@1957 246 bool is_getfield() const { return java_code() == Bytecodes::_getfield; }
jrose@1957 247 bool is_putfield() const { return java_code() == Bytecodes::_putfield; }
jrose@1957 248 bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; }
jrose@1957 249 bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; }
jrose@1957 250
jrose@1957 251 bool is_getter() const { return is_getfield() || is_getstatic(); }
jrose@1957 252 bool is_static() const { return is_getstatic() || is_putstatic(); }
jrose@1957 253
jrose@1957 254 bool is_valid() const { return is_getfield() ||
jrose@1957 255 is_putfield() ||
jrose@1957 256 is_getstatic() ||
jrose@1957 257 is_putstatic(); }
duke@435 258 void verify() const;
duke@435 259 };
duke@435 260
duke@435 261 // Abstraction for checkcast
duke@435 262 class Bytecode_checkcast: public Bytecode {
duke@435 263 public:
never@2462 264 Bytecode_checkcast(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 265 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }
duke@435 266
duke@435 267 // Returns index
jrose@1920 268 long index() const { return get_index_u2(Bytecodes::_checkcast); };
duke@435 269 };
duke@435 270
duke@435 271 // Abstraction for instanceof
duke@435 272 class Bytecode_instanceof: public Bytecode {
duke@435 273 public:
never@2462 274 Bytecode_instanceof(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 275 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }
duke@435 276
duke@435 277 // Returns index
jrose@1920 278 long index() const { return get_index_u2(Bytecodes::_instanceof); };
duke@435 279 };
duke@435 280
duke@435 281 class Bytecode_new: public Bytecode {
duke@435 282 public:
never@2462 283 Bytecode_new(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 284 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }
duke@435 285
duke@435 286 // Returns index
jrose@1920 287 long index() const { return get_index_u2(Bytecodes::_new); };
duke@435 288 };
duke@435 289
duke@435 290 class Bytecode_multianewarray: public Bytecode {
duke@435 291 public:
never@2462 292 Bytecode_multianewarray(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 293 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }
duke@435 294
duke@435 295 // Returns index
jrose@1920 296 long index() const { return get_index_u2(Bytecodes::_multianewarray); };
duke@435 297 };
duke@435 298
duke@435 299 class Bytecode_anewarray: public Bytecode {
duke@435 300 public:
never@2462 301 Bytecode_anewarray(methodOop method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 302 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }
duke@435 303
duke@435 304 // Returns index
jrose@1920 305 long index() const { return get_index_u2(Bytecodes::_anewarray); };
duke@435 306 };
duke@435 307
duke@435 308 // Abstraction for ldc, ldc_w and ldc2_w
never@2462 309 class Bytecode_loadconstant: public Bytecode {
jrose@1957 310 private:
never@2462 311 const methodHandle _method;
jrose@1957 312
jrose@1957 313 int raw_index() const;
jrose@1957 314
duke@435 315 public:
never@2462 316 Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); }
jrose@1957 317
duke@435 318 void verify() const {
jrose@1957 319 assert(_method.not_null(), "must supply method");
duke@435 320 Bytecodes::Code stdc = Bytecodes::java_code(code());
duke@435 321 assert(stdc == Bytecodes::_ldc ||
duke@435 322 stdc == Bytecodes::_ldc_w ||
duke@435 323 stdc == Bytecodes::_ldc2_w, "load constant");
duke@435 324 }
duke@435 325
jrose@1957 326 // Only non-standard bytecodes (fast_aldc) have CP cache indexes.
jrose@1957 327 bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }
duke@435 328
jrose@1957 329 int pool_index() const; // index into constant pool
jrose@1957 330 int cache_index() const { // index into CP cache (or -1 if none)
jrose@1957 331 return has_cache_index() ? raw_index() : -1;
jrose@1957 332 }
jrose@1957 333
jrose@1957 334 BasicType result_type() const; // returns the result type of the ldc
jrose@1957 335
jrose@1957 336 oop resolve_constant(TRAPS) const;
duke@435 337 };
duke@435 338
stefank@2314 339 #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP

mercurial