Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "interpreter_common.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 22 | #include "class_root.h" |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 23 | #include "debugger.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 24 | #include "dex/dex_file_types.h" |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 25 | #include "entrypoints/runtime_asm_entrypoints.h" |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 26 | #include "intrinsics_enum.h" |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 27 | #include "jit/jit.h" |
Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 28 | #include "jvalue-inl.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 29 | #include "method_handles-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 30 | #include "method_handles.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 31 | #include "mirror/array-inl.h" |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 32 | #include "mirror/class.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 33 | #include "mirror/emulated_stack_frame.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 34 | #include "mirror/method_handle_impl-inl.h" |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 35 | #include "mirror/var_handle.h" |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 36 | #include "reflection-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include "reflection.h" |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 38 | #include "shadow_frame-inl.h" |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 39 | #include "stack.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 40 | #include "thread-inl.h" |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 41 | #include "transaction.h" |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 42 | #include "var_handles.h" |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 43 | #include "well_known_classes.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | namespace interpreter { |
| 47 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 48 | void ThrowNullPointerExceptionFromInterpreter() { |
| 49 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 52 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 53 | bool transaction_active> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 54 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 55 | uint16_t inst_data) { |
| 56 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 57 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 58 | ArtField* f = |
| 59 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 60 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 61 | if (UNLIKELY(f == nullptr)) { |
| 62 | CHECK(self->IsExceptionPending()); |
| 63 | return false; |
| 64 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 65 | ObjPtr<mirror::Object> obj; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 66 | if (is_static) { |
| 67 | obj = f->GetDeclaringClass(); |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 68 | if (transaction_active) { |
| 69 | if (Runtime::Current()->GetTransaction()->ReadConstraint(obj.Ptr(), f)) { |
| 70 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of " |
| 71 | + obj->PrettyTypeOf() + " since it does not belong to clinit's class."); |
| 72 | return false; |
| 73 | } |
| 74 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 75 | } else { |
| 76 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 77 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 78 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | } |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 82 | |
| 83 | JValue result; |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 84 | if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) { |
| 85 | // Instrumentation threw an error! |
| 86 | CHECK(self->IsExceptionPending()); |
| 87 | return false; |
| 88 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 89 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 90 | switch (field_type) { |
| 91 | case Primitive::kPrimBoolean: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 92 | shadow_frame.SetVReg(vregA, result.GetZ()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 93 | break; |
| 94 | case Primitive::kPrimByte: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 95 | shadow_frame.SetVReg(vregA, result.GetB()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 96 | break; |
| 97 | case Primitive::kPrimChar: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 98 | shadow_frame.SetVReg(vregA, result.GetC()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 99 | break; |
| 100 | case Primitive::kPrimShort: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 101 | shadow_frame.SetVReg(vregA, result.GetS()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 102 | break; |
| 103 | case Primitive::kPrimInt: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 104 | shadow_frame.SetVReg(vregA, result.GetI()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 105 | break; |
| 106 | case Primitive::kPrimLong: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 107 | shadow_frame.SetVRegLong(vregA, result.GetJ()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 108 | break; |
| 109 | case Primitive::kPrimNot: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 110 | shadow_frame.SetVRegReference(vregA, result.GetL()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 111 | break; |
| 112 | default: |
| 113 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 114 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | // Explicitly instantiate all DoFieldGet functions. |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 120 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 121 | template bool DoFieldGet<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 122 | ShadowFrame& shadow_frame, \ |
| 123 | const Instruction* inst, \ |
| 124 | uint16_t inst_data) |
| 125 | |
| 126 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 127 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 128 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 129 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, true); \ |
| 130 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 131 | |
| 132 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 133 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 134 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 135 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 136 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 137 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 138 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 139 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 140 | |
| 141 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 142 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 143 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 144 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 145 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 146 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 147 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 148 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 149 | |
| 150 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 151 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 152 | |
| 153 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 154 | // Returns true on success, otherwise throws an exception and returns false. |
| 155 | template<Primitive::Type field_type> |
| 156 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 157 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 158 | if (UNLIKELY(obj == nullptr)) { |
| 159 | // We lost the reference to the field index so we cannot get a more |
| 160 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 161 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | MemberOffset field_offset(inst->VRegC_22c()); |
| 165 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 166 | // the field from the base of the object, we need to look for it first. |
| 167 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 168 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 169 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 170 | field_offset.Uint32Value()); |
| 171 | DCHECK(f != nullptr); |
| 172 | DCHECK(!f->IsStatic()); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 173 | Thread* self = Thread::Current(); |
| 174 | StackHandleScope<1> hs(self); |
Mathieu Chartier | a314773 | 2016-10-26 22:57:02 -0700 | [diff] [blame] | 175 | // Save obj in case the instrumentation event has thread suspension. |
| 176 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 177 | instrumentation->FieldReadEvent(self, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 178 | obj.Ptr(), |
| 179 | shadow_frame.GetMethod(), |
| 180 | shadow_frame.GetDexPC(), |
| 181 | f); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 182 | if (UNLIKELY(self->IsExceptionPending())) { |
| 183 | return false; |
| 184 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 185 | } |
| 186 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 187 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 188 | switch (field_type) { |
| 189 | case Primitive::kPrimInt: |
| 190 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 191 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 192 | case Primitive::kPrimBoolean: |
| 193 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 194 | break; |
| 195 | case Primitive::kPrimByte: |
| 196 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 197 | break; |
| 198 | case Primitive::kPrimChar: |
| 199 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 200 | break; |
| 201 | case Primitive::kPrimShort: |
| 202 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 203 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 204 | case Primitive::kPrimLong: |
| 205 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 206 | break; |
| 207 | case Primitive::kPrimNot: |
| 208 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 209 | break; |
| 210 | default: |
| 211 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 212 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | // Explicitly instantiate all DoIGetQuick functions. |
| 218 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 219 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 220 | uint16_t inst_data) |
| 221 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 222 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 223 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 224 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 225 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 226 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 227 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 228 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 229 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 230 | |
| 231 | template<Primitive::Type field_type> |
| 232 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 233 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 234 | JValue field_value; |
| 235 | switch (field_type) { |
| 236 | case Primitive::kPrimBoolean: |
| 237 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 238 | break; |
| 239 | case Primitive::kPrimByte: |
| 240 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 241 | break; |
| 242 | case Primitive::kPrimChar: |
| 243 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 244 | break; |
| 245 | case Primitive::kPrimShort: |
| 246 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 247 | break; |
| 248 | case Primitive::kPrimInt: |
| 249 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 250 | break; |
| 251 | case Primitive::kPrimLong: |
| 252 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 253 | break; |
| 254 | case Primitive::kPrimNot: |
| 255 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 256 | break; |
| 257 | default: |
| 258 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 259 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 260 | } |
| 261 | return field_value; |
| 262 | } |
| 263 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 264 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 265 | bool transaction_active> |
| 266 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 267 | uint16_t inst_data) { |
| 268 | const bool do_assignability_check = do_access_check; |
| 269 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 270 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 271 | ArtField* f = |
| 272 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 273 | Primitive::ComponentSize(field_type)); |
| 274 | if (UNLIKELY(f == nullptr)) { |
| 275 | CHECK(self->IsExceptionPending()); |
| 276 | return false; |
| 277 | } |
| 278 | ObjPtr<mirror::Object> obj; |
| 279 | if (is_static) { |
| 280 | obj = f->GetDeclaringClass(); |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 281 | if (transaction_active) { |
| 282 | if (Runtime::Current()->GetTransaction()->WriteConstraint(obj.Ptr(), f)) { |
| 283 | Runtime::Current()->AbortTransactionAndThrowAbortError( |
| 284 | self, "Can't set fields of " + obj->PrettyTypeOf()); |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 289 | } else { |
| 290 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 291 | if (UNLIKELY(obj == nullptr)) { |
| 292 | ThrowNullPointerExceptionForFieldAccess(f, false); |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 298 | JValue value = GetFieldValue<field_type>(shadow_frame, vregA); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 299 | return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self, |
| 300 | shadow_frame, |
| 301 | obj, |
| 302 | f, |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 303 | value); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 306 | // Explicitly instantiate all DoFieldPut functions. |
| 307 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 308 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 309 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 310 | |
| 311 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 312 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 313 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 314 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 315 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 316 | |
| 317 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 318 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 319 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 320 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 321 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 322 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 323 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 324 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 325 | |
| 326 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 327 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 328 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 329 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 330 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 331 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 332 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 333 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 334 | |
| 335 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 336 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 337 | |
| 338 | template<Primitive::Type field_type, bool transaction_active> |
| 339 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 340 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 341 | if (UNLIKELY(obj == nullptr)) { |
| 342 | // We lost the reference to the field index so we cannot get a more |
| 343 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 344 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
| 347 | MemberOffset field_offset(inst->VRegC_22c()); |
| 348 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 349 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 350 | // the field from the base of the object, we need to look for it first. |
| 351 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 352 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 353 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 354 | field_offset.Uint32Value()); |
| 355 | DCHECK(f != nullptr); |
| 356 | DCHECK(!f->IsStatic()); |
| 357 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 358 | Thread* self = Thread::Current(); |
| 359 | StackHandleScope<2> hs(self); |
Mathieu Chartier | a314773 | 2016-10-26 22:57:02 -0700 | [diff] [blame] | 360 | // Save obj in case the instrumentation event has thread suspension. |
| 361 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 362 | mirror::Object* fake_root = nullptr; |
| 363 | HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>( |
| 364 | field_type == Primitive::kPrimNot ? field_value.GetGCRoot() : &fake_root)); |
| 365 | instrumentation->FieldWriteEvent(self, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 366 | obj.Ptr(), |
| 367 | shadow_frame.GetMethod(), |
| 368 | shadow_frame.GetDexPC(), |
| 369 | f, |
| 370 | field_value); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 371 | if (UNLIKELY(self->IsExceptionPending())) { |
| 372 | return false; |
| 373 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 374 | } |
| 375 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 376 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 377 | case Primitive::kPrimBoolean: |
| 378 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 379 | break; |
| 380 | case Primitive::kPrimByte: |
| 381 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 382 | break; |
| 383 | case Primitive::kPrimChar: |
| 384 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 385 | break; |
| 386 | case Primitive::kPrimShort: |
| 387 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 388 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 389 | case Primitive::kPrimInt: |
| 390 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 391 | break; |
| 392 | case Primitive::kPrimLong: |
| 393 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 394 | break; |
| 395 | case Primitive::kPrimNot: |
| 396 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 397 | break; |
| 398 | default: |
| 399 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 400 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | // Explicitly instantiate all DoIPutQuick functions. |
| 406 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 407 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 408 | const Instruction* inst, \ |
| 409 | uint16_t inst_data) |
| 410 | |
| 411 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 412 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 413 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 414 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 415 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 416 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 417 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 418 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 419 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 420 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 421 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 422 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 423 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 424 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 425 | // We execute any instrumentation events that are triggered by this exception and change the |
| 426 | // shadow_frame's dex_pc to that of the exception handler if there is one in the current method. |
| 427 | // Return true if we should continue executing in the current method and false if we need to go up |
| 428 | // the stack to find an exception handler. |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 429 | // We accept a null Instrumentation* meaning we must not report anything to the instrumentation. |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 430 | // TODO We should have a better way to skip instrumentation reporting or possibly rethink that |
| 431 | // behavior. |
| 432 | bool MoveToExceptionHandler(Thread* self, |
| 433 | ShadowFrame& shadow_frame, |
| 434 | const instrumentation::Instrumentation* instrumentation) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 435 | self->VerifyStack(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 436 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 437 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 438 | if (instrumentation != nullptr && |
| 439 | instrumentation->HasExceptionThrownListeners() && |
| 440 | self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
| 441 | // See b/65049545 for why we don't need to check to see if the exception has changed. |
Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 442 | instrumentation->ExceptionThrownEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 443 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 444 | bool clear_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 445 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 446 | hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 447 | if (found_dex_pc == dex::kDexNoIndex) { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 448 | if (instrumentation != nullptr) { |
| 449 | if (shadow_frame.NeedsNotifyPop()) { |
| 450 | instrumentation->WatchedFramePopped(self, shadow_frame); |
| 451 | } |
| 452 | // Exception is not caught by the current method. We will unwind to the |
| 453 | // caller. Notify any instrumentation listener. |
| 454 | instrumentation->MethodUnwindEvent(self, |
| 455 | shadow_frame.GetThisObject(), |
| 456 | shadow_frame.GetMethod(), |
| 457 | shadow_frame.GetDexPC()); |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 458 | } |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 459 | return false; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 460 | } else { |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 461 | shadow_frame.SetDexPC(found_dex_pc); |
| 462 | if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) { |
| 463 | self->ClearException(); |
| 464 | instrumentation->ExceptionHandledEvent(self, exception.Get()); |
| 465 | if (UNLIKELY(self->IsExceptionPending())) { |
| 466 | // Exception handled event threw an exception. Try to find the handler for this one. |
| 467 | return MoveToExceptionHandler(self, shadow_frame, instrumentation); |
| 468 | } else if (!clear_exception) { |
| 469 | self->SetException(exception.Get()); |
| 470 | } |
| 471 | } else if (clear_exception) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 472 | self->ClearException(); |
| 473 | } |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 474 | return true; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 475 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 478 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 479 | LOG(FATAL) << "Unexpected instruction: " |
| 480 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 481 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 484 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 485 | va_list args; |
| 486 | va_start(args, fmt); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 487 | AbortTransactionV(self, fmt, args); |
| 488 | va_end(args); |
| 489 | } |
| 490 | |
| 491 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 492 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 493 | // Constructs abort message. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 494 | std::string abort_msg; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 495 | android::base::StringAppendV(&abort_msg, fmt, args); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 496 | // Throws an exception so we can abort the transaction and rollback every change. |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 497 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 500 | // START DECLARATIONS : |
| 501 | // |
| 502 | // These additional declarations are required because clang complains |
| 503 | // about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions. |
| 504 | // |
| 505 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 506 | template <bool is_range, bool do_assignability_check> |
Vladimir Marko | d16363a | 2017-02-01 14:09:13 +0000 | [diff] [blame] | 507 | static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method, |
| 508 | Thread* self, |
| 509 | ShadowFrame& shadow_frame, |
| 510 | JValue* result, |
| 511 | uint16_t number_of_inputs, |
| 512 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 513 | uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 514 | |
| 515 | template <bool is_range> |
Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 516 | ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame, |
| 517 | ShadowFrame* callee_frame, |
| 518 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 519 | const size_t first_src_reg, |
| 520 | const size_t first_dest_reg, |
| 521 | const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 522 | |
| 523 | // END DECLARATIONS. |
| 524 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 525 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 526 | ArtMethod* caller, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 527 | ShadowFrame* shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 528 | uint16_t arg_offset, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 529 | JValue* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 530 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 531 | ArtMethod* method = shadow_frame->GetMethod(); |
| 532 | // Ensure static methods are initialized. |
| 533 | if (method->IsStatic()) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 534 | ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass(); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 535 | if (UNLIKELY(!declaringClass->IsInitialized())) { |
| 536 | self->PushShadowFrame(shadow_frame); |
| 537 | StackHandleScope<1> hs(self); |
| 538 | Handle<mirror::Class> h_class(hs.NewHandle(declaringClass)); |
Nicolas Geoffray | 0182229 | 2017-03-09 09:03:19 +0000 | [diff] [blame] | 539 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, |
| 540 | true))) { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 541 | self->PopShadowFrame(); |
| 542 | DCHECK(self->IsExceptionPending()); |
| 543 | return; |
| 544 | } |
| 545 | self->PopShadowFrame(); |
| 546 | CHECK(h_class->IsInitializing()); |
Nicolas Geoffray | 0182229 | 2017-03-09 09:03:19 +0000 | [diff] [blame] | 547 | // Reload from shadow frame in case the method moved, this is faster than adding a handle. |
| 548 | method = shadow_frame->GetMethod(); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 551 | // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise, |
| 552 | // check that the arg_offset isn't greater than the number of registers. A stronger check is |
| 553 | // difficult since the frame may contain space for all the registers in the method, or only enough |
| 554 | // space for the arguments. |
| 555 | if (kIsDebugBuild) { |
| 556 | if (method->GetCodeItem() == nullptr) { |
| 557 | DCHECK_EQ(0u, arg_offset) << method->PrettyMethod(); |
| 558 | } else { |
| 559 | DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs()); |
| 560 | } |
| 561 | } |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 562 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 563 | if (jit != nullptr && caller != nullptr) { |
| 564 | jit->NotifyInterpreterToCompiledCodeTransition(self, caller); |
| 565 | } |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 566 | method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset), |
| 567 | (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 568 | result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 571 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 572 | uint16_t this_obj_vreg, |
| 573 | JValue result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 574 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 575 | ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg); |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 576 | if (existing == nullptr) { |
| 577 | // If it's null, we come from compiled code that was deoptimized. Nothing to do, |
| 578 | // as the compiler verified there was no alias. |
| 579 | // Set the new string result of the StringFactory. |
| 580 | shadow_frame->SetVRegReference(this_obj_vreg, result.GetL()); |
| 581 | return; |
| 582 | } |
| 583 | // Set the string init result into all aliases. |
| 584 | for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) { |
| 585 | if (shadow_frame->GetVRegReference(i) == existing) { |
| 586 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 587 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 588 | shadow_frame->SetVRegReference(i, result.GetL()); |
| 589 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 590 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 595 | template<bool is_range> |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 596 | static bool DoMethodHandleInvokeCommon(Thread* self, |
| 597 | ShadowFrame& shadow_frame, |
| 598 | bool invoke_exact, |
| 599 | const Instruction* inst, |
| 600 | uint16_t inst_data, |
| 601 | JValue* result) |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 602 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 603 | // Make sure to check for async exceptions |
| 604 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 605 | return false; |
| 606 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 607 | // Invoke-polymorphic instructions always take a receiver. i.e, they are never static. |
| 608 | const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc(); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 609 | const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc(); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 610 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 611 | // Initialize |result| to 0 as this is the default return value for |
| 612 | // polymorphic invocations of method handle types with void return |
| 613 | // and provides sane return result in error cases. |
| 614 | result->SetJ(0); |
| 615 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 616 | // The invoke_method_idx here is the name of the signature polymorphic method that |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 617 | // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact) |
| 618 | // and not the method that we'll dispatch to in the end. |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 619 | StackHandleScope<2> hs(self); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 620 | Handle<mirror::MethodHandle> method_handle(hs.NewHandle( |
| 621 | ObjPtr<mirror::MethodHandle>::DownCast( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 622 | MakeObjPtr(shadow_frame.GetVRegReference(vRegC))))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 623 | if (UNLIKELY(method_handle == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 624 | // Note that the invoke type is kVirtual here because a call to a signature |
| 625 | // polymorphic method is shaped like a virtual call at the bytecode level. |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 626 | ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
| 630 | // The vRegH value gives the index of the proto_id associated with this |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 631 | // signature polymorphic call site. |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 632 | const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 633 | const dex::ProtoIndex callsite_proto_id(vRegH); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 634 | |
| 635 | // Call through to the classlinker and ask it to resolve the static type associated |
| 636 | // with the callsite. This information is stored in the dex cache so it's |
| 637 | // guaranteed to be fast after the first resolution. |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 638 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 639 | Handle<mirror::MethodType> callsite_type(hs.NewHandle( |
| 640 | class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod()))); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 641 | |
| 642 | // This implies we couldn't resolve one or more types in this method handle. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 643 | if (UNLIKELY(callsite_type == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 644 | CHECK(self->IsExceptionPending()); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 648 | // There is a common dispatch method for method handles that takes |
| 649 | // arguments either from a range or an array of arguments depending |
| 650 | // on whether the DEX instruction is invoke-polymorphic/range or |
| 651 | // invoke-polymorphic. The array here is for the latter. |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 652 | if (UNLIKELY(is_range)) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 653 | // VRegC is the register holding the method handle. Arguments passed |
| 654 | // to the method handle's target do not include the method handle. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 655 | RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 656 | if (invoke_exact) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 657 | return MethodHandleInvokeExact(self, |
| 658 | shadow_frame, |
| 659 | method_handle, |
| 660 | callsite_type, |
| 661 | &operands, |
| 662 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 663 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 664 | return MethodHandleInvoke(self, |
| 665 | shadow_frame, |
| 666 | method_handle, |
| 667 | callsite_type, |
| 668 | &operands, |
| 669 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 670 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 671 | } else { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 672 | // Get the register arguments for the invoke. |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 673 | uint32_t args[Instruction::kMaxVarArgRegs] = {}; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 674 | inst->GetVarArgs(args, inst_data); |
| 675 | // Drop the first register which is the method handle performing the invoke. |
Alexey Frunze | 8631a46 | 2017-01-19 19:07:37 -0800 | [diff] [blame] | 676 | memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1)); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 677 | args[Instruction::kMaxVarArgRegs - 1] = 0; |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 678 | VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 679 | if (invoke_exact) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 680 | return MethodHandleInvokeExact(self, |
| 681 | shadow_frame, |
| 682 | method_handle, |
| 683 | callsite_type, |
| 684 | &operands, |
| 685 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 686 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 687 | return MethodHandleInvoke(self, |
| 688 | shadow_frame, |
| 689 | method_handle, |
| 690 | callsite_type, |
| 691 | &operands, |
| 692 | result); |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | bool DoMethodHandleInvokeExact(Thread* self, |
| 698 | ShadowFrame& shadow_frame, |
| 699 | const Instruction* inst, |
| 700 | uint16_t inst_data, |
| 701 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 702 | if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) { |
| 703 | static const bool kIsRange = false; |
| 704 | return DoMethodHandleInvokeCommon<kIsRange>( |
| 705 | self, shadow_frame, true /* is_exact */, inst, inst_data, result); |
| 706 | } else { |
| 707 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 708 | static const bool kIsRange = true; |
| 709 | return DoMethodHandleInvokeCommon<kIsRange>( |
| 710 | self, shadow_frame, true /* is_exact */, inst, inst_data, result); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | bool DoMethodHandleInvoke(Thread* self, |
| 715 | ShadowFrame& shadow_frame, |
| 716 | const Instruction* inst, |
| 717 | uint16_t inst_data, |
| 718 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 719 | if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) { |
| 720 | static const bool kIsRange = false; |
| 721 | return DoMethodHandleInvokeCommon<kIsRange>( |
| 722 | self, shadow_frame, false /* is_exact */, inst, inst_data, result); |
| 723 | } else { |
| 724 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 725 | static const bool kIsRange = true; |
| 726 | return DoMethodHandleInvokeCommon<kIsRange>( |
| 727 | self, shadow_frame, false /* is_exact */, inst, inst_data, result); |
| 728 | } |
| 729 | } |
| 730 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 731 | static bool DoVarHandleInvokeCommon(Thread* self, |
| 732 | ShadowFrame& shadow_frame, |
| 733 | const Instruction* inst, |
| 734 | uint16_t inst_data, |
| 735 | JValue* result, |
| 736 | mirror::VarHandle::AccessMode access_mode) |
| 737 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 738 | // Make sure to check for async exceptions |
| 739 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 740 | return false; |
| 741 | } |
| 742 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 743 | StackHandleScope<2> hs(self); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 744 | bool is_var_args = inst->HasVarArgs(); |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 745 | const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc(); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 746 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 747 | Handle<mirror::MethodType> callsite_type(hs.NewHandle( |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 748 | class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod()))); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 749 | // This implies we couldn't resolve one or more types in this VarHandle. |
| 750 | if (UNLIKELY(callsite_type == nullptr)) { |
| 751 | CHECK(self->IsExceptionPending()); |
| 752 | return false; |
| 753 | } |
| 754 | |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 755 | const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc(); |
| 756 | ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC)); |
| 757 | Handle<mirror::VarHandle> var_handle(hs.NewHandle(down_cast<mirror::VarHandle*>(receiver.Ptr()))); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 758 | if (is_var_args) { |
| 759 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 760 | inst->GetVarArgs(args, inst_data); |
| 761 | VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc()); |
| 762 | NoReceiverInstructionOperands operands(&all_operands); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 763 | return VarHandleInvokeAccessor(self, |
| 764 | shadow_frame, |
| 765 | var_handle, |
| 766 | callsite_type, |
| 767 | access_mode, |
| 768 | &operands, |
| 769 | result); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 770 | } else { |
| 771 | RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc()); |
| 772 | NoReceiverInstructionOperands operands(&all_operands); |
Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 773 | return VarHandleInvokeAccessor(self, |
| 774 | shadow_frame, |
| 775 | var_handle, |
| 776 | callsite_type, |
| 777 | access_mode, |
| 778 | &operands, |
| 779 | result); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 780 | } |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 783 | #define DO_VAR_HANDLE_ACCESSOR(_access_mode) \ |
| 784 | bool DoVarHandle ## _access_mode(Thread* self, \ |
| 785 | ShadowFrame& shadow_frame, \ |
| 786 | const Instruction* inst, \ |
| 787 | uint16_t inst_data, \ |
| 788 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \ |
| 789 | const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \ |
| 790 | return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \ |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 793 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchange) |
| 794 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire) |
| 795 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease) |
| 796 | DO_VAR_HANDLE_ACCESSOR(CompareAndSet) |
| 797 | DO_VAR_HANDLE_ACCESSOR(Get) |
| 798 | DO_VAR_HANDLE_ACCESSOR(GetAcquire) |
| 799 | DO_VAR_HANDLE_ACCESSOR(GetAndAdd) |
| 800 | DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire) |
| 801 | DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease) |
| 802 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd) |
| 803 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire) |
| 804 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease) |
| 805 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr) |
| 806 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire) |
| 807 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease) |
| 808 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor) |
| 809 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire) |
| 810 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease) |
| 811 | DO_VAR_HANDLE_ACCESSOR(GetAndSet) |
| 812 | DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire) |
| 813 | DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease) |
| 814 | DO_VAR_HANDLE_ACCESSOR(GetOpaque) |
| 815 | DO_VAR_HANDLE_ACCESSOR(GetVolatile) |
| 816 | DO_VAR_HANDLE_ACCESSOR(Set) |
| 817 | DO_VAR_HANDLE_ACCESSOR(SetOpaque) |
| 818 | DO_VAR_HANDLE_ACCESSOR(SetRelease) |
| 819 | DO_VAR_HANDLE_ACCESSOR(SetVolatile) |
| 820 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet) |
| 821 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire) |
| 822 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain) |
| 823 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease) |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 824 | |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 825 | #undef DO_VAR_HANDLE_ACCESSOR |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 826 | |
| 827 | template<bool is_range> |
| 828 | bool DoInvokePolymorphic(Thread* self, |
| 829 | ShadowFrame& shadow_frame, |
| 830 | const Instruction* inst, |
| 831 | uint16_t inst_data, |
| 832 | JValue* result) { |
| 833 | const int invoke_method_idx = inst->VRegB(); |
| 834 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 835 | ArtMethod* invoke_method = |
| 836 | class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
| 837 | self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual); |
| 838 | |
| 839 | // Ensure intrinsic identifiers are initialized. |
| 840 | DCHECK(invoke_method->IsIntrinsic()); |
| 841 | |
| 842 | // Dispatch based on intrinsic identifier associated with method. |
| 843 | switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) { |
| 844 | #define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \ |
| 845 | case Intrinsics::k##Name: \ |
| 846 | return Do ## Name(self, shadow_frame, inst, inst_data, result); |
| 847 | #include "intrinsics_list.h" |
| 848 | SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC) |
| 849 | #undef INTRINSICS_LIST |
| 850 | #undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST |
| 851 | #undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC |
| 852 | default: |
| 853 | LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic(); |
| 854 | UNREACHABLE(); |
| 855 | return false; |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 859 | static JValue ConvertScalarBootstrapArgument(jvalue value) { |
| 860 | // value either contains a primitive scalar value if it corresponds |
| 861 | // to a primitive type, or it contains an integer value if it |
| 862 | // corresponds to an object instance reference id (e.g. a string id). |
| 863 | return JValue::FromPrimitive(value.j); |
| 864 | } |
| 865 | |
| 866 | static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type) |
| 867 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 868 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 869 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 870 | switch (type) { |
| 871 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 872 | case EncodedArrayValueIterator::ValueType::kByte: |
| 873 | case EncodedArrayValueIterator::ValueType::kChar: |
| 874 | case EncodedArrayValueIterator::ValueType::kShort: |
| 875 | // These types are disallowed by JVMS. Treat as integers. This |
| 876 | // will result in CCE's being raised if the BSM has one of these |
| 877 | // types. |
| 878 | case EncodedArrayValueIterator::ValueType::kInt: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 879 | return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 880 | case EncodedArrayValueIterator::ValueType::kLong: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 881 | return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 882 | case EncodedArrayValueIterator::ValueType::kFloat: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 883 | return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 884 | case EncodedArrayValueIterator::ValueType::kDouble: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 885 | return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 886 | case EncodedArrayValueIterator::ValueType::kMethodType: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 887 | return GetClassRoot<mirror::MethodType>(class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 888 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 889 | return GetClassRoot<mirror::MethodHandle>(class_roots); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 890 | case EncodedArrayValueIterator::ValueType::kString: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 891 | return GetClassRoot<mirror::String>(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 892 | case EncodedArrayValueIterator::ValueType::kType: |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 893 | return GetClassRoot<mirror::Class>(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 894 | case EncodedArrayValueIterator::ValueType::kField: |
| 895 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 896 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 897 | case EncodedArrayValueIterator::ValueType::kArray: |
| 898 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 899 | case EncodedArrayValueIterator::ValueType::kNull: |
| 900 | return nullptr; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static bool GetArgumentForBootstrapMethod(Thread* self, |
| 905 | ArtMethod* referrer, |
| 906 | EncodedArrayValueIterator::ValueType type, |
| 907 | const JValue* encoded_value, |
| 908 | JValue* decoded_value) |
| 909 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 910 | // The encoded_value contains either a scalar value (IJDF) or a |
| 911 | // scalar DEX file index to a reference type to be materialized. |
| 912 | switch (type) { |
| 913 | case EncodedArrayValueIterator::ValueType::kInt: |
| 914 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 915 | decoded_value->SetI(encoded_value->GetI()); |
| 916 | return true; |
| 917 | case EncodedArrayValueIterator::ValueType::kLong: |
| 918 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 919 | decoded_value->SetJ(encoded_value->GetJ()); |
| 920 | return true; |
| 921 | case EncodedArrayValueIterator::ValueType::kMethodType: { |
| 922 | StackHandleScope<2> hs(self); |
| 923 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 924 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 925 | dex::ProtoIndex proto_idx(encoded_value->GetC()); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 926 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 927 | ObjPtr<mirror::MethodType> o = |
| 928 | cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 929 | if (UNLIKELY(o.IsNull())) { |
| 930 | DCHECK(self->IsExceptionPending()); |
| 931 | return false; |
| 932 | } |
| 933 | decoded_value->SetL(o); |
| 934 | return true; |
| 935 | } |
| 936 | case EncodedArrayValueIterator::ValueType::kMethodHandle: { |
| 937 | uint32_t index = static_cast<uint32_t>(encoded_value->GetI()); |
| 938 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 939 | ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer); |
| 940 | if (UNLIKELY(o.IsNull())) { |
| 941 | DCHECK(self->IsExceptionPending()); |
| 942 | return false; |
| 943 | } |
| 944 | decoded_value->SetL(o); |
| 945 | return true; |
| 946 | } |
| 947 | case EncodedArrayValueIterator::ValueType::kString: { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 948 | dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI())); |
| 949 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame^] | 950 | ObjPtr<mirror::String> o = cl->ResolveString(index, referrer); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 951 | if (UNLIKELY(o.IsNull())) { |
| 952 | DCHECK(self->IsExceptionPending()); |
| 953 | return false; |
| 954 | } |
| 955 | decoded_value->SetL(o); |
| 956 | return true; |
| 957 | } |
| 958 | case EncodedArrayValueIterator::ValueType::kType: { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 959 | dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI())); |
| 960 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 961 | ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 962 | if (UNLIKELY(o.IsNull())) { |
| 963 | DCHECK(self->IsExceptionPending()); |
| 964 | return false; |
| 965 | } |
| 966 | decoded_value->SetL(o); |
| 967 | return true; |
| 968 | } |
| 969 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 970 | case EncodedArrayValueIterator::ValueType::kByte: |
| 971 | case EncodedArrayValueIterator::ValueType::kChar: |
| 972 | case EncodedArrayValueIterator::ValueType::kShort: |
| 973 | case EncodedArrayValueIterator::ValueType::kField: |
| 974 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 975 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 976 | case EncodedArrayValueIterator::ValueType::kArray: |
| 977 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 978 | case EncodedArrayValueIterator::ValueType::kNull: |
| 979 | // Unreachable - unsupported types that have been checked when |
| 980 | // determining the effect call site type based on the bootstrap |
| 981 | // argument types. |
| 982 | UNREACHABLE(); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | static bool PackArgumentForBootstrapMethod(Thread* self, |
| 987 | ArtMethod* referrer, |
| 988 | CallSiteArrayValueIterator* it, |
| 989 | ShadowFrameSetter* setter) |
| 990 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 991 | auto type = it->GetValueType(); |
| 992 | const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue()); |
| 993 | JValue decoded_value; |
| 994 | if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) { |
| 995 | return false; |
| 996 | } |
| 997 | switch (it->GetValueType()) { |
| 998 | case EncodedArrayValueIterator::ValueType::kInt: |
| 999 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 1000 | setter->Set(static_cast<uint32_t>(decoded_value.GetI())); |
| 1001 | return true; |
| 1002 | case EncodedArrayValueIterator::ValueType::kLong: |
| 1003 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 1004 | setter->SetLong(decoded_value.GetJ()); |
| 1005 | return true; |
| 1006 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| 1007 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| 1008 | case EncodedArrayValueIterator::ValueType::kString: |
| 1009 | case EncodedArrayValueIterator::ValueType::kType: |
| 1010 | setter->SetReference(decoded_value.GetL()); |
| 1011 | return true; |
| 1012 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 1013 | case EncodedArrayValueIterator::ValueType::kByte: |
| 1014 | case EncodedArrayValueIterator::ValueType::kChar: |
| 1015 | case EncodedArrayValueIterator::ValueType::kShort: |
| 1016 | case EncodedArrayValueIterator::ValueType::kField: |
| 1017 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 1018 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 1019 | case EncodedArrayValueIterator::ValueType::kArray: |
| 1020 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 1021 | case EncodedArrayValueIterator::ValueType::kNull: |
| 1022 | // Unreachable - unsupported types that have been checked when |
| 1023 | // determining the effect call site type based on the bootstrap |
| 1024 | // argument types. |
| 1025 | UNREACHABLE(); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | static bool PackCollectorArrayForBootstrapMethod(Thread* self, |
| 1030 | ArtMethod* referrer, |
| 1031 | ObjPtr<mirror::Class> array_type, |
| 1032 | int32_t array_length, |
| 1033 | CallSiteArrayValueIterator* it, |
| 1034 | ShadowFrameSetter* setter) |
| 1035 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1036 | StackHandleScope<1> hs(self); |
| 1037 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1038 | JValue decoded_value; |
| 1039 | |
| 1040 | #define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \ |
| 1041 | Handle<mirror::Type ## Array> array = \ |
| 1042 | hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \ |
| 1043 | if (array.IsNull()) { \ |
| 1044 | return false; \ |
| 1045 | } \ |
| 1046 | for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \ |
| 1047 | auto type = it->GetValueType(); \ |
| 1048 | DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \ |
| 1049 | const JValue encoded_value = \ |
| 1050 | ConvertScalarBootstrapArgument(it->GetJavaValue()); \ |
| 1051 | GetArgumentForBootstrapMethod(self, \ |
| 1052 | referrer, \ |
| 1053 | type, \ |
| 1054 | &encoded_value, \ |
| 1055 | &decoded_value); \ |
| 1056 | array->Set(i, decoded_value.Get ## Descriptor()); \ |
| 1057 | } \ |
| 1058 | setter->SetReference(array.Get()); \ |
| 1059 | return true; |
| 1060 | |
| 1061 | #define COLLECT_REFERENCE_ARRAY(T, Type) \ |
| 1062 | Handle<mirror::ObjectArray<T>> array = \ |
| 1063 | hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \ |
| 1064 | array_type, \ |
| 1065 | array_length)); \ |
| 1066 | if (array.IsNull()) { \ |
| 1067 | return false; \ |
| 1068 | } \ |
| 1069 | for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \ |
| 1070 | auto type = it->GetValueType(); \ |
| 1071 | DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \ |
| 1072 | const JValue encoded_value = \ |
| 1073 | ConvertScalarBootstrapArgument(it->GetJavaValue()); \ |
| 1074 | if (!GetArgumentForBootstrapMethod(self, \ |
| 1075 | referrer, \ |
| 1076 | type, \ |
| 1077 | &encoded_value, \ |
| 1078 | &decoded_value)) { \ |
| 1079 | return false; \ |
| 1080 | } \ |
| 1081 | ObjPtr<mirror::Object> o = decoded_value.GetL(); \ |
| 1082 | if (Runtime::Current()->IsActiveTransaction()) { \ |
| 1083 | array->Set<true>(i, ObjPtr<T>::DownCast(o)); \ |
| 1084 | } else { \ |
| 1085 | array->Set<false>(i, ObjPtr<T>::DownCast(o)); \ |
| 1086 | } \ |
| 1087 | } \ |
| 1088 | setter->SetReference(array.Get()); \ |
| 1089 | return true; |
| 1090 | |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1091 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots(); |
| 1092 | ObjPtr<mirror::Class> component_type = array_type->GetComponentType(); |
| 1093 | if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1094 | COLLECT_PRIMITIVE_ARRAY(I, Int); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1095 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1096 | COLLECT_PRIMITIVE_ARRAY(J, Long); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1097 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1098 | COLLECT_PRIMITIVE_ARRAY(F, Float); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1099 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1100 | COLLECT_PRIMITIVE_ARRAY(D, Double); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1101 | } else if (component_type == GetClassRoot<mirror::MethodType>()) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1102 | COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1103 | } else if (component_type == GetClassRoot<mirror::MethodHandle>()) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1104 | COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1105 | } else if (component_type == GetClassRoot<mirror::String>(class_roots)) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1106 | COLLECT_REFERENCE_ARRAY(mirror::String, String); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1107 | } else if (component_type == GetClassRoot<mirror::Class>()) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1108 | COLLECT_REFERENCE_ARRAY(mirror::Class, Type); |
| 1109 | } else { |
| 1110 | UNREACHABLE(); |
| 1111 | } |
| 1112 | #undef COLLECT_PRIMITIVE_ARRAY |
| 1113 | #undef COLLECT_REFERENCE_ARRAY |
| 1114 | } |
| 1115 | |
| 1116 | static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self, |
| 1117 | const DexFile* dex_file, |
| 1118 | uint32_t call_site_idx) |
| 1119 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1120 | const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx); |
| 1121 | CallSiteArrayValueIterator it(*dex_file, csi); |
| 1122 | DCHECK_GE(it.Size(), 1u); |
| 1123 | |
| 1124 | StackHandleScope<2> hs(self); |
| 1125 | // Create array for parameter types. |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1126 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 1127 | ObjPtr<mirror::Class> class_array_type = |
| 1128 | GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1129 | Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle( |
| 1130 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 1131 | class_array_type, |
| 1132 | static_cast<int>(it.Size()))); |
| 1133 | if (ptypes.IsNull()) { |
| 1134 | DCHECK(self->IsExceptionPending()); |
| 1135 | return nullptr; |
| 1136 | } |
| 1137 | |
| 1138 | // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup |
| 1139 | // that the runtime will construct. |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1140 | ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker)); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1141 | it.Next(); |
| 1142 | |
| 1143 | // The remaining parameter types are derived from the types of |
| 1144 | // arguments present in the DEX file. |
| 1145 | int index = 1; |
| 1146 | while (it.HasNext()) { |
| 1147 | ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType()); |
| 1148 | if (ptype.IsNull()) { |
| 1149 | ThrowClassCastException("Unsupported bootstrap argument type"); |
| 1150 | return nullptr; |
| 1151 | } |
| 1152 | ptypes->Set(index, ptype); |
| 1153 | index++; |
| 1154 | it.Next(); |
| 1155 | } |
| 1156 | DCHECK_EQ(static_cast<size_t>(index), it.Size()); |
| 1157 | |
| 1158 | // By definition, the return type is always a j.l.i.CallSite. |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1159 | Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>()); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1160 | return mirror::MethodType::Create(self, rtype, ptypes); |
| 1161 | } |
| 1162 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1163 | static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self, |
| 1164 | ShadowFrame& shadow_frame, |
| 1165 | uint32_t call_site_idx) |
| 1166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1167 | StackHandleScope<7> hs(self); |
| 1168 | // There are three mandatory arguments expected from the call site |
| 1169 | // value array in the DEX file: the bootstrap method handle, the |
| 1170 | // method name to pass to the bootstrap method, and the method type |
| 1171 | // to pass to the bootstrap method. |
| 1172 | static constexpr size_t kMandatoryArgumentsCount = 3; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1173 | ArtMethod* referrer = shadow_frame.GetMethod(); |
| 1174 | const DexFile* dex_file = referrer->GetDexFile(); |
| 1175 | const DexFile::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1176 | CallSiteArrayValueIterator it(*dex_file, csi); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1177 | if (it.Size() < kMandatoryArgumentsCount) { |
| 1178 | ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)", |
| 1179 | it.Size(), kMandatoryArgumentsCount); |
| 1180 | return nullptr; |
| 1181 | } |
| 1182 | |
| 1183 | if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) { |
| 1184 | ThrowBootstrapMethodError("First bootstrap argument is not a method handle"); |
| 1185 | return nullptr; |
| 1186 | } |
| 1187 | |
| 1188 | uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i); |
| 1189 | it.Next(); |
| 1190 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1191 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1192 | Handle<mirror::MethodHandle> bsm = |
| 1193 | hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer)); |
| 1194 | if (bsm.IsNull()) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1195 | DCHECK(self->IsExceptionPending()); |
| 1196 | return nullptr; |
| 1197 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1198 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1199 | if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) { |
| 1200 | // JLS suggests also accepting constructors. This is currently |
| 1201 | // hard as constructor invocations happen via transformers in ART |
| 1202 | // today. The constructor would need to be a class derived from java.lang.invoke.CallSite. |
| 1203 | ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind"); |
| 1204 | return nullptr; |
| 1205 | } |
| 1206 | |
| 1207 | // Construct the local call site type information based on the 3 |
| 1208 | // mandatory arguments provided by the runtime and the static arguments |
| 1209 | // in the DEX file. We will use these arguments to build a shadow frame. |
| 1210 | MutableHandle<mirror::MethodType> call_site_type = |
| 1211 | hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx)); |
| 1212 | if (call_site_type.IsNull()) { |
| 1213 | DCHECK(self->IsExceptionPending()); |
| 1214 | return nullptr; |
| 1215 | } |
| 1216 | |
| 1217 | // Check if this BSM is targeting a variable arity method. If so, |
| 1218 | // we'll need to collect the trailing arguments into an array. |
| 1219 | Handle<mirror::Array> collector_arguments; |
| 1220 | int32_t collector_arguments_length; |
| 1221 | if (bsm->GetTargetMethod()->IsVarargs()) { |
| 1222 | int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes(); |
| 1223 | if (number_of_bsm_parameters == 0) { |
| 1224 | ThrowBootstrapMethodError("Variable arity BSM does not have any arguments"); |
| 1225 | return nullptr; |
| 1226 | } |
| 1227 | Handle<mirror::Class> collector_array_class = |
| 1228 | hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1)); |
| 1229 | if (!collector_array_class->IsArrayClass()) { |
| 1230 | ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument"); |
| 1231 | return nullptr; |
| 1232 | } |
| 1233 | // The call site may include no arguments to be collected. In this |
| 1234 | // case the number of arguments must be at least the number of BSM |
| 1235 | // parameters less the collector array. |
| 1236 | if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) { |
| 1237 | ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get()); |
| 1238 | return nullptr; |
| 1239 | } |
| 1240 | // Check all the arguments to be collected match the collector array component type. |
| 1241 | for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) { |
| 1242 | if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) { |
| 1243 | ThrowClassCastException(collector_array_class->GetComponentType(), |
| 1244 | call_site_type->GetPTypes()->Get(i)); |
| 1245 | return nullptr; |
| 1246 | } |
| 1247 | } |
| 1248 | // Update the call site method type so it now includes the collector array. |
| 1249 | int32_t collector_arguments_start = number_of_bsm_parameters - 1; |
| 1250 | collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1; |
| 1251 | call_site_type.Assign( |
| 1252 | mirror::MethodType::CollectTrailingArguments(self, |
| 1253 | call_site_type.Get(), |
| 1254 | collector_array_class.Get(), |
| 1255 | collector_arguments_start)); |
| 1256 | if (call_site_type.IsNull()) { |
| 1257 | DCHECK(self->IsExceptionPending()); |
| 1258 | return nullptr; |
| 1259 | } |
| 1260 | } else { |
| 1261 | collector_arguments_length = 0; |
| 1262 | } |
| 1263 | |
| 1264 | if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) { |
| 1265 | ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get()); |
| 1266 | return nullptr; |
| 1267 | } |
| 1268 | |
| 1269 | // BSM invocation has a different set of exceptions that |
| 1270 | // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE |
| 1271 | // "opportunities". Unfortunately we cannot just leave this to the |
| 1272 | // method handle invocation as this might generate a WMTE. |
| 1273 | for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) { |
| 1274 | ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i); |
| 1275 | ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i); |
| 1276 | if (!IsParameterTypeConvertible(from, to)) { |
| 1277 | ThrowClassCastException(from, to); |
| 1278 | return nullptr; |
| 1279 | } |
| 1280 | } |
| 1281 | if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) { |
| 1282 | ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType()); |
| 1283 | return nullptr; |
| 1284 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Set-up a shadow frame for invoking the bootstrap method handle. |
| 1287 | ShadowFrameAllocaUniquePtr bootstrap_frame = |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1288 | CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(), |
| 1289 | nullptr, |
| 1290 | referrer, |
| 1291 | shadow_frame.GetDexPC()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1292 | ScopedStackedShadowFramePusher pusher( |
| 1293 | self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1294 | ShadowFrameSetter setter(bootstrap_frame.get(), 0u); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1295 | |
| 1296 | // The first parameter is a MethodHandles lookup instance. |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1297 | Handle<mirror::Class> lookup_class = |
| 1298 | hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()); |
| 1299 | ObjPtr<mirror::MethodHandlesLookup> lookup = |
| 1300 | mirror::MethodHandlesLookup::Create(self, lookup_class); |
| 1301 | if (lookup.IsNull()) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1302 | DCHECK(self->IsExceptionPending()); |
| 1303 | return nullptr; |
| 1304 | } |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1305 | setter.SetReference(lookup); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1306 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1307 | // Pack the remaining arguments into the frame. |
| 1308 | int number_of_arguments = call_site_type->GetNumberOfPTypes(); |
| 1309 | int argument_index; |
| 1310 | for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) { |
| 1311 | if (argument_index == number_of_arguments - 1 && |
| 1312 | call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) { |
| 1313 | ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index); |
| 1314 | if (!PackCollectorArrayForBootstrapMethod(self, |
| 1315 | referrer, |
| 1316 | array_type, |
| 1317 | collector_arguments_length, |
| 1318 | &it, |
| 1319 | &setter)) { |
| 1320 | DCHECK(self->IsExceptionPending()); |
| 1321 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1322 | } |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1323 | } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) { |
| 1324 | DCHECK(self->IsExceptionPending()); |
| 1325 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1326 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1327 | it.Next(); |
| 1328 | } |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1329 | DCHECK(!it.HasNext()); |
| 1330 | DCHECK(setter.Done()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Invoke the bootstrap method handle. |
| 1333 | JValue result; |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1334 | RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs()); |
| 1335 | bool invoke_success = MethodHandleInvoke(self, |
| 1336 | *bootstrap_frame, |
| 1337 | bsm, |
| 1338 | call_site_type, |
| 1339 | &operands, |
| 1340 | &result); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1341 | if (!invoke_success) { |
| 1342 | DCHECK(self->IsExceptionPending()); |
| 1343 | return nullptr; |
| 1344 | } |
| 1345 | |
| 1346 | Handle<mirror::Object> object(hs.NewHandle(result.GetL())); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1347 | if (UNLIKELY(object.IsNull())) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 1348 | // This will typically be for LambdaMetafactory which is not supported. |
Orion Hodson | 76e6adb | 2018-02-23 13:15:55 +0000 | [diff] [blame] | 1349 | ThrowClassCastException("Bootstrap method returned null"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1350 | return nullptr; |
| 1351 | } |
| 1352 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1353 | // Check the result type is a subclass of j.l.i.CallSite. |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1354 | ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker); |
| 1355 | if (UNLIKELY(!object->InstanceOf(call_site_class))) { |
| 1356 | ThrowClassCastException(object->GetClass(), call_site_class); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1357 | return nullptr; |
| 1358 | } |
| 1359 | |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1360 | // Check the call site target is not null as we're going to invoke it. |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1361 | Handle<mirror::CallSite> call_site = |
| 1362 | hs.NewHandle(ObjPtr<mirror::CallSite>::DownCast(ObjPtr<mirror::Object>(result.GetL()))); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1363 | Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget()); |
| 1364 | if (UNLIKELY(target.IsNull())) { |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1365 | ThrowClassCastException("Bootstrap method returned a CallSite with a null target"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1366 | return nullptr; |
| 1367 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1368 | return call_site.Get(); |
| 1369 | } |
| 1370 | |
| 1371 | template<bool is_range> |
| 1372 | bool DoInvokeCustom(Thread* self, |
| 1373 | ShadowFrame& shadow_frame, |
| 1374 | const Instruction* inst, |
| 1375 | uint16_t inst_data, |
| 1376 | JValue* result) |
| 1377 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 1378 | // Make sure to check for async exceptions |
| 1379 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 1380 | return false; |
| 1381 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1382 | // invoke-custom is not supported in transactions. In transactions |
| 1383 | // there is a limited set of types supported. invoke-custom allows |
| 1384 | // running arbitrary code and instantiating arbitrary types. |
| 1385 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
| 1386 | StackHandleScope<4> hs(self); |
| 1387 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache())); |
| 1388 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 1389 | MutableHandle<mirror::CallSite> |
| 1390 | call_site(hs.NewHandle(dex_cache->GetResolvedCallSite(call_site_idx))); |
| 1391 | if (call_site.IsNull()) { |
| 1392 | call_site.Assign(InvokeBootstrapMethod(self, shadow_frame, call_site_idx)); |
| 1393 | if (UNLIKELY(call_site.IsNull())) { |
| 1394 | CHECK(self->IsExceptionPending()); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1395 | if (!self->GetException()->IsError()) { |
| 1396 | // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error. |
| 1397 | ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method", |
| 1398 | call_site_idx); |
| 1399 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1400 | result->SetJ(0); |
| 1401 | return false; |
| 1402 | } |
| 1403 | mirror::CallSite* winning_call_site = |
| 1404 | dex_cache->SetResolvedCallSite(call_site_idx, call_site.Get()); |
| 1405 | call_site.Assign(winning_call_site); |
| 1406 | } |
| 1407 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1408 | Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget()); |
| 1409 | Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType()); |
| 1410 | DCHECK_EQ(static_cast<size_t>(inst->VRegA()), target_method_type->NumberOfVRegs()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1411 | if (is_range) { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1412 | RangeInstructionOperands operands(inst->VRegC_3rc(), inst->VRegA_3rc()); |
| 1413 | return MethodHandleInvokeExact(self, |
| 1414 | shadow_frame, |
| 1415 | target, |
| 1416 | target_method_type, |
| 1417 | &operands, |
| 1418 | result); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1419 | } else { |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1420 | uint32_t args[Instruction::kMaxVarArgRegs]; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1421 | inst->GetVarArgs(args, inst_data); |
Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 1422 | VarArgsInstructionOperands operands(args, inst->VRegA_35c()); |
| 1423 | return MethodHandleInvokeExact(self, |
| 1424 | shadow_frame, |
| 1425 | target, |
| 1426 | target_method_type, |
| 1427 | &operands, |
| 1428 | result); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1429 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 1432 | // Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. |
| 1433 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 1434 | size_t dest_reg, size_t src_reg) |
| 1435 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1436 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 1437 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
| 1438 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
| 1439 | |
| 1440 | // If both register locations contains the same value, the register probably holds a reference. |
| 1441 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 1442 | // in the references array even after the original vreg was overwritten to a non-reference. |
| 1443 | if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) { |
| 1444 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 1445 | } else { |
| 1446 | new_shadow_frame->SetVReg(dest_reg, src_value); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1450 | template <bool is_range> |
| 1451 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 1452 | ShadowFrame* callee_frame, |
| 1453 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 1454 | const size_t first_src_reg, |
| 1455 | const size_t first_dest_reg, |
| 1456 | const size_t num_regs) { |
| 1457 | if (is_range) { |
| 1458 | const size_t dest_reg_bound = first_dest_reg + num_regs; |
| 1459 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound; |
| 1460 | ++dest_reg, ++src_reg) { |
| 1461 | AssignRegister(callee_frame, caller_frame, dest_reg, src_reg); |
| 1462 | } |
| 1463 | } else { |
| 1464 | DCHECK_LE(num_regs, arraysize(arg)); |
| 1465 | |
| 1466 | for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) { |
| 1467 | AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]); |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1472 | template <bool is_range, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1473 | bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1474 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 1475 | Thread* self, |
| 1476 | ShadowFrame& shadow_frame, |
| 1477 | JValue* result, |
| 1478 | uint16_t number_of_inputs, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1479 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1480 | uint32_t vregC) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1481 | bool string_init = false; |
| 1482 | // Replace calls to String.<init> with equivalent StringFactory call. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1483 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 1484 | && called_method->IsConstructor())) { |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 1485 | called_method = WellKnownClasses::StringInitToStringFactory(called_method); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1486 | string_init = true; |
| 1487 | } |
| 1488 | |
Alex Light | daf58c8 | 2016-03-16 23:00:49 +0000 | [diff] [blame] | 1489 | // Compute method information. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1490 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1491 | // Number of registers for the callee's call frame. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1492 | uint16_t num_regs; |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1493 | // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to |
| 1494 | // PerformCall. A deoptimization could occur at any time, and we shouldn't change which |
| 1495 | // entrypoint to use once we start building the shadow frame. |
Mathieu Chartier | 448bbcf | 2017-07-06 12:05:13 -0700 | [diff] [blame] | 1496 | |
| 1497 | // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are |
| 1498 | // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image |
| 1499 | // pointer size here and this may case an overflow if it is called from the compiler. b/62402160 |
| 1500 | const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() || |
| 1501 | ClassLinker::ShouldUseInterpreterEntrypoint( |
| 1502 | called_method, |
| 1503 | called_method->GetEntryPointFromQuickCompiledCode()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1504 | if (LIKELY(accessor.HasCodeItem())) { |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1505 | // When transitioning to compiled code, space only needs to be reserved for the input registers. |
| 1506 | // The rest of the frame gets discarded. This also prevents accessing the called method's code |
| 1507 | // item, saving memory by keeping code items of compiled code untouched. |
Mathieu Chartier | 448bbcf | 2017-07-06 12:05:13 -0700 | [diff] [blame] | 1508 | if (!use_interpreter_entrypoint) { |
| 1509 | DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint"; |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1510 | num_regs = number_of_inputs; |
| 1511 | } else { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1512 | num_regs = accessor.RegistersSize(); |
| 1513 | DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize()); |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1514 | } |
Nicolas Geoffray | 0182229 | 2017-03-09 09:03:19 +0000 | [diff] [blame] | 1515 | } else { |
| 1516 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1517 | num_regs = number_of_inputs; |
Jeff Hao | df79ddb | 2017-02-27 14:47:06 -0800 | [diff] [blame] | 1518 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1519 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1520 | // Hack for String init: |
| 1521 | // |
| 1522 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 1523 | // invoke-x StringFactory(a, b, c, ...) |
| 1524 | // by effectively dropping the first virtual register from the invoke. |
| 1525 | // |
| 1526 | // (at this point the ArtMethod has already been replaced, |
| 1527 | // so we just need to fix-up the arguments) |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 1528 | // |
| 1529 | // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased |
| 1530 | // to handle the compiler optimization of replacing `this` with null without |
| 1531 | // throwing NullPointerException. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1532 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1533 | if (UNLIKELY(string_init)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1534 | DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1535 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1536 | // The new StringFactory call is static and has one fewer argument. |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1537 | if (!accessor.HasCodeItem()) { |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1538 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1539 | num_regs--; |
| 1540 | } // else ... don't need to change num_regs since it comes up from the string_init's code item |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1541 | number_of_inputs--; |
| 1542 | |
| 1543 | // Rewrite the var-args, dropping the 0th argument ("this") |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1544 | for (uint32_t i = 1; i < arraysize(arg); ++i) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1545 | arg[i - 1] = arg[i]; |
| 1546 | } |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1547 | arg[arraysize(arg) - 1] = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1548 | |
| 1549 | // Rewrite the non-var-arg case |
| 1550 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 1551 | } |
| 1552 | |
| 1553 | // Parameter registers go at the end of the shadow frame. |
| 1554 | DCHECK_GE(num_regs, number_of_inputs); |
| 1555 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 1556 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1557 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1558 | // Allocate shadow frame on the stack. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1559 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1560 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 1561 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1562 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1563 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1564 | // Initialize new shadow frame by copying the registers from the callee shadow frame. |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1565 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1566 | // Slow path. |
| 1567 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 1568 | // register the shadow frame as under construction and allow suspension again. |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 1569 | ScopedStackedShadowFramePusher pusher( |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 1570 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1571 | self->EndAssertNoThreadSuspension(old_cause); |
| 1572 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1573 | // ArtMethod here is needed to check type information of the call site against the callee. |
| 1574 | // Type information is retrieved from a DexFile/DexCache for that respective declared method. |
| 1575 | // |
| 1576 | // As a special case for proxy methods, which are not dex-backed, |
| 1577 | // we have to retrieve type information from the proxy's method |
| 1578 | // interface method instead (which is dex backed since proxies are never interfaces). |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1579 | ArtMethod* method = |
| 1580 | new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1581 | |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1582 | // We need to do runtime check on reference assignment. We need to load the shorty |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1583 | // to get the exact type of each reference argument. |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1584 | const DexFile::TypeList* params = method->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1585 | uint32_t shorty_len = 0; |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1586 | const char* shorty = method->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1587 | |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1588 | // Handle receiver apart since it's not part of the shorty. |
| 1589 | size_t dest_reg = first_dest_reg; |
| 1590 | size_t arg_offset = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1591 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1592 | if (!method->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1593 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1594 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 1595 | ++dest_reg; |
| 1596 | ++arg_offset; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1597 | DCHECK(!string_init); // All StringFactory methods are static. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1598 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1599 | |
| 1600 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1601 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1602 | // Skip the 0th 'shorty' type since it represents the return type. |
| 1603 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1604 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 1605 | switch (shorty[shorty_pos + 1]) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1606 | // Handle Object references. 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1607 | case 'L': { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1608 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1609 | if (do_assignability_check && o != nullptr) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1610 | const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_; |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1611 | ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1612 | if (arg_type == nullptr) { |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1613 | StackHandleScope<1> hs(self); |
| 1614 | // Preserve o since it is used below and GetClassFromTypeIndex may cause thread |
| 1615 | // suspension. |
| 1616 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 1617 | arg_type = method->ResolveClassFromTypeIndex(type_idx); |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1618 | if (arg_type == nullptr) { |
| 1619 | CHECK(self->IsExceptionPending()); |
| 1620 | return false; |
| 1621 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1622 | } |
| 1623 | if (!o->VerifierInstanceOf(arg_type)) { |
| 1624 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1625 | std::string temp1, temp2; |
Orion Hodson | fef0664 | 2016-11-25 16:07:11 +0000 | [diff] [blame] | 1626 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1627 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1628 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1629 | o->GetClass()->GetDescriptor(&temp1), |
| 1630 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1631 | return false; |
| 1632 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1633 | } |
Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 1634 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1635 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1636 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1637 | // Handle doubles and longs. 2 consecutive virtual register slots. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1638 | case 'J': case 'D': { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1639 | uint64_t wide_value = |
| 1640 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 1641 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1642 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1643 | // Skip the next virtual register slot since we already used it. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1644 | ++dest_reg; |
| 1645 | ++arg_offset; |
| 1646 | break; |
| 1647 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1648 | // Handle all other primitives that are always 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1649 | default: |
| 1650 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 1651 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1652 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1653 | } |
| 1654 | } else { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1655 | if (is_range) { |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1656 | DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1657 | } |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1658 | |
| 1659 | CopyRegisters<is_range>(shadow_frame, |
| 1660 | new_shadow_frame, |
| 1661 | arg, |
| 1662 | vregC, |
| 1663 | first_dest_reg, |
| 1664 | number_of_inputs); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1665 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1666 | } |
| 1667 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1668 | PerformCall(self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1669 | accessor, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1670 | shadow_frame.GetMethod(), |
| 1671 | first_dest_reg, |
| 1672 | new_shadow_frame, |
| 1673 | result, |
| 1674 | use_interpreter_entrypoint); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1675 | |
| 1676 | if (string_init && !self->IsExceptionPending()) { |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 1677 | SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1680 | return !self->IsExceptionPending(); |
| 1681 | } |
| 1682 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1683 | template<bool is_range, bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1684 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 1685 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 1686 | // Argument word count. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1687 | const uint16_t number_of_inputs = |
| 1688 | (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1689 | |
| 1690 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 1691 | // code. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1692 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1693 | uint32_t vregC = 0; |
| 1694 | if (is_range) { |
| 1695 | vregC = inst->VRegC_3rc(); |
| 1696 | } else { |
| 1697 | vregC = inst->VRegC_35c(); |
| 1698 | inst->GetVarArgs(arg, inst_data); |
| 1699 | } |
| 1700 | |
| 1701 | return DoCallCommon<is_range, do_assignability_check>( |
| 1702 | called_method, self, shadow_frame, |
| 1703 | result, number_of_inputs, arg, vregC); |
| 1704 | } |
| 1705 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1706 | template <bool is_range, bool do_access_check, bool transaction_active> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1707 | bool DoFilledNewArray(const Instruction* inst, |
| 1708 | const ShadowFrame& shadow_frame, |
| 1709 | Thread* self, |
| 1710 | JValue* result) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1711 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 1712 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 1713 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 1714 | if (!is_range) { |
| 1715 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 1716 | CHECK_LE(length, 5); |
| 1717 | } |
| 1718 | if (UNLIKELY(length < 0)) { |
| 1719 | ThrowNegativeArraySizeException(length); |
| 1720 | return false; |
| 1721 | } |
| 1722 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1723 | ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx), |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1724 | shadow_frame.GetMethod(), |
| 1725 | self, |
| 1726 | false, |
| 1727 | do_access_check); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1728 | if (UNLIKELY(array_class == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1729 | DCHECK(self->IsExceptionPending()); |
| 1730 | return false; |
| 1731 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1732 | CHECK(array_class->IsArrayClass()); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1733 | ObjPtr<mirror::Class> component_class = array_class->GetComponentType(); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1734 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 1735 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 1736 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1737 | ThrowRuntimeException("Bad filled array request for type %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1738 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1739 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1740 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 1741 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1742 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1743 | } |
| 1744 | return false; |
| 1745 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1746 | ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>( |
| 1747 | self, |
| 1748 | array_class, |
| 1749 | length, |
| 1750 | array_class->GetComponentSizeShift(), |
| 1751 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1752 | if (UNLIKELY(new_array == nullptr)) { |
| 1753 | self->AssertPendingOOMException(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1754 | return false; |
| 1755 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1756 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 1757 | uint32_t vregC = 0; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1758 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1759 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1760 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1761 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1762 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1763 | for (int32_t i = 0; i < length; ++i) { |
| 1764 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 1765 | if (is_primitive_int_component) { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1766 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 1767 | i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1768 | } else { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1769 | new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>( |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1770 | i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1774 | result->SetL(new_array); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1775 | return true; |
| 1776 | } |
| 1777 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1778 | // TODO: Use ObjPtr here. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1779 | template<typename T> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1780 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, |
| 1781 | int32_t count) |
| 1782 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1783 | Runtime* runtime = Runtime::Current(); |
| 1784 | for (int32_t i = 0; i < count; ++i) { |
| 1785 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 1786 | } |
| 1787 | } |
| 1788 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1789 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1790 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1791 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 1792 | DCHECK(array != nullptr); |
| 1793 | DCHECK_LE(count, array->GetLength()); |
| 1794 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 1795 | switch (primitive_component_type) { |
| 1796 | case Primitive::kPrimBoolean: |
| 1797 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 1798 | break; |
| 1799 | case Primitive::kPrimByte: |
| 1800 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 1801 | break; |
| 1802 | case Primitive::kPrimChar: |
| 1803 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 1804 | break; |
| 1805 | case Primitive::kPrimShort: |
| 1806 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 1807 | break; |
| 1808 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1809 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 1810 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1811 | case Primitive::kPrimFloat: |
| 1812 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 1813 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1814 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1815 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 1816 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1817 | case Primitive::kPrimDouble: |
| 1818 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 1819 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1820 | default: |
| 1821 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 1822 | << " in fill-array-data"; |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
| 1826 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1827 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1828 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1829 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1830 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 1831 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1832 | const Instruction* inst, uint16_t inst_data, \ |
| 1833 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1834 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 1835 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 1836 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 1837 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 1838 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1839 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1840 | // Explicit DoInvokePolymorphic template function declarations. |
| 1841 | #define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \ |
| 1842 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 1843 | bool DoInvokePolymorphic<_is_range>( \ |
| 1844 | Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1845 | uint16_t inst_data, JValue* result) |
| 1846 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false); |
| 1847 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1848 | #undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL |
| 1849 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 1850 | // Explicit DoInvokeCustom template function declarations. |
| 1851 | #define EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(_is_range) \ |
| 1852 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 1853 | bool DoInvokeCustom<_is_range>( \ |
| 1854 | Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1855 | uint16_t inst_data, JValue* result) |
| 1856 | EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(false); |
| 1857 | EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL(true); |
| 1858 | #undef EXPLICIT_DO_INVOKE_CUSTOM_TEMPLATE_DECL |
| 1859 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1860 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1861 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1862 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1863 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 1864 | const ShadowFrame& shadow_frame, \ |
| 1865 | Thread* self, JValue* result) |
| 1866 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 1867 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 1868 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 1869 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 1870 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 1871 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 1872 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 1873 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1874 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 1875 | |
| 1876 | } // namespace interpreter |
| 1877 | } // namespace art |