1 /*
  2  * Copyright (c) 1997, 2022, 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 template <typename OopT>
 46 static oop read_oop_local(OopT* p) {
 47   // We can't do a native access directly from p because load barriers
 48   // may self-heal. If that happens on a base pointer for compressed oops,
 49   // then there will be a crash later on. Only the stack watermark API is
 50   // allowed to heal oops, because it heals derived pointers before their
 51   // corresponding base pointers.
 52   oop obj = RawAccess<>::oop_load(p);
 53   return NativeAccess<>::oop_load(&obj);
 54 }
 55 
 56 template StackValue* StackValue::create_stack_value(const frame* fr, const RegisterMap* reg_map, ScopeValue* sv);
 57 template StackValue* StackValue::create_stack_value(const frame* fr, const SmallRegisterMap* reg_map, ScopeValue* sv);
 58 
 59 template<typename RegisterMapT>
 60 StackValue* StackValue::create_stack_value(const frame* fr, const RegisterMapT* reg_map, ScopeValue* sv) {
 61   return create_stack_value(sv, stack_value_address(fr, reg_map, sv), reg_map);
 62 }
 63 
 64 template StackValue* StackValue::create_stack_value(ScopeValue*, address, const RegisterMap*);
 65 template StackValue* StackValue::create_stack_value(ScopeValue*, address, const SmallRegisterMap*);
 66 
 67 template<typename RegisterMapT>
 68 StackValue* StackValue::create_stack_value(ScopeValue* sv, address value_addr, const RegisterMapT* reg_map) {
 69   if (sv->is_location()) {
 70     // Stack or register value
 71     Location loc = ((LocationValue *)sv)->location();
 72 
 73     // Then package it right depending on type
 74     // Note: the transfer of the data is thru a union that contains
 75     // an intptr_t. This is because an interpreter stack slot is
 76     // really an intptr_t. The use of a union containing an intptr_t
 77     // ensures that on a 64 bit platform we have proper alignment
 78     // and that we store the value where the interpreter will expect
 79     // to find it (i.e. proper endian). Similarly on a 32bit platform
 80     // using the intptr_t ensures that when a value is larger than
 81     // a stack slot (jlong/jdouble) that we capture the proper part
 82     // of the value for the stack slot in question.
 83     //
 84     switch( loc.type() ) {
 85     case Location::float_in_dbl: { // Holds a float in a double register?
 86       // The callee has no clue whether the register holds a float,
 87       // double or is unused.  He always saves a double.  Here we know
 88       // a double was saved, but we only want a float back.  Narrow the
 89       // saved double to the float that the JVM wants.
 90       assert( loc.is_register(), "floats always saved to stack in 1 word" );
 91       union { intptr_t p; jfloat jf; } value;
 92       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 93       value.jf = (jfloat) *(jdouble*) value_addr;
 94       return new StackValue(value.p); // 64-bit high half is stack junk
 95     }
 96     case Location::int_in_long: { // Holds an int in a long register?
 97       // The callee has no clue whether the register holds an int,
 98       // long or is unused.  He always saves a long.  Here we know
 99       // a long was saved, but we only want an int back.  Narrow the
100       // saved long to the int that the JVM wants.
101       assert( loc.is_register(), "ints always saved to stack in 1 word" );
102       union { intptr_t p; jint ji;} value;
103       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
104       value.ji = (jint) *(jlong*) value_addr;
105       return new StackValue(value.p); // 64-bit high half is stack junk
106     }
107 #ifdef _LP64
108     case Location::dbl:
109       // Double value in an aligned adjacent pair
110       return new StackValue(*(intptr_t*)value_addr);
111     case Location::lng:
112       // Long   value in an aligned adjacent pair
113       return new StackValue(*(intptr_t*)value_addr);
114     case Location::narrowoop: {
115       assert(UseCompressedOops, "");
116       union { intptr_t p; narrowOop noop;} value;
117       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
118       if (loc.is_register()) {
119         // The callee has no clue whether the register holds an int,
120         // long or is unused.  He always saves a long.  Here we know
121         // a long was saved, but we only want an int back.  Narrow the
122         // saved long to the int that the JVM wants.  We can't just
123         // use narrow_oop_cast directly, because we don't know what
124         // the high bits of the value might be.
125         static_assert(sizeof(narrowOop) == sizeof(juint), "size mismatch");
126         juint narrow_value = (juint) *(julong*)value_addr;
127         value.noop = CompressedOops::narrow_oop_cast(narrow_value);
128       } else {
129         value.noop = *(narrowOop*) value_addr;
130       }
131       // Decode narrowoop
132       oop val = read_oop_local(&value.noop);
133       Handle h(Thread::current(), val); // Wrap a handle around the oop
134       return new StackValue(h);
135     }
136 #endif
137     case Location::oop: {
138       oop val;
139       if (reg_map->in_cont() && reg_map->stack_chunk()->has_bitmap() && UseCompressedOops) {
140         val = CompressedOops::decode(*(narrowOop*)value_addr);
141       } else {
142         val = *(oop *)value_addr;
143       }
144 #ifdef _LP64
145       if (CompressedOops::is_base(val)) {
146          // Compiled code may produce decoded oop = narrow_oop_base
147          // when a narrow oop implicit null check is used.
148          // The narrow_oop_base could be NULL or be the address
149          // of the page below heap. Use NULL value for both cases.
150          val = (oop)NULL;
151       }
152 #endif
153       val = read_oop_local(&val);
154       assert(oopDesc::is_oop_or_null(val), "bad oop found at " INTPTR_FORMAT " in_cont: %d compressed: %d",
155         p2i(value_addr), reg_map->in_cont(), reg_map->in_cont() && reg_map->stack_chunk()->has_bitmap() && UseCompressedOops);
156       Handle h(Thread::current(), val); // Wrap a handle around the oop
157       return new StackValue(h);
158     }
159     case Location::addr: {
160       loc.print_on(tty);
161       ShouldNotReachHere(); // both C1 and C2 now inline jsrs
162     }
163     case Location::normal: {
164       // Just copy all other bits straight through
165       union { intptr_t p; jint ji;} value;
166       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
167       value.ji = *(jint*)value_addr;
168       return new StackValue(value.p);
169     }
170     case Location::invalid: {
171       return new StackValue();
172     }
173     case Location::vector: {
174       loc.print_on(tty);
175       ShouldNotReachHere(); // should be handled by VectorSupport::allocate_vector()
176     }
177     default:
178       loc.print_on(tty);
179       ShouldNotReachHere();
180     }
181 
182   } else if (sv->is_constant_int()) {
183     // Constant int: treat same as register int.
184     union { intptr_t p; jint ji;} value;
185     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
186     value.ji = (jint)((ConstantIntValue*)sv)->value();
187     return new StackValue(value.p);
188   } else if (sv->is_constant_oop()) {
189     // constant oop
190     return new StackValue(sv->as_ConstantOopReadValue()->value());
191 #ifdef _LP64
192   } else if (sv->is_constant_double()) {
193     // Constant double in a single stack slot
194     union { intptr_t p; double d; } value;
195     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
196     value.d = ((ConstantDoubleValue *)sv)->value();
197     return new StackValue(value.p);
198   } else if (sv->is_constant_long()) {
199     // Constant long in a single stack slot
200     union { intptr_t p; jlong jl; } value;
201     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
202     value.jl = ((ConstantLongValue *)sv)->value();
203     return new StackValue(value.p);
204 #endif
205   } else if (sv->is_object()) { // Scalar replaced object in compiled frame
206     Handle ov = ((ObjectValue *)sv)->value();
207     return new StackValue(ov, (ov.is_null()) ? 1 : 0);
208   } else if (sv->is_marker()) {
209     // Should never need to directly construct a marker.
210     ShouldNotReachHere();
211   }
212   // Unknown ScopeValue type
213   ShouldNotReachHere();
214   return new StackValue((intptr_t) 0);   // dummy
215 }
216 
217 template address StackValue::stack_value_address(const frame* fr, const RegisterMap* reg_map, ScopeValue* sv);
218 template address StackValue::stack_value_address(const frame* fr, const SmallRegisterMap* reg_map, ScopeValue* sv);
219 
220 template<typename RegisterMapT>
221 address StackValue::stack_value_address(const frame* fr, const RegisterMapT* reg_map, ScopeValue* sv) {
222   if (!sv->is_location()) {
223     return NULL;
224   }
225   Location loc = ((LocationValue *)sv)->location();
226   if (loc.type() == Location::invalid) {
227     return NULL;
228   }
229 
230   if (!reg_map->in_cont()) {
231     address value_addr = loc.is_register()
232       // Value was in a callee-save register
233       ? reg_map->location(VMRegImpl::as_VMReg(loc.register_number()), fr->sp())
234       // Else value was directly saved on the stack. The frame's original stack pointer,
235       // before any extension by its callee (due to Compiler1 linkage on SPARC), must be used.
236       : ((address)fr->unextended_sp()) + loc.stack_offset();
237 
238     assert(value_addr == NULL || reg_map->thread() == NULL || reg_map->thread()->is_in_usable_stack(value_addr), INTPTR_FORMAT, p2i(value_addr));
239     return value_addr;
240   }
241 
242   address value_addr = loc.is_register()
243     ? reg_map->as_RegisterMap()->stack_chunk()->reg_to_location(*fr, reg_map->as_RegisterMap(), VMRegImpl::as_VMReg(loc.register_number()))
244     : reg_map->as_RegisterMap()->stack_chunk()->usp_offset_to_location(*fr, loc.stack_offset());
245 
246   assert(value_addr == NULL || Continuation::is_in_usable_stack(value_addr, reg_map->as_RegisterMap()) || (reg_map->thread() != NULL && reg_map->thread()->is_in_usable_stack(value_addr)), INTPTR_FORMAT, p2i(value_addr));
247   return value_addr;
248 }
249 
250 #ifndef PRODUCT
251 
252 void StackValue::print_on(outputStream* st) const {
253   switch(_type) {
254     case T_INT:
255       st->print("%d (int) %f (float) %x (hex)",  *(int *)&_integer_value, *(float *)&_integer_value,  *(int *)&_integer_value);
256       break;
257 
258     case T_OBJECT:
259       if (_handle_value() != NULL) {
260         _handle_value()->print_value_on(st);
261       } else {
262         st->print("NULL");
263       }
264       st->print(" <" INTPTR_FORMAT ">", p2i(_handle_value()));
265       break;
266 
267     case T_CONFLICT:
268      st->print("conflict");
269      break;
270 
271     default:
272      ShouldNotReachHere();
273   }
274 }
275 
276 #endif