1 /*
2  * Copyright (c) 1997, 2020, 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 
locals() const52 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 
set_locals(StackValueCollection * values) const83 void compiledVFrame::set_locals(StackValueCollection* values) const {
84 
85   fatal("Should use update_local for each local update");
86 }
87 
update_local(BasicType type,int index,jvalue value)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 
update_stack(BasicType type,int index,jvalue value)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 
update_monitor(int index,MonitorInfo * val)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 
update_deferred_value(BasicType type,int index,jvalue value)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.
create_deferred_updates_after_object_deoptimization()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 
expressions() const182 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 
create_stack_value(ScopeValue * sv) const217 StackValue *compiledVFrame::create_stack_value(ScopeValue *sv) const {
218   return StackValue::create_stack_value(&_fr, register_map(), sv);
219 }
220 
resolve_monitor_lock(Location location) const221 BasicLock* compiledVFrame::resolve_monitor_lock(Location location) const {
222   return StackValue::resolve_monitor_lock(&_fr, location);
223 }
224 
225 
monitors() const226 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() || nm->is_aot(), "Expect a native method or precompiled method");
232     if (!method->is_synchronized()) {
233       return new GrowableArray<MonitorInfo*>(0);
234     }
235     // This monitor is really only needed for UseBiasedLocking, but
236     // return it in all cases for now as it might be useful for stack
237     // traces and tools as well
238     GrowableArray<MonitorInfo*> *monitors = new GrowableArray<MonitorInfo*>(1);
239     // Casting away const
240     frame& fr = (frame&) _fr;
241     MonitorInfo* info = new MonitorInfo(
242         fr.get_native_receiver(), fr.get_native_monitor(), false, false);
243     monitors->push(info);
244     return monitors;
245   }
246   GrowableArray<MonitorValue*>* monitors = scope()->monitors();
247   if (monitors == NULL) {
248     return new GrowableArray<MonitorInfo*>(0);
249   }
250   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(monitors->length());
251   for (int index = 0; index < monitors->length(); index++) {
252     MonitorValue* mv = monitors->at(index);
253     ScopeValue*   ov = mv->owner();
254     StackValue *owner_sv = create_stack_value(ov); // it is an oop
255     if (ov->is_object() && owner_sv->obj_is_scalar_replaced()) { // The owner object was scalar replaced
256       assert(mv->eliminated(), "monitor should be eliminated for scalar replaced object");
257       // Put klass for scalar replaced object.
258       ScopeValue* kv = ((ObjectValue *)ov)->klass();
259       assert(kv->is_constant_oop(), "klass should be oop constant for scalar replaced object");
260       Handle k(Thread::current(), ((ConstantOopReadValue*)kv)->value()());
261       assert(java_lang_Class::is_instance(k()), "must be");
262       result->push(new MonitorInfo(k(), resolve_monitor_lock(mv->basic_lock()),
263                                    mv->eliminated(), true));
264     } else {
265       result->push(new MonitorInfo(owner_sv->get_obj()(), resolve_monitor_lock(mv->basic_lock()),
266                                    mv->eliminated(), false));
267     }
268   }
269 
270   // Replace the original values with any stores that have been
271   // performed through compiledVFrame::update_monitors.
272   GrowableArray<jvmtiDeferredLocalVariableSet*>* list = JvmtiDeferredUpdates::deferred_locals(thread());
273   if (list != NULL ) {
274     // In real life this never happens or is typically a single element search
275     for (int i = 0; i < list->length(); i++) {
276       if (list->at(i)->matches(this)) {
277         list->at(i)->update_monitors(result);
278         break;
279       }
280     }
281   }
282 
283   return result;
284 }
285 
286 
compiledVFrame(const frame * fr,const RegisterMap * reg_map,JavaThread * thread,CompiledMethod * nm)287 compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, CompiledMethod* nm)
288 : javaVFrame(fr, reg_map, thread) {
289   _scope  = NULL;
290   _vframe_id = 0;
291   // Compiled method (native stub or Java code)
292   // native wrappers have no scope data, it is implied
293   if (!nm->is_compiled() || !nm->as_compiled_method()->is_native_method()) {
294       _scope  = nm->scope_desc_at(_fr.pc());
295   }
296 }
297 
compiledVFrame(const frame * fr,const RegisterMap * reg_map,JavaThread * thread,ScopeDesc * scope,int vframe_id)298 compiledVFrame::compiledVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread, ScopeDesc* scope, int vframe_id)
299 : javaVFrame(fr, reg_map, thread) {
300   _scope  = scope;
301   _vframe_id = vframe_id;
302   guarantee(_scope != NULL, "scope must be present");
303 }
304 
at_scope(int decode_offset,int vframe_id)305 compiledVFrame* compiledVFrame::at_scope(int decode_offset, int vframe_id) {
306   if (scope()->decode_offset() != decode_offset) {
307     ScopeDesc* scope = this->scope()->at_offset(decode_offset);
308     return new compiledVFrame(frame_pointer(), register_map(), thread(), scope, vframe_id);
309   }
310   assert(_vframe_id == vframe_id, "wrong frame id");
311   return this;
312 }
313 
is_top() const314 bool compiledVFrame::is_top() const {
315   // FIX IT: Remove this when new native stubs are in place
316   if (scope() == NULL) return true;
317   return scope()->is_top();
318 }
319 
320 
code() const321 CompiledMethod* compiledVFrame::code() const {
322   return CodeCache::find_compiled(_fr.pc());
323 }
324 
325 
method() const326 Method* compiledVFrame::method() const {
327   if (scope() == NULL) {
328     // native nmethods have no scope the method is implied
329     nmethod* nm = code()->as_nmethod();
330     assert(nm->is_native_method(), "must be native");
331     return nm->method();
332   }
333   return scope()->method();
334 }
335 
336 
bci() const337 int compiledVFrame::bci() const {
338   int raw = raw_bci();
339   return raw == SynchronizationEntryBCI ? 0 : raw;
340 }
341 
342 
raw_bci() const343 int compiledVFrame::raw_bci() const {
344   if (scope() == NULL) {
345     // native nmethods have no scope the method/bci is implied
346     nmethod* nm = code()->as_nmethod();
347     assert(nm->is_native_method(), "must be native");
348     return 0;
349   }
350   return scope()->bci();
351 }
352 
should_reexecute() const353 bool compiledVFrame::should_reexecute() const {
354   if (scope() == NULL) {
355     // native nmethods have no scope the method/bci is implied
356     nmethod* nm = code()->as_nmethod();
357     assert(nm->is_native_method(), "must be native");
358     return false;
359   }
360   return scope()->should_reexecute();
361 }
362 
has_ea_local_in_scope() const363 bool compiledVFrame::has_ea_local_in_scope() const {
364   if (scope() == NULL) {
365     // native nmethod, all objs escape
366     assert(code()->as_nmethod()->is_native_method(), "must be native");
367     return false;
368   }
369   return (scope()->objects() != NULL) || scope()->has_ea_local_in_scope();
370 }
371 
arg_escape() const372 bool compiledVFrame::arg_escape() const {
373   if (scope() == NULL) {
374     // native nmethod, all objs escape
375     assert(code()->as_nmethod()->is_native_method(), "must be native");
376     return false;
377   }
378   return scope()->arg_escape();
379 }
380 
sender() const381 vframe* compiledVFrame::sender() const {
382   const frame f = fr();
383   if (scope() == NULL) {
384     // native nmethods have no scope the method/bci is implied
385     nmethod* nm = code()->as_nmethod();
386     assert(nm->is_native_method(), "must be native");
387     return vframe::sender();
388   } else {
389     return scope()->is_top()
390       ? vframe::sender()
391       : new compiledVFrame(&f, register_map(), thread(), scope()->sender(), vframe_id() + 1);
392   }
393 }
394 
jvmtiDeferredLocalVariableSet(Method * method,int bci,intptr_t * id,int vframe_id)395 jvmtiDeferredLocalVariableSet::jvmtiDeferredLocalVariableSet(Method* method, int bci, intptr_t* id, int vframe_id) {
396   _method = method;
397   _bci = bci;
398   _id = id;
399   _vframe_id = vframe_id;
400   // Alway will need at least one, must be on C heap
401   _locals = new(ResourceObj::C_HEAP, mtCompiler) GrowableArray<jvmtiDeferredLocalVariable*> (1, mtCompiler);
402   _objects_are_deoptimized = false;
403 }
404 
~jvmtiDeferredLocalVariableSet()405 jvmtiDeferredLocalVariableSet::~jvmtiDeferredLocalVariableSet() {
406   for (int i = 0; i < _locals->length(); i++ ) {
407     delete _locals->at(i);
408   }
409   // Free growableArray and c heap for elements
410   delete _locals;
411 }
412 
matches(const vframe * vf)413 bool jvmtiDeferredLocalVariableSet::matches(const vframe* vf) {
414   if (!vf->is_compiled_frame()) return false;
415   compiledVFrame* cvf = (compiledVFrame*)vf;
416   if (cvf->fr().id() == id() && cvf->vframe_id() == vframe_id()) {
417     assert(cvf->method() == method() && cvf->bci() == bci(), "must agree");
418     return true;
419   }
420   return false;
421 }
422 
set_value_at(int idx,BasicType type,jvalue val)423 void jvmtiDeferredLocalVariableSet::set_value_at(int idx, BasicType type, jvalue val) {
424   for (int i = 0; i < _locals->length(); i++) {
425     if (_locals->at(i)->index() == idx) {
426       assert(_locals->at(i)->type() == type, "Wrong type");
427       _locals->at(i)->set_value(val);
428       return;
429     }
430   }
431   _locals->push(new jvmtiDeferredLocalVariable(idx, type, val));
432 }
433 
update_value(StackValueCollection * locals,BasicType type,int index,jvalue value)434 void jvmtiDeferredLocalVariableSet::update_value(StackValueCollection* locals, BasicType type, int index, jvalue value) {
435   switch (type) {
436     case T_BOOLEAN:
437       locals->set_int_at(index, value.z);
438       break;
439     case T_CHAR:
440       locals->set_int_at(index, value.c);
441       break;
442     case T_FLOAT:
443       locals->set_float_at(index, value.f);
444       break;
445     case T_DOUBLE:
446       locals->set_double_at(index, value.d);
447       break;
448     case T_BYTE:
449       locals->set_int_at(index, value.b);
450       break;
451     case T_SHORT:
452       locals->set_int_at(index, value.s);
453       break;
454     case T_INT:
455       locals->set_int_at(index, value.i);
456       break;
457     case T_LONG:
458       locals->set_long_at(index, value.j);
459       break;
460     case T_OBJECT:
461       {
462         Handle obj(Thread::current(), (oop)value.l);
463         locals->set_obj_at(index, obj);
464       }
465       break;
466     default:
467       ShouldNotReachHere();
468   }
469 }
470 
update_locals(StackValueCollection * locals)471 void jvmtiDeferredLocalVariableSet::update_locals(StackValueCollection* locals) {
472   for (int l = 0; l < _locals->length(); l ++) {
473     jvmtiDeferredLocalVariable* val = _locals->at(l);
474     if (val->index() >= 0 && val->index() < method()->max_locals()) {
475       update_value(locals, val->type(), val->index(), val->value());
476     }
477   }
478 }
479 
480 
update_stack(StackValueCollection * expressions)481 void jvmtiDeferredLocalVariableSet::update_stack(StackValueCollection* expressions) {
482   for (int l = 0; l < _locals->length(); l ++) {
483     jvmtiDeferredLocalVariable* val = _locals->at(l);
484     if (val->index() >= method()->max_locals() && val->index() < method()->max_locals() + method()->max_stack()) {
485       update_value(expressions, val->type(), val->index() - method()->max_locals(), val->value());
486     }
487   }
488 }
489 
490 
update_monitors(GrowableArray<MonitorInfo * > * monitors)491 void jvmtiDeferredLocalVariableSet::update_monitors(GrowableArray<MonitorInfo*>* monitors) {
492   for (int l = 0; l < _locals->length(); l ++) {
493     jvmtiDeferredLocalVariable* val = _locals->at(l);
494     if (val->index() >= method()->max_locals() + method()->max_stack()) {
495       int lock_index = val->index() - (method()->max_locals() + method()->max_stack());
496       MonitorInfo* info = monitors->at(lock_index);
497       // Originally the owner may have been scalar replaced but as an update
498       // exists it must have been deoptimized, i.e. reallocated to the heap, and
499       // now it is considered not to be scalar replaced.
500       MonitorInfo* new_info = new MonitorInfo((oopDesc*)val->value().l, info->lock(),
501                                               info->eliminated(), false);
502       monitors->at_put(lock_index, new_info);
503     }
504   }
505 }
506 
507 
oops_do(OopClosure * f)508 void jvmtiDeferredLocalVariableSet::oops_do(OopClosure* f) {
509   // The Method* is on the stack so a live activation keeps it alive
510   // either by mirror in interpreter or code in compiled code.
511   for (int i = 0; i < _locals->length(); i++) {
512     if (_locals->at(i)->type() == T_OBJECT) {
513       f->do_oop(_locals->at(i)->oop_addr());
514     }
515   }
516 }
517 
jvmtiDeferredLocalVariable(int index,BasicType type,jvalue value)518 jvmtiDeferredLocalVariable::jvmtiDeferredLocalVariable(int index, BasicType type, jvalue value) {
519   _index = index;
520   _type = type;
521   _value = value;
522 }
523 
524 
525 #ifndef PRODUCT
verify() const526 void compiledVFrame::verify() const {
527   Unimplemented();
528 }
529 #endif // PRODUCT
530