Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 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 | #ifndef ART_RUNTIME_METHOD_HANDLES_INL_H_ |
| 18 | #define ART_RUNTIME_METHOD_HANDLES_INL_H_ |
| 19 | |
| 20 | #include "method_handles.h" |
| 21 | |
| 22 | #include "common_throws.h" |
| 23 | #include "dex_instruction.h" |
| 24 | #include "interpreter/interpreter_common.h" |
| 25 | #include "jvalue.h" |
| 26 | #include "mirror/class.h" |
| 27 | #include "mirror/method_type.h" |
| 28 | #include "mirror/object.h" |
| 29 | #include "reflection.h" |
| 30 | #include "stack.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 34 | inline bool ConvertArgumentValue(Handle<mirror::MethodType> callsite_type, |
| 35 | Handle<mirror::MethodType> callee_type, |
| 36 | int index, |
| 37 | JValue* value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 38 | ObjPtr<mirror::Class> from_class(callsite_type->GetPTypes()->GetWithoutChecks(index)); |
| 39 | ObjPtr<mirror::Class> to_class(callee_type->GetPTypes()->GetWithoutChecks(index)); |
| 40 | if (from_class == to_class) { |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 41 | return true; |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // |value| may contain a bare heap pointer which is generally |
| 45 | // |unsafe. ConvertJValueCommon() saves |value|, |from_class|, and |
| 46 | // |to_class| to Handles where necessary to avoid issues if the heap |
| 47 | // changes. |
| 48 | if (ConvertJValueCommon(callsite_type, callee_type, from_class, to_class, value)) { |
| 49 | DCHECK(!Thread::Current()->IsExceptionPending()); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 50 | return true; |
| 51 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 52 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 53 | value->SetJ(0); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 58 | inline bool ConvertReturnValue(Handle<mirror::MethodType> callsite_type, |
| 59 | Handle<mirror::MethodType> callee_type, |
| 60 | JValue* value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 61 | ObjPtr<mirror::Class> from_class(callee_type->GetRType()); |
| 62 | ObjPtr<mirror::Class> to_class(callsite_type->GetRType()); |
| 63 | if (to_class->GetPrimitiveType() == Primitive::kPrimVoid || from_class == to_class) { |
| 64 | return true; |
Narayan Kamath | da24650 | 2016-10-20 18:39:22 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 67 | // |value| may contain a bare heap pointer which is generally |
| 68 | // unsafe. ConvertJValueCommon() saves |value|, |from_class|, and |
| 69 | // |to_class| to Handles where necessary to avoid issues if the heap |
| 70 | // changes. |
| 71 | if (ConvertJValueCommon(callsite_type, callee_type, from_class, to_class, value)) { |
| 72 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 73 | return true; |
| 74 | } else { |
| 75 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 76 | value->SetJ(0); |
| 77 | return false; |
| 78 | } |
Narayan Kamath | da24650 | 2016-10-20 18:39:22 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 81 | template <typename G, typename S> |
| 82 | bool PerformConversions(Thread* self, |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 83 | Handle<mirror::MethodType> callsite_type, |
| 84 | Handle<mirror::MethodType> callee_type, |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 85 | G* getter, |
| 86 | S* setter, |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 87 | int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_) { |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 88 | StackHandleScope<2> hs(self); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 89 | Handle<mirror::ObjectArray<mirror::Class>> from_types(hs.NewHandle(callsite_type->GetPTypes())); |
| 90 | Handle<mirror::ObjectArray<mirror::Class>> to_types(hs.NewHandle(callee_type->GetPTypes())); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 91 | |
| 92 | for (int32_t i = 0; i < num_conversions; ++i) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 93 | ObjPtr<mirror::Class> from(from_types->GetWithoutChecks(i)); |
| 94 | ObjPtr<mirror::Class> to(to_types->GetWithoutChecks(i)); |
| 95 | const Primitive::Type from_type = from_types->GetWithoutChecks(i)->GetPrimitiveType(); |
| 96 | const Primitive::Type to_type = to_types->GetWithoutChecks(i)->GetPrimitiveType(); |
| 97 | if (from == to) { |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 98 | // Easy case - the types are identical. Nothing left to do except to pass |
| 99 | // the arguments along verbatim. |
| 100 | if (Primitive::Is64BitType(from_type)) { |
| 101 | setter->SetLong(getter->GetLong()); |
| 102 | } else if (from_type == Primitive::kPrimNot) { |
| 103 | setter->SetReference(getter->GetReference()); |
| 104 | } else { |
| 105 | setter->Set(getter->Get()); |
| 106 | } |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 107 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 108 | JValue value; |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 109 | |
| 110 | if (Primitive::Is64BitType(from_type)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 111 | value.SetJ(getter->GetLong()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 112 | } else if (from_type == Primitive::kPrimNot) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 113 | value.SetL(getter->GetReference()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 114 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 115 | value.SetI(getter->Get()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 118 | // Caveat emptor - ObjPtr's not guaranteed valid after this call. |
| 119 | if (!ConvertArgumentValue(callsite_type, callee_type, i, &value)) { |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 120 | DCHECK(self->IsExceptionPending()); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if (Primitive::Is64BitType(to_type)) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 125 | setter->SetLong(value.GetJ()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 126 | } else if (to_type == Primitive::kPrimNot) { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 127 | setter->SetReference(value.GetL()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 128 | } else { |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 129 | setter->Set(value.GetI()); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 137 | template <bool is_range> |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 138 | bool ConvertAndCopyArgumentsFromCallerFrame(Thread* self, |
| 139 | Handle<mirror::MethodType> callsite_type, |
| 140 | Handle<mirror::MethodType> callee_type, |
| 141 | const ShadowFrame& caller_frame, |
| 142 | uint32_t first_src_reg, |
| 143 | uint32_t first_dest_reg, |
| 144 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 145 | ShadowFrame* callee_frame) |
| 146 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 147 | ObjPtr<mirror::ObjectArray<mirror::Class>> from_types(callsite_type->GetPTypes()); |
| 148 | ObjPtr<mirror::ObjectArray<mirror::Class>> to_types(callee_type->GetPTypes()); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 149 | |
| 150 | const int32_t num_method_params = from_types->GetLength(); |
| 151 | if (to_types->GetLength() != num_method_params) { |
| 152 | ThrowWrongMethodTypeException(callee_type.Get(), callsite_type.Get()); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 156 | ShadowFrameGetter<is_range> getter(first_src_reg, arg, caller_frame); |
| 157 | ShadowFrameSetter setter(callee_frame, first_dest_reg); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 158 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 159 | return PerformConversions<ShadowFrameGetter<is_range>, ShadowFrameSetter>(self, |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 160 | callsite_type, |
| 161 | callee_type, |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 162 | &getter, |
| 163 | &setter, |
| 164 | num_method_params); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 167 | } // namespace art |
| 168 | |
| 169 | #endif // ART_RUNTIME_METHOD_HANDLES_INL_H_ |