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