1 /*
  2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2007, 2021, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "gc/shared/collectedHeap.hpp"
 28 #include "interpreter/interpreter.hpp"
 29 #include "interpreter/interpreterRuntime.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "memory/universe.hpp"
 32 #include "oops/method.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/frame.inline.hpp"
 35 #include "runtime/handles.inline.hpp"
 36 #include "runtime/signature.hpp"
 37 #include "runtime/stackWatermarkSet.hpp"
 38 #include "vmreg_zero.inline.hpp"
 39 
 40 #ifdef ASSERT
 41 void RegisterMap::check_location_valid() {
 42   ShouldNotCallThis();
 43 }
 44 #endif
 45 
 46 bool frame::is_interpreted_frame() const {
 47   return zeroframe()->is_interpreter_frame();
 48 }
 49 
 50 bool frame::is_fake_stub_frame() const {
 51   return zeroframe()->is_fake_stub_frame();
 52 }
 53 
 54 frame frame::sender_for_entry_frame(RegisterMap *map) const {
 55   assert(zeroframe()->is_entry_frame(), "wrong type of frame");
 56   assert(map != NULL, "map must be set");
 57   assert(!entry_frame_is_first(), "next Java fp must be non zero");
 58   assert(entry_frame_call_wrapper()->anchor()->last_Java_sp() == sender_sp(),
 59          "sender should be next Java frame");
 60   map->clear();
 61   assert(map->include_argument_oops(), "should be set by clear");
 62   return frame(zeroframe()->next(), sender_sp());
 63 }
 64 
 65 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
 66   ShouldNotCallThis();
 67   return nullptr;
 68 }
 69 
 70 bool frame::optimized_entry_frame_is_first() const {
 71   ShouldNotCallThis();
 72   return false;
 73 }
 74 
 75 frame frame::sender_for_nonentry_frame(RegisterMap *map) const {
 76   assert(zeroframe()->is_interpreter_frame() ||
 77          zeroframe()->is_fake_stub_frame(), "wrong type of frame");
 78   return frame(zeroframe()->next(), sender_sp());
 79 }
 80 
 81 frame frame::sender(RegisterMap* map) const {
 82   // Default is not to follow arguments; the various
 83   // sender_for_xxx methods update this accordingly.
 84   map->set_include_argument_oops(false);
 85 
 86   frame result = zeroframe()->is_entry_frame() ?
 87                  sender_for_entry_frame(map) :
 88                  sender_for_nonentry_frame(map);
 89 
 90   if (map->process_frames()) {
 91     StackWatermarkSet::on_iteration(map->thread(), result);
 92   }
 93 
 94   return result;
 95 }
 96 
 97 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 98   return get_interpreterState()->monitor_base();
 99 }
100 
101 // Pointer beyond the "oldest/deepest" BasicObjectLock on stack.
102 template BasicObjectLock* frame::interpreter_frame_monitor_end<true>() const;
103 template BasicObjectLock* frame::interpreter_frame_monitor_end<false>() const;
104 
105 template <bool relative>
106 inline BasicObjectLock* frame::interpreter_frame_monitor_end() const {
107   return (BasicObjectLock*) get_interpreterState()->stack_base();
108 }
109 
110 void frame::patch_pc(Thread* thread, address pc) {
111   if (pc != NULL) {
112     assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
113     _pc = pc;
114     _deopt_state = is_deoptimized;
115   } else {
116     // We borrow this call to set the thread pointer in the interpreter
117     // state; the hook to set up deoptimized frames isn't supplied it.
118     assert(pc == NULL, "should be");
119     get_interpreterState()->set_thread(JavaThread::cast(thread));
120   }
121 }
122 
123 bool frame::safe_for_sender(JavaThread *thread) {
124   address sp = (address)_sp;
125 
126   // consider stack guards when trying to determine "safe" stack pointers
127   // sp must be within the usable part of the stack (not in guards)
128   if (!thread->is_in_usable_stack(sp)) {
129     return false;
130   }
131 
132   // an fp must be within the stack and above (but not equal) sp
133   if (!thread->is_in_stack_range_excl((address)fp(), sp)) {
134     return false;
135   }
136 
137   // All good.
138   return true;
139 }
140 
141 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
142   assert(is_interpreted_frame(), "Not an interpreted frame");
143   // These are reasonable sanity checks
144   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
145     return false;
146   }
147   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
148     return false;
149   }
150   // These are hacks to keep us out of trouble.
151   // The problem with these is that they mask other problems
152   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
153     return false;
154   }
155 
156   // do some validation of frame elements
157   // first the method
158 
159   Method* m = *interpreter_frame_method_addr();
160 
161   // validate the method we'd find in this potential sender
162   if (!Method::is_valid_method(m)) {
163     return false;
164   }
165 
166   // validate bci/bcp
167   address bcp = interpreter_frame_bcp();
168   if (m->validate_bci_from_bcp(bcp) < 0) {
169     return false;
170   }
171 
172   // validate ConstantPoolCache*
173   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
174   if (MetaspaceObj::is_valid(cp) == false) {
175     return false;
176   }
177 
178   // validate locals
179   address locals = (address) *interpreter_frame_locals_addr();
180   if (!thread->is_in_stack_range_incl(locals, (address)fp())) {
181     return false;
182   }
183 
184   return true;
185 }
186 
187 BasicType frame::interpreter_frame_result(oop* oop_result,
188                                           jvalue* value_result) {
189   assert(is_interpreted_frame(), "interpreted frame expected");
190   Method* method = interpreter_frame_method();
191   BasicType type = method->result_type();
192   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
193   oop obj;
194 
195   switch (type) {
196   case T_VOID:
197     break;
198   case T_BOOLEAN:
199     value_result->z = *(jboolean *) tos_addr;
200     break;
201   case T_BYTE:
202     value_result->b = *(jbyte *) tos_addr;
203     break;
204   case T_CHAR:
205     value_result->c = *(jchar *) tos_addr;
206     break;
207   case T_SHORT:
208     value_result->s = *(jshort *) tos_addr;
209     break;
210   case T_INT:
211     value_result->i = *(jint *) tos_addr;
212     break;
213   case T_LONG:
214     value_result->j = *(jlong *) tos_addr;
215     break;
216   case T_FLOAT:
217     value_result->f = *(jfloat *) tos_addr;
218     break;
219   case T_DOUBLE:
220     value_result->d = *(jdouble *) tos_addr;
221     break;
222 
223   case T_OBJECT:
224   case T_ARRAY:
225     if (method->is_native()) {
226       obj = get_interpreterState()->oop_temp();
227     }
228     else {
229       oop* obj_p = (oop *) tos_addr;
230       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
231     }
232     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
233     *oop_result = obj;
234     break;
235 
236   default:
237     ShouldNotReachHere();
238   }
239 
240   return type;
241 }
242 
243 template intptr_t* frame::interpreter_frame_tos_at<false>(jint offset) const;
244 template intptr_t* frame::interpreter_frame_tos_at<true >(jint offset) const;
245 
246 template <bool relative>
247 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
248   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
249   return &interpreter_frame_tos_address()[index];
250 }
251 
252 void frame::zero_print_on_error(int           frame_index,
253                                 outputStream* st,
254                                 char*         buf,
255                                 int           buflen) const {
256   // Divide the buffer between the field and the value
257   buflen >>= 1;
258   char *fieldbuf = buf;
259   char *valuebuf = buf + buflen;
260 
261   // Print each word of the frame
262   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
263     int offset = fp() - addr;
264 
265     // Fill in default values, then try and improve them
266     snprintf(fieldbuf, buflen, "word[%d]", offset);
267     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
268     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
269     fieldbuf[buflen - 1] = '\0';
270     valuebuf[buflen - 1] = '\0';
271 
272     // Print the result
273     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
274   }
275 }
276 
277 void ZeroFrame::identify_word(int   frame_index,
278                               int   offset,
279                               char* fieldbuf,
280                               char* valuebuf,
281                               int   buflen) const {
282   switch (offset) {
283   case next_frame_off:
284     strncpy(fieldbuf, "next_frame", buflen);
285     break;
286 
287   case frame_type_off:
288     strncpy(fieldbuf, "frame_type", buflen);
289     if (is_entry_frame())
290       strncpy(valuebuf, "ENTRY_FRAME", buflen);
291     else if (is_interpreter_frame())
292       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
293     else if (is_fake_stub_frame())
294       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
295     break;
296 
297   default:
298     if (is_entry_frame()) {
299       as_entry_frame()->identify_word(
300         frame_index, offset, fieldbuf, valuebuf, buflen);
301     }
302     else if (is_interpreter_frame()) {
303       as_interpreter_frame()->identify_word(
304         frame_index, offset, fieldbuf, valuebuf, buflen);
305     }
306     else if (is_fake_stub_frame()) {
307       as_fake_stub_frame()->identify_word(
308         frame_index, offset, fieldbuf, valuebuf, buflen);
309     }
310   }
311 }
312 
313 void EntryFrame::identify_word(int   frame_index,
314                                int   offset,
315                                char* fieldbuf,
316                                char* valuebuf,
317                                int   buflen) const {
318   switch (offset) {
319   case call_wrapper_off:
320     strncpy(fieldbuf, "call_wrapper", buflen);
321     break;
322 
323   default:
324     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
325   }
326 }
327 
328 void InterpreterFrame::identify_word(int   frame_index,
329                                      int   offset,
330                                      char* fieldbuf,
331                                      char* valuebuf,
332                                      int   buflen) const {
333   interpreterState istate = interpreter_state();
334   bool is_valid = istate->self_link() == istate;
335   intptr_t *addr = addr_of_word(offset);
336 
337   // Fixed part
338   if (addr >= (intptr_t *) istate) {
339     const char *field = istate->name_of_field_at_address((address) addr);
340     if (field) {
341       if (is_valid && !strcmp(field, "_method")) {
342         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
343       }
344       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
345         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
346                  (intptr_t) istate->bcp(),
347                  istate->method()->bci_from(istate->bcp()));
348       }
349       snprintf(fieldbuf, buflen, "%sistate->%s",
350                field[strlen(field) - 1] == ')' ? "(": "", field);
351     }
352     else if (addr == (intptr_t *) istate) {
353       strncpy(fieldbuf, "(vtable for istate)", buflen);
354     }
355     return;
356   }
357 
358   // Variable part
359   if (!is_valid)
360     return;
361 
362   // JNI stuff
363   if (istate->method()->is_native() && addr < istate->stack_base()) {
364     address hA = istate->method()->signature_handler();
365     if (hA != NULL) {
366       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
367         InterpreterRuntime::SignatureHandler *handler =
368           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
369 
370         intptr_t *params = istate->stack_base() - handler->argument_count();
371         if (addr >= params) {
372           int param = addr - params;
373           const char *desc = "";
374           if (param == 0)
375             desc = " (JNIEnv)";
376           else if (param == 1) {
377             if (istate->method()->is_static())
378               desc = " (mirror)";
379             else
380               desc = " (this)";
381           }
382           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
383           return;
384         }
385 
386         for (int i = 0; i < handler->argument_count(); i++) {
387           if (params[i] == (intptr_t) addr) {
388             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
389             return;
390           }
391         }
392       }
393     }
394     return;
395   }
396 
397   // Monitors and stack
398   identify_vp_word(frame_index, addr,
399                    (intptr_t *) istate->monitor_base(),
400                    istate->stack_base(),
401                    fieldbuf, buflen);
402 }
403 
404 void ZeroFrame::identify_vp_word(int       frame_index,
405                                  intptr_t* addr,
406                                  intptr_t* monitor_base,
407                                  intptr_t* stack_base,
408                                  char*     fieldbuf,
409                                  int       buflen) const {
410   // Monitors
411   if (addr >= stack_base && addr < monitor_base) {
412     int monitor_size = frame::interpreter_frame_monitor_size();
413     int last_index = (monitor_base - stack_base) / monitor_size - 1;
414     int index = last_index - (addr - stack_base) / monitor_size;
415     intptr_t monitor = (intptr_t) (
416       (BasicObjectLock *) monitor_base - 1 - index);
417     intptr_t offset = (intptr_t) addr - monitor;
418 
419     if (offset == BasicObjectLock::obj_offset_in_bytes())
420       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
421     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
422       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
423 
424     return;
425   }
426 
427   // Expression stack
428   if (addr < stack_base) {
429     snprintf(fieldbuf, buflen, "%s[%d]",
430              frame_index == 0 ? "stack_word" : "local",
431              (int) (stack_base - addr - 1));
432     return;
433   }
434 }
435 
436 #ifndef PRODUCT
437 
438 void frame::describe_pd(FrameValues& values, int frame_no) {
439 
440 }
441 
442 void frame::describe_top_pd(FrameValues& values) {
443 
444 }
445 
446 #endif
447 
448 intptr_t *frame::initial_deoptimization_info() {
449   // unused... but returns fp() to minimize changes introduced by 7087445
450   return fp();
451 }
452 
453 #ifndef PRODUCT
454 // This is a generic constructor which is only used by pns() in debug.cpp.
455 frame::frame(void* sp, void* fp, void* pc) {
456   Unimplemented();
457 }
458 
459 #endif