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 "method_type-inl.h"
18
19 #include "class-alloc-inl.h"
20 #include "class_root-inl.h"
21 #include "method_handles.h"
22 #include "obj_ptr-inl.h"
23 #include "object_array-alloc-inl.h"
24 #include "object_array-inl.h"
25
26 namespace art {
27 namespace mirror {
28
29 namespace {
30
AllocatePTypesArray(Thread * self,int count)31 ObjPtr<ObjectArray<Class>> AllocatePTypesArray(Thread* self, int count)
32 REQUIRES_SHARED(Locks::mutator_lock_) {
33 ObjPtr<Class> class_array_type = GetClassRoot<mirror::ObjectArray<mirror::Class>>();
34 return ObjectArray<Class>::Alloc(self, class_array_type, count);
35 }
36
37 } // namespace
38
Create(Thread * const self,Handle<Class> return_type,Handle<ObjectArray<Class>> parameter_types)39 ObjPtr<MethodType> MethodType::Create(Thread* const self,
40 Handle<Class> return_type,
41 Handle<ObjectArray<Class>> parameter_types) {
42 StackHandleScope<1> hs(self);
43 Handle<MethodType> mt(
44 hs.NewHandle(ObjPtr<MethodType>::DownCast(GetClassRoot<MethodType>()->AllocObject(self))));
45
46 if (mt == nullptr) {
47 self->AssertPendingOOMException();
48 return nullptr;
49 }
50
51 // We're initializing a newly allocated object, so we do not need to record that under
52 // a transaction. If the transaction is aborted, the whole object shall be unreachable.
53 mt->SetFieldObject</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
54 FormOffset(), nullptr);
55 mt->SetFieldObject</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
56 MethodDescriptorOffset(), nullptr);
57 mt->SetFieldObject</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
58 RTypeOffset(), return_type.Get());
59 mt->SetFieldObject</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
60 PTypesOffset(), parameter_types.Get());
61 mt->SetFieldObject</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
62 WrapAltOffset(), nullptr);
63
64 return mt.Get();
65 }
66
CloneWithoutLeadingParameter(Thread * const self,ObjPtr<MethodType> method_type)67 ObjPtr<MethodType> MethodType::CloneWithoutLeadingParameter(Thread* const self,
68 ObjPtr<MethodType> method_type) {
69 StackHandleScope<3> hs(self);
70 Handle<ObjectArray<Class>> src_ptypes = hs.NewHandle(method_type->GetPTypes());
71 Handle<Class> dst_rtype = hs.NewHandle(method_type->GetRType());
72 const int32_t dst_ptypes_count = method_type->GetNumberOfPTypes() - 1;
73 Handle<ObjectArray<Class>> dst_ptypes = hs.NewHandle(AllocatePTypesArray(self, dst_ptypes_count));
74 if (dst_ptypes.IsNull()) {
75 return nullptr;
76 }
77 for (int32_t i = 0; i < dst_ptypes_count; ++i) {
78 dst_ptypes->Set(i, src_ptypes->Get(i + 1));
79 }
80 return Create(self, dst_rtype, dst_ptypes);
81 }
82
CollectTrailingArguments(Thread * self,ObjPtr<MethodType> method_type,ObjPtr<Class> collector_array_class,int32_t start_index)83 ObjPtr<MethodType> MethodType::CollectTrailingArguments(Thread* self,
84 ObjPtr<MethodType> method_type,
85 ObjPtr<Class> collector_array_class,
86 int32_t start_index) {
87 int32_t ptypes_length = method_type->GetNumberOfPTypes();
88 if (start_index > ptypes_length) {
89 return method_type;
90 }
91
92 StackHandleScope<4> hs(self);
93 Handle<Class> collector_class = hs.NewHandle(collector_array_class);
94 Handle<Class> dst_rtype = hs.NewHandle(method_type->GetRType());
95 Handle<ObjectArray<Class>> src_ptypes = hs.NewHandle(method_type->GetPTypes());
96 Handle<ObjectArray<Class>> dst_ptypes = hs.NewHandle(AllocatePTypesArray(self, start_index + 1));
97 if (dst_ptypes.IsNull()) {
98 return nullptr;
99 }
100 for (int32_t i = 0; i < start_index; ++i) {
101 dst_ptypes->Set(i, src_ptypes->Get(i));
102 }
103 dst_ptypes->Set(start_index, collector_class.Get());
104 return Create(self, dst_rtype, dst_ptypes);
105 }
106
NumberOfVRegs()107 size_t MethodType::NumberOfVRegs() {
108 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
109 const int32_t p_types_length = p_types->GetLength();
110
111 // Initialize |num_vregs| with number of parameters and only increment it for
112 // types requiring a second vreg.
113 size_t num_vregs = static_cast<size_t>(p_types_length);
114 for (int32_t i = 0; i < p_types_length; ++i) {
115 ObjPtr<Class> klass = p_types->GetWithoutChecks(i);
116 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
117 ++num_vregs;
118 }
119 }
120 return num_vregs;
121 }
122
IsExactMatch(ObjPtr<MethodType> target)123 bool MethodType::IsExactMatch(ObjPtr<MethodType> target) {
124 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
125 const int32_t params_length = p_types->GetLength();
126
127 const ObjPtr<ObjectArray<Class>> target_p_types = target->GetPTypes();
128 if (params_length != target_p_types->GetLength()) {
129 return false;
130 }
131 for (int32_t i = 0; i < params_length; ++i) {
132 if (p_types->GetWithoutChecks(i) != target_p_types->GetWithoutChecks(i)) {
133 return false;
134 }
135 }
136 return GetRType() == target->GetRType();
137 }
138
IsConvertible(ObjPtr<MethodType> target)139 bool MethodType::IsConvertible(ObjPtr<MethodType> target) {
140 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
141 const int32_t params_length = p_types->GetLength();
142
143 const ObjPtr<ObjectArray<Class>> target_p_types = target->GetPTypes();
144 if (params_length != target_p_types->GetLength()) {
145 return false;
146 }
147
148 // Perform return check before invoking method handle otherwise side
149 // effects from the invocation may be observable before
150 // WrongMethodTypeException is raised.
151 if (!IsReturnTypeConvertible(target->GetRType(), GetRType())) {
152 return false;
153 }
154
155 for (int32_t i = 0; i < params_length; ++i) {
156 if (!IsParameterTypeConvertible(p_types->GetWithoutChecks(i),
157 target_p_types->GetWithoutChecks(i))) {
158 return false;
159 }
160 }
161 return true;
162 }
163
IsParameterInPlaceConvertible(ObjPtr<Class> from,ObjPtr<Class> to)164 static bool IsParameterInPlaceConvertible(ObjPtr<Class> from, ObjPtr<Class> to)
165 REQUIRES_SHARED(Locks::mutator_lock_) {
166 if (from == to) {
167 return true;
168 }
169
170 if (from->IsPrimitive() != to->IsPrimitive()) {
171 return false; // No in-place conversion from place conversion for box/unboxing.
172 }
173
174 if (from->IsPrimitive()) {
175 // `from` and `to` are both primitives. The supported in-place conversions use a 32-bit
176 // interpreter representation and are a subset of permitted conversions for MethodHandles.
177 // Conversions are documented in JLS 11 S5.1.2 "Widening Primitive Conversion".
178 Primitive::Type src = from->GetPrimitiveType();
179 Primitive::Type dst = to->GetPrimitiveType();
180 switch (src) {
181 case Primitive::Type::kPrimByte:
182 return dst == Primitive::Type::kPrimShort || dst == Primitive::Type::kPrimInt;
183 case Primitive::Type::kPrimChar:
184 FALLTHROUGH_INTENDED;
185 case Primitive::Type::kPrimShort:
186 return dst == Primitive::Type::kPrimInt;
187 default:
188 return false;
189 }
190 }
191
192 // `from` and `to` are both references, apply an assignability check.
193 return to->IsAssignableFrom(from);
194 }
195
IsInPlaceConvertible(ObjPtr<MethodType> target)196 bool MethodType::IsInPlaceConvertible(ObjPtr<MethodType> target) {
197 const ObjPtr<ObjectArray<Class>> ptypes = GetPTypes();
198 const ObjPtr<ObjectArray<Class>> target_ptypes = target->GetPTypes();
199 const int32_t ptypes_length = ptypes->GetLength();
200 if (ptypes_length != target_ptypes->GetLength()) {
201 return false;
202 }
203
204 for (int32_t i = 0; i < ptypes_length; ++i) {
205 if (!IsParameterInPlaceConvertible(ptypes->GetWithoutChecks(i),
206 target_ptypes->GetWithoutChecks(i))) {
207 return false;
208 }
209 }
210
211 return GetRType()->IsPrimitiveVoid() ||
212 IsParameterInPlaceConvertible(target->GetRType(), GetRType());
213 }
214
PrettyDescriptor()215 std::string MethodType::PrettyDescriptor() {
216 std::ostringstream ss;
217 ss << "(";
218
219 const ObjPtr<ObjectArray<Class>> p_types = GetPTypes();
220 const int32_t params_length = p_types->GetLength();
221 for (int32_t i = 0; i < params_length; ++i) {
222 ss << p_types->GetWithoutChecks(i)->PrettyDescriptor();
223 if (i != (params_length - 1)) {
224 ss << ", ";
225 }
226 }
227
228 ss << ")";
229 ss << GetRType()->PrettyDescriptor();
230
231 return ss.str();
232 }
233
234 } // namespace mirror
235 } // namespace art
236