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