• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   DCHECK(callee_type->IsExactMatch(caller_type.Get()));
155   Handle<mirror::ObjectArray<mirror::Class>> p_types(hs.NewHandle(callee_type->GetPTypes()));
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()));
162   CalculateFrameAndReferencesSize(p_types.Get(), r_type.Get(), &frame_size, &refs_size);
163 
164   // Step 3 : Allocate the arrays.
165   ObjPtr<mirror::Class> array_class(GetClassRoot<mirror::ObjectArray<mirror::Object>>());
166 
167   Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(
168       mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size)));
169   if (references == nullptr) {
170     DCHECK(self->IsExceptionPending());
171     return nullptr;
172   }
173 
174   Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size)));
175   if (stack_frame == nullptr) {
176     DCHECK(self->IsExceptionPending());
177     return nullptr;
178   }
179 
180   // Step 4 : Copy arguments.
181   ShadowFrameGetter getter(caller_frame, operands);
182   EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength());
183   CopyArguments<ShadowFrameGetter, EmulatedStackFrameAccessor>(self, caller_type, &getter, &setter);
184 
185   // Step 5: Construct the EmulatedStackFrame object.
186   Handle<EmulatedStackFrame> sf(hs.NewHandle(
187       ObjPtr<EmulatedStackFrame>::DownCast(GetClassRoot<EmulatedStackFrame>()->AllocObject(self))));
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 
WriteToShadowFrame(Thread * self,Handle<mirror::MethodType> callee_type,const uint32_t first_dest_reg,ShadowFrame * callee_frame)195 void EmulatedStackFrame::WriteToShadowFrame(Thread* self,
196                                             Handle<mirror::MethodType> callee_type,
197                                             const uint32_t first_dest_reg,
198                                             ShadowFrame* callee_frame) {
199   DCHECK(callee_type->IsExactMatch(GetType()));
200 
201   StackHandleScope<3> hs(self);
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 
208   CopyArguments<EmulatedStackFrameAccessor, ShadowFrameSetter>(self, callee_type, &getter, &setter);
209 }
210 
GetReturnValue(Thread * self,JValue * value)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 
SetReturnValue(Thread * self,const JValue & value)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 
257 }  // namespace mirror
258 }  // namespace art
259