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