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