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" |
| 26 | #include "method_handles.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 27 | #include "method_handles-inl.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" |
| 30 | #include "mirror/method_handle_impl.h" |
| 31 | #include "reflection.h" |
| 32 | #include "reflection-inl.h" |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 33 | #include "stack.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 34 | #include "unstarted_runtime.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 35 | #include "verifier/method_verifier.h" |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 36 | #include "well_known_classes.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace interpreter { |
| 40 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 41 | void ThrowNullPointerExceptionFromInterpreter() { |
| 42 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 45 | template<Primitive::Type field_type> |
| 46 | static ALWAYS_INLINE void DoFieldGetCommon(Thread* self, |
| 47 | const ShadowFrame& shadow_frame, |
| 48 | ObjPtr<mirror::Object>& obj, |
| 49 | ArtField* field, |
| 50 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 51 | field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
| 52 | |
| 53 | // Report this field access to instrumentation if needed. |
| 54 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 55 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 56 | StackHandleScope<1> hs(self); |
| 57 | // Wrap in handle wrapper in case the listener does thread suspension. |
| 58 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
| 59 | ObjPtr<mirror::Object> this_object; |
| 60 | if (!field->IsStatic()) { |
| 61 | this_object = obj; |
| 62 | } |
| 63 | instrumentation->FieldReadEvent(self, |
| 64 | this_object.Ptr(), |
| 65 | shadow_frame.GetMethod(), |
| 66 | shadow_frame.GetDexPC(), |
| 67 | field); |
| 68 | } |
| 69 | |
| 70 | switch (field_type) { |
| 71 | case Primitive::kPrimBoolean: |
| 72 | result->SetZ(field->GetBoolean(obj)); |
| 73 | break; |
| 74 | case Primitive::kPrimByte: |
| 75 | result->SetB(field->GetByte(obj)); |
| 76 | break; |
| 77 | case Primitive::kPrimChar: |
| 78 | result->SetC(field->GetChar(obj)); |
| 79 | break; |
| 80 | case Primitive::kPrimShort: |
| 81 | result->SetS(field->GetShort(obj)); |
| 82 | break; |
| 83 | case Primitive::kPrimInt: |
| 84 | result->SetI(field->GetInt(obj)); |
| 85 | break; |
| 86 | case Primitive::kPrimLong: |
| 87 | result->SetJ(field->GetLong(obj)); |
| 88 | break; |
| 89 | case Primitive::kPrimNot: |
| 90 | result->SetL(field->GetObject(obj)); |
| 91 | break; |
| 92 | default: |
| 93 | LOG(FATAL) << "Unreachable: " << field_type; |
| 94 | UNREACHABLE(); |
| 95 | } |
| 96 | } |
| 97 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 98 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 99 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 100 | uint16_t inst_data) { |
| 101 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 102 | 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] | 103 | ArtField* f = |
| 104 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 105 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 106 | if (UNLIKELY(f == nullptr)) { |
| 107 | CHECK(self->IsExceptionPending()); |
| 108 | return false; |
| 109 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 110 | ObjPtr<mirror::Object> obj; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 111 | if (is_static) { |
| 112 | obj = f->GetDeclaringClass(); |
| 113 | } else { |
| 114 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 115 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 116 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | } |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 120 | |
| 121 | JValue result; |
| 122 | DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 123 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 124 | switch (field_type) { |
| 125 | case Primitive::kPrimBoolean: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 126 | shadow_frame.SetVReg(vregA, result.GetZ()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 127 | break; |
| 128 | case Primitive::kPrimByte: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 129 | shadow_frame.SetVReg(vregA, result.GetB()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 130 | break; |
| 131 | case Primitive::kPrimChar: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 132 | shadow_frame.SetVReg(vregA, result.GetC()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 133 | break; |
| 134 | case Primitive::kPrimShort: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 135 | shadow_frame.SetVReg(vregA, result.GetS()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 136 | break; |
| 137 | case Primitive::kPrimInt: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 138 | shadow_frame.SetVReg(vregA, result.GetI()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 139 | break; |
| 140 | case Primitive::kPrimLong: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 141 | shadow_frame.SetVRegLong(vregA, result.GetJ()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 142 | break; |
| 143 | case Primitive::kPrimNot: |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 144 | shadow_frame.SetVRegReference(vregA, result.GetL()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 145 | break; |
| 146 | default: |
| 147 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 148 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | // Explicitly instantiate all DoFieldGet functions. |
| 154 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 155 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 156 | ShadowFrame& shadow_frame, \ |
| 157 | const Instruction* inst, \ |
| 158 | uint16_t inst_data) |
| 159 | |
| 160 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 161 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 162 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 163 | |
| 164 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 165 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 166 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 167 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 168 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 169 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 170 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 171 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 172 | |
| 173 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 174 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 175 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 176 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 177 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 178 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 179 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 180 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 181 | |
| 182 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 183 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 184 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 185 | // Helper for getters in invoke-polymorphic. |
| 186 | inline static void DoFieldGetForInvokePolymorphic(Thread* self, |
| 187 | const ShadowFrame& shadow_frame, |
| 188 | ObjPtr<mirror::Object>& obj, |
| 189 | ArtField* field, |
| 190 | Primitive::Type field_type, |
| 191 | JValue* result) |
| 192 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 193 | switch (field_type) { |
| 194 | case Primitive::kPrimBoolean: |
| 195 | DoFieldGetCommon<Primitive::kPrimBoolean>(self, shadow_frame, obj, field, result); |
| 196 | break; |
| 197 | case Primitive::kPrimByte: |
| 198 | DoFieldGetCommon<Primitive::kPrimByte>(self, shadow_frame, obj, field, result); |
| 199 | break; |
| 200 | case Primitive::kPrimChar: |
| 201 | DoFieldGetCommon<Primitive::kPrimChar>(self, shadow_frame, obj, field, result); |
| 202 | break; |
| 203 | case Primitive::kPrimShort: |
| 204 | DoFieldGetCommon<Primitive::kPrimShort>(self, shadow_frame, obj, field, result); |
| 205 | break; |
| 206 | case Primitive::kPrimInt: |
| 207 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 208 | break; |
| 209 | case Primitive::kPrimLong: |
| 210 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 211 | break; |
| 212 | case Primitive::kPrimFloat: |
| 213 | DoFieldGetCommon<Primitive::kPrimInt>(self, shadow_frame, obj, field, result); |
| 214 | break; |
| 215 | case Primitive::kPrimDouble: |
| 216 | DoFieldGetCommon<Primitive::kPrimLong>(self, shadow_frame, obj, field, result); |
| 217 | break; |
| 218 | case Primitive::kPrimNot: |
| 219 | DoFieldGetCommon<Primitive::kPrimNot>(self, shadow_frame, obj, field, result); |
| 220 | break; |
| 221 | case Primitive::kPrimVoid: |
| 222 | LOG(FATAL) << "Unreachable: " << field_type; |
| 223 | UNREACHABLE(); |
| 224 | } |
| 225 | } |
| 226 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 227 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 228 | // Returns true on success, otherwise throws an exception and returns false. |
| 229 | template<Primitive::Type field_type> |
| 230 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 231 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 232 | if (UNLIKELY(obj == nullptr)) { |
| 233 | // We lost the reference to the field index so we cannot get a more |
| 234 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 235 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | MemberOffset field_offset(inst->VRegC_22c()); |
| 239 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 240 | // the field from the base of the object, we need to look for it first. |
| 241 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 242 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 243 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 244 | field_offset.Uint32Value()); |
| 245 | DCHECK(f != nullptr); |
| 246 | DCHECK(!f->IsStatic()); |
Mathieu Chartier | a314773 | 2016-10-26 22:57:02 -0700 | [diff] [blame] | 247 | StackHandleScope<1> hs(Thread::Current()); |
| 248 | // Save obj in case the instrumentation event has thread suspension. |
| 249 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 250 | instrumentation->FieldReadEvent(Thread::Current(), |
| 251 | obj.Ptr(), |
| 252 | shadow_frame.GetMethod(), |
| 253 | shadow_frame.GetDexPC(), |
| 254 | f); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 255 | } |
| 256 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 257 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 258 | switch (field_type) { |
| 259 | case Primitive::kPrimInt: |
| 260 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 261 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 262 | case Primitive::kPrimBoolean: |
| 263 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 264 | break; |
| 265 | case Primitive::kPrimByte: |
| 266 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 267 | break; |
| 268 | case Primitive::kPrimChar: |
| 269 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 270 | break; |
| 271 | case Primitive::kPrimShort: |
| 272 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 273 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 274 | case Primitive::kPrimLong: |
| 275 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 276 | break; |
| 277 | case Primitive::kPrimNot: |
| 278 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 279 | break; |
| 280 | default: |
| 281 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 282 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | // Explicitly instantiate all DoIGetQuick functions. |
| 288 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 289 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 290 | uint16_t inst_data) |
| 291 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 292 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 293 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 294 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 295 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 296 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 297 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 298 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 299 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 300 | |
| 301 | template<Primitive::Type field_type> |
| 302 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 303 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 304 | JValue field_value; |
| 305 | switch (field_type) { |
| 306 | case Primitive::kPrimBoolean: |
| 307 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 308 | break; |
| 309 | case Primitive::kPrimByte: |
| 310 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 311 | break; |
| 312 | case Primitive::kPrimChar: |
| 313 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 314 | break; |
| 315 | case Primitive::kPrimShort: |
| 316 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 317 | break; |
| 318 | case Primitive::kPrimInt: |
| 319 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 320 | break; |
| 321 | case Primitive::kPrimLong: |
| 322 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 323 | break; |
| 324 | case Primitive::kPrimNot: |
| 325 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 326 | break; |
| 327 | default: |
| 328 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 329 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 330 | } |
| 331 | return field_value; |
| 332 | } |
| 333 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 334 | template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active> |
| 335 | static inline bool DoFieldPutCommon(Thread* self, |
| 336 | const ShadowFrame& shadow_frame, |
| 337 | ObjPtr<mirror::Object>& obj, |
| 338 | ArtField* f, |
| 339 | size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 340 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 341 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 342 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 343 | // the field from the base of the object, we need to look for it first. |
| 344 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 345 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
Mathieu Chartier | 0715c0b | 2016-10-03 22:49:46 -0700 | [diff] [blame] | 346 | StackHandleScope<1> hs(self); |
| 347 | // Wrap in handle wrapper in case the listener does thread suspension. |
| 348 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 349 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 350 | ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj; |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 351 | instrumentation->FieldWriteEvent(self, this_object.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 352 | shadow_frame.GetMethod(), |
| 353 | shadow_frame.GetDexPC(), |
| 354 | f, |
| 355 | field_value); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 356 | } |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 357 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 358 | switch (field_type) { |
| 359 | case Primitive::kPrimBoolean: |
| 360 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 361 | break; |
| 362 | case Primitive::kPrimByte: |
| 363 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 364 | break; |
| 365 | case Primitive::kPrimChar: |
| 366 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 367 | break; |
| 368 | case Primitive::kPrimShort: |
| 369 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 370 | break; |
| 371 | case Primitive::kPrimInt: |
| 372 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 373 | break; |
| 374 | case Primitive::kPrimLong: |
| 375 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 376 | break; |
| 377 | case Primitive::kPrimNot: { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 378 | ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 379 | if (do_assignability_check && reg != nullptr) { |
| 380 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 381 | // object in the destructor. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 382 | ObjPtr<mirror::Class> field_class; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 383 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 384 | StackHandleScope<2> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 385 | HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 386 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 387 | field_class = f->GetType<true>(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 388 | } |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 389 | if (!reg->VerifierInstanceOf(field_class.Ptr())) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 390 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 391 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 392 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 393 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 394 | reg->GetClass()->GetDescriptor(&temp1), |
| 395 | field_class->GetDescriptor(&temp2), |
| 396 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | f->SetObj<transaction_active>(obj, reg); |
| 401 | break; |
| 402 | } |
| 403 | default: |
| 404 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 405 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 410 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 411 | bool transaction_active> |
| 412 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 413 | uint16_t inst_data) { |
| 414 | const bool do_assignability_check = do_access_check; |
| 415 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 416 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 417 | ArtField* f = |
| 418 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 419 | Primitive::ComponentSize(field_type)); |
| 420 | if (UNLIKELY(f == nullptr)) { |
| 421 | CHECK(self->IsExceptionPending()); |
| 422 | return false; |
| 423 | } |
| 424 | ObjPtr<mirror::Object> obj; |
| 425 | if (is_static) { |
| 426 | obj = f->GetDeclaringClass(); |
| 427 | } else { |
| 428 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 429 | if (UNLIKELY(obj == nullptr)) { |
| 430 | ThrowNullPointerExceptionForFieldAccess(f, false); |
| 431 | return false; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 436 | return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self, |
| 437 | shadow_frame, |
| 438 | obj, |
| 439 | f, |
| 440 | vregA); |
| 441 | } |
| 442 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 443 | // Explicitly instantiate all DoFieldPut functions. |
| 444 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 445 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 446 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 447 | |
| 448 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 449 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 450 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 451 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 452 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 453 | |
| 454 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 455 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 456 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 457 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 458 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 459 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 460 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 461 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 462 | |
| 463 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 464 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 465 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 466 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 467 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 468 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 469 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 470 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 471 | |
| 472 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 473 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 474 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 475 | // Helper for setters in invoke-polymorphic. |
| 476 | bool DoFieldPutForInvokePolymorphic(Thread* self, |
| 477 | ShadowFrame& shadow_frame, |
| 478 | ObjPtr<mirror::Object>& obj, |
| 479 | ArtField* field, |
| 480 | Primitive::Type field_type, |
| 481 | size_t vregA) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 482 | static const bool kDoCheckAssignability = false; |
| 483 | static const bool kTransaction = false; |
| 484 | switch (field_type) { |
| 485 | case Primitive::kPrimBoolean: |
| 486 | return DoFieldPutCommon<Primitive::kPrimBoolean, kDoCheckAssignability, kTransaction>( |
| 487 | self, shadow_frame, obj, field, vregA); |
| 488 | case Primitive::kPrimByte: |
| 489 | return DoFieldPutCommon<Primitive::kPrimByte, kDoCheckAssignability, kTransaction>( |
| 490 | self, shadow_frame, obj, field, vregA); |
| 491 | case Primitive::kPrimChar: |
| 492 | return DoFieldPutCommon<Primitive::kPrimChar, kDoCheckAssignability, kTransaction>( |
| 493 | self, shadow_frame, obj, field, vregA); |
| 494 | case Primitive::kPrimShort: |
| 495 | return DoFieldPutCommon<Primitive::kPrimShort, kDoCheckAssignability, kTransaction>( |
| 496 | self, shadow_frame, obj, field, vregA); |
| 497 | case Primitive::kPrimInt: |
| 498 | return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>( |
| 499 | self, shadow_frame, obj, field, vregA); |
| 500 | case Primitive::kPrimLong: |
| 501 | return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>( |
| 502 | self, shadow_frame, obj, field, vregA); |
| 503 | case Primitive::kPrimFloat: |
| 504 | return DoFieldPutCommon<Primitive::kPrimInt, kDoCheckAssignability, kTransaction>( |
| 505 | self, shadow_frame, obj, field, vregA); |
| 506 | case Primitive::kPrimDouble: |
| 507 | return DoFieldPutCommon<Primitive::kPrimLong, kDoCheckAssignability, kTransaction>( |
| 508 | self, shadow_frame, obj, field, vregA); |
| 509 | case Primitive::kPrimNot: |
| 510 | return DoFieldPutCommon<Primitive::kPrimNot, kDoCheckAssignability, kTransaction>( |
| 511 | self, shadow_frame, obj, field, vregA); |
| 512 | case Primitive::kPrimVoid: |
| 513 | LOG(FATAL) << "Unreachable: " << field_type; |
| 514 | UNREACHABLE(); |
| 515 | } |
| 516 | } |
| 517 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 518 | template<Primitive::Type field_type, bool transaction_active> |
| 519 | 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] | 520 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 521 | if (UNLIKELY(obj == nullptr)) { |
| 522 | // We lost the reference to the field index so we cannot get a more |
| 523 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 524 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 525 | return false; |
| 526 | } |
| 527 | MemberOffset field_offset(inst->VRegC_22c()); |
| 528 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 529 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 530 | // the field from the base of the object, we need to look for it first. |
| 531 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 532 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 533 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 534 | field_offset.Uint32Value()); |
| 535 | DCHECK(f != nullptr); |
| 536 | DCHECK(!f->IsStatic()); |
| 537 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
Mathieu Chartier | a314773 | 2016-10-26 22:57:02 -0700 | [diff] [blame] | 538 | StackHandleScope<1> hs(Thread::Current()); |
| 539 | // Save obj in case the instrumentation event has thread suspension. |
| 540 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&obj); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 541 | instrumentation->FieldWriteEvent(Thread::Current(), |
| 542 | obj.Ptr(), |
| 543 | shadow_frame.GetMethod(), |
| 544 | shadow_frame.GetDexPC(), |
| 545 | f, |
| 546 | field_value); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 547 | } |
| 548 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 549 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 550 | case Primitive::kPrimBoolean: |
| 551 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 552 | break; |
| 553 | case Primitive::kPrimByte: |
| 554 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 555 | break; |
| 556 | case Primitive::kPrimChar: |
| 557 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 558 | break; |
| 559 | case Primitive::kPrimShort: |
| 560 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 561 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 562 | case Primitive::kPrimInt: |
| 563 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 564 | break; |
| 565 | case Primitive::kPrimLong: |
| 566 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 567 | break; |
| 568 | case Primitive::kPrimNot: |
| 569 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 570 | break; |
| 571 | default: |
| 572 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 573 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 574 | } |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | // Explicitly instantiate all DoIPutQuick functions. |
| 579 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 580 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 581 | const Instruction* inst, \ |
| 582 | uint16_t inst_data) |
| 583 | |
| 584 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 585 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 586 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 587 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 588 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 589 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 590 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 591 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 592 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 593 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 594 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 595 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 596 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 597 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 598 | // 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] | 599 | uint32_t FindNextInstructionFollowingException( |
| 600 | Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc, |
| 601 | const instrumentation::Instrumentation* instrumentation) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 602 | self->VerifyStack(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 603 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 604 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 605 | if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners() |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 606 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 607 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 608 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 609 | bool clear_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 610 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
| 611 | hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 612 | if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 613 | // Exception is not caught by the current method. We will unwind to the |
| 614 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 615 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 616 | shadow_frame.GetMethod(), dex_pc); |
| 617 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 618 | // 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] | 619 | if (clear_exception) { |
| 620 | self->ClearException(); |
| 621 | } |
| 622 | } |
| 623 | return found_dex_pc; |
| 624 | } |
| 625 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 626 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 627 | LOG(FATAL) << "Unexpected instruction: " |
| 628 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 629 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 632 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 633 | va_list args; |
| 634 | va_start(args, fmt); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 635 | AbortTransactionV(self, fmt, args); |
| 636 | va_end(args); |
| 637 | } |
| 638 | |
| 639 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 640 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 641 | // Constructs abort message. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 642 | std::string abort_msg; |
| 643 | StringAppendV(&abort_msg, fmt, args); |
| 644 | // 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] | 645 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 648 | // START DECLARATIONS : |
| 649 | // |
| 650 | // These additional declarations are required because clang complains |
| 651 | // about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions. |
| 652 | // |
| 653 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 654 | template <bool is_range, bool do_assignability_check> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 655 | REQUIRES_SHARED(Locks::mutator_lock_) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 656 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 657 | Thread* self, |
| 658 | ShadowFrame& shadow_frame, |
| 659 | JValue* result, |
| 660 | uint16_t number_of_inputs, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 661 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 662 | uint32_t vregC) ALWAYS_INLINE; |
| 663 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 664 | template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_) |
| 665 | static inline bool DoCallPolymorphic(ArtMethod* called_method, |
| 666 | Handle<mirror::MethodType> callsite_type, |
| 667 | Handle<mirror::MethodType> target_type, |
| 668 | Thread* self, |
| 669 | ShadowFrame& shadow_frame, |
| 670 | JValue* result, |
| 671 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 672 | uint32_t vregC) ALWAYS_INLINE; |
| 673 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 674 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 675 | static inline bool DoCallTransform(ArtMethod* called_method, |
| 676 | Handle<mirror::MethodType> callsite_type, |
| 677 | Thread* self, |
| 678 | ShadowFrame& shadow_frame, |
| 679 | Handle<mirror::MethodHandleImpl> receiver, |
| 680 | JValue* result) ALWAYS_INLINE; |
| 681 | |
| 682 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 683 | inline void PerformCall(Thread* self, |
| 684 | const DexFile::CodeItem* code_item, |
| 685 | ArtMethod* caller_method, |
| 686 | const size_t first_dest_reg, |
| 687 | ShadowFrame* callee_frame, |
| 688 | JValue* result) ALWAYS_INLINE; |
| 689 | |
| 690 | template <bool is_range> |
| 691 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 692 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 693 | ShadowFrame* callee_frame, |
| 694 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 695 | const size_t first_src_reg, |
| 696 | const size_t first_dest_reg, |
| 697 | const size_t num_regs) ALWAYS_INLINE; |
| 698 | |
| 699 | // END DECLARATIONS. |
| 700 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 701 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 702 | ArtMethod* caller, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 703 | const DexFile::CodeItem* code_item, |
| 704 | ShadowFrame* shadow_frame, |
| 705 | JValue* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 706 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 707 | ArtMethod* method = shadow_frame->GetMethod(); |
| 708 | // Ensure static methods are initialized. |
| 709 | if (method->IsStatic()) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 710 | ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass(); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 711 | if (UNLIKELY(!declaringClass->IsInitialized())) { |
| 712 | self->PushShadowFrame(shadow_frame); |
| 713 | StackHandleScope<1> hs(self); |
| 714 | Handle<mirror::Class> h_class(hs.NewHandle(declaringClass)); |
| 715 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, |
| 716 | true))) { |
| 717 | self->PopShadowFrame(); |
| 718 | DCHECK(self->IsExceptionPending()); |
| 719 | return; |
| 720 | } |
| 721 | self->PopShadowFrame(); |
| 722 | CHECK(h_class->IsInitializing()); |
| 723 | // Reload from shadow frame in case the method moved, this is faster than adding a handle. |
| 724 | method = shadow_frame->GetMethod(); |
| 725 | } |
| 726 | } |
| 727 | uint16_t arg_offset = (code_item == nullptr) |
| 728 | ? 0 |
| 729 | : code_item->registers_size_ - code_item->ins_size_; |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 730 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 731 | if (jit != nullptr && caller != nullptr) { |
| 732 | jit->NotifyInterpreterToCompiledCodeTransition(self, caller); |
| 733 | } |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 734 | method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset), |
| 735 | (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 736 | result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 739 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 740 | uint16_t this_obj_vreg, |
| 741 | JValue result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 742 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 743 | ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg); |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 744 | if (existing == nullptr) { |
| 745 | // If it's null, we come from compiled code that was deoptimized. Nothing to do, |
| 746 | // as the compiler verified there was no alias. |
| 747 | // Set the new string result of the StringFactory. |
| 748 | shadow_frame->SetVRegReference(this_obj_vreg, result.GetL()); |
| 749 | return; |
| 750 | } |
| 751 | // Set the string init result into all aliases. |
| 752 | for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) { |
| 753 | if (shadow_frame->GetVRegReference(i) == existing) { |
| 754 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 755 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 756 | shadow_frame->SetVRegReference(i, result.GetL()); |
| 757 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 758 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 763 | inline static bool IsInvokeExact(const DexFile& dex_file, int invoke_method_idx) { |
| 764 | // This check uses string comparison as it needs less code and data |
| 765 | // to do than fetching the associated ArtMethod from the DexCache |
| 766 | // and checking against ArtMethods in the well known classes. The |
| 767 | // verifier needs to perform a more rigorous check. |
| 768 | const char* method_name = dex_file.GetMethodName(dex_file.GetMethodId(invoke_method_idx)); |
| 769 | bool is_invoke_exact = (0 == strcmp(method_name, "invokeExact")); |
| 770 | DCHECK(is_invoke_exact || (0 == strcmp(method_name, "invoke"))); |
| 771 | return is_invoke_exact; |
| 772 | } |
| 773 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 774 | template<bool is_range, bool do_access_check> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 775 | inline bool DoInvokePolymorphic(Thread* self, |
| 776 | ShadowFrame& shadow_frame, |
| 777 | const Instruction* inst, |
| 778 | uint16_t inst_data, |
| 779 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 780 | // Invoke-polymorphic instructions always take a receiver. i.e, they are never static. |
| 781 | const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc(); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 782 | 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] | 783 | |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 784 | // Determine if this invocation is MethodHandle.invoke() or |
| 785 | // MethodHandle.invokeExact(). |
| 786 | bool is_invoke_exact = IsInvokeExact(shadow_frame.GetMethod()->GetDeclaringClass()->GetDexFile(), |
| 787 | invoke_method_idx); |
| 788 | |
| 789 | // 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] | 790 | // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact) |
| 791 | // and not the method that we'll dispatch to in the end. |
| 792 | // |
| 793 | // TODO(narayan) We'll have to check in the verifier that this is in fact a |
| 794 | // signature polymorphic method so that we disallow calls via invoke-polymorphic |
| 795 | // to non sig-poly methods. This would also have the side effect of verifying |
| 796 | // that vRegC really is a reference type. |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 797 | StackHandleScope<6> hs(self); |
| 798 | Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 799 | ObjPtr<mirror::MethodHandleImpl>::DownCast( |
| 800 | MakeObjPtr(shadow_frame.GetVRegReference(vRegC))))); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 801 | if (UNLIKELY(method_handle.Get() == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 802 | // Note that the invoke type is kVirtual here because a call to a signature |
| 803 | // polymorphic method is shaped like a virtual call at the bytecode level. |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 804 | ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 805 | result->SetJ(0); |
| 806 | return false; |
| 807 | } |
| 808 | |
| 809 | // The vRegH value gives the index of the proto_id associated with this |
| 810 | // signature polymorphic callsite. |
| 811 | const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 812 | |
| 813 | // Call through to the classlinker and ask it to resolve the static type associated |
| 814 | // with the callsite. This information is stored in the dex cache so it's |
| 815 | // guaranteed to be fast after the first resolution. |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 816 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 817 | Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass())); |
| 818 | Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType( |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 819 | caller_class->GetDexFile(), callsite_proto_id, |
| 820 | hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()), |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 821 | hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader())))); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 822 | |
| 823 | // This implies we couldn't resolve one or more types in this method handle. |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 824 | if (UNLIKELY(callsite_type.Get() == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 825 | CHECK(self->IsExceptionPending()); |
| 826 | result->SetJ(0); |
| 827 | return false; |
| 828 | } |
| 829 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 830 | const MethodHandleKind handle_kind = method_handle->GetHandleKind(); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 831 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 832 | CHECK(handle_type.Get() != nullptr); |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 833 | if (UNLIKELY(is_invoke_exact && !callsite_type->IsExactMatch(handle_type.Get()))) { |
| 834 | ThrowWrongMethodTypeException(callsite_type.Get(), handle_type.Get()); |
| 835 | return false; |
| 836 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 837 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 838 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; |
| 839 | uint32_t receiver_vregC = 0; |
| 840 | if (is_range) { |
| 841 | receiver_vregC = (inst->VRegC_4rcc() + 1); |
| 842 | } else { |
| 843 | inst->GetVarArgs(arg, inst_data); |
| 844 | arg[0] = arg[1]; |
| 845 | arg[1] = arg[2]; |
| 846 | arg[2] = arg[3]; |
| 847 | arg[3] = arg[4]; |
| 848 | arg[4] = 0; |
| 849 | receiver_vregC = arg[0]; |
| 850 | } |
| 851 | |
| 852 | if (IsInvoke(handle_kind)) { |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 853 | // Get the method we're actually invoking along with the kind of |
| 854 | // invoke that is desired. We don't need to perform access checks at this |
| 855 | // point because they would have been performed on our behalf at the point |
| 856 | // of creation of the method handle. |
| 857 | ArtMethod* called_method = method_handle->GetTargetMethod(); |
| 858 | CHECK(called_method != nullptr); |
| 859 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 860 | if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 861 | ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(receiver_vregC); |
| 862 | ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass(); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 863 | // Verify that _vRegC is an object reference and of the type expected by |
| 864 | // the receiver. |
| 865 | called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface( |
| 866 | called_method, kRuntimePointerSize); |
| 867 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | } else if (handle_kind == kInvokeDirect) { |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 871 | if (called_method->IsConstructor()) { |
| 872 | // TODO(narayan) : We need to handle the case where the target method is a |
| 873 | // constructor here. |
| 874 | UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet."; |
| 875 | return false; |
| 876 | } |
| 877 | |
| 878 | // Nothing special to do in the case where we're not dealing with a |
| 879 | // constructor. It's a private method, and we've already access checked at |
| 880 | // the point of creating the handle. |
| 881 | } else if (handle_kind == kInvokeSuper) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 882 | ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass(); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 883 | |
| 884 | // Note that we're not dynamically dispatching on the type of the receiver |
| 885 | // here. We use the static type of the "receiver" object that we've |
| 886 | // recorded in the method handle's type, which will be the same as the |
| 887 | // special caller that was specified at the point of lookup. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 888 | ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 889 | if (!declaring_class->IsInterface()) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 890 | ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass(); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 891 | uint16_t vtable_index = called_method->GetMethodIndex(); |
| 892 | DCHECK(super_class != nullptr); |
| 893 | DCHECK(super_class->HasVTable()); |
| 894 | // Note that super_class is a super of referrer_class and called_method |
| 895 | // will always be declared by super_class (or one of its super classes). |
| 896 | DCHECK_LT(vtable_index, super_class->GetVTableLength()); |
| 897 | called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize); |
| 898 | } else { |
| 899 | called_method = referrer_class->FindVirtualMethodForInterfaceSuper( |
| 900 | called_method, kRuntimePointerSize); |
| 901 | } |
| 902 | |
| 903 | CHECK(called_method != nullptr); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 904 | } |
| 905 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 906 | if (handle_kind == kInvokeTransform) { |
| 907 | return DoCallTransform(called_method, |
| 908 | callsite_type, |
| 909 | self, |
| 910 | shadow_frame, |
| 911 | method_handle /* receiver */, |
| 912 | result); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 913 | } else { |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 914 | return DoCallPolymorphic<is_range>(called_method, |
| 915 | callsite_type, |
| 916 | handle_type, |
| 917 | self, |
| 918 | shadow_frame, |
| 919 | result, |
| 920 | arg, |
| 921 | receiver_vregC); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 922 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 923 | } else { |
Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame^] | 924 | DCHECK(!is_range); |
| 925 | ArtField* field = method_handle->GetTargetField(); |
| 926 | Primitive::Type field_type = field->GetTypeAsPrimitiveType();; |
| 927 | |
| 928 | if (!is_invoke_exact) { |
| 929 | // TODO(oth): conversion plumbing for invoke(). |
| 930 | UNIMPLEMENTED(FATAL); |
| 931 | } |
| 932 | |
| 933 | switch (handle_kind) { |
| 934 | case kInstanceGet: { |
| 935 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(receiver_vregC); |
| 936 | DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result); |
| 937 | return true; |
| 938 | } |
| 939 | case kInstancePut: { |
| 940 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(receiver_vregC); |
| 941 | return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[1]); |
| 942 | } |
| 943 | case kStaticGet: { |
| 944 | ObjPtr<mirror::Object> obj = field->GetDeclaringClass(); |
| 945 | DoFieldGetForInvokePolymorphic(self, shadow_frame, obj, field, field_type, result); |
| 946 | return true; |
| 947 | } |
| 948 | case kStaticPut: { |
| 949 | ObjPtr<mirror::Object> obj = field->GetDeclaringClass(); |
| 950 | return DoFieldPutForInvokePolymorphic(self, shadow_frame, obj, field, field_type, arg[0]); |
| 951 | } |
| 952 | default: |
| 953 | LOG(FATAL) << "Unreachable: " << handle_kind; |
| 954 | UNREACHABLE(); |
| 955 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 959 | // Calculate the number of ins for a proxy or native method, where we |
| 960 | // can't just look at the code item. |
| 961 | static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method) |
| 962 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 963 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
| 964 | |
| 965 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 966 | size_t num_ins = 0; |
| 967 | // Separate accounting for the receiver, which isn't a part of the |
| 968 | // shorty. |
| 969 | if (!method->IsStatic()) { |
| 970 | ++num_ins; |
| 971 | } |
| 972 | |
| 973 | uint32_t shorty_len = 0; |
| 974 | const char* shorty = method->GetShorty(&shorty_len); |
| 975 | for (size_t i = 1; i < shorty_len; ++i) { |
| 976 | const char c = shorty[i]; |
| 977 | ++num_ins; |
| 978 | if (c == 'J' || c == 'D') { |
| 979 | ++num_ins; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | return num_ins; |
| 984 | } |
| 985 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 986 | |
| 987 | inline void PerformCall(Thread* self, |
| 988 | const DexFile::CodeItem* code_item, |
| 989 | ArtMethod* caller_method, |
| 990 | const size_t first_dest_reg, |
| 991 | ShadowFrame* callee_frame, |
| 992 | JValue* result) { |
| 993 | if (LIKELY(Runtime::Current()->IsStarted())) { |
| 994 | ArtMethod* target = callee_frame->GetMethod(); |
| 995 | if (ClassLinker::ShouldUseInterpreterEntrypoint( |
| 996 | target, |
| 997 | target->GetEntryPointFromQuickCompiledCode())) { |
| 998 | ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result); |
| 999 | } else { |
| 1000 | ArtInterpreterToCompiledCodeBridge( |
| 1001 | self, caller_method, code_item, callee_frame, result); |
| 1002 | } |
| 1003 | } else { |
| 1004 | UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | template <bool is_range> |
| 1009 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 1010 | ShadowFrame* callee_frame, |
| 1011 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 1012 | const size_t first_src_reg, |
| 1013 | const size_t first_dest_reg, |
| 1014 | const size_t num_regs) { |
| 1015 | if (is_range) { |
| 1016 | const size_t dest_reg_bound = first_dest_reg + num_regs; |
| 1017 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound; |
| 1018 | ++dest_reg, ++src_reg) { |
| 1019 | AssignRegister(callee_frame, caller_frame, dest_reg, src_reg); |
| 1020 | } |
| 1021 | } else { |
| 1022 | DCHECK_LE(num_regs, arraysize(arg)); |
| 1023 | |
| 1024 | for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) { |
| 1025 | AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // Returns true iff. the callsite type for a polymorphic invoke is transformer |
| 1031 | // like, i.e that it has a single input argument whose type is |
| 1032 | // dalvik.system.EmulatedStackFrame. |
| 1033 | static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type) |
| 1034 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1035 | ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes()); |
| 1036 | if (param_types->GetLength() == 1) { |
| 1037 | ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0)); |
| 1038 | return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame); |
| 1039 | } |
| 1040 | |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1044 | template <bool is_range> |
| 1045 | static inline bool DoCallPolymorphic(ArtMethod* called_method, |
| 1046 | Handle<mirror::MethodType> callsite_type, |
| 1047 | Handle<mirror::MethodType> target_type, |
| 1048 | Thread* self, |
| 1049 | ShadowFrame& shadow_frame, |
| 1050 | JValue* result, |
| 1051 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1052 | uint32_t first_src_reg) { |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1053 | // TODO(narayan): Wire in the String.init hacks. |
| 1054 | |
| 1055 | // Compute method information. |
| 1056 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
| 1057 | |
| 1058 | // Number of registers for the callee's call frame. Note that for non-exact |
| 1059 | // invokes, we always derive this information from the callee method. We |
| 1060 | // cannot guarantee during verification that the number of registers encoded |
| 1061 | // in the invoke is equal to the number of ins for the callee. This is because |
| 1062 | // some transformations (such as boxing a long -> Long or wideining an |
| 1063 | // int -> long will change that number. |
| 1064 | uint16_t num_regs; |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1065 | size_t num_input_regs; |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1066 | size_t first_dest_reg; |
| 1067 | if (LIKELY(code_item != nullptr)) { |
| 1068 | num_regs = code_item->registers_size_; |
| 1069 | first_dest_reg = num_regs - code_item->ins_size_; |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1070 | num_input_regs = code_item->ins_size_; |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1071 | // Parameter registers go at the end of the shadow frame. |
| 1072 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1073 | } else { |
| 1074 | // No local regs for proxy and native methods. |
| 1075 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1076 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1077 | first_dest_reg = 0; |
| 1078 | } |
| 1079 | |
| 1080 | // Allocate shadow frame on the stack. |
| 1081 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 1082 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 1083 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 1084 | |
| 1085 | // Thread might be suspended during PerformArgumentConversions due to the |
| 1086 | // allocations performed during boxing. |
| 1087 | { |
| 1088 | ScopedStackedShadowFramePusher pusher( |
| 1089 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1090 | if (callsite_type->IsExactMatch(target_type.Get())) { |
| 1091 | // This is an exact invoke, we can take the fast path of just copying all |
| 1092 | // registers without performing any argument conversions. |
| 1093 | CopyRegisters<is_range>(shadow_frame, |
| 1094 | new_shadow_frame, |
| 1095 | arg, |
| 1096 | first_src_reg, |
| 1097 | first_dest_reg, |
| 1098 | num_input_regs); |
| 1099 | } else { |
| 1100 | // This includes the case where we're entering this invoke-polymorphic |
| 1101 | // from a transformer method. In that case, the callsite_type will contain |
| 1102 | // a single argument of type dalvik.system.EmulatedStackFrame. In that |
| 1103 | // case, we'll have to unmarshal the EmulatedStackFrame into the |
| 1104 | // new_shadow_frame and perform argument conversions on it. |
| 1105 | if (IsCallerTransformer(callsite_type)) { |
| 1106 | // The emulated stack frame will be the first ahnd only argument |
| 1107 | // when we're coming through from a transformer. |
| 1108 | // |
| 1109 | // TODO(narayan): This should be a mirror::EmulatedStackFrame after that |
| 1110 | // type is introduced. |
| 1111 | ObjPtr<mirror::Object> emulated_stack_frame( |
| 1112 | shadow_frame.GetVRegReference(first_src_reg)); |
| 1113 | if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self, |
| 1114 | emulated_stack_frame, |
| 1115 | target_type, |
| 1116 | first_dest_reg, |
| 1117 | new_shadow_frame)) { |
| 1118 | DCHECK(self->IsExceptionPending()); |
| 1119 | result->SetL(0); |
| 1120 | return false; |
| 1121 | } |
| 1122 | } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self, |
| 1123 | callsite_type, |
| 1124 | target_type, |
| 1125 | shadow_frame, |
| 1126 | first_src_reg, |
| 1127 | first_dest_reg, |
| 1128 | arg, |
| 1129 | new_shadow_frame)) { |
| 1130 | DCHECK(self->IsExceptionPending()); |
| 1131 | result->SetL(0); |
| 1132 | return false; |
| 1133 | } |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1137 | PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1138 | |
| 1139 | // TODO(narayan): Perform return value conversions. |
| 1140 | |
| 1141 | return !self->IsExceptionPending(); |
| 1142 | } |
| 1143 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1144 | static inline bool DoCallTransform(ArtMethod* called_method, |
| 1145 | Handle<mirror::MethodType> callsite_type, |
| 1146 | Thread* self, |
| 1147 | ShadowFrame& shadow_frame, |
| 1148 | Handle<mirror::MethodHandleImpl> receiver, |
| 1149 | JValue* result) { |
| 1150 | // This can be fixed, because the method we're calling here |
| 1151 | // (MethodHandle.transformInternal) doesn't have any locals and the signature |
| 1152 | // is known : |
| 1153 | // |
| 1154 | // private MethodHandle.transformInternal(EmulatedStackFrame sf); |
| 1155 | // |
| 1156 | // This means we need only two vregs : |
| 1157 | // - One for the receiver object. |
| 1158 | // - One for the only method argument (an EmulatedStackFrame). |
| 1159 | static constexpr size_t kNumRegsForTransform = 2; |
| 1160 | |
| 1161 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
| 1162 | DCHECK(code_item != nullptr); |
| 1163 | DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_); |
| 1164 | DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_); |
| 1165 | |
| 1166 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 1167 | CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0); |
| 1168 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 1169 | |
| 1170 | // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and |
| 1171 | // then construct an argument list object that's passed through to the |
| 1172 | // method. Note that the ArgumentList reference is currently a nullptr. |
| 1173 | // |
| 1174 | // NOTE(narayan): If the caller is a transformer method (i.e, there is only |
| 1175 | // one argument and its type is EmulatedStackFrame), we can directly pass that |
| 1176 | // through without having to do any additional work. |
| 1177 | UNUSED(callsite_type); |
| 1178 | |
| 1179 | new_shadow_frame->SetVRegReference(0, receiver.Get()); |
| 1180 | // TODO(narayan): This is the EmulatedStackFrame, currently nullptr. |
| 1181 | new_shadow_frame->SetVRegReference(1, nullptr); |
| 1182 | |
| 1183 | PerformCall(self, |
| 1184 | code_item, |
| 1185 | shadow_frame.GetMethod(), |
| 1186 | 0 /* first dest reg */, |
| 1187 | new_shadow_frame, |
| 1188 | result); |
| 1189 | |
| 1190 | return !self->IsExceptionPending(); |
| 1191 | } |
| 1192 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1193 | template <bool is_range, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1194 | bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1195 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 1196 | Thread* self, |
| 1197 | ShadowFrame& shadow_frame, |
| 1198 | JValue* result, |
| 1199 | uint16_t number_of_inputs, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1200 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1201 | uint32_t vregC) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1202 | bool string_init = false; |
| 1203 | // Replace calls to String.<init> with equivalent StringFactory call. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1204 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 1205 | && called_method->IsConstructor())) { |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 1206 | called_method = WellKnownClasses::StringInitToStringFactory(called_method); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1207 | string_init = true; |
| 1208 | } |
| 1209 | |
Alex Light | daf58c8 | 2016-03-16 23:00:49 +0000 | [diff] [blame] | 1210 | // Compute method information. |
| 1211 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1212 | |
| 1213 | // Number of registers for the callee's call frame. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1214 | uint16_t num_regs; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1215 | if (LIKELY(code_item != nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1216 | num_regs = code_item->registers_size_; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1217 | DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1218 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1219 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1220 | num_regs = number_of_inputs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1223 | // Hack for String init: |
| 1224 | // |
| 1225 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 1226 | // invoke-x StringFactory(a, b, c, ...) |
| 1227 | // by effectively dropping the first virtual register from the invoke. |
| 1228 | // |
| 1229 | // (at this point the ArtMethod has already been replaced, |
| 1230 | // so we just need to fix-up the arguments) |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 1231 | // |
| 1232 | // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased |
| 1233 | // to handle the compiler optimization of replacing `this` with null without |
| 1234 | // throwing NullPointerException. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1235 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1236 | if (UNLIKELY(string_init)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1237 | 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] | 1238 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1239 | // The new StringFactory call is static and has one fewer argument. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1240 | if (code_item == nullptr) { |
| 1241 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1242 | num_regs--; |
| 1243 | } // 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] | 1244 | number_of_inputs--; |
| 1245 | |
| 1246 | // Rewrite the var-args, dropping the 0th argument ("this") |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1247 | for (uint32_t i = 1; i < arraysize(arg); ++i) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1248 | arg[i - 1] = arg[i]; |
| 1249 | } |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1250 | arg[arraysize(arg) - 1] = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1251 | |
| 1252 | // Rewrite the non-var-arg case |
| 1253 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 1254 | } |
| 1255 | |
| 1256 | // Parameter registers go at the end of the shadow frame. |
| 1257 | DCHECK_GE(num_regs, number_of_inputs); |
| 1258 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 1259 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1260 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1261 | // Allocate shadow frame on the stack. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1262 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1263 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 1264 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1265 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1266 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1267 | // 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] | 1268 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1269 | // Slow path. |
| 1270 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 1271 | // register the shadow frame as under construction and allow suspension again. |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 1272 | ScopedStackedShadowFramePusher pusher( |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 1273 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1274 | self->EndAssertNoThreadSuspension(old_cause); |
| 1275 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1276 | // ArtMethod here is needed to check type information of the call site against the callee. |
| 1277 | // Type information is retrieved from a DexFile/DexCache for that respective declared method. |
| 1278 | // |
| 1279 | // As a special case for proxy methods, which are not dex-backed, |
| 1280 | // we have to retrieve type information from the proxy's method |
| 1281 | // interface method instead (which is dex backed since proxies are never interfaces). |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1282 | ArtMethod* method = |
| 1283 | new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1284 | |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1285 | // 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] | 1286 | // to get the exact type of each reference argument. |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1287 | const DexFile::TypeList* params = method->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1288 | uint32_t shorty_len = 0; |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1289 | const char* shorty = method->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1290 | |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1291 | // Handle receiver apart since it's not part of the shorty. |
| 1292 | size_t dest_reg = first_dest_reg; |
| 1293 | size_t arg_offset = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1294 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1295 | if (!method->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1296 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1297 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 1298 | ++dest_reg; |
| 1299 | ++arg_offset; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1300 | DCHECK(!string_init); // All StringFactory methods are static. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1301 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1302 | |
| 1303 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1304 | 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] | 1305 | // Skip the 0th 'shorty' type since it represents the return type. |
| 1306 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1307 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 1308 | switch (shorty[shorty_pos + 1]) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1309 | // Handle Object references. 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1310 | case 'L': { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1311 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1312 | if (do_assignability_check && o != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1313 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1314 | const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_; |
| 1315 | ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx, |
| 1316 | pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1317 | if (arg_type == nullptr) { |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1318 | StackHandleScope<1> hs(self); |
| 1319 | // Preserve o since it is used below and GetClassFromTypeIndex may cause thread |
| 1320 | // suspension. |
| 1321 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o); |
| 1322 | arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size); |
| 1323 | if (arg_type == nullptr) { |
| 1324 | CHECK(self->IsExceptionPending()); |
| 1325 | return false; |
| 1326 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1327 | } |
| 1328 | if (!o->VerifierInstanceOf(arg_type)) { |
| 1329 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1330 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1331 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1332 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1333 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1334 | o->GetClass()->GetDescriptor(&temp1), |
| 1335 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1336 | return false; |
| 1337 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1338 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1339 | new_shadow_frame->SetVRegReference(dest_reg, o.Ptr()); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1340 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1341 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1342 | // Handle doubles and longs. 2 consecutive virtual register slots. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1343 | case 'J': case 'D': { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1344 | uint64_t wide_value = |
| 1345 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 1346 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1347 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1348 | // Skip the next virtual register slot since we already used it. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1349 | ++dest_reg; |
| 1350 | ++arg_offset; |
| 1351 | break; |
| 1352 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1353 | // Handle all other primitives that are always 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1354 | default: |
| 1355 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 1356 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1357 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1358 | } |
| 1359 | } else { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1360 | if (is_range) { |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1361 | DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1362 | } |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1363 | |
| 1364 | CopyRegisters<is_range>(shadow_frame, |
| 1365 | new_shadow_frame, |
| 1366 | arg, |
| 1367 | vregC, |
| 1368 | first_dest_reg, |
| 1369 | number_of_inputs); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1370 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1373 | PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1374 | |
| 1375 | if (string_init && !self->IsExceptionPending()) { |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 1376 | SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1379 | return !self->IsExceptionPending(); |
| 1380 | } |
| 1381 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1382 | template<bool is_range, bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1383 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 1384 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 1385 | // Argument word count. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1386 | const uint16_t number_of_inputs = |
| 1387 | (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1388 | |
| 1389 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 1390 | // code. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1391 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1392 | uint32_t vregC = 0; |
| 1393 | if (is_range) { |
| 1394 | vregC = inst->VRegC_3rc(); |
| 1395 | } else { |
| 1396 | vregC = inst->VRegC_35c(); |
| 1397 | inst->GetVarArgs(arg, inst_data); |
| 1398 | } |
| 1399 | |
| 1400 | return DoCallCommon<is_range, do_assignability_check>( |
| 1401 | called_method, self, shadow_frame, |
| 1402 | result, number_of_inputs, arg, vregC); |
| 1403 | } |
| 1404 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1405 | template <bool is_range, bool do_access_check, bool transaction_active> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1406 | bool DoFilledNewArray(const Instruction* inst, |
| 1407 | const ShadowFrame& shadow_frame, |
| 1408 | Thread* self, |
| 1409 | JValue* result) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1410 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 1411 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 1412 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 1413 | if (!is_range) { |
| 1414 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 1415 | CHECK_LE(length, 5); |
| 1416 | } |
| 1417 | if (UNLIKELY(length < 0)) { |
| 1418 | ThrowNegativeArraySizeException(length); |
| 1419 | return false; |
| 1420 | } |
| 1421 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1422 | ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx, |
| 1423 | shadow_frame.GetMethod(), |
| 1424 | self, |
| 1425 | false, |
| 1426 | do_access_check); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1427 | if (UNLIKELY(array_class == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1428 | DCHECK(self->IsExceptionPending()); |
| 1429 | return false; |
| 1430 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1431 | CHECK(array_class->IsArrayClass()); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1432 | ObjPtr<mirror::Class> component_class = array_class->GetComponentType(); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1433 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 1434 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 1435 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1436 | ThrowRuntimeException("Bad filled array request for type %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1437 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1438 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1439 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 1440 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1441 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1442 | } |
| 1443 | return false; |
| 1444 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1445 | ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>( |
| 1446 | self, |
| 1447 | array_class, |
| 1448 | length, |
| 1449 | array_class->GetComponentSizeShift(), |
| 1450 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1451 | if (UNLIKELY(new_array == nullptr)) { |
| 1452 | self->AssertPendingOOMException(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1455 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 1456 | uint32_t vregC = 0; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1457 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1458 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1459 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1460 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1461 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1462 | for (int32_t i = 0; i < length; ++i) { |
| 1463 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 1464 | if (is_primitive_int_component) { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1465 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 1466 | i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1467 | } else { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1468 | new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>( |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1469 | i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1473 | result->SetL(new_array); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1474 | return true; |
| 1475 | } |
| 1476 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1477 | // TODO: Use ObjPtr here. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1478 | template<typename T> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1479 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, |
| 1480 | int32_t count) |
| 1481 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1482 | Runtime* runtime = Runtime::Current(); |
| 1483 | for (int32_t i = 0; i < count; ++i) { |
| 1484 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1488 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1489 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1490 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 1491 | DCHECK(array != nullptr); |
| 1492 | DCHECK_LE(count, array->GetLength()); |
| 1493 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 1494 | switch (primitive_component_type) { |
| 1495 | case Primitive::kPrimBoolean: |
| 1496 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 1497 | break; |
| 1498 | case Primitive::kPrimByte: |
| 1499 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 1500 | break; |
| 1501 | case Primitive::kPrimChar: |
| 1502 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 1503 | break; |
| 1504 | case Primitive::kPrimShort: |
| 1505 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 1506 | break; |
| 1507 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1508 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 1509 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1510 | case Primitive::kPrimFloat: |
| 1511 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 1512 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1513 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1514 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 1515 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1516 | case Primitive::kPrimDouble: |
| 1517 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 1518 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1519 | default: |
| 1520 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 1521 | << " in fill-array-data"; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1526 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1527 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1528 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1529 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 1530 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1531 | const Instruction* inst, uint16_t inst_data, \ |
| 1532 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1533 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 1534 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 1535 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 1536 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 1537 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1538 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1539 | // Explicit DoInvokePolymorphic template function declarations. |
| 1540 | #define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 1541 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 1542 | bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \ |
| 1543 | Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1544 | uint16_t inst_data, JValue* result) |
| 1545 | |
| 1546 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false); |
| 1547 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true); |
| 1548 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false); |
| 1549 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true); |
| 1550 | #undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL |
| 1551 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1552 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1553 | #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] | 1554 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1555 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 1556 | const ShadowFrame& shadow_frame, \ |
| 1557 | Thread* self, JValue* result) |
| 1558 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 1559 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 1560 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 1561 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 1562 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 1563 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 1564 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 1565 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1566 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 1567 | |
| 1568 | } // namespace interpreter |
| 1569 | } // namespace art |