1 /*
   2  * Copyright (c) 1997, 2018, 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/continuation.hpp"
  38 #include "runtime/frame.inline.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/monitorChunk.hpp"
  41 #include "runtime/signature.hpp"
  42 #include "runtime/stubRoutines.hpp"
  43 #include "runtime/vframeArray.hpp"
  44 #include "runtime/vframe_hp.hpp"
  45 #ifdef COMPILER2
  46 #include "opto/matcher.hpp"
  47 #endif
  48 
  49 
  50 // ------------- compiledVFrame --------------
  51 
  52 StackValueCollection* compiledVFrame::locals() const {
  53   // Natives has no scope
  54   if (scope() == NULL) return new StackValueCollection(0);
  55   GrowableArray<ScopeValue*>*  scv_list = scope()->locals();
  56   if (scv_list == NULL) return new StackValueCollection(0);
  57 
  58   // scv_list is the list of ScopeValues describing the JVM stack state.
  59   // There is one scv_list entry for every JVM stack state in use.
  60   int length = scv_list->length();
  61   StackValueCollection* result = new StackValueCollection(length);
  62   for (int i = 0; i < length; i++) {
  63     result->add(create_stack_value(scv_list->at(i)));
  64   }
  65 
  66   // Replace the original values with any stores that have been
  67   // performed through compiledVFrame::update_locals.
  68   if (register_map()->cont() == NULL) { // LOOM TODO
  69     GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
  70     if (list != NULL ) {
  71       // In real life this never happens or is typically a single element search
  72       for (int i = 0; i < list->length(); i++) {
  73         if (list->at(i)->matches(this)) {
  74           list->at(i)->update_locals(result);
  75           break;
  76         }
  77       }
  78     }
  79   }
  80 
  81   return result;
  82 }
  83 
  84 
  85 void compiledVFrame::set_locals(StackValueCollection* values) const {
  86 
  87   fatal("Should use update_local for each local update");
  88 }
  89 
  90 void compiledVFrame::update_local(BasicType type, int index, jvalue value) {
  91   assert(index >= 0 && index < method()->max_locals(), "out of bounds");
  92   update_deferred_value(type, index, value);
  93 }
  94 
  95 void compiledVFrame::update_stack(BasicType type, int index, jvalue value) {
  96   assert(index >= 0 && index < method()->max_stack(), "out of bounds");
  97   update_deferred_value(type, index + method()->max_locals(), value);
  98 }
  99 
 100 void compiledVFrame::update_monitor(int index, MonitorInfo* val) {
 101   assert(index >= 0, "out of bounds");
 102   jvalue value;
 103   value.l = (jobject) val->owner();
 104   update_deferred_value(T_OBJECT, index + method()->max_locals() + method()->max_stack(), value);
 105 }
 106 
 107 void compiledVFrame::update_deferred_value(BasicType type, int index, jvalue value) {
 108   assert(fr().is_deoptimized_frame() || thread()->must_deopt_id() == fr().id(),
 109          "frame must be scheduled for deoptimization");
 110   assert(!Continuation::is_frame_in_continuation(thread(), fr()), "No support for deferred values in continuations");
 111   GrowableArray<jvmtiDeferredLocalVariableSet*>* deferred = thread()->deferred_locals();
 112   jvmtiDeferredLocalVariableSet* locals = NULL;
 113   if (deferred != NULL ) {
 114     // See if this vframe has already had locals with deferred writes
 115     for (int f = 0; f < deferred->length(); f++ ) {
 116       if (deferred->at(f)->matches(this)) {
 117         locals = deferred->at(f);
 118         break;
 119       }
 120     }
 121     // No matching vframe must push a new vframe
 122   } else {
 123     // No deferred updates pending for this thread.
 124     // allocate in C heap
 125     deferred =  new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariableSet*> (1, true);
 126     thread()->set_deferred_locals(deferred);
 127   }
 128   if (locals == NULL) {
 129     locals = new jvmtiDeferredLocalVariableSet(method(), bci(), fr().id(), vframe_id());
 130     deferred->push(locals);
 131     assert(locals->id() == fr().id(), "Huh? Must match");
 132   }
 133   locals->set_value_at(index, type, value);
 134 }
 135 
 136 StackValueCollection* compiledVFrame::expressions() const {
 137   // Natives has no scope
 138   if (scope() == NULL) return new StackValueCollection(0);
 139   GrowableArray<ScopeValue*>*  scv_list = scope()->expressions();
 140   if (scv_list == NULL) return new StackValueCollection(0);
 141 
 142   // scv_list is the list of ScopeValues describing the JVM stack state.
 143   // There is one scv_list entry for every JVM stack state in use.
 144   int length = scv_list->length();
 145   StackValueCollection* result = new StackValueCollection(length);
 146   for (int i = 0; i < length; i++) {
 147     result->add(create_stack_value(scv_list->at(i)));
 148   }
 149 
 150   if (register_map()->cont() == NULL) { // LOOM TODO
 151     // Replace the original values with any stores that have been
 152     // performed through compiledVFrame::update_stack.
 153     GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
 154     if (list != NULL ) {
 155       // In real life this never happens or is typically a single element search
 156       for (int i = 0; i < list->length(); i++) {
 157         if (list->at(i)->matches(this)) {
 158           list->at(i)->update_stack(result);
 159           break;
 160         }
 161       }
 162     }
 163   }
 164 
 165   return result;
 166 }
 167 
 168 
 169 // The implementation of the following two methods was factorized into the
 170 // class StackValue because it is also used from within deoptimization.cpp for
 171 // rematerialization and relocking of non-escaping objects.
 172 
 173 StackValue *compiledVFrame::create_stack_value(ScopeValue *sv) const {
 174   return StackValue::create_stack_value(&_fr, register_map(), sv);
 175 }
 176 
 177 BasicLock* compiledVFrame::resolve_monitor_lock(Location location) const {
 178   return StackValue::resolve_monitor_lock(&_fr, location);
 179 }
 180 
 181 
 182 GrowableArray<MonitorInfo*>* compiledVFrame::monitors() const {
 183   // Natives has no scope
 184   if (scope() == NULL) {
 185     CompiledMethod* nm = code();
 186     Method* method = nm->method();
 187     assert(method->is_native() || nm->is_aot(), "Expect a native method or precompiled method");
 188     if (!method->is_synchronized()) {
 189       return new GrowableArray<MonitorInfo*>(0);
 190     }
 191     // This monitor is really only needed for UseBiasedLocking, but
 192     // return it in all cases for now as it might be useful for stack
 193     // traces and tools as well
 194     GrowableArray<MonitorInfo*> *monitors = new GrowableArray<MonitorInfo*>(1);
 195     // Casting away const
 196     frame& fr = (frame&) _fr;
 197     MonitorInfo* info = new MonitorInfo(
 198         fr.get_native_receiver(), fr.get_native_monitor(), false, false);
 199     monitors->push(info);
 200     return monitors;
 201   }
 202   GrowableArray<MonitorValue*>* monitors = scope()->monitors();
 203   if (monitors == NULL) {
 204     return new GrowableArray<MonitorInfo*>(0);
 205   }
 206   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(monitors->length());
 207   for (int index = 0; index < monitors->length(); index++) {
 208     MonitorValue* mv = monitors->at(index);
 209     ScopeValue*   ov = mv->owner();
 210     StackValue *owner_sv = create_stack_value(ov); // it is an oop
 211     if (ov->is_object() && owner_sv->obj_is_scalar_replaced()) { // The owner object was scalar replaced
 212       assert(mv->eliminated(), "monitor should be eliminated for scalar replaced object");
 213       // Put klass for scalar replaced object.
 214       ScopeValue* kv = ((ObjectValue *)ov)->klass();
 215       assert(kv->is_constant_oop(), "klass should be oop constant for scalar replaced object");
 216       Handle k(Thread::current(), ((ConstantOopReadValue*)kv)->value()());
 217       assert(java_lang_Class::is_instance(k()), "must be");
 218       result->push(new MonitorInfo(k(), resolve_monitor_lock(mv->basic_lock()),
 219                                    mv->eliminated(), true));
 220     } else {
 221       result->push(new MonitorInfo(owner_sv->get_obj()(), resolve_monitor_lock(mv->basic_lock()),
 222                                    mv->eliminated(), false));
 223     }
 224   }
 225 
 226   // Replace the original values with any stores that have been
 227   // performed through compiledVFrame::update_monitors.
 228   GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread()->deferred_locals();
 229   if (list != NULL ) {
 230     // In real life this never happens or is typically a single element search
 231     for (int i = 0; i < list->length(); i++) {
 232       if (list->at(i)->matches(this)) {
 233         list->at(i)->update_monitors(result);
 234         break;
 235       }
 236     }
 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   _vframe_id = 0;
 247   // Compiled method (native stub or Java code)
 248   // native wrappers have no scope data, it is implied
 249   if (!nm->is_compiled() || !nm->as_compiled_method()->is_native_method()) {
 250       _scope  = nm->scope_desc_at(_fr.pc());
 251   }
 252 }
 253 
 254 compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope, int vframe_id)
 255 : javaVFrame(fr, reg_map, thread) {
 256   _scope  = scope;
 257   _vframe_id = vframe_id;
 258   guarantee(_scope != NULL, "scope must be present");
 259 }
 260 
 261 compiledVFrame* compiledVFrame::at_scope(int decode_offset, int vframe_id) {
 262   if (scope()->decode_offset() != decode_offset) {
 263     ScopeDesc* scope = this->scope()->at_offset(decode_offset);
 264     return new compiledVFrame(frame_pointer(), register_map(), thread(), scope, vframe_id);
 265   }
 266   assert(_vframe_id == vframe_id, "wrong frame id");
 267   return this;
 268 }
 269 
 270 bool compiledVFrame::is_top() const {
 271   // FIX IT: Remove this when new native stubs are in place
 272   if (scope() == NULL) return true;
 273   return scope()->is_top();
 274 }
 275 
 276 
 277 CompiledMethod* compiledVFrame::code() const {
 278   return CodeCache::find_compiled(_fr.pc());
 279 }
 280 
 281 
 282 Method* compiledVFrame::method() const {
 283   if (scope() == NULL) {
 284     // native nmethods have no scope the method is implied
 285     nmethod* nm = code()->as_nmethod();
 286     assert(nm->is_native_method(), "must be native");
 287     return nm->method();
 288   }
 289   return scope()->method();
 290 }
 291 
 292 
 293 int compiledVFrame::bci() const {
 294   int raw = raw_bci();
 295   return raw == SynchronizationEntryBCI ? 0 : raw;
 296 }
 297 
 298 
 299 int compiledVFrame::raw_bci() 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 0;
 305   }
 306   return scope()->bci();
 307 }
 308 
 309 bool compiledVFrame::should_reexecute() const {
 310   if (scope() == NULL) {
 311     // native nmethods have no scope the method/bci is implied
 312     nmethod* nm = code()->as_nmethod();
 313     assert(nm->is_native_method(), "must be native");
 314     return false;
 315   }
 316   return scope()->should_reexecute();
 317 }
 318 
 319 vframe* compiledVFrame::sender() const {
 320   const frame f = fr();
 321   if (scope() == NULL) {
 322     // native nmethods have no scope the method/bci is implied
 323     nmethod* nm = code()->as_nmethod();
 324     assert(nm->is_native_method(), "must be native");
 325     return vframe::sender();
 326   } else {
 327     return scope()->is_top()
 328       ? vframe::sender()
 329       : new compiledVFrame(&f, register_map(), thread(), scope()->sender(), vframe_id() + 1);
 330   }
 331 }
 332 
 333 jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id, int vframe_id) {
 334   _method = method;
 335   _bci = bci;
 336   _id = id;
 337   _vframe_id = vframe_id;
 338   // Alway will need at least one, must be on C heap
 339   _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariable*> (1, true);
 340 }
 341 
 342 jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() {
 343   for (int i = 0; i < _locals->length(); i++ ) {
 344     delete _locals->at(i);
 345   }
 346   // Free growableArray and c heap for elements
 347   delete _locals;
 348 }
 349 
 350 bool jvmtiDeferredLocalVariableSet::matches(const vframe* vf) {
 351   if (!vf->is_compiled_frame()) return false;
 352   compiledVFrame* cvf = (compiledVFrame*)vf;
 353   if (cvf->fr().id() == id() && cvf->vframe_id() == vframe_id()) {
 354     assert(cvf->method() == method() && cvf->bci() == bci(), "must agree");
 355     return true;
 356   }
 357   return false;
 358 }
 359 
 360 void jvmtiDeferredLocalVariableSet::set_value_at(int idx, BasicType type, jvalue val) {
 361   for (int i = 0; i < _locals->length(); i++) {
 362     if (_locals->at(i)->index() == idx) {
 363       assert(_locals->at(i)->type() == type, "Wrong type");
 364       _locals->at(i)->set_value(val);
 365       return;
 366     }
 367   }
 368   _locals->push(new jvmtiDeferredLocalVariable(idx, type, val));
 369 }
 370 
 371 void jvmtiDeferredLocalVariableSet::update_value(StackValueCollection* locals, BasicType type, int index, jvalue value) {
 372   switch (type) {
 373     case T_BOOLEAN:
 374       locals->set_int_at(index, value.z);
 375       break;
 376     case T_CHAR:
 377       locals->set_int_at(index, value.c);
 378       break;
 379     case T_FLOAT:
 380       locals->set_float_at(index, value.f);
 381       break;
 382     case T_DOUBLE:
 383       locals->set_double_at(index, value.d);
 384       break;
 385     case T_BYTE:
 386       locals->set_int_at(index, value.b);
 387       break;
 388     case T_SHORT:
 389       locals->set_int_at(index, value.s);
 390       break;
 391     case T_INT:
 392       locals->set_int_at(index, value.i);
 393       break;
 394     case T_LONG:
 395       locals->set_long_at(index, value.j);
 396       break;
 397     case T_OBJECT:
 398       {
 399         Handle obj(Thread::current(), (oop)value.l);
 400         locals->set_obj_at(index, obj);
 401       }
 402       break;
 403     default:
 404       ShouldNotReachHere();
 405   }
 406 }
 407 
 408 void jvmtiDeferredLocalVariableSet::update_locals(StackValueCollection* locals) {
 409   for (int l = 0; l < _locals->length(); l ++) {
 410     jvmtiDeferredLocalVariable* val = _locals->at(l);
 411     if (val->index() >= 0 && val->index() < method()->max_locals()) {
 412       update_value(locals, val->type(), val->index(), val->value());
 413     }
 414   }
 415 }
 416 
 417 
 418 void jvmtiDeferredLocalVariableSet::update_stack(StackValueCollection* expressions) {
 419   for (int l = 0; l < _locals->length(); l ++) {
 420     jvmtiDeferredLocalVariable* val = _locals->at(l);
 421     if (val->index() >= method()->max_locals() && val->index() < method()->max_locals() + method()->max_stack()) {
 422       update_value(expressions, val->type(), val->index() - method()->max_locals(), val->value());
 423     }
 424   }
 425 }
 426 
 427 
 428 void jvmtiDeferredLocalVariableSet::update_monitors(GrowableArray<MonitorInfo*>* monitors) {
 429   for (int l = 0; l < _locals->length(); l ++) {
 430     jvmtiDeferredLocalVariable* val = _locals->at(l);
 431     if (val->index() >= method()->max_locals() + method()->max_stack()) {
 432       int lock_index = val->index() - (method()->max_locals() + method()->max_stack());
 433       MonitorInfo* info = monitors->at(lock_index);
 434       MonitorInfo* new_info = new MonitorInfo((oopDesc*)val->value().l, info->lock(), info->eliminated(), info->owner_is_scalar_replaced());
 435       monitors->at_put(lock_index, new_info);
 436     }
 437   }
 438 }
 439 
 440 
 441 void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) {
 442   // The Method* is on the stack so a live activation keeps it alive
 443   // either by mirror in interpreter or code in compiled code.
 444   for (int i = 0; i < _locals->length(); i++) {
 445     if (_locals->at(i)->type() == T_OBJECT) {
 446       f->do_oop(_locals->at(i)->oop_addr());
 447     }
 448   }
 449 }
 450 
 451 jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) {
 452   _index = index;
 453   _type = type;
 454   _value = value;
 455 }
 456 
 457 
 458 #ifndef PRODUCT
 459 void compiledVFrame::verify() const {
 460   Unimplemented();
 461 }
 462 #endif // PRODUCT