1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/debugInfoRec.hpp"
  29 #include "code/nmethod.hpp"
  30 #include "code/pcDesc.hpp"
  31 #include "code/scopeDesc.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "interpreter/oopMapCache.hpp"
  34 #include "oops/instanceKlass.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/basicLock.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/monitorChunk.hpp"
  39 #include "runtime/signature.hpp"
  40 #include "runtime/stubRoutines.hpp"
  41 #include "runtime/vframeArray.hpp"
  42 #include "runtime/vframe_hp.hpp"
  43 #ifdef COMPILER2
  44 #include "opto/matcher.hpp"
  45 #endif
  46 
  47 
  48 // ------------- compiledVFrame --------------
  49 
  50 StackValueCollection* compiledVFrame::locals() const {
  51   // Natives has no scope
  52   if (scope() == NULL) return new StackValueCollection(0);
  53   GrowableArray<ScopeValue*>*  scv_list = scope()->locals();
  54   if (scv_list == NULL) return new StackValueCollection(0);
  55 
  56   // scv_list is the list of ScopeValues describing the JVM stack state.
  57   // There is one scv_list entry for every JVM stack state in use.
  58   int length = scv_list->length();
  59   StackValueCollection* result = new StackValueCollection(length);
  60   // In rare instances set_locals may have occurred in which case
  61   // there are local values that are not described by the ScopeValue anymore
  62   GrowableArray<jvmtiDeferredLocalVariable*>* deferred = NULL;
  63   GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
  64   if (list != NULL ) {
  65     // In real life this never happens or is typically a single element search
  66     for (int i = 0; i < list->length(); i++) {
  67       if (list->at(i)->matches((vframe*)this)) {
  68         deferred = list->at(i)->locals();
  69         break;
  70       }
  71     }
  72   }
  73 
  74   for( int i = 0; i < length; i++ ) {
  75     result->add( create_stack_value(scv_list->at(i)) );
  76   }
  77 
  78   // Replace specified locals with any deferred writes that are present
  79   if (deferred != NULL) {
  80     for ( int l = 0;  l < deferred->length() ; l ++) {
  81       jvmtiDeferredLocalVariable* val = deferred->at(l);
  82       switch (val->type()) {
  83       case T_BOOLEAN:
  84         result->set_int_at(val->index(), val->value().z);
  85         break;
  86       case T_CHAR:
  87         result->set_int_at(val->index(), val->value().c);
  88         break;
  89       case T_FLOAT:
  90         result->set_float_at(val->index(), val->value().f);
  91         break;
  92       case T_DOUBLE:
  93         result->set_double_at(val->index(), val->value().d);
  94         break;
  95       case T_BYTE:
  96         result->set_int_at(val->index(), val->value().b);
  97         break;
  98       case T_SHORT:
  99         result->set_int_at(val->index(), val->value().s);
 100         break;
 101       case T_INT:
 102         result->set_int_at(val->index(), val->value().i);
 103         break;
 104       case T_LONG:
 105         result->set_long_at(val->index(), val->value().j);
 106         break;
 107       case T_OBJECT:
 108         {
 109           Handle obj((oop)val->value().l);
 110           result->set_obj_at(val->index(), obj);
 111         }
 112         break;
 113       default:
 114         ShouldNotReachHere();
 115       }
 116     }
 117   }
 118 
 119   return result;
 120 }
 121 
 122 
 123 void compiledVFrame::set_locals(StackValueCollection* values) const {
 124 
 125   fatal("Should use update_local for each local update");
 126 }
 127 
 128 void compiledVFrame::update_local(BasicType type, int index, jvalue value) {
 129 
 130 #ifdef ASSERT
 131 
 132   assert(fr().is_deoptimized_frame(), "frame must be scheduled for deoptimization");
 133 #endif /* ASSERT */
 134   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = thread()->deferred_locals();
 135   if (deferred != NULL ) {
 136     // See if this vframe has already had locals with deferred writes
 137     int f;
 138     for ( f = 0 ; f < deferred->length() ; f++ ) {
 139       if (deferred->at(f)->matches(this)) {
 140         // Matching, vframe now see if the local already had deferred write
 141         GrowableArray<jvmtiDeferredLocalVariable*>* locals = deferred->at(f)->locals();
 142         int l;
 143         for (l = 0 ; l < locals->length() ; l++ ) {
 144           if (locals->at(l)->index() == index) {
 145             locals->at(l)->set_value(value);
 146             return;
 147           }
 148         }
 149         // No matching local already present. Push a new value onto the deferred collection
 150         locals->push(new jvmtiDeferredLocalVariable(index, type, value));
 151         return;
 152       }
 153     }
 154     // No matching vframe must push a new vframe
 155   } else {
 156     // No deferred updates pending for this thread.
 157     // allocate in C heap
 158     deferred =  new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariableSet*> (1, true);
 159     thread()->set_deferred_locals(deferred);
 160   }
 161   deferred->push(new jvmtiDeferredLocalVariableSet(method(), bci(), fr().id()));
 162   assert(deferred->top()->id() == fr().id(), "Huh? Must match");
 163   deferred->top()->set_local_at(index, type, value);
 164 }
 165 
 166 StackValueCollection* compiledVFrame::expressions() const {
 167   // Natives has no scope
 168   if (scope() == NULL) return new StackValueCollection(0);
 169   GrowableArray<ScopeValue*>*  scv_list = scope()->expressions();
 170   if (scv_list == NULL) return new StackValueCollection(0);
 171 
 172   // scv_list is the list of ScopeValues describing the JVM stack state.
 173   // There is one scv_list entry for every JVM stack state in use.
 174   int length = scv_list->length();
 175   StackValueCollection* result = new StackValueCollection(length);
 176   for( int i = 0; i < length; i++ )
 177     result->add( create_stack_value(scv_list->at(i)) );
 178 
 179   return result;
 180 }
 181 
 182 
 183 // The implementation of the following two methods was factorized into the
 184 // class StackValue because it is also used from within deoptimization.cpp for
 185 // rematerialization and relocking of non-escaping objects.
 186 
 187 StackValue *compiledVFrame::create_stack_value(ScopeValue *sv) const {
 188   return StackValue::create_stack_value(&_fr, register_map(), sv);
 189 }
 190 
 191 BasicLock* compiledVFrame::resolve_monitor_lock(Location location) const {
 192   return StackValue::resolve_monitor_lock(&_fr, location);
 193 }
 194 
 195 
 196 GrowableArray<MonitorInfo*>* compiledVFrame::monitors() const {
 197   // Natives has no scope
 198   if (scope() == NULL) {
 199     CompiledMethod* nm = code();
 200     Method* method = nm->method();
 201     assert(method->is_native(), "");
 202     if (!method->is_synchronized()) {
 203       return new GrowableArray<MonitorInfo*>(0);
 204     }
 205     // This monitor is really only needed for UseBiasedLocking, but
 206     // return it in all cases for now as it might be useful for stack
 207     // traces and tools as well
 208     GrowableArray<MonitorInfo*> *monitors = new GrowableArray<MonitorInfo*>(1);
 209     // Casting away const
 210     frame& fr = (frame&) _fr;
 211     MonitorInfo* info = new MonitorInfo(
 212         fr.get_native_receiver(), fr.get_native_monitor(), false, false);
 213     monitors->push(info);
 214     return monitors;
 215   }
 216   GrowableArray<MonitorValue*>* monitors = scope()->monitors();
 217   if (monitors == NULL) {
 218     return new GrowableArray<MonitorInfo*>(0);
 219   }
 220   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(monitors->length());
 221   for (int index = 0; index < monitors->length(); index++) {
 222     MonitorValue* mv = monitors->at(index);
 223     ScopeValue*   ov = mv->owner();
 224     StackValue *owner_sv = create_stack_value(ov); // it is an oop
 225     if (ov->is_object() && owner_sv->obj_is_scalar_replaced()) { // The owner object was scalar replaced
 226       assert(mv->eliminated(), "monitor should be eliminated for scalar replaced object");
 227       // Put klass for scalar replaced object.
 228       ScopeValue* kv = ((ObjectValue *)ov)->klass();
 229       assert(kv->is_constant_oop(), "klass should be oop constant for scalar replaced object");
 230       Handle k(((ConstantOopReadValue*)kv)->value()());
 231       assert(java_lang_Class::is_instance(k()), "must be");
 232       result->push(new MonitorInfo(k(), resolve_monitor_lock(mv->basic_lock()),
 233                                    mv->eliminated(), true));
 234     } else {
 235       result->push(new MonitorInfo(owner_sv->get_obj()(), resolve_monitor_lock(mv->basic_lock()),
 236                                    mv->eliminated(), false));
 237     }
 238   }
 239   return result;
 240 }
 241 
 242 
 243 compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, CompiledMethod* nm)
 244 : javaVFrame(fr, reg_map, thread) {
 245   _scope  = NULL;
 246   // Compiled method (native stub or Java code)
 247   // native wrappers have no scope data, it is implied
 248   if (!nm->is_compiled() || !nm->as_compiled_method()->is_native_method()) {
 249       _scope  = nm->scope_desc_at(_fr.pc());
 250   }
 251 }
 252 
 253 compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope)
 254 : javaVFrame(fr, reg_map, thread) {
 255   _scope  = scope;
 256   guarantee(_scope != NULL, "scope must be present");
 257 }
 258 
 259 
 260 bool compiledVFrame::is_top() const {
 261   // FIX IT: Remove this when new native stubs are in place
 262   if (scope() == NULL) return true;
 263   return scope()->is_top();
 264 }
 265 
 266 
 267 CompiledMethod* compiledVFrame::code() const {
 268   return CodeCache::find_compiled(_fr.pc());
 269 }
 270 
 271 
 272 Method* compiledVFrame::method() const {
 273   if (scope() == NULL) {
 274     // native nmethods have no scope the method is implied
 275     nmethod* nm = code()->as_nmethod();
 276     assert(nm->is_native_method(), "must be native");
 277     return nm->method();
 278   }
 279   return scope()->method();
 280 }
 281 
 282 
 283 int compiledVFrame::bci() const {
 284   int raw = raw_bci();
 285   return raw == SynchronizationEntryBCI ? 0 : raw;
 286 }
 287 
 288 
 289 int compiledVFrame::raw_bci() const {
 290   if (scope() == NULL) {
 291     // native nmethods have no scope the method/bci is implied
 292     nmethod* nm = code()->as_nmethod();
 293     assert(nm->is_native_method(), "must be native");
 294     return 0;
 295   }
 296   return scope()->bci();
 297 }
 298 
 299 bool compiledVFrame::should_reexecute() const {
 300   if (scope() == NULL) {
 301     // native nmethods have no scope the method/bci is implied
 302     nmethod* nm = code()->as_nmethod();
 303     assert(nm->is_native_method(), "must be native");
 304     return false;
 305   }
 306   return scope()->should_reexecute();
 307 }
 308 
 309 vframe* compiledVFrame::sender() const {
 310   const frame f = fr();
 311   if (scope() == NULL) {
 312     // native nmethods have no scope the method/bci is implied
 313     nmethod* nm = code()->as_nmethod();
 314     assert(nm->is_native_method(), "must be native");
 315     return vframe::sender();
 316   } else {
 317     return scope()->is_top()
 318       ? vframe::sender()
 319       : new compiledVFrame(&f, register_map(), thread(), scope()->sender());
 320   }
 321 }
 322 
 323 jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id) {
 324   _method = method;
 325   _bci = bci;
 326   _id = id;
 327   // Alway will need at least one, must be on C heap
 328   _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariable*> (1, true);
 329 }
 330 
 331 jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() {
 332   for (int i = 0; i < _locals->length() ; i++ ) {
 333     delete _locals->at(i);
 334   }
 335   // Free growableArray and c heap for elements
 336   delete _locals;
 337 }
 338 
 339 bool jvmtiDeferredLocalVariableSet::matches(vframe* vf) {
 340   if (!vf->is_compiled_frame()) return false;
 341   compiledVFrame* cvf = (compiledVFrame*)vf;
 342   return cvf->fr().id() == id() && cvf->method() == method() && cvf->bci() == bci();
 343 }
 344 
 345 void jvmtiDeferredLocalVariableSet::set_local_at(int idx, BasicType type, jvalue val) {
 346   int i;
 347   for ( i = 0 ; i < locals()->length() ; i++ ) {
 348     if ( locals()->at(i)->index() == idx) {
 349       assert(locals()->at(i)->type() == type, "Wrong type");
 350       locals()->at(i)->set_value(val);
 351       return;
 352     }
 353   }
 354   locals()->push(new jvmtiDeferredLocalVariable(idx, type, val));
 355 }
 356 
 357 void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) {
 358   // The Method* is on the stack so a live activation keeps it alive
 359   // either by mirror in interpreter or code in compiled code.
 360   for ( int i = 0; i < locals()->length(); i++ ) {
 361     if ( locals()->at(i)->type() == T_OBJECT) {
 362       f->do_oop(locals()->at(i)->oop_addr());
 363     }
 364   }
 365 }
 366 
 367 jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) {
 368   _index = index;
 369   _type = type;
 370   _value = value;
 371 }
 372 
 373 
 374 #ifndef PRODUCT
 375 void compiledVFrame::verify() const {
 376   Unimplemented();
 377 }
 378 #endif // PRODUCT