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