Home | History | Annotate | Download | only in mirror
      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.h"
     18 
     19 #include "class-inl.h"
     20 #include "gc_root-inl.h"
     21 #include "jvalue-inl.h"
     22 #include "method_handles.h"
     23 #include "method_handles-inl.h"
     24 #include "reflection-inl.h"
     25 
     26 namespace art {
     27 namespace mirror {
     28 
     29 GcRoot<mirror::Class> EmulatedStackFrame::static_class_;
     30 
     31 // Calculates the size of a stack frame based on the size of its argument
     32 // types and return types.
     33 static void CalculateFrameAndReferencesSize(ObjPtr<mirror::ObjectArray<mirror::Class>> p_types,
     34                                             ObjPtr<mirror::Class> r_type,
     35                                             size_t* frame_size_out,
     36                                             size_t* references_size_out)
     37     REQUIRES_SHARED(Locks::mutator_lock_) {
     38   const size_t length = p_types->GetLength();
     39   size_t frame_size = 0;
     40   size_t references_size = 0;
     41   for (size_t i = 0; i < length; ++i) {
     42     ObjPtr<mirror::Class> type = p_types->GetWithoutChecks(i);
     43     const Primitive::Type primitive_type = type->GetPrimitiveType();
     44     if (primitive_type == Primitive::kPrimNot) {
     45       references_size++;
     46     } else if (Primitive::Is64BitType(primitive_type)) {
     47       frame_size += 8;
     48     } else {
     49       frame_size += 4;
     50     }
     51   }
     52 
     53   const Primitive::Type return_type = r_type->GetPrimitiveType();
     54   if (return_type == Primitive::kPrimNot) {
     55     references_size++;
     56   } else if (Primitive::Is64BitType(return_type)) {
     57     frame_size += 8;
     58   } else {
     59     frame_size += 4;
     60   }
     61 
     62   (*frame_size_out) = frame_size;
     63   (*references_size_out) = references_size;
     64 }
     65 
     66 // Allows for read or write access to an emulated stack frame. Each
     67 // accessor index has an associated index into the references / stack frame
     68 // arrays which is incremented on every read or write to the frame.
     69 //
     70 // This class is used in conjunction with PerformConversions, either as a setter
     71 // or as a getter.
     72 class EmulatedStackFrameAccessor {
     73  public:
     74   EmulatedStackFrameAccessor(Handle<mirror::ObjectArray<mirror::Object>> references,
     75                              Handle<mirror::ByteArray> stack_frame,
     76                              size_t stack_frame_size) :
     77     references_(references),
     78     stack_frame_(stack_frame),
     79     stack_frame_size_(stack_frame_size),
     80     reference_idx_(0u),
     81     stack_frame_idx_(0u) {
     82   }
     83 
     84   ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> reference)
     85       REQUIRES_SHARED(Locks::mutator_lock_) {
     86     references_->Set(reference_idx_++, reference);
     87   }
     88 
     89   ALWAYS_INLINE void Set(const uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
     90     int8_t* array = stack_frame_->GetData();
     91 
     92     CHECK_LE((stack_frame_idx_ + 4u), stack_frame_size_);
     93     memcpy(array + stack_frame_idx_, &value, sizeof(uint32_t));
     94     stack_frame_idx_ += 4u;
     95   }
     96 
     97   ALWAYS_INLINE void SetLong(const int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
     98     int8_t* array = stack_frame_->GetData();
     99 
    100     CHECK_LE((stack_frame_idx_ + 8u), stack_frame_size_);
    101     memcpy(array + stack_frame_idx_, &value, sizeof(int64_t));
    102     stack_frame_idx_ += 8u;
    103   }
    104 
    105   ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
    106     return ObjPtr<mirror::Object>(references_->Get(reference_idx_++));
    107   }
    108 
    109   ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
    110     const int8_t* array = stack_frame_->GetData();
    111 
    112     CHECK_LE((stack_frame_idx_ + 4u), stack_frame_size_);
    113     uint32_t val = 0;
    114 
    115     memcpy(&val, array + stack_frame_idx_, sizeof(uint32_t));
    116     stack_frame_idx_ += 4u;
    117     return val;
    118   }
    119 
    120   ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
    121     const int8_t* array = stack_frame_->GetData();
    122 
    123     CHECK_LE((stack_frame_idx_ + 8u), stack_frame_size_);
    124     int64_t val = 0;
    125 
    126     memcpy(&val, array + stack_frame_idx_, sizeof(int64_t));
    127     stack_frame_idx_ += 8u;
    128     return val;
    129   }
    130 
    131  private:
    132   Handle<mirror::ObjectArray<mirror::Object>> references_;
    133   Handle<mirror::ByteArray> stack_frame_;
    134   const size_t stack_frame_size_;
    135 
    136   size_t reference_idx_;
    137   size_t stack_frame_idx_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(EmulatedStackFrameAccessor);
    140 };
    141 
    142 template <bool is_range>
    143 mirror::EmulatedStackFrame* EmulatedStackFrame::CreateFromShadowFrameAndArgs(
    144     Thread* self,
    145     Handle<mirror::MethodType> caller_type,
    146     Handle<mirror::MethodType> callee_type,
    147     const ShadowFrame& caller_frame,
    148     const uint32_t first_src_reg,
    149     const uint32_t (&arg)[Instruction::kMaxVarArgRegs]) {
    150   StackHandleScope<6> hs(self);
    151 
    152   // Step 1: We must throw a WrongMethodTypeException if there's a mismatch in the
    153   // number of arguments between the caller and the callsite.
    154   Handle<mirror::ObjectArray<mirror::Class>> from_types(hs.NewHandle(caller_type->GetPTypes()));
    155   Handle<mirror::ObjectArray<mirror::Class>> to_types(hs.NewHandle(callee_type->GetPTypes()));
    156 
    157   const int32_t num_method_params = from_types->GetLength();
    158   if (to_types->GetLength() != num_method_params) {
    159     ThrowWrongMethodTypeException(callee_type.Get(), caller_type.Get());
    160     return nullptr;
    161   }
    162 
    163   // Step 2: Calculate the size of the reference / byte arrays in the emulated
    164   // stack frame.
    165   size_t frame_size = 0;
    166   size_t refs_size = 0;
    167   Handle<mirror::Class> r_type(hs.NewHandle(callee_type->GetRType()));
    168   CalculateFrameAndReferencesSize(to_types.Get(), r_type.Get(), &frame_size, &refs_size);
    169 
    170   // Step 3 : Allocate the arrays.
    171   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
    172   ObjPtr<mirror::Class> array_class(class_linker->GetClassRoot(ClassLinker::kObjectArrayClass));
    173 
    174   Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(
    175       mirror::ObjectArray<mirror::Object>::Alloc(self, array_class, refs_size)));
    176   if (references == nullptr) {
    177     DCHECK(self->IsExceptionPending());
    178     return nullptr;
    179   }
    180 
    181   Handle<ByteArray> stack_frame(hs.NewHandle(ByteArray::Alloc(self, frame_size)));
    182   if (stack_frame == nullptr) {
    183     DCHECK(self->IsExceptionPending());
    184     return nullptr;
    185   }
    186 
    187   // Step 4 : Perform argument conversions (if required).
    188   ShadowFrameGetter<is_range> getter(first_src_reg, arg, caller_frame);
    189   EmulatedStackFrameAccessor setter(references, stack_frame, stack_frame->GetLength());
    190   if (!PerformConversions<ShadowFrameGetter<is_range>, EmulatedStackFrameAccessor>(
    191           self, caller_type, callee_type, &getter, &setter, num_method_params)) {
    192     return nullptr;
    193   }
    194 
    195   // Step 5: Construct the EmulatedStackFrame object.
    196   Handle<EmulatedStackFrame> sf(hs.NewHandle(
    197       ObjPtr<EmulatedStackFrame>::DownCast(StaticClass()->AllocObject(self))));
    198   sf->SetFieldObject<false>(CallsiteTypeOffset(), caller_type.Get());
    199   sf->SetFieldObject<false>(TypeOffset(), callee_type.Get());
    200   sf->SetFieldObject<false>(ReferencesOffset(), references.Get());
    201   sf->SetFieldObject<false>(StackFrameOffset(), stack_frame.Get());
    202 
    203   return sf.Get();
    204 }
    205 
    206 bool EmulatedStackFrame::WriteToShadowFrame(Thread* self,
    207                                             Handle<mirror::MethodType> callee_type,
    208                                             const uint32_t first_dest_reg,
    209                                             ShadowFrame* callee_frame) {
    210   ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(GetType()->GetPTypes());
    211   ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes());
    212 
    213   const int32_t num_method_params = from_types->GetLength();
    214   if (to_types->GetLength() != num_method_params) {
    215     ThrowWrongMethodTypeException(callee_type.Get(), GetType());
    216     return false;
    217   }
    218 
    219   StackHandleScope<3> hs(self);
    220   Handle<mirror::MethodType> frame_callsite_type(hs.NewHandle(GetType()));
    221   Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
    222   Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
    223 
    224   EmulatedStackFrameAccessor getter(references, stack_frame, stack_frame->GetLength());
    225   ShadowFrameSetter setter(callee_frame, first_dest_reg);
    226 
    227   return PerformConversions<EmulatedStackFrameAccessor, ShadowFrameSetter>(
    228       self, frame_callsite_type, callee_type, &getter, &setter, num_method_params);
    229 }
    230 
    231 void EmulatedStackFrame::GetReturnValue(Thread* self, JValue* value) {
    232   StackHandleScope<2> hs(self);
    233   Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType()));
    234 
    235   const Primitive::Type type = r_type->GetPrimitiveType();
    236   if (type == Primitive::kPrimNot) {
    237     Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
    238     value->SetL(references->GetWithoutChecks(references->GetLength() - 1));
    239   } else {
    240     Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
    241     const int8_t* array = stack_frame->GetData();
    242     const size_t length = stack_frame->GetLength();
    243     if (Primitive::Is64BitType(type)) {
    244       int64_t primitive = 0;
    245       memcpy(&primitive, array + length - sizeof(int64_t), sizeof(int64_t));
    246       value->SetJ(primitive);
    247     } else {
    248       uint32_t primitive = 0;
    249       memcpy(&primitive, array + length - sizeof(uint32_t), sizeof(uint32_t));
    250       value->SetI(primitive);
    251     }
    252   }
    253 }
    254 
    255 void EmulatedStackFrame::SetReturnValue(Thread* self, const JValue& value) {
    256   StackHandleScope<2> hs(self);
    257   Handle<mirror::Class> r_type(hs.NewHandle(GetType()->GetRType()));
    258 
    259   const Primitive::Type type = r_type->GetPrimitiveType();
    260   if (type == Primitive::kPrimNot) {
    261     Handle<mirror::ObjectArray<mirror::Object>> references(hs.NewHandle(GetReferences()));
    262     references->SetWithoutChecks<false>(references->GetLength() - 1, value.GetL());
    263   } else {
    264     Handle<ByteArray> stack_frame(hs.NewHandle(GetStackFrame()));
    265     int8_t* array = stack_frame->GetData();
    266     const size_t length = stack_frame->GetLength();
    267     if (Primitive::Is64BitType(type)) {
    268       const int64_t primitive = value.GetJ();
    269       memcpy(array + length - sizeof(int64_t), &primitive, sizeof(int64_t));
    270     } else {
    271       const uint32_t primitive = value.GetI();
    272       memcpy(array + length - sizeof(uint32_t), &primitive, sizeof(uint32_t));
    273     }
    274   }
    275 }
    276 
    277 void EmulatedStackFrame::SetClass(Class* klass) {
    278   CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
    279   CHECK(klass != nullptr);
    280   static_class_ = GcRoot<Class>(klass);
    281 }
    282 
    283 void EmulatedStackFrame::ResetClass() {
    284   CHECK(!static_class_.IsNull());
    285   static_class_ = GcRoot<Class>(nullptr);
    286 }
    287 
    288 void EmulatedStackFrame::VisitRoots(RootVisitor* visitor) {
    289   static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
    290 }
    291 
    292 // Explicit DoInvokePolymorphic template function declarations.
    293 #define EXPLICIT_CREATE_FROM_SHADOW_FRAME_AND_ARGS_DECL(_is_range)                         \
    294   template REQUIRES_SHARED(Locks::mutator_lock_)                                           \
    295   mirror::EmulatedStackFrame* EmulatedStackFrame::CreateFromShadowFrameAndArgs<_is_range>( \
    296     Thread* self,                                                                          \
    297     Handle<mirror::MethodType> caller_type,                                                \
    298     Handle<mirror::MethodType> callee_type,                                                \
    299     const ShadowFrame& caller_frame,                                                       \
    300     const uint32_t first_src_reg,                                                          \
    301     const uint32_t (&arg)[Instruction::kMaxVarArgRegs])                                    \
    302 
    303 EXPLICIT_CREATE_FROM_SHADOW_FRAME_AND_ARGS_DECL(true);
    304 EXPLICIT_CREATE_FROM_SHADOW_FRAME_AND_ARGS_DECL(false);
    305 #undef EXPLICIT_CREATE_FROM_SHADOW_FRAME_AND_ARGS_DECL
    306 
    307 
    308 }  // namespace mirror
    309 }  // namespace art
    310