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