blob: be9f0d34111792907288da9a3f3f0a1752fb762e [file] [log] [blame]
Narayan Kamath208f8572016-08-03 12:46:58 +01001/*
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"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_instruction.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010024#include "interpreter/interpreter_common.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010025#include "interpreter/shadow_frame-inl.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070026#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010027#include "mirror/class.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000028#include "mirror/method_type-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010029#include "mirror/object.h"
30#include "reflection.h"
31#include "stack.h"
32
Dmitrii Ishcheikin6f309912024-01-24 16:27:53 +000033namespace art HIDDEN {
Narayan Kamath208f8572016-08-03 12:46:58 +010034
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010035// A convenience class that allows for iteration through a list of
36// input argument registers. This is used to iterate over input
37// arguments while performing standard argument conversions.
38class ShadowFrameGetter {
39 public:
40 ShadowFrameGetter(const ShadowFrame& shadow_frame,
41 const InstructionOperands* const operands,
42 size_t operand_index = 0u)
43 : shadow_frame_(shadow_frame), operands_(operands), operand_index_(operand_index) {}
44
45 ALWAYS_INLINE uint32_t Get() REQUIRES_SHARED(Locks::mutator_lock_) {
46 return shadow_frame_.GetVReg(Next());
47 }
48
49 ALWAYS_INLINE int64_t GetLong() REQUIRES_SHARED(Locks::mutator_lock_) {
50 return shadow_frame_.GetVRegLong(NextLong());
51 }
52
53 ALWAYS_INLINE ObjPtr<mirror::Object> GetReference() REQUIRES_SHARED(Locks::mutator_lock_) {
54 return shadow_frame_.GetVRegReference(Next());
55 }
56
57 private:
58 uint32_t Next() {
59 const uint32_t next = operands_->GetOperand(operand_index_);
60 operand_index_ += 1;
61 return next;
62 }
63
64 uint32_t NextLong() {
65 const uint32_t next = operands_->GetOperand(operand_index_);
66 operand_index_ += 2;
67 return next;
68 }
69
70 const ShadowFrame& shadow_frame_;
71 const InstructionOperands* const operands_; // the set of register operands to read
72 size_t operand_index_; // the next register operand to read from frame
73};
74
75// A convenience class that allows values to be written to a given shadow frame,
76// starting at location |first_dst_reg|.
77class ShadowFrameSetter {
78 public:
79 ShadowFrameSetter(ShadowFrame* shadow_frame, size_t first_dst_reg)
80 : shadow_frame_(shadow_frame), arg_index_(first_dst_reg) {}
81
82 ALWAYS_INLINE void Set(uint32_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
83 DCHECK_LT(arg_index_, shadow_frame_->NumberOfVRegs());
84 shadow_frame_->SetVReg(arg_index_++, value);
85 }
86
87 ALWAYS_INLINE void SetReference(ObjPtr<mirror::Object> value)
88 REQUIRES_SHARED(Locks::mutator_lock_) {
89 DCHECK_LT(arg_index_, shadow_frame_->NumberOfVRegs());
90 shadow_frame_->SetVRegReference(arg_index_++, value);
91 }
92
93 ALWAYS_INLINE void SetLong(int64_t value) REQUIRES_SHARED(Locks::mutator_lock_) {
94 DCHECK_LT(arg_index_, shadow_frame_->NumberOfVRegs());
95 shadow_frame_->SetVRegLong(arg_index_, value);
96 arg_index_ += 2;
97 }
98
99 ALWAYS_INLINE bool Done() const {
100 return arg_index_ == shadow_frame_->NumberOfVRegs();
101 }
102
103 private:
104 ShadowFrame* shadow_frame_;
105 size_t arg_index_;
106};
107
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100108inline bool ConvertArgumentValue(const ThrowWrongMethodTypeFunction& throw_wmt,
109 ObjPtr<mirror::Class> from,
110 ObjPtr<mirror::Class> to,
111 /*inout*/ JValue* value) {
112 if (from == to) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100113 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100114 }
115
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100116 // `*value` may contain a bare heap pointer which is generally unsafe.
117 // `ConvertJValueCommon()` saves `*value`, `from`, and `to` to Handles
118 // where necessary to avoid issues if the heap changes.
119 if (ConvertJValueCommon(throw_wmt, from, to, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100120 DCHECK(!Thread::Current()->IsExceptionPending());
Narayan Kamath208f8572016-08-03 12:46:58 +0100121 return true;
122 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100123 DCHECK(Thread::Current()->IsExceptionPending());
124 value->SetJ(0);
Narayan Kamath208f8572016-08-03 12:46:58 +0100125 return false;
126 }
127}
128
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100129inline bool ConvertReturnValue(const ThrowWrongMethodTypeFunction& throw_wmt,
130 ObjPtr<mirror::Class> from,
131 ObjPtr<mirror::Class> to,
132 /*inout*/ JValue* value) {
133 if (to->GetPrimitiveType() == Primitive::kPrimVoid || from == to) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100134 return true;
Narayan Kamathda246502016-10-20 18:39:22 +0100135 }
136
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100137 // `*value` may contain a bare heap pointer which is generally unsafe.
138 // `ConvertJValueCommon()` saves `*value`, `from`, and `to` to Handles
139 // where necessary to avoid issues if the heap changes.
140 if (ConvertJValueCommon(throw_wmt, from, to, value)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100141 DCHECK(!Thread::Current()->IsExceptionPending());
142 return true;
143 } else {
144 DCHECK(Thread::Current()->IsExceptionPending());
145 value->SetJ(0);
146 return false;
147 }
Narayan Kamathda246502016-10-20 18:39:22 +0100148}
149
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100150template <typename FromPTypes, typename ToPTypes, typename G, typename S>
151bool PerformConversions(const ThrowWrongMethodTypeFunction& throw_wmt,
152 FromPTypes from_types,
153 ToPTypes to_types,
Narayan Kamath000e1882016-10-24 17:14:25 +0100154 G* getter,
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100155 S* setter) {
156 DCHECK_EQ(from_types.GetLength(), to_types.GetLength());
157 for (int32_t i = 0, length = to_types.GetLength(); i != length; ++i) {
158 ObjPtr<mirror::Class> from = from_types.Get(i);
159 ObjPtr<mirror::Class> to = to_types.Get(i);
Orion Hodsonb8b93872018-01-30 07:51:10 +0000160 const Primitive::Type from_type = from->GetPrimitiveType();
161 const Primitive::Type to_type = to->GetPrimitiveType();
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100162 if (from == to) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100163 // Easy case - the types are identical. Nothing left to do except to pass
164 // the arguments along verbatim.
165 if (Primitive::Is64BitType(from_type)) {
166 setter->SetLong(getter->GetLong());
167 } else if (from_type == Primitive::kPrimNot) {
168 setter->SetReference(getter->GetReference());
169 } else {
170 setter->Set(getter->Get());
171 }
Narayan Kamath000e1882016-10-24 17:14:25 +0100172 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100173 JValue value;
Narayan Kamath000e1882016-10-24 17:14:25 +0100174 if (Primitive::Is64BitType(from_type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100175 value.SetJ(getter->GetLong());
Narayan Kamath000e1882016-10-24 17:14:25 +0100176 } else if (from_type == Primitive::kPrimNot) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100177 value.SetL(getter->GetReference());
Narayan Kamath000e1882016-10-24 17:14:25 +0100178 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100179 value.SetI(getter->Get());
Narayan Kamath000e1882016-10-24 17:14:25 +0100180 }
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100181 // Caveat emptor - ObjPtr's not guaranteed valid after this call.
Vladimir Marko4f2fccc2023-11-09 18:07:24 +0100182 if (!ConvertArgumentValue(throw_wmt, from, to, &value)) {
183 DCHECK(Thread::Current()->IsExceptionPending());
Narayan Kamath000e1882016-10-24 17:14:25 +0100184 return false;
185 }
Narayan Kamath000e1882016-10-24 17:14:25 +0100186 if (Primitive::Is64BitType(to_type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100187 setter->SetLong(value.GetJ());
Narayan Kamath000e1882016-10-24 17:14:25 +0100188 } else if (to_type == Primitive::kPrimNot) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100189 setter->SetReference(value.GetL());
Narayan Kamath000e1882016-10-24 17:14:25 +0100190 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100191 setter->Set(value.GetI());
Narayan Kamath000e1882016-10-24 17:14:25 +0100192 }
193 }
194 }
Narayan Kamath000e1882016-10-24 17:14:25 +0100195 return true;
196}
197
Orion Hodsonb8b93872018-01-30 07:51:10 +0000198template <typename G, typename S>
Orion Hodsonc6270df2022-01-28 18:11:03 +0000199bool CopyArguments(Thread* self,
200 Handle<mirror::MethodType> method_type,
201 G* getter,
202 S* setter) REQUIRES_SHARED(Locks::mutator_lock_) {
203 StackHandleScope<2> hs(self);
204 Handle<mirror::ObjectArray<mirror::Class>> ptypes(hs.NewHandle(method_type->GetPTypes()));
205 int32_t ptypes_length = ptypes->GetLength();
206
207 for (int32_t i = 0; i < ptypes_length; ++i) {
208 ObjPtr<mirror::Class> ptype(ptypes->GetWithoutChecks(i));
209 Primitive::Type primitive = ptype->GetPrimitiveType();
210 if (Primitive::Is64BitType(primitive)) {
211 setter->SetLong(getter->GetLong());
212 } else if (primitive == Primitive::kPrimNot) {
213 setter->SetReference(getter->GetReference());
214 } else {
215 setter->Set(getter->Get());
216 }
217 }
218 return true;
219}
220
Narayan Kamath208f8572016-08-03 12:46:58 +0100221} // namespace art
222
223#endif // ART_RUNTIME_METHOD_HANDLES_INL_H_