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