1 /*
  2  * Copyright (c) 1997, 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 #include "code/debugInfo.hpp"
 26 #include "oops/access.hpp"
 27 #include "oops/compressedOops.inline.hpp"
 28 #include "oops/oop.hpp"
 29 #include "runtime/frame.inline.hpp"
 30 #include "runtime/globals.hpp"
 31 #include "runtime/handles.inline.hpp"
 32 #include "runtime/stackValue.hpp"
 33 #if INCLUDE_ZGC
 34 #include "gc/z/zBarrier.inline.hpp"
 35 #endif
 36 #if INCLUDE_SHENANDOAHGC
 37 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
 38 #endif
 39 
 40 class RegisterMap;
 41 class SmallRegisterMap;
 42 
 43 template StackValue* StackValue::create_stack_value(const frame* fr, const RegisterMap* reg_map, ScopeValue* sv);
 44 template StackValue* StackValue::create_stack_value(const frame* fr, const SmallRegisterMapNoArgs* reg_map, ScopeValue* sv);
 45 
 46 template<typename RegisterMapT>
 47 StackValue* StackValue::create_stack_value(const frame* fr, const RegisterMapT* reg_map, ScopeValue* sv) {
 48   return create_stack_value(sv, stack_value_address(fr, reg_map, sv), reg_map);
 49 }
 50 
 51 static oop oop_from_oop_location(stackChunkOop chunk, void* addr) {
 52   if (addr == nullptr) {
 53     return nullptr;
 54   }
 55 
 56   if (UseCompressedOops) {
 57     // When compressed oops is enabled, an oop location may
 58     // contain narrow oop values - we deal with that here
 59 
 60     if (chunk != nullptr && chunk->has_bitmap()) {
 61       // Transformed stack chunk with narrow oops
 62       return chunk->load_oop((narrowOop*)addr);
 63     }
 64 
 65 #ifdef _LP64
 66     if (CompressedOops::is_base(*(void**)addr)) {
 67       // Compiled code may produce decoded oop = narrow_oop_base
 68       // when a narrow oop implicit null check is used.
 69       // The narrow_oop_base could be null or be the address
 70       // of the page below heap. Use null value for both cases.
 71       return nullptr;
 72     }
 73 #endif
 74   }
 75 
 76   if (chunk != nullptr) {
 77     // Load oop from chunk
 78     return chunk->load_oop((oop*)addr);
 79   }
 80 
 81   // Load oop from stack
 82   oop val = *(oop*)addr;
 83 
 84 #if INCLUDE_SHENANDOAHGC
 85   if (UseShenandoahGC) {
 86     // Pass the value through the barrier to avoid capturing bad oops as
 87     // stack values. Note: do not heal the location, to avoid accidentally
 88     // corrupting the stack. Stack watermark barriers are supposed to handle
 89     // the healing.
 90     val = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(val);
 91   }
 92 #endif
 93 
 94   return val;
 95 }
 96 
 97 static oop oop_from_narrowOop_location(stackChunkOop chunk, void* addr, bool is_register) {
 98   assert(UseCompressedOops, "Narrow oops should not exist");
 99   assert(addr != nullptr, "Not expecting null address");
100   narrowOop* narrow_addr;
101   if (is_register) {
102     // The callee has no clue whether the register holds an int,
103     // long or is unused.  He always saves a long.  Here we know
104     // a long was saved, but we only want an int back.  Narrow the
105     // saved long to the int that the JVM wants.  We can't just
106     // use narrow_oop_cast directly, because we don't know what
107     // the high bits of the value might be.
108     narrow_addr = ((narrowOop*)addr) BIG_ENDIAN_ONLY(+ 1);
109   } else {
110     narrow_addr = (narrowOop*)addr;
111   }
112 
113   if (chunk != nullptr) {
114     // Load oop from chunk
115     return chunk->load_oop(narrow_addr);
116   }
117 
118   // Load oop from stack
119   oop val = CompressedOops::decode(*narrow_addr);
120 
121 #if INCLUDE_SHENANDOAHGC
122   if (UseShenandoahGC) {
123     // Pass the value through the barrier to avoid capturing bad oops as
124     // stack values. Note: do not heal the location, to avoid accidentally
125     // corrupting the stack. Stack watermark barriers are supposed to handle
126     // the healing.
127     val = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(val);
128   }
129 #endif
130 
131   return val;
132 }
133 
134 StackValue* StackValue::create_stack_value_from_oop_location(stackChunkOop chunk, void* addr) {
135   oop val = oop_from_oop_location(chunk, addr);
136   assert(oopDesc::is_oop_or_null(val), "bad oop found at " INTPTR_FORMAT " in_cont: %d compressed: %d",
137          p2i(addr), chunk != nullptr, chunk != nullptr && chunk->has_bitmap() && UseCompressedOops);
138   Handle h(Thread::current(), val); // Wrap a handle around the oop
139   return new StackValue(h);
140 }
141 
142 StackValue* StackValue::create_stack_value_from_narrowOop_location(stackChunkOop chunk, void* addr, bool is_register) {
143   oop val = oop_from_narrowOop_location(chunk, addr, is_register);
144   assert(oopDesc::is_oop_or_null(val), "bad oop found at " INTPTR_FORMAT " in_cont: %d compressed: %d",
145          p2i(addr), chunk != nullptr, chunk != nullptr && chunk->has_bitmap() && UseCompressedOops);
146   Handle h(Thread::current(), val); // Wrap a handle around the oop
147   return new StackValue(h);
148 }
149 
150 template<typename RegisterMapT>
151 StackValue* StackValue::create_stack_value(ScopeValue* sv, address value_addr, const RegisterMapT* reg_map) {
152   stackChunkOop chunk = reg_map->stack_chunk()();
153   if (sv->is_location()) {
154     // Stack or register value
155     Location loc = ((LocationValue *)sv)->location();
156 
157     // Then package it right depending on type
158     // Note: the transfer of the data is thru a union that contains
159     // an intptr_t. This is because an interpreter stack slot is
160     // really an intptr_t. The use of a union containing an intptr_t
161     // ensures that on a 64 bit platform we have proper alignment
162     // and that we store the value where the interpreter will expect
163     // to find it (i.e. proper endian). Similarly on a 32bit platform
164     // using the intptr_t ensures that when a value is larger than
165     // a stack slot (jlong/jdouble) that we capture the proper part
166     // of the value for the stack slot in question.
167     //
168     switch( loc.type() ) {
169     case Location::float_in_dbl: { // Holds a float in a double register?
170       // The callee has no clue whether the register holds a float,
171       // double or is unused.  He always saves a double.  Here we know
172       // a double was saved, but we only want a float back.  Narrow the
173       // saved double to the float that the JVM wants.
174       assert( loc.is_register(), "floats always saved to stack in 1 word" );
175       union { intptr_t p; jfloat jf; } value;
176       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
177       value.jf = (jfloat) *(jdouble*) value_addr;
178       return new StackValue(value.p); // 64-bit high half is stack junk
179     }
180     case Location::int_in_long: { // Holds an int in a long register?
181       // The callee has no clue whether the register holds an int,
182       // long or is unused.  He always saves a long.  Here we know
183       // a long was saved, but we only want an int back.  Narrow the
184       // saved long to the int that the JVM wants.
185       assert( loc.is_register(), "ints always saved to stack in 1 word" );
186       union { intptr_t p; jint ji;} value;
187       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
188       value.ji = (jint) *(jlong*) value_addr;
189       return new StackValue(value.p); // 64-bit high half is stack junk
190     }
191 #ifdef _LP64
192     case Location::dbl:
193       // Double value in an aligned adjacent pair
194       return new StackValue(*(intptr_t*)value_addr);
195     case Location::lng:
196       // Long   value in an aligned adjacent pair
197       return new StackValue(*(intptr_t*)value_addr);
198     case Location::narrowoop:
199       return create_stack_value_from_narrowOop_location(reg_map->stack_chunk()(), (void*)value_addr, loc.is_register());
200 #endif
201     case Location::oop:
202       return create_stack_value_from_oop_location(reg_map->stack_chunk()(), (void*)value_addr);
203     case Location::addr: {
204       loc.print_on(tty);
205       ShouldNotReachHere(); // both C1 and C2 now inline jsrs
206     }
207     case Location::normal: {
208       // Just copy all other bits straight through
209       union { intptr_t p; jint ji;} value;
210       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
211       value.ji = *(jint*)value_addr;
212       return new StackValue(value.p);
213     }
214     case Location::invalid: {
215       return new StackValue();
216     }
217     case Location::vector: {
218       loc.print_on(tty);
219       ShouldNotReachHere(); // should be handled by VectorSupport::allocate_vector()
220     }
221     default:
222       loc.print_on(tty);
223       ShouldNotReachHere();
224     }
225 
226   } else if (sv->is_constant_int()) {
227     // Constant int: treat same as register int.
228     union { intptr_t p; jint ji;} value;
229     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
230     value.ji = (jint)((ConstantIntValue*)sv)->value();
231     return new StackValue(value.p);
232   } else if (sv->is_constant_oop()) {
233     // constant oop
234     return new StackValue(sv->as_ConstantOopReadValue()->value());
235 #ifdef _LP64
236   } else if (sv->is_constant_double()) {
237     // Constant double in a single stack slot
238     union { intptr_t p; double d; } value;
239     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
240     value.d = ((ConstantDoubleValue *)sv)->value();
241     return new StackValue(value.p);
242   } else if (sv->is_constant_long()) {
243     // Constant long in a single stack slot
244     union { intptr_t p; jlong jl; } value;
245     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
246     value.jl = ((ConstantLongValue *)sv)->value();
247     return new StackValue(value.p);
248 #endif
249   } else if (sv->is_object()) { // Scalar replaced object in compiled frame
250     ObjectValue* ov = (ObjectValue *)sv;
251     Handle hdl = ov->value();
252     return new StackValue(hdl, hdl.is_null() && ov->is_scalar_replaced() ? 1 : 0);
253   } else if (sv->is_marker()) {
254     // Should never need to directly construct a marker.
255     ShouldNotReachHere();
256   }
257   // Unknown ScopeValue type
258   ShouldNotReachHere();
259   return new StackValue((intptr_t) 0);   // dummy
260 }
261 
262 template address StackValue::stack_value_address(const frame* fr, const RegisterMap* reg_map, ScopeValue* sv);
263 template address StackValue::stack_value_address(const frame* fr, const SmallRegisterMapNoArgs* reg_map, ScopeValue* sv);
264 
265 template<typename RegisterMapT>
266 address StackValue::stack_value_address(const frame* fr, const RegisterMapT* reg_map, ScopeValue* sv) {
267   if (!sv->is_location()) {
268     return nullptr;
269   }
270   Location loc = ((LocationValue *)sv)->location();
271   if (loc.type() == Location::invalid) {
272     return nullptr;
273   }
274 
275   if (!reg_map->in_cont()) {
276     address value_addr = loc.is_register()
277       // Value was in a callee-save register
278       ? reg_map->location(VMRegImpl::as_VMReg(loc.register_number()), fr->sp())
279       // Else value was directly saved on the stack. The frame's original stack pointer,
280       // before any extension by its callee (due to Compiler1 linkage on SPARC), must be used.
281       : ((address)fr->unextended_sp()) + loc.stack_offset();
282 
283     assert(value_addr == nullptr || reg_map->thread() == nullptr || reg_map->thread()->is_in_usable_stack(value_addr), INTPTR_FORMAT, p2i(value_addr));
284     return value_addr;
285   }
286 
287   address value_addr = loc.is_register()
288     ? reg_map->as_RegisterMap()->stack_chunk()->reg_to_location(*fr, reg_map->as_RegisterMap(), VMRegImpl::as_VMReg(loc.register_number()))
289     : reg_map->as_RegisterMap()->stack_chunk()->usp_offset_to_location(*fr, loc.stack_offset());
290 
291   assert(value_addr == nullptr || Continuation::is_in_usable_stack(value_addr, reg_map->as_RegisterMap()) || (reg_map->thread() != nullptr && reg_map->thread()->is_in_usable_stack(value_addr)), INTPTR_FORMAT, p2i(value_addr));
292   return value_addr;
293 }
294 
295 BasicLock* StackValue::resolve_monitor_lock(const frame& fr, Location location) {
296   assert(location.is_stack(), "for now we only look at the stack");
297   int word_offset = location.stack_offset() / wordSize;
298   // (stack picture)
299   // high: [     ]  word_offset + 1
300   // low   [     ]  word_offset
301   //
302   // sp->  [     ]  0
303   // the word_offset is the distance from the stack pointer to the lowest address
304   // The frame's original stack pointer, before any extension by its callee
305   // (due to Compiler1 linkage on SPARC), must be used.
306   return (BasicLock*) (fr.unextended_sp() + word_offset);
307 }
308 
309 
310 #ifndef PRODUCT
311 
312 void StackValue::print_on(outputStream* st) const {
313   switch(_type) {
314     case T_INT:
315       st->print("%d (int) %f (float) %x (hex)",  *(int *)&_integer_value, *(float *)&_integer_value,  *(int *)&_integer_value);
316       break;
317 
318     case T_OBJECT:
319       if (_handle_value() != nullptr) {
320         _handle_value()->print_value_on(st);
321       } else {
322         st->print("null");
323       }
324       st->print(" <" INTPTR_FORMAT ">", p2i(_handle_value()));
325       break;
326 
327     case T_CONFLICT:
328      st->print("conflict");
329      break;
330 
331     default:
332      ShouldNotReachHere();
333   }
334 }
335 
336 #endif