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
ConvertArgumentValue(Handle<mirror::MethodType> callsite_type,Handle<mirror::MethodType> callee_type,int index,JValue * value)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) {
41 return true;
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());
50 return true;
51 } else {
52 DCHECK(Thread::Current()->IsExceptionPending());
53 value->SetJ(0);
54 return false;
55 }
56 }
57
ConvertReturnValue(Handle<mirror::MethodType> callsite_type,Handle<mirror::MethodType> callee_type,JValue * value)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;
65 }
66
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 }
79 }
80
81 template <typename G, typename S>
PerformConversions(Thread * self,Handle<mirror::MethodType> callsite_type,Handle<mirror::MethodType> callee_type,G * getter,S * setter,int32_t num_conversions)82 bool PerformConversions(Thread* self,
83 Handle<mirror::MethodType> callsite_type,
84 Handle<mirror::MethodType> callee_type,
85 G* getter,
86 S* setter,
87 int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_) {
88 StackHandleScope<2> hs(self);
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()));
91
92 for (int32_t i = 0; i < num_conversions; ++i) {
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) {
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 }
107 } else {
108 JValue value;
109
110 if (Primitive::Is64BitType(from_type)) {
111 value.SetJ(getter->GetLong());
112 } else if (from_type == Primitive::kPrimNot) {
113 value.SetL(getter->GetReference());
114 } else {
115 value.SetI(getter->Get());
116 }
117
118 // Caveat emptor - ObjPtr's not guaranteed valid after this call.
119 if (!ConvertArgumentValue(callsite_type, callee_type, i, &value)) {
120 DCHECK(self->IsExceptionPending());
121 return false;
122 }
123
124 if (Primitive::Is64BitType(to_type)) {
125 setter->SetLong(value.GetJ());
126 } else if (to_type == Primitive::kPrimNot) {
127 setter->SetReference(value.GetL());
128 } else {
129 setter->Set(value.GetI());
130 }
131 }
132 }
133
134 return true;
135 }
136
137 } // namespace art
138
139 #endif // ART_RUNTIME_METHOD_HANDLES_INL_H_
140