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