1 /*
  2  * Copyright (c) 2003, 2023, 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 != nullptr, "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 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
 66   ShouldNotCallThis();
 67   return nullptr;
 68 }
 69 
 70 bool frame::upcall_stub_frame_is_first() const {
 71   ShouldNotCallThis();
 72   return false;
 73 }
 74 
 75 JavaThread** frame::saved_thread_address(const frame& f) {
 76   Unimplemented();
 77   return nullptr;
 78 }
 79 
 80 frame frame::sender_for_nonentry_frame(RegisterMap *map) const {
 81   assert(zeroframe()->is_interpreter_frame() ||
 82          zeroframe()->is_fake_stub_frame(), "wrong type of frame");
 83   return frame(zeroframe()->next(), sender_sp());
 84 }
 85 
 86 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
 87   return get_interpreterState()->monitor_base();
 88 }
 89 
 90 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
 91   return (BasicObjectLock*) get_interpreterState()->stack_base();
 92 }
 93 
 94 void frame::patch_pc(Thread* thread, address pc) {
 95   if (pc != nullptr) {
 96     assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
 97     _pc = pc;
 98     _deopt_state = is_deoptimized;
 99   } else {
100     // We borrow this call to set the thread pointer in the interpreter
101     // state; the hook to set up deoptimized frames isn't supplied it.
102     assert(pc == nullptr, "should be");
103     get_interpreterState()->set_thread(JavaThread::cast(thread));
104   }
105 }
106 
107 bool frame::safe_for_sender(JavaThread *thread) {
108   address sp = (address)_sp;
109 
110   // consider stack guards when trying to determine "safe" stack pointers
111   // sp must be within the usable part of the stack (not in guards)
112   if (!thread->is_in_usable_stack(sp)) {
113     return false;
114   }
115 
116   // an fp must be within the stack and above (but not equal) sp
117   if (!thread->is_in_stack_range_excl((address)fp(), sp)) {
118     return false;
119   }
120 
121   // All good.
122   return true;
123 }
124 
125 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
126   assert(is_interpreted_frame(), "Not an interpreted frame");
127   // These are reasonable sanity checks
128   if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
129     return false;
130   }
131   if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
132     return false;
133   }
134   // These are hacks to keep us out of trouble.
135   // The problem with these is that they mask other problems
136   if (fp() <= sp()) {        // this attempts to deal with unsigned comparison above
137     return false;
138   }
139 
140   // do some validation of frame elements
141   // first the method
142 
143   Method* m = *interpreter_frame_method_addr();
144 
145   // validate the method we'd find in this potential sender
146   if (!Method::is_valid_method(m)) {
147     return false;
148   }
149 
150   // validate bci/bcp
151   address bcp = interpreter_frame_bcp();
152   if (m->validate_bci_from_bcp(bcp) < 0) {
153     return false;
154   }
155 
156   // validate ConstantPoolCache*
157   ConstantPoolCache* cp = *interpreter_frame_cache_addr();
158   if (MetaspaceObj::is_valid(cp) == false) {
159     return false;
160   }
161 
162   // validate locals
163   address locals = (address)interpreter_frame_locals();
164   if (!thread->is_in_stack_range_incl(locals, (address)fp())) {
165     return false;
166   }
167 
168   return true;
169 }
170 
171 BasicType frame::interpreter_frame_result(oop* oop_result,
172                                           jvalue* value_result) {
173   assert(is_interpreted_frame(), "interpreted frame expected");
174   Method* method = interpreter_frame_method();
175   BasicType type = method->result_type();
176   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
177   oop obj;
178 
179   switch (type) {
180   case T_VOID:
181     break;
182   case T_BOOLEAN:
183     value_result->z = *(jboolean *) tos_addr;
184     break;
185   case T_BYTE:
186     value_result->b = *(jbyte *) tos_addr;
187     break;
188   case T_CHAR:
189     value_result->c = *(jchar *) tos_addr;
190     break;
191   case T_SHORT:
192     value_result->s = *(jshort *) tos_addr;
193     break;
194   case T_INT:
195     value_result->i = *(jint *) tos_addr;
196     break;
197   case T_LONG:
198     value_result->j = *(jlong *) tos_addr;
199     break;
200   case T_FLOAT:
201     value_result->f = *(jfloat *) tos_addr;
202     break;
203   case T_DOUBLE:
204     value_result->d = *(jdouble *) tos_addr;
205     break;
206 
207   case T_OBJECT:
208   case T_ARRAY:
209     if (method->is_native()) {
210       obj = get_interpreterState()->oop_temp();
211     }
212     else {
213       oop* obj_p = (oop *) tos_addr;
214       obj = (obj_p == nullptr) ? (oop) nullptr : *obj_p;
215     }
216     assert(obj == nullptr || Universe::heap()->is_in(obj), "sanity check");
217     *oop_result = obj;
218     break;
219 
220   default:
221     ShouldNotReachHere();
222   }
223 
224   return type;
225 }
226 
227 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
228   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
229   return &interpreter_frame_tos_address()[index];
230 }
231 
232 void frame::zero_print_on_error(int           frame_index,
233                                 outputStream* st,
234                                 char*         buf,
235                                 int           buflen) const {
236   // Divide the buffer between the field and the value
237   buflen >>= 1;
238   char *fieldbuf = buf;
239   char *valuebuf = buf + buflen;
240 
241   // Print each word of the frame
242   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
243     int offset = fp() - addr;
244 
245     // Fill in default values, then try and improve them
246     snprintf(fieldbuf, buflen, "word[%d]", offset);
247     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
248     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
249     fieldbuf[buflen - 1] = '\0';
250     valuebuf[buflen - 1] = '\0';
251 
252     // Print the result
253     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
254   }
255 }
256 
257 void ZeroFrame::identify_word(int   frame_index,
258                               int   offset,
259                               char* fieldbuf,
260                               char* valuebuf,
261                               int   buflen) const {
262   switch (offset) {
263   case next_frame_off:
264     strncpy(fieldbuf, "next_frame", buflen);
265     break;
266 
267   case frame_type_off:
268     strncpy(fieldbuf, "frame_type", buflen);
269     if (is_entry_frame())
270       strncpy(valuebuf, "ENTRY_FRAME", buflen);
271     else if (is_interpreter_frame())
272       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
273     else if (is_fake_stub_frame())
274       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
275     break;
276 
277   default:
278     if (is_entry_frame()) {
279       as_entry_frame()->identify_word(
280         frame_index, offset, fieldbuf, valuebuf, buflen);
281     }
282     else if (is_interpreter_frame()) {
283       as_interpreter_frame()->identify_word(
284         frame_index, offset, fieldbuf, valuebuf, buflen);
285     }
286     else if (is_fake_stub_frame()) {
287       as_fake_stub_frame()->identify_word(
288         frame_index, offset, fieldbuf, valuebuf, buflen);
289     }
290   }
291 }
292 
293 void EntryFrame::identify_word(int   frame_index,
294                                int   offset,
295                                char* fieldbuf,
296                                char* valuebuf,
297                                int   buflen) const {
298   switch (offset) {
299   case call_wrapper_off:
300     strncpy(fieldbuf, "call_wrapper", buflen);
301     break;
302 
303   default:
304     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
305   }
306 }
307 
308 void InterpreterFrame::identify_word(int   frame_index,
309                                      int   offset,
310                                      char* fieldbuf,
311                                      char* valuebuf,
312                                      int   buflen) const {
313   interpreterState istate = interpreter_state();
314   bool is_valid = istate->self_link() == istate;
315   intptr_t *addr = addr_of_word(offset);
316 
317   // Fixed part
318   if (addr >= (intptr_t *) istate) {
319     const char *field = istate->name_of_field_at_address((address) addr);
320     if (field) {
321       if (is_valid && !strcmp(field, "_method")) {
322         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
323       }
324       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
325         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
326                  (intptr_t) istate->bcp(),
327                  istate->method()->bci_from(istate->bcp()));
328       }
329       snprintf(fieldbuf, buflen, "%sistate->%s",
330                field[strlen(field) - 1] == ')' ? "(": "", field);
331     }
332     else if (addr == (intptr_t *) istate) {
333       strncpy(fieldbuf, "(vtable for istate)", buflen);
334     }
335     return;
336   }
337 
338   // Variable part
339   if (!is_valid)
340     return;
341 
342   // JNI stuff
343   if (istate->method()->is_native() && addr < istate->stack_base()) {
344     address hA = istate->method()->signature_handler();
345     if (hA != nullptr) {
346       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
347         InterpreterRuntime::SignatureHandler *handler =
348           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
349 
350         intptr_t *params = istate->stack_base() - handler->argument_count();
351         if (addr >= params) {
352           int param = addr - params;
353           const char *desc = "";
354           if (param == 0)
355             desc = " (JNIEnv)";
356           else if (param == 1) {
357             if (istate->method()->is_static())
358               desc = " (mirror)";
359             else
360               desc = " (this)";
361           }
362           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
363           return;
364         }
365 
366         for (int i = 0; i < handler->argument_count(); i++) {
367           if (params[i] == (intptr_t) addr) {
368             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
369             return;
370           }
371         }
372       }
373     }
374     return;
375   }
376 
377   // Monitors and stack
378   identify_vp_word(frame_index, addr,
379                    (intptr_t *) istate->monitor_base(),
380                    istate->stack_base(),
381                    fieldbuf, buflen);
382 }
383 
384 void ZeroFrame::identify_vp_word(int       frame_index,
385                                  intptr_t* addr,
386                                  intptr_t* monitor_base,
387                                  intptr_t* stack_base,
388                                  char*     fieldbuf,
389                                  int       buflen) const {
390   // Monitors
391   if (addr >= stack_base && addr < monitor_base) {
392     int monitor_size = frame::interpreter_frame_monitor_size();
393     int last_index = (monitor_base - stack_base) / monitor_size - 1;
394     int index = last_index - (addr - stack_base) / monitor_size;
395     intptr_t monitor = (intptr_t) (
396       (BasicObjectLock *) monitor_base - 1 - index);
397     intptr_t offset = (intptr_t) addr - monitor;
398 
399     if (offset == in_bytes(BasicObjectLock::obj_offset()))
400       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
401     else if (offset == in_bytes(BasicObjectLock::lock_offset()))
402       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
403 
404     return;
405   }
406 
407   // Expression stack
408   if (addr < stack_base) {
409     snprintf(fieldbuf, buflen, "%s[%d]",
410              frame_index == 0 ? "stack_word" : "local",
411              (int) (stack_base - addr - 1));
412     return;
413   }
414 }
415 
416 #ifndef PRODUCT
417 
418 void frame::describe_pd(FrameValues& values, int frame_no) {
419 
420 }
421 
422 #endif
423 
424 intptr_t *frame::initial_deoptimization_info() {
425   // unused... but returns fp() to minimize changes introduced by 7087445
426   return fp();
427 }
428 
429 #ifndef PRODUCT
430 // This is a generic constructor which is only used by pns() in debug.cpp.
431 frame::frame(void* sp, void* fp, void* pc) {
432   Unimplemented();
433 }
434 
435 #endif