blob: cfdab8fee7e30533a81edb4141c851cd4c60c188 [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 Markob4eb1b12018-05-24 11:09:38 +010022#include "class_root.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
32namespace art {
33namespace 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
154 // Step 1: We must throw a WrongMethodTypeException if there's a mismatch in the
155 // number of arguments between the caller and the callsite.
156 Handle<mirror::ObjectArray<mirror::Class>> from_types(hs.NewHandle(caller_type->GetPTypes()));
157 Handle<mirror::ObjectArray<mirror::Class>> to_types(hs.NewHandle(callee_type->GetPTypes()));
158
159 const int32_t num_method_params = from_types->GetLength();
160 if (to_types->GetLength() != num_method_params) {
161 ThrowWrongMethodTypeException(callee_type.Get(), caller_type.Get());
162 return nullptr;
163 }
164
165 // Step 2: Calculate the size of the reference / byte arrays in the emulated
166 // stack frame.
167 size_t frame_size = 0;
168 size_t refs_size = 0;
169 Handle<mirror::Class> r_type(hs.NewHandle(callee_type->GetRType()));
170 CalculateFrameAndReferencesSize(to_types.Get(), r_type.Get(), &frame_size, &refs_size);
171
172 // Step 3 : Allocate the arrays.
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100173 ObjPtr<mirror::Class> array_class(GetClassRoot<mirror::ObjectArray<mirror::Object>>());
Narayan Kamath000e1882016-10-24 17:14:25 +0100174
175 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(
176 mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800177 if (references == nullptr) {
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000178 DCHECK(self->IsExceptionPending());
179 return nullptr;
180 }
181
Narayan Kamath000e1882016-10-24 17:14:25 +0100182 Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800183 if (stack_frame == nullptr) {
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000184 DCHECK(self->IsExceptionPending());
185 return nullptr;
186 }
Narayan Kamath000e1882016-10-24 17:14:25 +0100187
188 // Step 4 : Perform argument conversions (if required).
Orion Hodson928033d2018-02-07 05:30:54 +0000189 ShadowFrameGetter getter(caller_frame, operands);
Narayan Kamath000e1882016-10-24 17:14:25 +0100190 EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength());
Orion Hodson960d4f72017-11-10 15:32:38 +0000191 if (!PerformConversions<ShadowFrameGetter, EmulatedStackFrameAccessor>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100192 self, caller_type, callee_type, &getter, &setter, num_method_params)) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100193 return nullptr;
194 }
195
196 // Step 5: Construct the EmulatedStackFrame object.
197 Handle<EmulatedStackFrame> sf(hs.NewHandle(
Vladimir Markoadbceb72018-05-29 14:34:14 +0100198 ObjPtr<EmulatedStackFrame>::DownCast(GetClassRoot<EmulatedStackFrame>()->AllocObject(self))));
Narayan Kamathb79bbd82017-01-16 17:48:28 +0000199 sf->SetFieldObject<false>(CallsiteTypeOffset(), caller_type.Get());
Narayan Kamath000e1882016-10-24 17:14:25 +0100200 sf->SetFieldObject<false>(TypeOffset(), callee_type.Get());
201 sf->SetFieldObject<false>(ReferencesOffset(), references.Get());
202 sf->SetFieldObject<false>(StackFrameOffset(), stack_frame.Get());
203
204 return sf.Get();
205}
206
207bool EmulatedStackFrame::WriteToShadowFrame(Thread* self,
208 Handle<mirror::MethodType> callee_type,
209 const uint32_t first_dest_reg,
210 ShadowFrame* callee_frame) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100211 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(GetType()->GetPTypes());
212 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
Narayan Kamath000e1882016-10-24 17:14:25 +0100213
214 const int32_t num_method_params = from_types->GetLength();
215 if (to_types->GetLength() != num_method_params) {
216 ThrowWrongMethodTypeException(callee_type.Get(), GetType());
217 return false;
218 }
219
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100220 StackHandleScope<3> hs(self);
221 Handle<mirror::MethodType> frame_callsite_type(hs.NewHandle(GetType()));
Narayan Kamath000e1882016-10-24 17:14:25 +0100222 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
223 Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
224
225 EmulatedStackFrameAccessor getter(references, stack_frame, stack_frame->GetLength());
226 ShadowFrameSetter setter(callee_frame, first_dest_reg);
227
228 return PerformConversions<EmulatedStackFrameAccessor, ShadowFrameSetter>(
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100229 self, frame_callsite_type, callee_type, &getter, &setter, num_method_params);
Narayan Kamath000e1882016-10-24 17:14:25 +0100230}
231
232void EmulatedStackFrame::GetReturnValue(Thread* self, JValue* value) {
233 StackHandleScope<2> hs(self);
234 Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType()));
235
236 const Primitive::Type type = r_type->GetPrimitiveType();
237 if (type == Primitive::kPrimNot) {
238 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
239 value->SetL(references->GetWithoutChecks(references->GetLength() - 1));
240 } else {
241 Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
242 const int8_t* array = stack_frame->GetData();
243 const size_t length = stack_frame->GetLength();
244 if (Primitive::Is64BitType(type)) {
245 int64_t primitive = 0;
246 memcpy(&primitive, array + length - sizeof(int64_t), sizeof(int64_t));
247 value->SetJ(primitive);
248 } else {
249 uint32_t primitive = 0;
250 memcpy(&primitive, array + length - sizeof(uint32_t), sizeof(uint32_t));
251 value->SetI(primitive);
252 }
253 }
254}
255
256void EmulatedStackFrame::SetReturnValue(Thread* self, const JValue& value) {
257 StackHandleScope<2> hs(self);
258 Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType()));
259
260 const Primitive::Type type = r_type->GetPrimitiveType();
261 if (type == Primitive::kPrimNot) {
262 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
263 references->SetWithoutChecks<false>(references->GetLength() - 1, value.GetL());
264 } else {
265 Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
266 int8_t* array = stack_frame->GetData();
267 const size_t length = stack_frame->GetLength();
268 if (Primitive::Is64BitType(type)) {
269 const int64_t primitive = value.GetJ();
270 memcpy(array + length - sizeof(int64_t), &primitive, sizeof(int64_t));
271 } else {
272 const uint32_t primitive = value.GetI();
273 memcpy(array + length - sizeof(uint32_t), &primitive, sizeof(uint32_t));
274 }
275 }
276}
277
Narayan Kamath000e1882016-10-24 17:14:25 +0100278} // namespace mirror
279} // namespace art