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