blob: 08b8ad937ab163b4e9d58eb598c5ef749688b9c8 [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"
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
32namespace art {
33
Orion Hodsonba28f9f2016-10-26 10:56:25 +010034inline 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 Kamath208f8572016-08-03 12:46:58 +010041 return true;
Orion Hodsonba28f9f2016-10-26 10:56:25 +010042 }
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 Kamath208f8572016-08-03 12:46:58 +010050 return true;
51 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +010052 DCHECK(Thread::Current()->IsExceptionPending());
53 value->SetJ(0);
Narayan Kamath208f8572016-08-03 12:46:58 +010054 return false;
55 }
56}
57
Orion Hodsonba28f9f2016-10-26 10:56:25 +010058inline 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 Kamathda246502016-10-20 18:39:22 +010065 }
66
Orion Hodsonba28f9f2016-10-26 10:56:25 +010067 // |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 Kamathda246502016-10-20 18:39:22 +010079}
80
Narayan Kamath000e1882016-10-24 17:14:25 +010081template <typename G, typename S>
82bool PerformConversions(Thread* self,
Orion Hodsonba28f9f2016-10-26 10:56:25 +010083 Handle<mirror::MethodType> callsite_type,
84 Handle<mirror::MethodType> callee_type,
Narayan Kamath000e1882016-10-24 17:14:25 +010085 G* getter,
86 S* setter,
Orion Hodsonba28f9f2016-10-26 10:56:25 +010087 int32_t num_conversions) REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamath000e1882016-10-24 17:14:25 +010088 StackHandleScope<2> hs(self);
Orion Hodsonba28f9f2016-10-26 10:56:25 +010089 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 Kamath000e1882016-10-24 17:14:25 +010091
92 for (int32_t i = 0; i < num_conversions; ++i) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +010093 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 Kamath000e1882016-10-24 17:14:25 +010098 // 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 Kamath000e1882016-10-24 17:14:25 +0100107 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100108 JValue value;
Narayan Kamath000e1882016-10-24 17:14:25 +0100109
110 if (Primitive::Is64BitType(from_type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100111 value.SetJ(getter->GetLong());
Narayan Kamath000e1882016-10-24 17:14:25 +0100112 } else if (from_type == Primitive::kPrimNot) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100113 value.SetL(getter->GetReference());
Narayan Kamath000e1882016-10-24 17:14:25 +0100114 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100115 value.SetI(getter->Get());
Narayan Kamath000e1882016-10-24 17:14:25 +0100116 }
117
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100118 // Caveat emptor - ObjPtr's not guaranteed valid after this call.
119 if (!ConvertArgumentValue(callsite_type, callee_type, i, &value)) {
Narayan Kamath000e1882016-10-24 17:14:25 +0100120 DCHECK(self->IsExceptionPending());
121 return false;
122 }
123
124 if (Primitive::Is64BitType(to_type)) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100125 setter->SetLong(value.GetJ());
Narayan Kamath000e1882016-10-24 17:14:25 +0100126 } else if (to_type == Primitive::kPrimNot) {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100127 setter->SetReference(value.GetL());
Narayan Kamath000e1882016-10-24 17:14:25 +0100128 } else {
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100129 setter->Set(value.GetI());
Narayan Kamath000e1882016-10-24 17:14:25 +0100130 }
131 }
132 }
133
134 return true;
135}
136
Narayan Kamath208f8572016-08-03 12:46:58 +0100137} // namespace art
138
139#endif // ART_RUNTIME_METHOD_HANDLES_INL_H_