Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 17 | #include "emulated_stack_frame-inl.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 18 | |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 19 | #include "array-alloc-inl.h" |
| 20 | #include "array-inl.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 21 | #include "class-alloc-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 22 | #include "class_root-inl.h" |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 23 | #include "handle.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 24 | #include "jvalue-inl.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 25 | #include "method_handles-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 26 | #include "method_handles.h" |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 27 | #include "method_type-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 28 | #include "object_array-alloc-inl.h" |
| 29 | #include "object_array-inl.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 30 | #include "reflection-inl.h" |
| 31 | |
Dmitrii Ishcheikin | ddfb9fa | 2024-01-16 18:09:15 +0000 | [diff] [blame] | 32 | namespace art HIDDEN { |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 33 | namespace mirror { |
| 34 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 35 | // Calculates the size of a stack frame based on the size of its argument |
| 36 | // types and return types. |
| 37 | static void CalculateFrameAndReferencesSize(ObjPtr<mirror::ObjectArray<mirror::Class>> p_types, |
| 38 | ObjPtr<mirror::Class> r_type, |
| 39 | size_t* frame_size_out, |
| 40 | size_t* references_size_out) |
| 41 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 42 | const size_t length = p_types->GetLength(); |
| 43 | size_t frame_size = 0; |
| 44 | size_t references_size = 0; |
| 45 | for (size_t i = 0; i < length; ++i) { |
| 46 | ObjPtr<mirror::Class> type = p_types->GetWithoutChecks(i); |
| 47 | const Primitive::Type primitive_type = type->GetPrimitiveType(); |
| 48 | if (primitive_type == Primitive::kPrimNot) { |
| 49 | references_size++; |
| 50 | } else if (Primitive::Is64BitType(primitive_type)) { |
| 51 | frame_size += 8; |
| 52 | } else { |
| 53 | frame_size += 4; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | const Primitive::Type return_type = r_type->GetPrimitiveType(); |
| 58 | if (return_type == Primitive::kPrimNot) { |
| 59 | references_size++; |
| 60 | } else if (Primitive::Is64BitType(return_type)) { |
| 61 | frame_size += 8; |
| 62 | } else { |
| 63 | frame_size += 4; |
| 64 | } |
| 65 | |
| 66 | (*frame_size_out) = frame_size; |
| 67 | (*references_size_out) = references_size; |
| 68 | } |
| 69 | |
| 70 | // Allows for read or write access to an emulated stack frame. Each |
| 71 | // accessor index has an associated index into the references / stack frame |
| 72 | // arrays which is incremented on every read or write to the frame. |
| 73 | // |
| 74 | // This class is used in conjunction with PerformConversions, either as a setter |
| 75 | // or as a getter. |
| 76 | class EmulatedStackFrameAccessor { |
| 77 | public: |
| 78 | EmulatedStackFrameAccessor(Handle<mirror::ObjectArray<mirror::Object>> references, |
| 79 | Handle<mirror::ByteArray> stack_frame, |
| 80 | size_t stack_frame_size) : |
| 81 | references_(references), |
| 82 | stack_frame_(stack_frame), |
| 83 | stack_frame_size_(stack_frame_size), |
| 84 | reference_idx_(0u), |
| 85 | stack_frame_idx_(0u) { |
| 86 | } |
| 87 | |
| 88 | ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> reference) |
| 89 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 90 | references_->Set(reference_idx_++, reference); |
| 91 | } |
| 92 | |
| 93 | ALWAYS_INLINE void Set(const uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 94 | int8_t* array = stack_frame_->GetData(); |
| 95 | |
| 96 | CHECK_LE((stack_frame_idx_ + 4u), stack_frame_size_); |
| 97 | memcpy(array + stack_frame_idx_, &value, sizeof(uint32_t)); |
| 98 | stack_frame_idx_ += 4u; |
| 99 | } |
| 100 | |
| 101 | ALWAYS_INLINE void SetLong(const int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 102 | int8_t* array = stack_frame_->GetData(); |
| 103 | |
| 104 | CHECK_LE((stack_frame_idx_ + 8u), stack_frame_size_); |
| 105 | memcpy(array + stack_frame_idx_, &value, sizeof(int64_t)); |
| 106 | stack_frame_idx_ += 8u; |
| 107 | } |
| 108 | |
| 109 | ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 110 | return references_->Get(reference_idx_++); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 114 | const int8_t* array = stack_frame_->GetData(); |
| 115 | |
| 116 | CHECK_LE((stack_frame_idx_ + 4u), stack_frame_size_); |
| 117 | uint32_t val = 0; |
| 118 | |
| 119 | memcpy(&val, array + stack_frame_idx_, sizeof(uint32_t)); |
| 120 | stack_frame_idx_ += 4u; |
| 121 | return val; |
| 122 | } |
| 123 | |
| 124 | ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 125 | const int8_t* array = stack_frame_->GetData(); |
| 126 | |
| 127 | CHECK_LE((stack_frame_idx_ + 8u), stack_frame_size_); |
| 128 | int64_t val = 0; |
| 129 | |
| 130 | memcpy(&val, array + stack_frame_idx_, sizeof(int64_t)); |
| 131 | stack_frame_idx_ += 8u; |
| 132 | return val; |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | Handle<mirror::ObjectArray<mirror::Object>> references_; |
| 137 | Handle<mirror::ByteArray> stack_frame_; |
| 138 | const size_t stack_frame_size_; |
| 139 | |
| 140 | size_t reference_idx_; |
| 141 | size_t stack_frame_idx_; |
| 142 | |
| 143 | DISALLOW_COPY_AND_ASSIGN(EmulatedStackFrameAccessor); |
| 144 | }; |
| 145 | |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 146 | ObjPtr<mirror::EmulatedStackFrame> EmulatedStackFrame::CreateFromShadowFrameAndArgs( |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 147 | Thread* self, |
| 148 | Handle<mirror::MethodType> caller_type, |
| 149 | Handle<mirror::MethodType> callee_type, |
| 150 | const ShadowFrame& caller_frame, |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 151 | const InstructionOperands* const operands) { |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 152 | StackHandleScope<6> hs(self); |
| 153 | |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 154 | DCHECK(callee_type->IsExactMatch(caller_type.Get())); |
| 155 | Handle<mirror::ObjectArray<mirror::Class>> p_types(hs.NewHandle(callee_type->GetPTypes())); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 156 | |
| 157 | // Step 2: Calculate the size of the reference / byte arrays in the emulated |
| 158 | // stack frame. |
| 159 | size_t frame_size = 0; |
| 160 | size_t refs_size = 0; |
| 161 | Handle<mirror::Class> r_type(hs.NewHandle(callee_type->GetRType())); |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 162 | CalculateFrameAndReferencesSize(p_types.Get(), r_type.Get(), &frame_size, &refs_size); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 163 | |
| 164 | // Step 3 : Allocate the arrays. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 165 | ObjPtr<mirror::Class> array_class(GetClassRoot<mirror::ObjectArray<mirror::Object>>()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 166 | |
| 167 | Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle( |
| 168 | mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 169 | if (references == nullptr) { |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 170 | DCHECK(self->IsExceptionPending()); |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 174 | Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 175 | if (stack_frame == nullptr) { |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 176 | DCHECK(self->IsExceptionPending()); |
| 177 | return nullptr; |
| 178 | } |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 179 | |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 180 | // Step 4 : Copy arguments. |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 181 | ShadowFrameGetter getter(caller_frame, operands); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 182 | EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength()); |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 183 | CopyArguments<ShadowFrameGetter, EmulatedStackFrameAccessor>(self, caller_type, &getter, &setter); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 184 | |
| 185 | // Step 5: Construct the EmulatedStackFrame object. |
| 186 | Handle<EmulatedStackFrame> sf(hs.NewHandle( |
Vladimir Marko | adbceb7 | 2018-05-29 14:34:14 +0100 | [diff] [blame] | 187 | ObjPtr<EmulatedStackFrame>::DownCast(GetClassRoot<EmulatedStackFrame>()->AllocObject(self)))); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 188 | sf->SetFieldObject<false>(TypeOffset(), callee_type.Get()); |
| 189 | sf->SetFieldObject<false>(ReferencesOffset(), references.Get()); |
| 190 | sf->SetFieldObject<false>(StackFrameOffset(), stack_frame.Get()); |
| 191 | |
| 192 | return sf.Get(); |
| 193 | } |
| 194 | |
Orion Hodson | 2362c5a | 2022-01-29 09:48:52 +0000 | [diff] [blame] | 195 | void EmulatedStackFrame::WriteToShadowFrame(Thread* self, |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 196 | Handle<mirror::MethodType> callee_type, |
| 197 | const uint32_t first_dest_reg, |
| 198 | ShadowFrame* callee_frame) { |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 199 | DCHECK(callee_type->IsExactMatch(GetType())); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 200 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 201 | StackHandleScope<3> hs(self); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 202 | Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences())); |
| 203 | Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame())); |
| 204 | |
| 205 | EmulatedStackFrameAccessor getter(references, stack_frame, stack_frame->GetLength()); |
| 206 | ShadowFrameSetter setter(callee_frame, first_dest_reg); |
| 207 | |
Orion Hodson | c6270df | 2022-01-28 18:11:03 +0000 | [diff] [blame] | 208 | CopyArguments<EmulatedStackFrameAccessor, ShadowFrameSetter>(self, callee_type, &getter, &setter); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void EmulatedStackFrame::GetReturnValue(Thread* self, JValue* value) { |
| 212 | StackHandleScope<2> hs(self); |
| 213 | Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType())); |
| 214 | |
| 215 | const Primitive::Type type = r_type->GetPrimitiveType(); |
| 216 | if (type == Primitive::kPrimNot) { |
| 217 | Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences())); |
| 218 | value->SetL(references->GetWithoutChecks(references->GetLength() - 1)); |
| 219 | } else { |
| 220 | Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame())); |
| 221 | const int8_t* array = stack_frame->GetData(); |
| 222 | const size_t length = stack_frame->GetLength(); |
| 223 | if (Primitive::Is64BitType(type)) { |
| 224 | int64_t primitive = 0; |
| 225 | memcpy(&primitive, array + length - sizeof(int64_t), sizeof(int64_t)); |
| 226 | value->SetJ(primitive); |
| 227 | } else { |
| 228 | uint32_t primitive = 0; |
| 229 | memcpy(&primitive, array + length - sizeof(uint32_t), sizeof(uint32_t)); |
| 230 | value->SetI(primitive); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void EmulatedStackFrame::SetReturnValue(Thread* self, const JValue& value) { |
| 236 | StackHandleScope<2> hs(self); |
| 237 | Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType())); |
| 238 | |
| 239 | const Primitive::Type type = r_type->GetPrimitiveType(); |
| 240 | if (type == Primitive::kPrimNot) { |
| 241 | Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences())); |
| 242 | references->SetWithoutChecks<false>(references->GetLength() - 1, value.GetL()); |
| 243 | } else { |
| 244 | Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame())); |
| 245 | int8_t* array = stack_frame->GetData(); |
| 246 | const size_t length = stack_frame->GetLength(); |
| 247 | if (Primitive::Is64BitType(type)) { |
| 248 | const int64_t primitive = value.GetJ(); |
| 249 | memcpy(array + length - sizeof(int64_t), &primitive, sizeof(int64_t)); |
| 250 | } else { |
| 251 | const uint32_t primitive = value.GetI(); |
| 252 | memcpy(array + length - sizeof(uint32_t), &primitive, sizeof(uint32_t)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 257 | } // namespace mirror |
| 258 | } // namespace art |