1 /*
  2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
  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   ShouldNotCallThis();
125   return false;
126 }
127 
128 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
129   ShouldNotCallThis();
130   return false;
131 }
132 
133 BasicType frame::interpreter_frame_result(oop* oop_result,
134                                           jvalue* value_result) {
135   assert(is_interpreted_frame(), "interpreted frame expected");
136   Method* method = interpreter_frame_method();
137   BasicType type = method->result_type();
138   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
139   oop obj;
140 
141   switch (type) {
142   case T_VOID:
143     break;
144   case T_BOOLEAN:
145     value_result->z = *(jboolean *) tos_addr;
146     break;
147   case T_BYTE:
148     value_result->b = *(jbyte *) tos_addr;
149     break;
150   case T_CHAR:
151     value_result->c = *(jchar *) tos_addr;
152     break;
153   case T_SHORT:
154     value_result->s = *(jshort *) tos_addr;
155     break;
156   case T_INT:
157     value_result->i = *(jint *) tos_addr;
158     break;
159   case T_LONG:
160     value_result->j = *(jlong *) tos_addr;
161     break;
162   case T_FLOAT:
163     value_result->f = *(jfloat *) tos_addr;
164     break;
165   case T_DOUBLE:
166     value_result->d = *(jdouble *) tos_addr;
167     break;
168 
169   case T_OBJECT:
170   case T_ARRAY:
171     if (method->is_native()) {
172       obj = get_interpreterState()->oop_temp();
173     }
174     else {
175       oop* obj_p = (oop *) tos_addr;
176       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
177     }
178     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
179     *oop_result = obj;
180     break;
181 
182   default:
183     ShouldNotReachHere();
184   }
185 
186   return type;
187 }
188 
189 template intptr_t* frame::interpreter_frame_tos_at<false>(jint offset) const;
190 template intptr_t* frame::interpreter_frame_tos_at<true >(jint offset) const;
191 
192 template <bool relative>
193 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
194   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
195   return &interpreter_frame_tos_address()[index];
196 }
197 
198 void frame::zero_print_on_error(int           frame_index,
199                                 outputStream* st,
200                                 char*         buf,
201                                 int           buflen) const {
202   // Divide the buffer between the field and the value
203   buflen >>= 1;
204   char *fieldbuf = buf;
205   char *valuebuf = buf + buflen;
206 
207   // Print each word of the frame
208   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
209     int offset = fp() - addr;
210 
211     // Fill in default values, then try and improve them
212     snprintf(fieldbuf, buflen, "word[%d]", offset);
213     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
214     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
215     fieldbuf[buflen - 1] = '\0';
216     valuebuf[buflen - 1] = '\0';
217 
218     // Print the result
219     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
220   }
221 }
222 
223 void ZeroFrame::identify_word(int   frame_index,
224                               int   offset,
225                               char* fieldbuf,
226                               char* valuebuf,
227                               int   buflen) const {
228   switch (offset) {
229   case next_frame_off:
230     strncpy(fieldbuf, "next_frame", buflen);
231     break;
232 
233   case frame_type_off:
234     strncpy(fieldbuf, "frame_type", buflen);
235     if (is_entry_frame())
236       strncpy(valuebuf, "ENTRY_FRAME", buflen);
237     else if (is_interpreter_frame())
238       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
239     else if (is_fake_stub_frame())
240       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
241     break;
242 
243   default:
244     if (is_entry_frame()) {
245       as_entry_frame()->identify_word(
246         frame_index, offset, fieldbuf, valuebuf, buflen);
247     }
248     else if (is_interpreter_frame()) {
249       as_interpreter_frame()->identify_word(
250         frame_index, offset, fieldbuf, valuebuf, buflen);
251     }
252     else if (is_fake_stub_frame()) {
253       as_fake_stub_frame()->identify_word(
254         frame_index, offset, fieldbuf, valuebuf, buflen);
255     }
256   }
257 }
258 
259 void EntryFrame::identify_word(int   frame_index,
260                                int   offset,
261                                char* fieldbuf,
262                                char* valuebuf,
263                                int   buflen) const {
264   switch (offset) {
265   case call_wrapper_off:
266     strncpy(fieldbuf, "call_wrapper", buflen);
267     break;
268 
269   default:
270     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
271   }
272 }
273 
274 void InterpreterFrame::identify_word(int   frame_index,
275                                      int   offset,
276                                      char* fieldbuf,
277                                      char* valuebuf,
278                                      int   buflen) const {
279   interpreterState istate = interpreter_state();
280   bool is_valid = istate->self_link() == istate;
281   intptr_t *addr = addr_of_word(offset);
282 
283   // Fixed part
284   if (addr >= (intptr_t *) istate) {
285     const char *field = istate->name_of_field_at_address((address) addr);
286     if (field) {
287       if (is_valid && !strcmp(field, "_method")) {
288         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
289       }
290       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
291         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
292                  (intptr_t) istate->bcp(),
293                  istate->method()->bci_from(istate->bcp()));
294       }
295       snprintf(fieldbuf, buflen, "%sistate->%s",
296                field[strlen(field) - 1] == ')' ? "(": "", field);
297     }
298     else if (addr == (intptr_t *) istate) {
299       strncpy(fieldbuf, "(vtable for istate)", buflen);
300     }
301     return;
302   }
303 
304   // Variable part
305   if (!is_valid)
306     return;
307 
308   // JNI stuff
309   if (istate->method()->is_native() && addr < istate->stack_base()) {
310     address hA = istate->method()->signature_handler();
311     if (hA != NULL) {
312       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
313         InterpreterRuntime::SignatureHandler *handler =
314           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
315 
316         intptr_t *params = istate->stack_base() - handler->argument_count();
317         if (addr >= params) {
318           int param = addr - params;
319           const char *desc = "";
320           if (param == 0)
321             desc = " (JNIEnv)";
322           else if (param == 1) {
323             if (istate->method()->is_static())
324               desc = " (mirror)";
325             else
326               desc = " (this)";
327           }
328           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
329           return;
330         }
331 
332         for (int i = 0; i < handler->argument_count(); i++) {
333           if (params[i] == (intptr_t) addr) {
334             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
335             return;
336           }
337         }
338       }
339     }
340     return;
341   }
342 
343   // Monitors and stack
344   identify_vp_word(frame_index, addr,
345                    (intptr_t *) istate->monitor_base(),
346                    istate->stack_base(),
347                    fieldbuf, buflen);
348 }
349 
350 void ZeroFrame::identify_vp_word(int       frame_index,
351                                  intptr_t* addr,
352                                  intptr_t* monitor_base,
353                                  intptr_t* stack_base,
354                                  char*     fieldbuf,
355                                  int       buflen) const {
356   // Monitors
357   if (addr >= stack_base && addr < monitor_base) {
358     int monitor_size = frame::interpreter_frame_monitor_size();
359     int last_index = (monitor_base - stack_base) / monitor_size - 1;
360     int index = last_index - (addr - stack_base) / monitor_size;
361     intptr_t monitor = (intptr_t) (
362       (BasicObjectLock *) monitor_base - 1 - index);
363     intptr_t offset = (intptr_t) addr - monitor;
364 
365     if (offset == BasicObjectLock::obj_offset_in_bytes())
366       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
367     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
368       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
369 
370     return;
371   }
372 
373   // Expression stack
374   if (addr < stack_base) {
375     snprintf(fieldbuf, buflen, "%s[%d]",
376              frame_index == 0 ? "stack_word" : "local",
377              (int) (stack_base - addr - 1));
378     return;
379   }
380 }
381 
382 #ifndef PRODUCT
383 
384 void frame::describe_pd(FrameValues& values, int frame_no) {
385 
386 }
387 
388 void frame::describe_top_pd(FrameValues& values) {
389 
390 }
391 
392 #endif
393 
394 intptr_t *frame::initial_deoptimization_info() {
395   // unused... but returns fp() to minimize changes introduced by 7087445
396   return fp();
397 }
398 
399 #ifndef PRODUCT
400 // This is a generic constructor which is only used by pns() in debug.cpp.
401 frame::frame(void* sp, void* fp, void* pc) {
402   Unimplemented();
403 }
404 
405 #endif