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