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
17 #include "emulated_stack_frame-inl.h"
18
19 #include "array-alloc-inl.h"
20 #include "array-inl.h"
21 #include "class-alloc-inl.h"
22 #include "class_root-inl.h"
23 #include "handle.h"
24 #include "jvalue-inl.h"
25 #include "method_handles-inl.h"
26 #include "method_handles.h"
27 #include "method_type-inl.h"
28 #include "object_array-alloc-inl.h"
29 #include "object_array-inl.h"
30 #include "reflection-inl.h"
31
32 namespace art {
33 namespace mirror {
34
35 // Calculates the size of a stack frame based on the size of its argument
36 // types and return types.
CalculateFrameAndReferencesSize(ObjPtr<mirror::ObjectArray<mirror::Class>> p_types,ObjPtr<mirror::Class> r_type,size_t * frame_size_out,size_t * references_size_out)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:
EmulatedStackFrameAccessor(Handle<mirror::ObjectArray<mirror::Object>> references,Handle<mirror::ByteArray> stack_frame,size_t stack_frame_size)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
SetReference(ObjPtr<mirror::Object> reference)88 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> reference)
89 REQUIRES_SHARED(Locks::mutator_lock_) {
90 references_->Set(reference_idx_++, reference);
91 }
92
Set(const uint32_t value)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
SetLong(const int64_t value)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
GetReference()109 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
110 return references_->Get(reference_idx_++);
111 }
112
Get()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
GetLong()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
CreateFromShadowFrameAndArgs(Thread * self,Handle<mirror::MethodType> caller_type,Handle<mirror::MethodType> callee_type,const ShadowFrame & caller_frame,const InstructionOperands * const operands)146 ObjPtr<mirror::EmulatedStackFrame> EmulatedStackFrame::CreateFromShadowFrameAndArgs(
147 Thread* self,
148 Handle<mirror::MethodType> caller_type,
149 Handle<mirror::MethodType> callee_type,
150 const ShadowFrame& caller_frame,
151 const InstructionOperands* const operands) {
152 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.
173 ObjPtr<mirror::Class> array_class(GetClassRoot<mirror::ObjectArray<mirror::Object>>());
174
175 Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(
176 mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size)));
177 if (references == nullptr) {
178 DCHECK(self->IsExceptionPending());
179 return nullptr;
180 }
181
182 Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size)));
183 if (stack_frame == nullptr) {
184 DCHECK(self->IsExceptionPending());
185 return nullptr;
186 }
187
188 // Step 4 : Perform argument conversions (if required).
189 ShadowFrameGetter getter(caller_frame, operands);
190 EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength());
191 if (!PerformConversions<ShadowFrameGetter, EmulatedStackFrameAccessor>(
192 self, caller_type, callee_type, &getter, &setter, num_method_params)) {
193 return nullptr;
194 }
195
196 // Step 5: Construct the EmulatedStackFrame object.
197 Handle<EmulatedStackFrame> sf(hs.NewHandle(
198 ObjPtr<EmulatedStackFrame>::DownCast(GetClassRoot<EmulatedStackFrame>()->AllocObject(self))));
199 sf->SetFieldObject<false>(CallsiteTypeOffset(), caller_type.Get());
200 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
WriteToShadowFrame(Thread * self,Handle<mirror::MethodType> callee_type,const uint32_t first_dest_reg,ShadowFrame * callee_frame)207 bool EmulatedStackFrame::WriteToShadowFrame(Thread* self,
208 Handle<mirror::MethodType> callee_type,
209 const uint32_t first_dest_reg,
210 ShadowFrame* callee_frame) {
211 ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(GetType()->GetPTypes());
212 ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
213
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
220 StackHandleScope<3> hs(self);
221 Handle<mirror::MethodType> frame_callsite_type(hs.NewHandle(GetType()));
222 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>(
229 self, frame_callsite_type, callee_type, &getter, &setter, num_method_params);
230 }
231
GetReturnValue(Thread * self,JValue * value)232 void 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
SetReturnValue(Thread * self,const JValue & value)256 void 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
278 } // namespace mirror
279 } // namespace art
280