1 /*
  2  * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_OOPS_STACKCHUNKFRAMESTREAM_INLINE_HPP
 26 #define SHARE_OOPS_STACKCHUNKFRAMESTREAM_INLINE_HPP
 27 
 28 #include "runtime/stackChunkFrameStream.hpp"
 29 
 30 #include "code/codeCache.inline.hpp"
 31 #include "compiler/oopMap.hpp"
 32 #include "interpreter/interpreter.hpp"
 33 #include "logging/log.hpp"
 34 #include "oops/instanceStackChunkKlass.inline.hpp"
 35 #include "oops/method.hpp"
 36 #include "oops/oop.hpp"
 37 #include "oops/stackChunkOop.inline.hpp"
 38 #include "runtime/frame.inline.hpp"
 39 #include "utilities/debug.hpp"
 40 #include "utilities/devirtualizer.inline.hpp"
 41 #include "utilities/globalDefinitions.hpp"
 42 #include "utilities/macros.hpp"
 43 #include CPU_HEADER_INLINE(stackChunkFrameStream)
 44 
 45 #ifdef ASSERT
 46 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue);
 47 #endif
 48 
 49 template <ChunkFrames frame_kind>
 50 StackChunkFrameStream<frame_kind>::StackChunkFrameStream(stackChunkOop chunk) DEBUG_ONLY(: _chunk(chunk)) {
 51   assert(chunk->is_stackChunk_noinline(), "");
 52   assert(frame_kind == ChunkFrames::Mixed || !chunk->has_mixed_frames(), "");
 53 
 54   DEBUG_ONLY(_index = 0;)
 55   _end = chunk->bottom_address();
 56   _sp = chunk->start_address() + chunk->sp();
 57   assert(_sp <= chunk->end_address() + frame::metadata_words, "");
 58 
 59   get_cb();
 60 
 61   if (frame_kind == ChunkFrames::Mixed) {
 62     _unextended_sp = (!is_done() && is_interpreted()) ? unextended_sp_for_interpreter_frame() : _sp;
 63     assert(_unextended_sp >= _sp - frame::metadata_words, "");
 64   } else {
 65     _unextended_sp = _sp;
 66   }
 67 
 68   if (is_stub()) {
 69     get_oopmap(pc(), 0);
 70     DEBUG_ONLY(_has_stub = true);
 71   } DEBUG_ONLY(else _has_stub = false;)
 72 }
 73 
 74 template <ChunkFrames frame_kind>
 75 StackChunkFrameStream<frame_kind>::StackChunkFrameStream(stackChunkOop chunk, const frame& f)
 76   DEBUG_ONLY(: _chunk(chunk)) {
 77   assert(chunk->is_stackChunk_noinline(), "");
 78   assert(frame_kind == ChunkFrames::Mixed || !chunk->has_mixed_frames(), "");
 79   // assert(!is_empty(), ""); -- allowed to be empty
 80 
 81   DEBUG_ONLY(_index = 0;)
 82 
 83   _end = chunk->bottom_address();
 84 
 85   assert(chunk->is_in_chunk(f.sp()), "");
 86   _sp = f.sp();
 87   if (frame_kind == ChunkFrames::Mixed) {
 88     _unextended_sp = f.unextended_sp();
 89     assert(_unextended_sp >= _sp - frame::metadata_words, "");
 90   } else {
 91     _unextended_sp = _sp;
 92   }
 93   assert(_sp >= chunk->start_address(), "");
 94   assert(_sp <= chunk->end_address() + frame::metadata_words, "");
 95 
 96   if (f.cb() != nullptr) {
 97     _oopmap = nullptr;
 98     _cb = f.cb();
 99   } else {
100     get_cb();
101   }
102 
103   if (is_stub()) {
104     get_oopmap(pc(), 0);
105     DEBUG_ONLY(_has_stub = true);
106   } DEBUG_ONLY(else _has_stub = false;)
107 }
108 
109 template <ChunkFrames frame_kind>
110 inline bool StackChunkFrameStream<frame_kind>::is_stub() const {
111   return cb() != nullptr && _cb->is_runtime_stub();
112 }
113 
114 template <ChunkFrames frame_kind>
115 inline bool StackChunkFrameStream<frame_kind>::is_compiled() const {
116   return cb() != nullptr && _cb->is_nmethod();
117 }
118 
119 template <>
120 inline bool StackChunkFrameStream<ChunkFrames::Mixed>::is_interpreted() const {
121   return !is_done() && Interpreter::contains(pc());
122 }
123 
124 template <>
125 inline bool StackChunkFrameStream<ChunkFrames::CompiledOnly>::is_interpreted() const {
126   return false;
127 }
128 
129 // StackChunkFrameStream<frame_kind>::frame_size() returns the words required to
130 // store the given frame as the only frame in a StackChunk. This is the size of the
131 // frame itself plus its stack arguments plus metadata at the caller's frame top (1)
132 //
133 // |====================|          ---
134 // | F0's stackargs     |           ^
135 // |                    |           |
136 // |--------------------|           |
137 // | metadata@top       | <- caller's sp
138 // |====================|           |
139 // | metadata@bottom(2) |           |
140 // |--------------------|
141 // |                    |       size S0
142 // | Frame F0           |                 ---     |====================|          ---
143 // |                    |           |      ^      | F1's stackargs     |           ^
144 // |                    |           |      |      |                    |           |
145 // |--------------------|           |   overlap   |--------------------|           |
146 // | metadata@top(1)    |<- sp      v      v      | metadata@top       | <- caller's sp
147 // |====================|          ---    ---     |====================|           |
148 //                                                | metadata@bottom    |           |
149 //           |                                    |--------------------|
150 //           |                                    | Frame F1           |       size S1
151 //      Stack Growth                              | (F0's callee)      |
152 //           |                                    |                    |           |
153 //           |                                    |                    |           |
154 //           v                                    |--------------------|           |
155 //                                                | metadata@top       |<- sp      v
156 //                                                |====================|          ---
157 //
158 // 2 frames of the same kind (interpreted or compiled) overlap. So the total
159 // size required in the StackChunk is S0 + S1 - overlap, where the overlap is
160 // the size of F1's stackargs plus frame::metadata_words_at_top.
161 //
162 // The callers of frame_size() are supposed to deduct the overlap.  The bottom
163 // frame in the StackChunk obviously does not overlap with it's caller, as it is
164 // in the parent chunk.
165 //
166 // There is no overlap if caller/callee are of different kinds. In that case the
167 // caller is extended to accomodate the callee's stack arguments. The extension
168 // is not counted though in the caller's size, so there is indeed no overlap.
169 //
170 // See ppc implementation of StackChunkFrameStream<frame_kind>::interpreter_frame_size()
171 // for more details.
172 //
173 // (1) Metadata at frame top (see frame::metadata_words_at_top)
174 //     Part of the overlap. Used on ppc64, empty on x86_64, aarch64
175 // (2) Metadata at the frame bottom (see frame::metadata_words_at_bottom)
176 //     Not part of the overlap.
177 //     Used on x86_64 (saved rbp, ret. addr.), aarch64. Empty on ppc64.
178 //
179 template <ChunkFrames frame_kind>
180 inline int StackChunkFrameStream<frame_kind>::frame_size() const {
181   return is_interpreted() ? interpreter_frame_size()
182                           : cb()->frame_size() + stack_argsize() + frame::metadata_words_at_top;
183 }
184 
185 template <ChunkFrames frame_kind>
186 inline int StackChunkFrameStream<frame_kind>::stack_argsize() const {
187   if (is_interpreted()) {
188     return interpreter_frame_stack_argsize();
189   }
190   if (is_stub()) {
191     return 0;
192   }
193   assert(cb() != nullptr, "");
194   assert(cb()->is_nmethod(), "");
195   assert(cb()->as_nmethod()->method() != nullptr, "");
196   return (cb()->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
197 }
198 
199 template <ChunkFrames frame_kind>
200 template <typename RegisterMapT>
201 inline int StackChunkFrameStream<frame_kind>::num_oops(RegisterMapT* map) const {
202   if (is_interpreted()) {
203     return interpreter_frame_num_oops(map);
204   } else if (is_compiled()) {
205     return oopmap()->num_oops();
206   } else {
207     assert(is_stub(), "invariant");
208     return 0;
209   }
210 }
211 
212 template <ChunkFrames frame_kind>
213 inline void StackChunkFrameStream<frame_kind>::initialize_register_map(RegisterMap* map) {
214   update_reg_map_pd(map);
215 }
216 
217 template <ChunkFrames frame_kind>
218 template <typename RegisterMapT>
219 inline void StackChunkFrameStream<frame_kind>::next(RegisterMapT* map, bool stop) {
220   update_reg_map(map);
221   bool is_runtime_stub = is_stub();
222   if (frame_kind == ChunkFrames::Mixed) {
223     if (is_interpreted()) {
224       next_for_interpreter_frame();
225     } else {
226       _sp = _unextended_sp + cb()->frame_size();
227       if (_sp >= _end - frame::metadata_words) {
228         _sp = _end;
229 #ifndef ZERO
230       } else if (cb()->is_nmethod() && cb()->as_nmethod()->needs_stack_repair()) {
231         _sp = frame::repair_sender_sp(cb()->as_nmethod(), _unextended_sp, (intptr_t**)(_sp - frame::sender_sp_offset));
232 #endif
233       }
234       _unextended_sp = is_interpreted() ? unextended_sp_for_interpreter_frame() : _sp;
235     }
236     assert(_unextended_sp >= _sp - frame::metadata_words, "");
237   } else {
238     _sp = _unextended_sp + cb()->frame_size();
239 #ifndef ZERO
240     if (cb()->is_nmethod() && cb()->as_nmethod()->needs_stack_repair()) {
241       _sp = frame::repair_sender_sp(cb()->as_nmethod(), _unextended_sp, (intptr_t**)(_sp - frame::sender_sp_offset));
242     }
243 #endif
244     _unextended_sp = _sp;
245   }
246   assert(!is_interpreted() || _unextended_sp == unextended_sp_for_interpreter_frame(), "");
247 
248   DEBUG_ONLY(_index++;)
249   if (stop) {
250     return;
251   }
252 
253   get_cb();
254   update_reg_map_pd(map);
255   if (is_runtime_stub && cb() != nullptr) { // there's no post-call nop and no fast oopmap lookup
256     // caller could have been deoptimized so use orig_pc()
257     _oopmap = cb()->oop_map_for_return_address(orig_pc());
258   }
259 }
260 
261 template <ChunkFrames frame_kind>
262 inline void StackChunkFrameStream<frame_kind>::get_cb() {
263   _oopmap = nullptr;
264   if (is_done() || is_interpreted()) {
265     _cb = nullptr;
266     return;
267   }
268 
269   assert(pc() != nullptr, "");
270   assert(dbg_is_safe(pc(), -1), "");
271 
272   _cb = CodeCache::find_blob_fast(pc());
273 
274   assert(_cb != nullptr, "");
275   assert(is_interpreted() || ((is_stub() || is_compiled()) && _cb->frame_size() > 0), "");
276 }
277 
278 template <ChunkFrames frame_kind>
279 inline void StackChunkFrameStream<frame_kind>::get_oopmap() const {
280   if (is_interpreted()) {
281     return;
282   }
283   assert(is_compiled(), "");
284   get_oopmap(pc(), CodeCache::find_oopmap_slot_fast(pc()));
285 }
286 
287 template <ChunkFrames frame_kind>
288 inline void StackChunkFrameStream<frame_kind>::get_oopmap(address pc, int oopmap_slot) const {
289   assert(cb() != nullptr, "");
290   assert(!is_compiled() || !cb()->as_nmethod()->is_deopt_pc(pc), "");
291   if (oopmap_slot >= 0) {
292     assert(oopmap_slot >= 0, "");
293     assert(cb()->oop_map_for_slot(oopmap_slot, pc) != nullptr, "");
294     assert(cb()->oop_map_for_slot(oopmap_slot, pc) == cb()->oop_map_for_return_address(pc), "");
295 
296     _oopmap = cb()->oop_map_for_slot(oopmap_slot, pc);
297   } else {
298     _oopmap = cb()->oop_map_for_return_address(pc);
299   }
300   assert(_oopmap != nullptr, "");
301 }
302 
303 template <ChunkFrames frame_kind>
304 template <typename RegisterMapT>
305 inline void* StackChunkFrameStream<frame_kind>::reg_to_loc(VMReg reg, const RegisterMapT* map) const {
306   assert(!is_done(), "");
307   return reg->is_reg() ? (void*)map->location(reg, sp()) // see frame::update_map_with_saved_link(&map, link_addr);
308                        : (void*)((address)unextended_sp() + (reg->reg2stack() * VMRegImpl::stack_slot_size));
309 }
310 
311 template<>
312 template<>
313 inline void StackChunkFrameStream<ChunkFrames::Mixed>::update_reg_map(RegisterMap* map) {
314   assert(!map->in_cont() || map->stack_chunk() == _chunk, "");
315   if (map->update_map() && is_stub()) {
316     frame f = to_frame();
317     oopmap()->update_register_map(&f, map); // we have callee-save registers in this case
318   }
319 }
320 
321 template<>
322 template<>
323 inline void StackChunkFrameStream<ChunkFrames::CompiledOnly>::update_reg_map(RegisterMap* map) {
324   assert(!map->in_cont() || map->stack_chunk() == _chunk, "");
325   if (map->update_map() && is_stub()) {
326     frame f = to_frame();
327     oopmap()->update_register_map(&f, map); // we have callee-save registers in this case
328   }
329 }
330 
331 template <ChunkFrames frame_kind>
332 template <typename RegisterMapT>
333 inline void StackChunkFrameStream<frame_kind>::update_reg_map(RegisterMapT* map) {}
334 
335 template <ChunkFrames frame_kind>
336 inline address StackChunkFrameStream<frame_kind>::orig_pc() const {
337   address pc1 = pc();
338   if (is_interpreted() || is_stub()) {
339     return pc1;
340   }
341   nmethod* nm = cb()->as_nmethod();
342   if (nm->is_deopt_pc(pc1)) {
343     pc1 = *(address*)((address)unextended_sp() + nm->orig_pc_offset());
344   }
345 
346   assert(pc1 != nullptr, "");
347   assert(!nm->is_deopt_pc(pc1), "");
348   assert(_cb == CodeCache::find_blob_fast(pc1), "");
349 
350   return pc1;
351 }
352 
353 template<ChunkFrames frame_kind>
354 void StackChunkFrameStream<frame_kind>::handle_deopted() const {
355   assert(!is_done(), "");
356 
357   if (_oopmap != nullptr) {
358     return;
359   }
360   if (is_interpreted()) {
361     return;
362   }
363   assert(is_compiled(), "");
364 
365   address pc1 = pc();
366   int oopmap_slot = CodeCache::find_oopmap_slot_fast(pc1);
367   if (oopmap_slot < 0) { // UNLIKELY; we could have marked frames for deoptimization in thaw_chunk
368     if (cb()->as_nmethod()->is_deopt_pc(pc1)) {
369       pc1 = orig_pc();
370       oopmap_slot = CodeCache::find_oopmap_slot_fast(pc1);
371     }
372   }
373   get_oopmap(pc1, oopmap_slot);
374 }
375 
376 template <ChunkFrames frame_kind>
377 template <class OopClosureType, class RegisterMapT>
378 inline void StackChunkFrameStream<frame_kind>::iterate_oops(OopClosureType* closure, const RegisterMapT* map) const {
379   if (is_interpreted()) {
380     frame f = to_frame();
381     f.oops_interpreted_do(closure, map, true);
382   } else {
383     DEBUG_ONLY(int oops = 0;)
384     for (OopMapStream oms(oopmap()); !oms.is_done(); oms.next()) {
385       OopMapValue omv = oms.current();
386       if (omv.type() != OopMapValue::oop_value && omv.type() != OopMapValue::narrowoop_value) {
387         continue;
388       }
389 
390       assert(UseCompressedOops || omv.type() == OopMapValue::oop_value, "");
391       DEBUG_ONLY(oops++;)
392 
393       void* p = reg_to_loc(omv.reg(), map);
394       assert(p != nullptr, "");
395       assert((_has_stub && _index == 1) || is_in_frame(p), "");
396 
397       log_develop_trace(continuations)("StackChunkFrameStream::iterate_oops narrow: %d reg: %s p: " INTPTR_FORMAT " sp offset: " INTPTR_FORMAT,
398           omv.type() == OopMapValue::narrowoop_value, omv.reg()->name(), p2i(p), (intptr_t*)p - sp());
399           omv.type() == OopMapValue::narrowoop_value ? Devirtualizer::do_oop(closure, (narrowOop*)p) : Devirtualizer::do_oop(closure, (oop*)p);
400     }
401     assert(oops == oopmap()->num_oops(), "oops: %d oopmap->num_oops(): %d", oops, oopmap()->num_oops());
402   }
403 }
404 
405 template <ChunkFrames frame_kind>
406 template <class DerivedOopClosureType, class RegisterMapT>
407 inline void StackChunkFrameStream<frame_kind>::iterate_derived_pointers(DerivedOopClosureType* closure, const RegisterMapT* map) const {
408   if (!is_compiled()) {
409     // Only compiled frames have derived pointers
410     return;
411   }
412 
413   assert(oopmap()->has_derived_oops() == oopmap()->has_any(OopMapValue::derived_oop_value), "");
414   if (!oopmap()->has_derived_oops()) {
415     return;
416   }
417 
418   for (OopMapStream oms(oopmap()); !oms.is_done(); oms.next()) {
419     OopMapValue omv = oms.current();
420     if (omv.type() != OopMapValue::derived_oop_value) {
421       continue;
422     }
423 
424     // see OopMapDo<OopMapFnT, DerivedOopFnT, ValueFilterT>::walk_derived_pointers1
425     intptr_t* derived_loc = (intptr_t*)reg_to_loc(omv.reg(), map);
426     intptr_t* base_loc    = (intptr_t*)reg_to_loc(omv.content_reg(), map);
427 
428     assert((_has_stub && _index == 1) || is_in_frame(base_loc), "");
429     assert((_has_stub && _index == 1) || is_in_frame(derived_loc), "");
430     assert(derived_loc != base_loc, "Base and derived in same location");
431     assert(is_in_oops(base_loc, map), "not found: " INTPTR_FORMAT, p2i(base_loc));
432     assert(!is_in_oops(derived_loc, map), "found: " INTPTR_FORMAT, p2i(derived_loc));
433 
434     Devirtualizer::do_derived_oop(closure, (derived_base*)base_loc, (derived_pointer*)derived_loc);
435   }
436 }
437 
438 #ifdef ASSERT
439 
440 template <ChunkFrames frame_kind>
441 template <typename RegisterMapT>
442 bool StackChunkFrameStream<frame_kind>::is_in_oops(void* p, const RegisterMapT* map) const {
443   for (OopMapStream oms(oopmap()); !oms.is_done(); oms.next()) {
444     if (oms.current().type() != OopMapValue::oop_value) {
445       continue;
446     }
447     if (reg_to_loc(oms.current().reg(), map) == p) {
448       return true;
449     }
450   }
451   return false;
452 }
453 
454 template <ChunkFrames frame_kind>
455 void StackChunkFrameStream<frame_kind>::assert_is_interpreted_and_frame_type_mixed() const {
456   assert(is_interpreted(), "");
457   assert(frame_kind == ChunkFrames::Mixed, "");
458 }
459 
460 #endif
461 
462 #endif // SHARE_OOPS_STACKCHUNKFRAMESTREAM_INLINE_HPP