1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009, 2010 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "runtime/atomic.hpp"
  28 #include "runtime/biasedLocking.hpp"
  29 #include "runtime/deoptimization.hpp"
  30 #include "runtime/thread.hpp"
  31 #include "shark/llvmHeaders.hpp"
  32 #include "shark/sharkRuntime.hpp"
  33 #include "utilities/macros.hpp"
  34 #ifdef ZERO
  35 # include "stack_zero.inline.hpp"
  36 #endif
  37 
  38 using namespace llvm;
  39 
  40 JRT_ENTRY(int, SharkRuntime::find_exception_handler(JavaThread* thread,
  41                                                     int*        indexes,
  42                                                     int         num_indexes))
  43   constantPoolHandle pool(thread, method(thread)->constants());
  44   Klass* exc_klass = ((oop) tos_at(thread, 0))->klass();
  45 
  46   for (int i = 0; i < num_indexes; i++) {
  47     Klass* tmp = pool->klass_at(indexes[i], CHECK_0);
  48 
  49     if (exc_klass() == tmp)
  50       return i;
  51 
  52     if (exc_klass()->is_subtype_of(tmp))
  53       return i;
  54   }
  55 
  56   return -1;
  57 JRT_END
  58 
  59 JRT_ENTRY(void, SharkRuntime::monitorenter(JavaThread*      thread,
  60                                            BasicObjectLock* lock))
  61   if (PrintBiasedLockingStatistics)
  62     Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
  63 
  64   Handle object(thread, lock->obj());
  65   assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
  66   if (UseBiasedLocking) {
  67     // Retry fast entry if bias is revoked to avoid unnecessary inflation
  68     ObjectSynchronizer::fast_enter(object, lock->lock(), true, CHECK);
  69   } else {
  70     ObjectSynchronizer::slow_enter(object, lock->lock(), CHECK);
  71   }
  72   assert(Universe::heap()->is_in_reserved_or_null(lock->obj()), "should be");
  73 JRT_END
  74 
  75 JRT_ENTRY(void, SharkRuntime::monitorexit(JavaThread*      thread,
  76                                           BasicObjectLock* lock))
  77   Handle object(thread, lock->obj());
  78   assert(Universe::heap()->is_in_reserved_or_null(object()), "should be");
  79   if (lock == NULL || object()->is_unlocked()) {
  80     THROW(vmSymbols::java_lang_IllegalMonitorStateException());
  81   }
  82   ObjectSynchronizer::slow_exit(object(), lock->lock(), thread);
  83 JRT_END
  84 
  85 JRT_ENTRY(void, SharkRuntime::new_instance(JavaThread* thread, int index))
  86   Klass* k_oop = method(thread)->constants()->klass_at(index, CHECK);
  87   InstanceKlass* klass = InstanceKlass::cast(k);
  88 
  89   // Make sure we are not instantiating an abstract klass
  90   klass->check_valid_for_instantiation(true, CHECK);
  91 
  92   // Make sure klass is initialized
  93   klass->initialize(CHECK);
  94 
  95   // At this point the class may not be fully initialized
  96   // because of recursive initialization. If it is fully
  97   // initialized & has_finalized is not set, we rewrite
  98   // it into its fast version (Note: no locking is needed
  99   // here since this is an atomic byte write and can be
 100   // done more than once).
 101   //
 102   // Note: In case of classes with has_finalized we don't
 103   //       rewrite since that saves us an extra check in
 104   //       the fast version which then would call the
 105   //       slow version anyway (and do a call back into
 106   //       Java).
 107   //       If we have a breakpoint, then we don't rewrite
 108   //       because the _breakpoint bytecode would be lost.
 109   oop obj = klass->allocate_instance(CHECK);
 110   thread->set_vm_result(obj);
 111 JRT_END
 112 
 113 JRT_ENTRY(void, SharkRuntime::newarray(JavaThread* thread,
 114                                        BasicType   type,
 115                                        int         size))
 116   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 117   thread->set_vm_result(obj);
 118 JRT_END
 119 
 120 JRT_ENTRY(void, SharkRuntime::anewarray(JavaThread* thread,
 121                                         int         index,
 122                                         int         size))
 123   Klass* klass = method(thread)->constants()->klass_at(index, CHECK);
 124   objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 125   thread->set_vm_result(obj);
 126 JRT_END
 127 
 128 JRT_ENTRY(void, SharkRuntime::multianewarray(JavaThread* thread,
 129                                              int         index,
 130                                              int         ndims,
 131                                              int*        dims))
 132   Klass* klass = method(thread)->constants()->klass_at(index, CHECK);
 133   oop obj = ArrayKlass::cast(klass)->multi_allocate(ndims, dims, CHECK);
 134   thread->set_vm_result(obj);
 135 JRT_END
 136 
 137 JRT_ENTRY(void, SharkRuntime::register_finalizer(JavaThread* thread,
 138                                                  oop         object))
 139   assert(oopDesc::is_oop(object), "should be");
 140   assert(object->klass()->has_finalizer(), "should have");
 141   InstanceKlass::register_finalizer(instanceOop(object), CHECK);
 142 JRT_END
 143 
 144 JRT_ENTRY(void, SharkRuntime::throw_ArithmeticException(JavaThread* thread,
 145                                                         const char* file,
 146                                                         int         line))
 147   Exceptions::_throw_msg(
 148     thread, file, line,
 149     vmSymbols::java_lang_ArithmeticException(),
 150     "");
 151 JRT_END
 152 
 153 JRT_ENTRY(void, SharkRuntime::throw_ArrayIndexOutOfBoundsException(
 154                                                      JavaThread* thread,
 155                                                      const char* file,
 156                                                      int         line,
 157                                                      int         index))
 158   char msg[jintAsStringSize];
 159   snprintf(msg, sizeof(msg), "%d", index);
 160   Exceptions::_throw_msg(
 161     thread, file, line,
 162     vmSymbols::java_lang_ArrayIndexOutOfBoundsException(),
 163     msg);
 164 JRT_END
 165 
 166 JRT_ENTRY(void, SharkRuntime::throw_ClassCastException(JavaThread* thread,
 167                                                        const char* file,
 168                                                        int         line))
 169   Exceptions::_throw_msg(
 170     thread, file, line,
 171     vmSymbols::java_lang_ClassCastException(),
 172     "");
 173 JRT_END
 174 
 175 JRT_ENTRY(void, SharkRuntime::throw_NullPointerException(JavaThread* thread,
 176                                                          const char* file,
 177                                                          int         line))
 178   Exceptions::_throw_msg(
 179     thread, file, line,
 180     vmSymbols::java_lang_NullPointerException(),
 181     "");
 182 JRT_END
 183 
 184 // Non-VM calls
 185 // Nothing in these must ever GC!
 186 
 187 void SharkRuntime::dump(const char *name, intptr_t value) {
 188   oop valueOop = (oop) value;
 189   tty->print("%s = ", name);
 190   if (valueOop->is_oop(true))
 191     valueOop->print_on(tty);
 192   else if (value >= ' ' && value <= '~')
 193     tty->print("'%c' (%d)", value, value);
 194   else
 195     tty->print("%p", value);
 196   tty->print_cr("");
 197 }
 198 
 199 bool SharkRuntime::is_subtype_of(Klass* check_klass, Klass* object_klass) {
 200   return object_klass->is_subtype_of(check_klass);
 201 }
 202 
 203 int SharkRuntime::uncommon_trap(JavaThread* thread, int trap_request) {
 204   Thread *THREAD = thread;
 205 
 206   // In C2, uncommon_trap_blob creates a frame, so all the various
 207   // deoptimization functions expect to find the frame of the method
 208   // being deopted one frame down on the stack.  We create a dummy
 209   // frame to mirror this.
 210   FakeStubFrame *stubframe = FakeStubFrame::build(CHECK_0);
 211   thread->push_zero_frame(stubframe);
 212 
 213   // Initiate the trap
 214   thread->set_last_Java_frame();
 215   Deoptimization::UnrollBlock *urb =
 216     Deoptimization::uncommon_trap(thread, trap_request, Deoptimization::Unpack_uncommon_trap);
 217   thread->reset_last_Java_frame();
 218   assert(urb->unpack_kind() == Deoptimization::Unpack_uncommon_trap, "expected Unpack_uncommon_trap");
 219 
 220   // Pop our dummy frame and the frame being deoptimized
 221   thread->pop_zero_frame();
 222   thread->pop_zero_frame();
 223 
 224   // Push skeleton frames
 225   int number_of_frames = urb->number_of_frames();
 226   for (int i = 0; i < number_of_frames; i++) {
 227     intptr_t size = urb->frame_sizes()[i];
 228     InterpreterFrame *frame = InterpreterFrame::build(size, CHECK_0);
 229     thread->push_zero_frame(frame);
 230   }
 231 
 232   // Push another dummy frame
 233   stubframe = FakeStubFrame::build(CHECK_0);
 234   thread->push_zero_frame(stubframe);
 235 
 236   // Fill in the skeleton frames
 237   thread->set_last_Java_frame();
 238   Deoptimization::unpack_frames(thread, Deoptimization::Unpack_uncommon_trap);
 239   thread->reset_last_Java_frame();
 240 
 241   // Pop our dummy frame
 242   thread->pop_zero_frame();
 243 
 244   // Fall back into the interpreter
 245   return number_of_frames;
 246 }
 247 
 248 FakeStubFrame* FakeStubFrame::build(TRAPS) {
 249   ZeroStack *stack = ((JavaThread *) THREAD)->zero_stack();
 250   stack->overflow_check(header_words, CHECK_NULL);
 251 
 252   stack->push(0); // next_frame, filled in later
 253   intptr_t *fp = stack->sp();
 254   assert(fp - stack->sp() == next_frame_off, "should be");
 255 
 256   stack->push(FAKE_STUB_FRAME);
 257   assert(fp - stack->sp() == frame_type_off, "should be");
 258 
 259   return (FakeStubFrame *) fp;
 260 }