blob: a4fde7237b409f6bafd2b1458098a888347cbaec [file] [log] [blame]
Narayan Kamath000e1882016-10-24 17:14:25 +01001/*
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 Marko423bebb2019-03-26 15:17:21 +000017#include "emulated_stack_frame-inl.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010018
Andreas Gampe8e0f0432018-10-24 13:38:03 -070019#include "array-alloc-inl.h"
20#include "array-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070021#include "class-alloc-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010022#include "class_root-inl.h"
Vladimir Marko423bebb2019-03-26 15:17:21 +000023#include "handle.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010024#include "jvalue-inl.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010025#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "method_handles.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000027#include "method_type-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070028#include "object_array-alloc-inl.h"
29#include "object_array-inl.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010030#include "reflection-inl.h"
31
Dmitrii Ishcheikinddfb9fa2024-01-16 18:09:15 +000032namespace art HIDDEN {
Narayan Kamath000e1882016-10-24 17:14:25 +010033namespace mirror {
34
Narayan Kamath000e1882016-10-24 17:14:25 +010035// Calculates the size of a stack frame based on the size of its argument
36// types and return types.
37static 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.
76class 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 Markod7e9bbf2019-03-28 13:18:57 +0000110 return references_->Get(reference_idx_++);
Narayan Kamath000e1882016-10-24 17:14:25 +0100111 }
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 Marko423bebb2019-03-26 15:17:21 +0000146ObjPtr<mirror::EmulatedStackFrame> EmulatedStackFrame::CreateFromShadowFrameAndArgs(
Narayan Kamath000e1882016-10-24 17:14:25 +0100147 Thread* self,
148 Handle<mirror::MethodType> caller_type,
149 Handle<mirror::MethodType> callee_type,
150 const ShadowFrame& caller_frame,
Orion Hodson960d4f72017-11-10 15:32:38 +0000151 const InstructionOperands* const operands) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100152 StackHandleScope<6> hs(self);
153
Orion Hodsonc6270df2022-01-28 18:11:03 +0000154 DCHECK(callee_type->IsExactMatch(caller_type.Get()));
155 Handle<mirror::ObjectArray<mirror::Class>> p_types(hs.NewHandle(callee_type->GetPTypes()));
Narayan Kamath000e1882016-10-24 17:14:25 +0100156
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 Hodsonc6270df2022-01-28 18:11:03 +0000162 CalculateFrameAndReferencesSize(p_types.Get(), r_type.Get(), &frame_size, &refs_size);
Narayan Kamath000e1882016-10-24 17:14:25 +0100163
164 // Step 3 : Allocate the arrays.
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100165 ObjPtr<mirror::Class> array_class(GetClassRoot<mirror::ObjectArray<mirror::Object>>());
Narayan Kamath000e1882016-10-24 17:14:25 +0100166
167 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(
168 mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800169 if (references == nullptr) {
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000170 DCHECK(self->IsExceptionPending());
171 return nullptr;
172 }
173
Narayan Kamath000e1882016-10-24 17:14:25 +0100174 Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800175 if (stack_frame == nullptr) {
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000176 DCHECK(self->IsExceptionPending());
177 return nullptr;
178 }
Narayan Kamath000e1882016-10-24 17:14:25 +0100179
Orion Hodsonc6270df2022-01-28 18:11:03 +0000180 // Step 4 : Copy arguments.
Orion Hodson928033d2018-02-07 05:30:54 +0000181 ShadowFrameGetter getter(caller_frame, operands);
Narayan Kamath000e1882016-10-24 17:14:25 +0100182 EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength());
Orion Hodson2362c5a2022-01-29 09:48:52 +0000183 CopyArguments<ShadowFrameGetter, EmulatedStackFrameAccessor>(self, caller_type, &getter, &setter);
Narayan Kamath000e1882016-10-24 17:14:25 +0100184
185 // Step 5: Construct the EmulatedStackFrame object.
186 Handle<EmulatedStackFrame> sf(hs.NewHandle(
Vladimir Markoadbceb72018-05-29 14:34:14 +0100187 ObjPtr<EmulatedStackFrame>::DownCast(GetClassRoot<EmulatedStackFrame>()->AllocObject(self))));
Narayan Kamath000e1882016-10-24 17:14:25 +0100188 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 Hodson2362c5a2022-01-29 09:48:52 +0000195void EmulatedStackFrame::WriteToShadowFrame(Thread* self,
Narayan Kamath000e1882016-10-24 17:14:25 +0100196 Handle<mirror::MethodType> callee_type,
197 const uint32_t first_dest_reg,
198 ShadowFrame* callee_frame) {
Orion Hodsonc6270df2022-01-28 18:11:03 +0000199 DCHECK(callee_type->IsExactMatch(GetType()));
Narayan Kamath000e1882016-10-24 17:14:25 +0100200
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100201 StackHandleScope<3> hs(self);
Narayan Kamath000e1882016-10-24 17:14:25 +0100202 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 Hodsonc6270df2022-01-28 18:11:03 +0000208 CopyArguments<EmulatedStackFrameAccessor, ShadowFrameSetter>(self, callee_type, &getter, &setter);
Narayan Kamath000e1882016-10-24 17:14:25 +0100209}
210
211void 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
235void 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 Kamath000e1882016-10-24 17:14:25 +0100257} // namespace mirror
258} // namespace art