1 /*
2 * Copyright (C) 2018 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 "var_handles.h"
18
19 #include "common_throws.h"
20 #include "dex/dex_instruction.h"
21 #include "handle.h"
22 #include "method_handles-inl.h"
23 #include "mirror/method_type-inl.h"
24 #include "mirror/var_handle.h"
25
26 namespace art {
27
28 namespace {
29
VarHandleInvokeAccessorWithConversions(Thread * self,ShadowFrame & shadow_frame,Handle<mirror::VarHandle> var_handle,Handle<mirror::MethodType> callsite_type,const mirror::VarHandle::AccessMode access_mode,const InstructionOperands * const operands,JValue * result)30 bool VarHandleInvokeAccessorWithConversions(Thread* self,
31 ShadowFrame& shadow_frame,
32 Handle<mirror::VarHandle> var_handle,
33 Handle<mirror::MethodType> callsite_type,
34 const mirror::VarHandle::AccessMode access_mode,
35 const InstructionOperands* const operands,
36 JValue* result)
37 REQUIRES_SHARED(Locks::mutator_lock_) {
38 StackHandleScope<1> hs(self);
39 Handle<mirror::MethodType> accessor_type(hs.NewHandle(
40 var_handle->GetMethodTypeForAccessMode(self, access_mode)));
41 const size_t num_vregs = accessor_type->NumberOfVRegs();
42 const int num_params = accessor_type->GetPTypes()->GetLength();
43 ShadowFrameAllocaUniquePtr accessor_frame =
44 CREATE_SHADOW_FRAME(num_vregs, nullptr, shadow_frame.GetMethod(), shadow_frame.GetDexPC());
45 ShadowFrameGetter getter(shadow_frame, operands);
46 static const uint32_t kFirstDestinationReg = 0;
47 ShadowFrameSetter setter(accessor_frame.get(), kFirstDestinationReg);
48 if (!PerformConversions(self, callsite_type, accessor_type, &getter, &setter, num_params)) {
49 return false;
50 }
51 RangeInstructionOperands accessor_operands(kFirstDestinationReg,
52 kFirstDestinationReg + num_vregs);
53 if (!var_handle->Access(access_mode, accessor_frame.get(), &accessor_operands, result)) {
54 return false;
55 }
56 return ConvertReturnValue(callsite_type, accessor_type, result);
57 }
58
59 } // namespace
60
VarHandleInvokeAccessor(Thread * self,ShadowFrame & shadow_frame,Handle<mirror::VarHandle> var_handle,Handle<mirror::MethodType> callsite_type,const mirror::VarHandle::AccessMode access_mode,const InstructionOperands * const operands,JValue * result)61 bool VarHandleInvokeAccessor(Thread* self,
62 ShadowFrame& shadow_frame,
63 Handle<mirror::VarHandle> var_handle,
64 Handle<mirror::MethodType> callsite_type,
65 const mirror::VarHandle::AccessMode access_mode,
66 const InstructionOperands* const operands,
67 JValue* result) {
68 if (var_handle.IsNull()) {
69 ThrowNullPointerExceptionFromDexPC();
70 return false;
71 }
72
73 if (!var_handle->IsAccessModeSupported(access_mode)) {
74 ThrowUnsupportedOperationException();
75 return false;
76 }
77
78 mirror::VarHandle::MatchKind match_kind =
79 var_handle->GetMethodTypeMatchForAccessMode(access_mode, callsite_type.Get());
80 if (LIKELY(match_kind == mirror::VarHandle::MatchKind::kExact)) {
81 return var_handle->Access(access_mode, &shadow_frame, operands, result);
82 } else if (match_kind == mirror::VarHandle::MatchKind::kWithConversions) {
83 return VarHandleInvokeAccessorWithConversions(self,
84 shadow_frame,
85 var_handle,
86 callsite_type,
87 access_mode,
88 operands,
89 result);
90 } else {
91 DCHECK_EQ(match_kind, mirror::VarHandle::MatchKind::kNone);
92 ThrowWrongMethodTypeException(var_handle->PrettyDescriptorForAccessMode(access_mode),
93 callsite_type->PrettyDescriptor());
94 return false;
95 }
96 }
97
98 } // namespace art
99