src/share/vm/interpreter/interpreter.cpp

Thu, 30 Oct 2008 15:48:59 -0400

author
kamg
date
Thu, 30 Oct 2008 15:48:59 -0400
changeset 848
c7ec737733a6
parent 435
a61af66fc99e
child 1145
e5b0439ef4ae
permissions
-rw-r--r--

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-2007 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 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_interpreter.cpp.incl"
duke@435 27
duke@435 28 # define __ _masm->
duke@435 29
duke@435 30
duke@435 31 //------------------------------------------------------------------------------------------------------------------------
duke@435 32 // Implementation of InterpreterCodelet
duke@435 33
duke@435 34 void InterpreterCodelet::initialize(const char* description, Bytecodes::Code bytecode) {
duke@435 35 _description = description;
duke@435 36 _bytecode = bytecode;
duke@435 37 }
duke@435 38
duke@435 39
duke@435 40 void InterpreterCodelet::verify() {
duke@435 41 }
duke@435 42
duke@435 43
duke@435 44 void InterpreterCodelet::print() {
duke@435 45 if (PrintInterpreter) {
duke@435 46 tty->cr();
duke@435 47 tty->print_cr("----------------------------------------------------------------------");
duke@435 48 }
duke@435 49
duke@435 50 if (description() != NULL) tty->print("%s ", description());
duke@435 51 if (bytecode() >= 0 ) tty->print("%d %s ", bytecode(), Bytecodes::name(bytecode()));
duke@435 52 tty->print_cr("[" INTPTR_FORMAT ", " INTPTR_FORMAT "] %d bytes",
duke@435 53 code_begin(), code_end(), code_size());
duke@435 54
duke@435 55 if (PrintInterpreter) {
duke@435 56 tty->cr();
duke@435 57 Disassembler::decode(code_begin(), code_end(), tty);
duke@435 58 }
duke@435 59 }
duke@435 60
duke@435 61
duke@435 62 //------------------------------------------------------------------------------------------------------------------------
duke@435 63 // Implementation of platform independent aspects of Interpreter
duke@435 64
duke@435 65 void AbstractInterpreter::initialize() {
duke@435 66 if (_code != NULL) return;
duke@435 67
duke@435 68 // make sure 'imported' classes are initialized
duke@435 69 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset();
duke@435 70 if (PrintBytecodeHistogram) BytecodeHistogram::reset();
duke@435 71 if (PrintBytecodePairHistogram) BytecodePairHistogram::reset();
duke@435 72
duke@435 73 InvocationCounter::reinitialize(DelayCompilationDuringStartup);
duke@435 74
duke@435 75 }
duke@435 76
duke@435 77 void AbstractInterpreter::print() {
duke@435 78 tty->cr();
duke@435 79 tty->print_cr("----------------------------------------------------------------------");
duke@435 80 tty->print_cr("Interpreter");
duke@435 81 tty->cr();
duke@435 82 tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024);
duke@435 83 tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024);
duke@435 84 tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024);
duke@435 85 tty->cr();
duke@435 86 tty->print_cr("# of codelets = %6d" , _code->number_of_stubs());
duke@435 87 tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs());
duke@435 88 tty->cr();
duke@435 89 _code->print();
duke@435 90 tty->print_cr("----------------------------------------------------------------------");
duke@435 91 tty->cr();
duke@435 92 }
duke@435 93
duke@435 94
duke@435 95 void interpreter_init() {
duke@435 96 Interpreter::initialize();
duke@435 97 #ifndef PRODUCT
duke@435 98 if (TraceBytecodes) BytecodeTracer::set_closure(BytecodeTracer::std_closure());
duke@435 99 #endif // PRODUCT
duke@435 100 // need to hit every safepoint in order to call zapping routine
duke@435 101 // register the interpreter
duke@435 102 VTune::register_stub(
duke@435 103 "Interpreter",
duke@435 104 AbstractInterpreter::code()->code_start(),
duke@435 105 AbstractInterpreter::code()->code_end()
duke@435 106 );
duke@435 107 Forte::register_stub(
duke@435 108 "Interpreter",
duke@435 109 AbstractInterpreter::code()->code_start(),
duke@435 110 AbstractInterpreter::code()->code_end()
duke@435 111 );
duke@435 112
duke@435 113 // notify JVMTI profiler
duke@435 114 if (JvmtiExport::should_post_dynamic_code_generated()) {
duke@435 115 JvmtiExport::post_dynamic_code_generated("Interpreter",
duke@435 116 AbstractInterpreter::code()->code_start(),
duke@435 117 AbstractInterpreter::code()->code_end());
duke@435 118 }
duke@435 119 }
duke@435 120
duke@435 121 //------------------------------------------------------------------------------------------------------------------------
duke@435 122 // Implementation of interpreter
duke@435 123
duke@435 124 StubQueue* AbstractInterpreter::_code = NULL;
duke@435 125 bool AbstractInterpreter::_notice_safepoints = false;
duke@435 126 address AbstractInterpreter::_rethrow_exception_entry = NULL;
duke@435 127
duke@435 128 address AbstractInterpreter::_native_entry_begin = NULL;
duke@435 129 address AbstractInterpreter::_native_entry_end = NULL;
duke@435 130 address AbstractInterpreter::_slow_signature_handler;
duke@435 131 address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries];
duke@435 132 address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers];
duke@435 133
duke@435 134 //------------------------------------------------------------------------------------------------------------------------
duke@435 135 // Generation of complete interpreter
duke@435 136
duke@435 137 AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) {
duke@435 138 _masm = NULL;
duke@435 139 }
duke@435 140
duke@435 141
duke@435 142 static const BasicType types[Interpreter::number_of_result_handlers] = {
duke@435 143 T_BOOLEAN,
duke@435 144 T_CHAR ,
duke@435 145 T_BYTE ,
duke@435 146 T_SHORT ,
duke@435 147 T_INT ,
duke@435 148 T_LONG ,
duke@435 149 T_VOID ,
duke@435 150 T_FLOAT ,
duke@435 151 T_DOUBLE ,
duke@435 152 T_OBJECT
duke@435 153 };
duke@435 154
duke@435 155 void AbstractInterpreterGenerator::generate_all() {
duke@435 156
duke@435 157
duke@435 158 { CodeletMark cm(_masm, "slow signature handler");
duke@435 159 Interpreter::_slow_signature_handler = generate_slow_signature_handler();
duke@435 160 }
duke@435 161
duke@435 162 }
duke@435 163
duke@435 164 //------------------------------------------------------------------------------------------------------------------------
duke@435 165 // Entry points
duke@435 166
duke@435 167 AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(methodHandle m) {
duke@435 168 // Abstract method?
duke@435 169 if (m->is_abstract()) return abstract;
duke@435 170
duke@435 171 // Native method?
duke@435 172 // Note: This test must come _before_ the test for intrinsic
duke@435 173 // methods. See also comments below.
duke@435 174 if (m->is_native()) {
duke@435 175 return m->is_synchronized() ? native_synchronized : native;
duke@435 176 }
duke@435 177
duke@435 178 // Synchronized?
duke@435 179 if (m->is_synchronized()) {
duke@435 180 return zerolocals_synchronized;
duke@435 181 }
duke@435 182
duke@435 183 if (RegisterFinalizersAtInit && m->code_size() == 1 &&
duke@435 184 m->intrinsic_id() == vmIntrinsics::_Object_init) {
duke@435 185 // We need to execute the special return bytecode to check for
duke@435 186 // finalizer registration so create a normal frame.
duke@435 187 return zerolocals;
duke@435 188 }
duke@435 189
duke@435 190 // Empty method?
duke@435 191 if (m->is_empty_method()) {
duke@435 192 return empty;
duke@435 193 }
duke@435 194
duke@435 195 // Accessor method?
duke@435 196 if (m->is_accessor()) {
duke@435 197 assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1");
duke@435 198 return accessor;
duke@435 199 }
duke@435 200
duke@435 201 // Special intrinsic method?
duke@435 202 // Note: This test must come _after_ the test for native methods,
duke@435 203 // otherwise we will run into problems with JDK 1.2, see also
duke@435 204 // AbstractInterpreterGenerator::generate_method_entry() for
duke@435 205 // for details.
duke@435 206 switch (m->intrinsic_id()) {
duke@435 207 case vmIntrinsics::_dsin : return java_lang_math_sin ;
duke@435 208 case vmIntrinsics::_dcos : return java_lang_math_cos ;
duke@435 209 case vmIntrinsics::_dtan : return java_lang_math_tan ;
duke@435 210 case vmIntrinsics::_dabs : return java_lang_math_abs ;
duke@435 211 case vmIntrinsics::_dsqrt : return java_lang_math_sqrt ;
duke@435 212 case vmIntrinsics::_dlog : return java_lang_math_log ;
duke@435 213 case vmIntrinsics::_dlog10: return java_lang_math_log10;
duke@435 214 }
duke@435 215
duke@435 216 // Note: for now: zero locals for all non-empty methods
duke@435 217 return zerolocals;
duke@435 218 }
duke@435 219
duke@435 220
duke@435 221 // Return true if the interpreter can prove that the given bytecode has
duke@435 222 // not yet been executed (in Java semantics, not in actual operation).
duke@435 223 bool AbstractInterpreter::is_not_reached(methodHandle method, int bci) {
duke@435 224 address bcp = method->bcp_from(bci);
duke@435 225
duke@435 226 if (!Bytecode_at(bcp)->must_rewrite()) {
duke@435 227 // might have been reached
duke@435 228 return false;
duke@435 229 }
duke@435 230
duke@435 231 // the bytecode might not be rewritten if the method is an accessor, etc.
duke@435 232 address ientry = method->interpreter_entry();
duke@435 233 if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) &&
duke@435 234 ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized))
duke@435 235 return false; // interpreter does not run this method!
duke@435 236
duke@435 237 // otherwise, we can be sure this bytecode has never been executed
duke@435 238 return true;
duke@435 239 }
duke@435 240
duke@435 241
duke@435 242 #ifndef PRODUCT
duke@435 243 void AbstractInterpreter::print_method_kind(MethodKind kind) {
duke@435 244 switch (kind) {
duke@435 245 case zerolocals : tty->print("zerolocals" ); break;
duke@435 246 case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break;
duke@435 247 case native : tty->print("native" ); break;
duke@435 248 case native_synchronized : tty->print("native_synchronized" ); break;
duke@435 249 case empty : tty->print("empty" ); break;
duke@435 250 case accessor : tty->print("accessor" ); break;
duke@435 251 case abstract : tty->print("abstract" ); break;
duke@435 252 case java_lang_math_sin : tty->print("java_lang_math_sin" ); break;
duke@435 253 case java_lang_math_cos : tty->print("java_lang_math_cos" ); break;
duke@435 254 case java_lang_math_tan : tty->print("java_lang_math_tan" ); break;
duke@435 255 case java_lang_math_abs : tty->print("java_lang_math_abs" ); break;
duke@435 256 case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break;
duke@435 257 case java_lang_math_log : tty->print("java_lang_math_log" ); break;
duke@435 258 case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break;
duke@435 259 default : ShouldNotReachHere();
duke@435 260 }
duke@435 261 }
duke@435 262 #endif // PRODUCT
duke@435 263
duke@435 264 static BasicType constant_pool_type(methodOop method, int index) {
duke@435 265 constantTag tag = method->constants()->tag_at(index);
duke@435 266 if (tag.is_int ()) return T_INT;
duke@435 267 else if (tag.is_float ()) return T_FLOAT;
duke@435 268 else if (tag.is_long ()) return T_LONG;
duke@435 269 else if (tag.is_double ()) return T_DOUBLE;
duke@435 270 else if (tag.is_string ()) return T_OBJECT;
duke@435 271 else if (tag.is_unresolved_string()) return T_OBJECT;
duke@435 272 else if (tag.is_klass ()) return T_OBJECT;
duke@435 273 else if (tag.is_unresolved_klass ()) return T_OBJECT;
duke@435 274 ShouldNotReachHere();
duke@435 275 return T_ILLEGAL;
duke@435 276 }
duke@435 277
duke@435 278
duke@435 279 //------------------------------------------------------------------------------------------------------------------------
duke@435 280 // Deoptimization support
duke@435 281
duke@435 282 // If deoptimization happens, this method returns the point where to continue in
duke@435 283 // interpreter. For calls (invokexxxx, newxxxx) the continuation is at next
duke@435 284 // bci and the top of stack is in eax/edx/FPU tos.
duke@435 285 // For putfield/getfield, put/getstatic, the continuation is at the same
duke@435 286 // bci and the TOS is on stack.
duke@435 287
duke@435 288 // Note: deopt_entry(type, 0) means reexecute bytecode
duke@435 289 // deopt_entry(type, length) means continue at next bytecode
duke@435 290
duke@435 291 address AbstractInterpreter::continuation_for(methodOop method, address bcp, int callee_parameters, bool is_top_frame, bool& use_next_mdp) {
duke@435 292 assert(method->contains(bcp), "just checkin'");
duke@435 293 Bytecodes::Code code = Bytecodes::java_code_at(bcp);
duke@435 294 int bci = method->bci_from(bcp);
duke@435 295 int length = -1; // initial value for debugging
duke@435 296 // compute continuation length
duke@435 297 length = Bytecodes::length_at(bcp);
duke@435 298 // compute result type
duke@435 299 BasicType type = T_ILLEGAL;
duke@435 300 // when continuing after a compiler safepoint, re-execute the bytecode
duke@435 301 // (an invoke is continued after the safepoint)
duke@435 302 use_next_mdp = true;
duke@435 303 switch (code) {
duke@435 304 case Bytecodes::_lookupswitch:
duke@435 305 case Bytecodes::_tableswitch:
duke@435 306 case Bytecodes::_fast_binaryswitch:
duke@435 307 case Bytecodes::_fast_linearswitch:
duke@435 308 // recompute condtional expression folded into _if<cond>
duke@435 309 case Bytecodes::_lcmp :
duke@435 310 case Bytecodes::_fcmpl :
duke@435 311 case Bytecodes::_fcmpg :
duke@435 312 case Bytecodes::_dcmpl :
duke@435 313 case Bytecodes::_dcmpg :
duke@435 314 case Bytecodes::_ifnull :
duke@435 315 case Bytecodes::_ifnonnull :
duke@435 316 case Bytecodes::_goto :
duke@435 317 case Bytecodes::_goto_w :
duke@435 318 case Bytecodes::_ifeq :
duke@435 319 case Bytecodes::_ifne :
duke@435 320 case Bytecodes::_iflt :
duke@435 321 case Bytecodes::_ifge :
duke@435 322 case Bytecodes::_ifgt :
duke@435 323 case Bytecodes::_ifle :
duke@435 324 case Bytecodes::_if_icmpeq :
duke@435 325 case Bytecodes::_if_icmpne :
duke@435 326 case Bytecodes::_if_icmplt :
duke@435 327 case Bytecodes::_if_icmpge :
duke@435 328 case Bytecodes::_if_icmpgt :
duke@435 329 case Bytecodes::_if_icmple :
duke@435 330 case Bytecodes::_if_acmpeq :
duke@435 331 case Bytecodes::_if_acmpne :
duke@435 332 // special cases
duke@435 333 case Bytecodes::_getfield :
duke@435 334 case Bytecodes::_putfield :
duke@435 335 case Bytecodes::_getstatic :
duke@435 336 case Bytecodes::_putstatic :
duke@435 337 case Bytecodes::_aastore :
duke@435 338 // reexecute the operation and TOS value is on stack
duke@435 339 assert(is_top_frame, "must be top frame");
duke@435 340 use_next_mdp = false;
duke@435 341 return Interpreter::deopt_entry(vtos, 0);
duke@435 342 break;
duke@435 343
duke@435 344 #ifdef COMPILER1
duke@435 345 case Bytecodes::_athrow :
duke@435 346 assert(is_top_frame, "must be top frame");
duke@435 347 use_next_mdp = false;
duke@435 348 return Interpreter::rethrow_exception_entry();
duke@435 349 break;
duke@435 350 #endif /* COMPILER1 */
duke@435 351
duke@435 352 case Bytecodes::_invokevirtual :
duke@435 353 case Bytecodes::_invokespecial :
duke@435 354 case Bytecodes::_invokestatic :
duke@435 355 case Bytecodes::_invokeinterface: {
duke@435 356 Thread *thread = Thread::current();
duke@435 357 ResourceMark rm(thread);
duke@435 358 methodHandle mh(thread, method);
duke@435 359 type = Bytecode_invoke_at(mh, bci)->result_type(thread);
duke@435 360 // since the cache entry might not be initialized:
duke@435 361 // (NOT needed for the old calling convension)
duke@435 362 if (!is_top_frame) {
duke@435 363 int index = Bytes::get_native_u2(bcp+1);
duke@435 364 method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters);
duke@435 365 }
duke@435 366 break;
duke@435 367 }
duke@435 368
duke@435 369 case Bytecodes::_ldc :
duke@435 370 type = constant_pool_type( method, *(bcp+1) );
duke@435 371 break;
duke@435 372
duke@435 373 case Bytecodes::_ldc_w : // fall through
duke@435 374 case Bytecodes::_ldc2_w:
duke@435 375 type = constant_pool_type( method, Bytes::get_Java_u2(bcp+1) );
duke@435 376 break;
duke@435 377
duke@435 378 default:
duke@435 379 type = Bytecodes::result_type(code);
duke@435 380 break;
duke@435 381 }
duke@435 382
duke@435 383 // return entry point for computed continuation state & bytecode length
duke@435 384 return
duke@435 385 is_top_frame
duke@435 386 ? Interpreter::deopt_entry (as_TosState(type), length)
duke@435 387 : Interpreter::return_entry(as_TosState(type), length);
duke@435 388 }
duke@435 389
duke@435 390 void AbstractInterpreterGenerator::bang_stack_shadow_pages(bool native_call) {
duke@435 391 // Quick & dirty stack overflow checking: bang the stack & handle trap.
duke@435 392 // Note that we do the banging after the frame is setup, since the exception
duke@435 393 // handling code expects to find a valid interpreter frame on the stack.
duke@435 394 // Doing the banging earlier fails if the caller frame is not an interpreter
duke@435 395 // frame.
duke@435 396 // (Also, the exception throwing code expects to unlock any synchronized
duke@435 397 // method receiever, so do the banging after locking the receiver.)
duke@435 398
duke@435 399 // Bang each page in the shadow zone. We can't assume it's been done for
duke@435 400 // an interpreter frame with greater than a page of locals, so each page
duke@435 401 // needs to be checked. Only true for non-native.
duke@435 402 if (UseStackBanging) {
duke@435 403 const int start_page = native_call ? StackShadowPages : 1;
duke@435 404 const int page_size = os::vm_page_size();
duke@435 405 for (int pages = start_page; pages <= StackShadowPages ; pages++) {
duke@435 406 __ bang_stack_with_offset(pages*page_size);
duke@435 407 }
duke@435 408 }
duke@435 409 }

mercurial