src/share/vm/interpreter/bytecodeStream.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 2508
b92c45f2bc75
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_BYTECODESTREAM_HPP
stefank@2314 26 #define SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP
stefank@2314 27
stefank@2314 28 #include "interpreter/bytecode.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
duke@435 41 // A BytecodeStream is used for fast iteration over the bytecodes
duke@435 42 // of a methodOop.
duke@435 43 //
duke@435 44 // Usage:
duke@435 45 //
duke@435 46 // BytecodeStream s(method);
duke@435 47 // Bytecodes::Code c;
duke@435 48 // while ((c = s.next()) >= 0) {
duke@435 49 // ...
duke@435 50 // }
jrose@1920 51
duke@435 52 // A RawBytecodeStream is a simple version of BytecodeStream.
duke@435 53 // It is used ONLY when we know the bytecodes haven't been rewritten
jrose@1920 54 // yet, such as in the rewriter or the verifier.
duke@435 55
jrose@1920 56 // Here is the common base class for both RawBytecodeStream and BytecodeStream:
jrose@1920 57 class BaseBytecodeStream: StackObj {
duke@435 58 protected:
duke@435 59 // stream buffer
duke@435 60 methodHandle _method; // read from method directly
duke@435 61
duke@435 62 // reading position
duke@435 63 int _bci; // bci if current bytecode
duke@435 64 int _next_bci; // bci of next bytecode
duke@435 65 int _end_bci; // bci after the current iteration interval
duke@435 66
duke@435 67 // last bytecode read
jrose@1920 68 Bytecodes::Code _raw_code;
duke@435 69 bool _is_wide;
jrose@1920 70 bool _is_raw; // false in 'cooked' BytecodeStream
jrose@1920 71
jrose@1920 72 // Construction
jrose@1920 73 BaseBytecodeStream(methodHandle method) : _method(method) {
jrose@1920 74 set_interval(0, _method->code_size());
jrose@1920 75 _is_raw = false;
jrose@1920 76 }
duke@435 77
duke@435 78 public:
duke@435 79 // Iteration control
duke@435 80 void set_interval(int beg_bci, int end_bci) {
duke@435 81 // iterate over the interval [beg_bci, end_bci)
duke@435 82 assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");
duke@435 83 assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");
duke@435 84 // setup of iteration pointers
duke@435 85 _bci = beg_bci;
duke@435 86 _next_bci = beg_bci;
duke@435 87 _end_bci = end_bci;
duke@435 88 }
duke@435 89 void set_start (int beg_bci) {
duke@435 90 set_interval(beg_bci, _method->code_size());
duke@435 91 }
duke@435 92
jrose@1920 93 bool is_raw() const { return _is_raw; }
jrose@1920 94
jrose@1920 95 // Stream attributes
jrose@1920 96 methodHandle method() const { return _method; }
jrose@1920 97
jrose@1920 98 int bci() const { return _bci; }
jrose@1920 99 int next_bci() const { return _next_bci; }
jrose@1920 100 int end_bci() const { return _end_bci; }
jrose@1920 101
jrose@1920 102 Bytecodes::Code raw_code() const { return _raw_code; }
jrose@1920 103 bool is_wide() const { return _is_wide; }
jrose@1920 104 int instruction_size() const { return (_next_bci - _bci); }
jrose@1920 105 bool is_last_bytecode() const { return _next_bci >= _end_bci; }
jrose@1920 106
jrose@1920 107 address bcp() const { return method()->code_base() + _bci; }
never@2462 108 Bytecode bytecode() const { return Bytecode(_method(), bcp()); }
jrose@1920 109
jrose@1920 110 // State changes
jrose@1920 111 void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
jrose@1920 112
jrose@1920 113 // Bytecode-specific attributes
never@2462 114 int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); }
never@2462 115 int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); }
jrose@1920 116
jrose@1920 117 // One-byte indices.
jrose@1920 118 int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); }
jrose@1920 119
jrose@1920 120 protected:
jrose@1920 121 void assert_raw_index_size(int size) const NOT_DEBUG_RETURN;
jrose@1920 122 void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN;
jrose@1920 123 };
jrose@1920 124
jrose@1920 125 class RawBytecodeStream: public BaseBytecodeStream {
jrose@1920 126 public:
jrose@1920 127 // Construction
jrose@1920 128 RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) {
jrose@1920 129 _is_raw = true;
jrose@1920 130 }
jrose@1920 131
jrose@1920 132 public:
duke@435 133 // Iteration
duke@435 134 // Use raw_next() rather than next() for faster method reference
duke@435 135 Bytecodes::Code raw_next() {
duke@435 136 Bytecodes::Code code;
duke@435 137 // set reading position
duke@435 138 _bci = _next_bci;
duke@435 139 assert(!is_last_bytecode(), "caller should check is_last_bytecode()");
duke@435 140
jrose@1920 141 address bcp = this->bcp();
duke@435 142 code = Bytecodes::code_or_bp_at(bcp);
duke@435 143
duke@435 144 // set next bytecode position
duke@435 145 int l = Bytecodes::length_for(code);
duke@435 146 if (l > 0 && (_bci + l) <= _end_bci) {
duke@435 147 assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch
duke@435 148 && code != Bytecodes::_lookupswitch, "can't be special bytecode");
duke@435 149 _is_wide = false;
duke@435 150 _next_bci += l;
jrose@1920 151 _raw_code = code;
duke@435 152 return code;
duke@435 153 } else {
duke@435 154 return raw_next_special(code);
duke@435 155 }
duke@435 156 }
duke@435 157 Bytecodes::Code raw_next_special(Bytecodes::Code code);
duke@435 158
jrose@1920 159 // Unsigned indices, widening, with no swapping of bytes
jrose@1920 160 int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); }
jrose@1920 161 // Get an unsigned 2-byte index, with no swapping of bytes.
jrose@1920 162 int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); }
jrose@1161 163
jrose@1161 164 private:
jrose@1920 165 int get_index_u2_raw(address p) const {
jrose@1920 166 assert_raw_index_size(2); assert_raw_stream(true);
jrose@1920 167 return Bytes::get_Java_u2(p);
jrose@1161 168 }
duke@435 169 };
duke@435 170
duke@435 171 // In BytecodeStream, non-java bytecodes will be translated into the
duke@435 172 // corresponding java bytecodes.
duke@435 173
jrose@1920 174 class BytecodeStream: public BaseBytecodeStream {
jrose@1920 175 Bytecodes::Code _code;
jrose@1920 176
duke@435 177 public:
duke@435 178 // Construction
jrose@1920 179 BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { }
duke@435 180
duke@435 181 // Iteration
duke@435 182 Bytecodes::Code next() {
jrose@1920 183 Bytecodes::Code raw_code, code;
duke@435 184 // set reading position
duke@435 185 _bci = _next_bci;
duke@435 186 if (is_last_bytecode()) {
duke@435 187 // indicate end of bytecode stream
jrose@1920 188 raw_code = code = Bytecodes::_illegal;
duke@435 189 } else {
duke@435 190 // get bytecode
jrose@1920 191 address bcp = this->bcp();
never@2462 192 raw_code = Bytecodes::code_at(_method(), bcp);
jrose@1920 193 code = Bytecodes::java_code(raw_code);
duke@435 194 // set next bytecode position
duke@435 195 //
duke@435 196 // note that we cannot advance before having the
duke@435 197 // tty bytecode otherwise the stepping is wrong!
duke@435 198 // (carefull: length_for(...) must be used first!)
duke@435 199 int l = Bytecodes::length_for(code);
never@2462 200 if (l == 0) l = Bytecodes::length_at(_method(), bcp);
duke@435 201 _next_bci += l;
duke@435 202 assert(_bci < _next_bci, "length must be > 0");
duke@435 203 // set attributes
duke@435 204 _is_wide = false;
duke@435 205 // check for special (uncommon) cases
duke@435 206 if (code == Bytecodes::_wide) {
jrose@1920 207 raw_code = (Bytecodes::Code)bcp[1];
jrose@1920 208 code = raw_code; // wide BCs are always Java-normal
duke@435 209 _is_wide = true;
duke@435 210 }
duke@435 211 assert(Bytecodes::is_java_code(code), "sanity check");
duke@435 212 }
jrose@1920 213 _raw_code = raw_code;
duke@435 214 _code = code;
duke@435 215 return _code;
duke@435 216 }
duke@435 217
duke@435 218 bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); }
jrose@1920 219 Bytecodes::Code code() const { return _code; }
jrose@1920 220
jrose@1920 221 // Unsigned indices, widening
never@2462 222 int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); }
jrose@1920 223 // Get an unsigned 2-byte index, swapping the bytes if necessary.
jrose@1920 224 int get_index_u2() const { assert_raw_stream(false);
never@2462 225 return bytecode().get_index_u2(raw_code(), false); }
jrose@1920 226 // Get an unsigned 2-byte index in native order.
jrose@1920 227 int get_index_u2_cpcache() const { assert_raw_stream(false);
never@2462 228 return bytecode().get_index_u2_cpcache(raw_code()); }
jrose@1920 229 int get_index_u4() const { assert_raw_stream(false);
never@2462 230 return bytecode().get_index_u4(raw_code()); }
never@2462 231 bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); }
duke@435 232 };
stefank@2314 233
stefank@2314 234 #endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP

mercurial