1 /*
  2  * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2021 SAP SE. 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 "compiler/oopMap.hpp"
 28 #include "interpreter/interpreter.hpp"
 29 #include "memory/resourceArea.hpp"
 30 #include "memory/universe.hpp"
 31 #include "oops/markWord.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/javaCalls.hpp"
 37 #include "runtime/jniHandles.inline.hpp"
 38 #include "runtime/monitorChunk.hpp"
 39 #include "runtime/os.inline.hpp"
 40 #include "runtime/signature.hpp"
 41 #include "runtime/stackWatermarkSet.hpp"
 42 #include "runtime/stubCodeGenerator.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #ifdef COMPILER1
 45 #include "c1/c1_Runtime1.hpp"
 46 #include "runtime/vframeArray.hpp"
 47 #endif
 48 
 49 #ifdef ASSERT
 50 void RegisterMap::check_location_valid() {
 51 }
 52 #endif // ASSERT
 53 
 54 bool frame::safe_for_sender(JavaThread *thread) {
 55   address sp = (address)_sp;
 56   address fp = (address)_fp;
 57   address unextended_sp = (address)_unextended_sp;
 58 
 59   // consider stack guards when trying to determine "safe" stack pointers
 60   // sp must be within the usable part of the stack (not in guards)
 61   if (!thread->is_in_usable_stack(sp)) {
 62     return false;
 63   }
 64 
 65   // Unextended sp must be within the stack
 66   if (!thread->is_in_full_stack_checked(unextended_sp)) {
 67     return false;
 68   }
 69 
 70   // An fp must be within the stack and above (but not equal) sp.
 71   bool fp_safe = thread->is_in_stack_range_excl(fp, sp);
 72   // An interpreter fp must be fp_safe.
 73   // Moreover, it must be at a distance at least the size of the ijava_state structure.
 74   bool fp_interp_safe = fp_safe && ((fp - sp) >= ijava_state_size);
 75 
 76   // We know sp/unextended_sp are safe, only fp is questionable here
 77 
 78   // If the current frame is known to the code cache then we can attempt to
 79   // construct the sender and do some validation of it. This goes a long way
 80   // toward eliminating issues when we get in frame construction code
 81 
 82   if (_cb != NULL ){
 83 
 84     // First check if the frame is complete and the test is reliable.
 85     // Unfortunately we can only check frame completeness for runtime stubs
 86     // and nmethods. Other generic buffer blobs are more problematic
 87     // so we just assume they are OK.
 88     // Adapter blobs never have a complete frame and are never OK
 89     if (!_cb->is_frame_complete_at(_pc)) {
 90       if (_cb->is_compiled() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
 91         return false;
 92       }
 93     }
 94 
 95     // Could just be some random pointer within the codeBlob.
 96     if (!_cb->code_contains(_pc)) {
 97       return false;
 98     }
 99 
100     // Entry frame checks
101     if (is_entry_frame()) {
102       // An entry frame must have a valid fp.
103       return fp_safe && is_entry_frame_valid(thread);
104     }
105 
106     if (is_interpreted_frame() && !fp_interp_safe) {
107       return false;
108     }
109 
110     // At this point, there still is a chance that fp_safe is false.
111     // In particular, (fp == NULL) might be true. So let's check and
112     // bail out before we actually dereference from fp.
113     if (!fp_safe) {
114       return false;
115     }
116 
117     abi_minframe* sender_abi = (abi_minframe*) fp;
118     intptr_t* sender_sp = (intptr_t*) fp;
119     address   sender_pc = (address) sender_abi->lr;;
120 
121     // We must always be able to find a recognizable pc.
122     CodeBlob* sender_blob = CodeCache::find_blob_unsafe(sender_pc);
123     if (sender_blob == NULL) {
124       return false;
125     }
126 
127     // Could be a zombie method
128     if (sender_blob->is_zombie() || sender_blob->is_unloaded()) {
129       return false;
130     }
131 
132     // It should be safe to construct the sender though it might not be valid.
133 
134     frame sender(sender_sp, sender_pc);
135 
136     // Do we have a valid fp?
137     address sender_fp = (address) sender.fp();
138 
139     // sender_fp must be within the stack and above (but not
140     // equal) current frame's fp.
141     if (!thread->is_in_stack_range_excl(sender_fp, fp)) {
142         return false;
143     }
144 
145     // If the potential sender is the interpreter then we can do some more checking.
146     if (Interpreter::contains(sender_pc)) {
147       return sender.is_interpreted_frame_valid(thread);
148     }
149 
150     // Could just be some random pointer within the codeBlob.
151     if (!sender.cb()->code_contains(sender_pc)) {
152       return false;
153     }
154 
155     // We should never be able to see an adapter if the current frame is something from code cache.
156     if (sender_blob->is_adapter_blob()) {
157       return false;
158     }
159 
160     if (sender.is_entry_frame()) {
161       return sender.is_entry_frame_valid(thread);
162     }
163 
164     // Frame size is always greater than zero. If the sender frame size is zero or less,
165     // something is really weird and we better give up.
166     if (sender_blob->frame_size() <= 0) {
167       return false;
168     }
169 
170     return true;
171   }
172 
173   // Must be native-compiled frame. Since sender will try and use fp to find
174   // linkages it must be safe
175 
176   if (!fp_safe) {
177     return false;
178   }
179 
180   return true;
181 }
182 
183 bool frame::is_interpreted_frame() const  {
184   return Interpreter::contains(pc());
185 }
186 
187 frame frame::sender_for_entry_frame(RegisterMap *map) const {
188   assert(map != NULL, "map must be set");
189   // Java frame called from C; skip all C frames and return top C
190   // frame of that chunk as the sender.
191   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
192   assert(!entry_frame_is_first(), "next Java fp must be non zero");
193   assert(jfa->last_Java_sp() > _sp, "must be above this frame on stack");
194   map->clear();
195   assert(map->include_argument_oops(), "should be set by clear");
196 
197   if (jfa->last_Java_pc() != NULL) {
198     frame fr(jfa->last_Java_sp(), jfa->last_Java_pc());
199     return fr;
200   }
201   // Last_java_pc is not set, if we come here from compiled code. The
202   // constructor retrieves the PC from the stack.
203   frame fr(jfa->last_Java_sp());
204   return fr;
205 }
206 
207 OptimizedEntryBlob::FrameData* OptimizedEntryBlob::frame_data_for_frame(const frame& frame) const {
208   ShouldNotCallThis();
209   return nullptr;
210 }
211 
212 bool frame::optimized_entry_frame_is_first() const {
213   ShouldNotCallThis();
214   return false;
215 }
216 
217 frame frame::sender_for_interpreter_frame(RegisterMap *map) const {
218   // Pass callers initial_caller_sp as unextended_sp.
219   return frame(sender_sp(), sender_pc(), (intptr_t*)get_ijava_state()->sender_sp);
220 }
221 

222 frame frame::sender_for_compiled_frame(RegisterMap *map) const {
223   assert(map != NULL, "map must be set");
224 
225   // Frame owned by compiler.
226   address pc = *compiled_sender_pc_addr(_cb);
227   frame caller(compiled_sender_sp(_cb), pc);
228 
229   // Now adjust the map.
230 
231   // Get the rest.
232   if (map->update_map()) {
233     // Tell GC to use argument oopmaps for some runtime stubs that need it.
234     map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
235     if (_cb->oop_maps() != NULL) {
236       OopMapSet::update_register_map(this, map);
237     }
238   }
239 
240   return caller;
241 }
242 
243 intptr_t* frame::compiled_sender_sp(CodeBlob* cb) const {
244   return sender_sp();
245 }
246 
247 address* frame::compiled_sender_pc_addr(CodeBlob* cb) const {
248   return sender_pc_addr();
249 }
250 
251 frame frame::sender_raw(RegisterMap* map) const {
252   // Default is we do have to follow them. The sender_for_xxx will
253   // update it accordingly.
254   map->set_include_argument_oops(false);
255 
256   if (is_entry_frame())       return sender_for_entry_frame(map);
257   if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
258   assert(_cb == CodeCache::find_blob(pc()),"Must be the same");
259 
260   if (_cb != NULL) {
261     return sender_for_compiled_frame(map);
262   }
263   // Must be native-compiled frame, i.e. the marshaling code for native
264   // methods that exists in the core system.
265   return frame(sender_sp(), sender_pc());
266 }
267 
268 frame frame::sender(RegisterMap* map) const {
269   frame result = sender_raw(map);
270 
271   if (map->process_frames()) {
272     StackWatermarkSet::on_iteration(map->thread(), result);
273   }
274 
275   return result;
276 }
277 
278 void frame::patch_pc(Thread* thread, address pc) {
279   assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
280   if (TracePcPatching) {
281     tty->print_cr("patch_pc at address " PTR_FORMAT " [" PTR_FORMAT " -> " PTR_FORMAT "]",
282                   p2i(&((address*) _sp)[-1]), p2i(((address*) _sp)[-1]), p2i(pc));
283   }
284   own_abi()->lr = (uint64_t)pc;
285   if (_cb != NULL && _cb->is_nmethod() && ((nmethod*)_cb)->is_deopt_pc(_pc)) {
286     address orig = (((nmethod*)_cb)->get_original_pc(this));
287     assert(orig == _pc, "expected original to be stored before patching");
288     _deopt_state = is_deoptimized;
289     // Leave _pc as is.
290   } else {
291     _deopt_state = not_deoptimized;
292     _pc = pc;
293   }
294 }
295 
296 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
297   // Is there anything to do?
298   assert(is_interpreted_frame(), "Not an interpreted frame");
299   return true;
300 }
301 
302 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
303   assert(is_interpreted_frame(), "interpreted frame expected");
304   Method* method = interpreter_frame_method();
305   BasicType type = method->result_type();
306 
307   if (method->is_native()) {
308     // Prior to calling into the runtime to notify the method exit the possible
309     // result value is saved into the interpreter frame.
310     address lresult = (address)&(get_ijava_state()->lresult);
311     address fresult = (address)&(get_ijava_state()->fresult);
312 
313     switch (method->result_type()) {
314       case T_OBJECT:
315       case T_ARRAY: {
316         *oop_result = JNIHandles::resolve(*(jobject*)lresult);
317         break;
318       }
319       // We use std/stfd to store the values.
320       case T_BOOLEAN : value_result->z = (jboolean) *(unsigned long*)lresult; break;
321       case T_INT     : value_result->i = (jint)     *(long*)lresult;          break;
322       case T_CHAR    : value_result->c = (jchar)    *(unsigned long*)lresult; break;
323       case T_SHORT   : value_result->s = (jshort)   *(long*)lresult;          break;
324       case T_BYTE    : value_result->z = (jbyte)    *(long*)lresult;          break;
325       case T_LONG    : value_result->j = (jlong)    *(long*)lresult;          break;
326       case T_FLOAT   : value_result->f = (jfloat)   *(double*)fresult;        break;
327       case T_DOUBLE  : value_result->d = (jdouble)  *(double*)fresult;        break;
328       case T_VOID    : /* Nothing to do */ break;
329       default        : ShouldNotReachHere();
330     }
331   } else {
332     intptr_t* tos_addr = interpreter_frame_tos_address();
333     switch (method->result_type()) {
334       case T_OBJECT:
335       case T_ARRAY: {
336         oop obj = *(oop*)tos_addr;
337         assert(Universe::is_in_heap_or_null(obj), "sanity check");
338         *oop_result = obj;
339       }
340       case T_BOOLEAN : value_result->z = (jboolean) *(jint*)tos_addr; break;
341       case T_BYTE    : value_result->b = (jbyte) *(jint*)tos_addr; break;
342       case T_CHAR    : value_result->c = (jchar) *(jint*)tos_addr; break;
343       case T_SHORT   : value_result->s = (jshort) *(jint*)tos_addr; break;
344       case T_INT     : value_result->i = *(jint*)tos_addr; break;
345       case T_LONG    : value_result->j = *(jlong*)tos_addr; break;
346       case T_FLOAT   : value_result->f = *(jfloat*)tos_addr; break;
347       case T_DOUBLE  : value_result->d = *(jdouble*)tos_addr; break;
348       case T_VOID    : /* Nothing to do */ break;
349       default        : ShouldNotReachHere();
350     }
351   }
352   return type;
353 }
354 
355 #ifndef PRODUCT
356 
357 void frame::describe_pd(FrameValues& values, int frame_no) {
358   if (is_interpreted_frame()) {
359 #define DESCRIBE_ADDRESS(name) \
360   values.describe(frame_no, (intptr_t*)&(get_ijava_state()->name), #name);
361 
362       DESCRIBE_ADDRESS(method);
363       DESCRIBE_ADDRESS(mirror);
364       DESCRIBE_ADDRESS(locals);
365       DESCRIBE_ADDRESS(monitors);
366       DESCRIBE_ADDRESS(cpoolCache);
367       DESCRIBE_ADDRESS(bcp);
368       DESCRIBE_ADDRESS(esp);
369       DESCRIBE_ADDRESS(mdx);
370       DESCRIBE_ADDRESS(top_frame_sp);
371       DESCRIBE_ADDRESS(sender_sp);
372       DESCRIBE_ADDRESS(oop_tmp);
373       DESCRIBE_ADDRESS(lresult);
374       DESCRIBE_ADDRESS(fresult);
375   }
376 }




377 #endif
378 
379 intptr_t *frame::initial_deoptimization_info() {
380   // unused... but returns fp() to minimize changes introduced by 7087445
381   return fp();
382 }
383 
384 #ifndef PRODUCT
385 // This is a generic constructor which is only used by pns() in debug.cpp.
386 frame::frame(void* sp, void* fp, void* pc) : _sp((intptr_t*)sp), _unextended_sp((intptr_t*)sp) {
387   find_codeblob_and_set_pc_and_deopt_state((address)pc); // also sets _fp and adjusts _unextended_sp
388 }
389 
390 #endif

















--- EOF ---