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 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 21 | #include "debugger.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 22 | #include "mirror/array-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 23 | #include "unstarted_runtime.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace interpreter { |
| 27 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 28 | void ThrowNullPointerExceptionFromInterpreter() { |
| 29 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 33 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 34 | uint16_t inst_data) { |
| 35 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 36 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 37 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 38 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 39 | if (UNLIKELY(f == nullptr)) { |
| 40 | CHECK(self->IsExceptionPending()); |
| 41 | return false; |
| 42 | } |
| 43 | Object* obj; |
| 44 | if (is_static) { |
| 45 | obj = f->GetDeclaringClass(); |
| 46 | } else { |
| 47 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 48 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 49 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 53 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 54 | // Report this field access to instrumentation if needed. |
| 55 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 56 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 57 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 58 | instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(), |
| 59 | shadow_frame.GetDexPC(), f); |
| 60 | } |
| 61 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 62 | switch (field_type) { |
| 63 | case Primitive::kPrimBoolean: |
| 64 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 65 | break; |
| 66 | case Primitive::kPrimByte: |
| 67 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 68 | break; |
| 69 | case Primitive::kPrimChar: |
| 70 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 71 | break; |
| 72 | case Primitive::kPrimShort: |
| 73 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 74 | break; |
| 75 | case Primitive::kPrimInt: |
| 76 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 77 | break; |
| 78 | case Primitive::kPrimLong: |
| 79 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 80 | break; |
| 81 | case Primitive::kPrimNot: |
| 82 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj)); |
| 83 | break; |
| 84 | default: |
| 85 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 86 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | // Explicitly instantiate all DoFieldGet functions. |
| 92 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 93 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 94 | ShadowFrame& shadow_frame, \ |
| 95 | const Instruction* inst, \ |
| 96 | uint16_t inst_data) |
| 97 | |
| 98 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 99 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 100 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 101 | |
| 102 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 103 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 104 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 105 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 106 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 107 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 108 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 109 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 110 | |
| 111 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 112 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 113 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 114 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 115 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 116 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 117 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 118 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 119 | |
| 120 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 121 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 122 | |
| 123 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 124 | // Returns true on success, otherwise throws an exception and returns false. |
| 125 | template<Primitive::Type field_type> |
| 126 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 127 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 128 | if (UNLIKELY(obj == nullptr)) { |
| 129 | // We lost the reference to the field index so we cannot get a more |
| 130 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 131 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | MemberOffset field_offset(inst->VRegC_22c()); |
| 135 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 136 | // the field from the base of the object, we need to look for it first. |
| 137 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 138 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 139 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 140 | field_offset.Uint32Value()); |
| 141 | DCHECK(f != nullptr); |
| 142 | DCHECK(!f->IsStatic()); |
| 143 | instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 144 | shadow_frame.GetDexPC(), f); |
| 145 | } |
| 146 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 147 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 148 | switch (field_type) { |
| 149 | case Primitive::kPrimInt: |
| 150 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 151 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 152 | case Primitive::kPrimBoolean: |
| 153 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 154 | break; |
| 155 | case Primitive::kPrimByte: |
| 156 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 157 | break; |
| 158 | case Primitive::kPrimChar: |
| 159 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 160 | break; |
| 161 | case Primitive::kPrimShort: |
| 162 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 163 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 164 | case Primitive::kPrimLong: |
| 165 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 166 | break; |
| 167 | case Primitive::kPrimNot: |
| 168 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 169 | break; |
| 170 | default: |
| 171 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 172 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | // Explicitly instantiate all DoIGetQuick functions. |
| 178 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 179 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 180 | uint16_t inst_data) |
| 181 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 182 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 183 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 184 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 185 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 186 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 187 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 188 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 189 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 190 | |
| 191 | template<Primitive::Type field_type> |
| 192 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
| 193 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 194 | JValue field_value; |
| 195 | switch (field_type) { |
| 196 | case Primitive::kPrimBoolean: |
| 197 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 198 | break; |
| 199 | case Primitive::kPrimByte: |
| 200 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 201 | break; |
| 202 | case Primitive::kPrimChar: |
| 203 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 204 | break; |
| 205 | case Primitive::kPrimShort: |
| 206 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 207 | break; |
| 208 | case Primitive::kPrimInt: |
| 209 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 210 | break; |
| 211 | case Primitive::kPrimLong: |
| 212 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 213 | break; |
| 214 | case Primitive::kPrimNot: |
| 215 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 216 | break; |
| 217 | default: |
| 218 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 219 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 220 | } |
| 221 | return field_value; |
| 222 | } |
| 223 | |
| 224 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 225 | bool transaction_active> |
| 226 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 227 | uint16_t inst_data) { |
| 228 | bool do_assignability_check = do_access_check; |
| 229 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 230 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 231 | ArtField* f = FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 232 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 233 | if (UNLIKELY(f == nullptr)) { |
| 234 | CHECK(self->IsExceptionPending()); |
| 235 | return false; |
| 236 | } |
| 237 | Object* obj; |
| 238 | if (is_static) { |
| 239 | obj = f->GetDeclaringClass(); |
| 240 | } else { |
| 241 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 242 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 243 | ThrowNullPointerExceptionForFieldAccess(f, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 247 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 248 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 249 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 250 | // the field from the base of the object, we need to look for it first. |
| 251 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 252 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 253 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 254 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 255 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 256 | shadow_frame.GetDexPC(), f, field_value); |
| 257 | } |
| 258 | switch (field_type) { |
| 259 | case Primitive::kPrimBoolean: |
| 260 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 261 | break; |
| 262 | case Primitive::kPrimByte: |
| 263 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 264 | break; |
| 265 | case Primitive::kPrimChar: |
| 266 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 267 | break; |
| 268 | case Primitive::kPrimShort: |
| 269 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 270 | break; |
| 271 | case Primitive::kPrimInt: |
| 272 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 273 | break; |
| 274 | case Primitive::kPrimLong: |
| 275 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 276 | break; |
| 277 | case Primitive::kPrimNot: { |
| 278 | Object* reg = shadow_frame.GetVRegReference(vregA); |
| 279 | if (do_assignability_check && reg != nullptr) { |
| 280 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 281 | // object in the destructor. |
| 282 | Class* field_class; |
| 283 | { |
| 284 | StackHandleScope<3> hs(self); |
| 285 | HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f)); |
| 286 | HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 287 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame^] | 288 | field_class = h_f->GetType<true>(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 289 | } |
| 290 | if (!reg->VerifierInstanceOf(field_class)) { |
| 291 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 292 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 293 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 294 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 295 | reg->GetClass()->GetDescriptor(&temp1), |
| 296 | field_class->GetDescriptor(&temp2), |
| 297 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | f->SetObj<transaction_active>(obj, reg); |
| 302 | break; |
| 303 | } |
| 304 | default: |
| 305 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 306 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 307 | } |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | // Explicitly instantiate all DoFieldPut functions. |
| 312 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 313 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 314 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 315 | |
| 316 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 317 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 318 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 319 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 320 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 321 | |
| 322 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 323 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 324 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 325 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 326 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 327 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 328 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 329 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 330 | |
| 331 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 332 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 333 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 334 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 335 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 336 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 337 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 338 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 339 | |
| 340 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 341 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 342 | |
| 343 | template<Primitive::Type field_type, bool transaction_active> |
| 344 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 345 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 346 | if (UNLIKELY(obj == nullptr)) { |
| 347 | // We lost the reference to the field index so we cannot get a more |
| 348 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 349 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | MemberOffset field_offset(inst->VRegC_22c()); |
| 353 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 354 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 355 | // the field from the base of the object, we need to look for it first. |
| 356 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 357 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 358 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 359 | field_offset.Uint32Value()); |
| 360 | DCHECK(f != nullptr); |
| 361 | DCHECK(!f->IsStatic()); |
| 362 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 363 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 364 | shadow_frame.GetDexPC(), f, field_value); |
| 365 | } |
| 366 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 367 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 368 | case Primitive::kPrimBoolean: |
| 369 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 370 | break; |
| 371 | case Primitive::kPrimByte: |
| 372 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 373 | break; |
| 374 | case Primitive::kPrimChar: |
| 375 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 376 | break; |
| 377 | case Primitive::kPrimShort: |
| 378 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 379 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 380 | case Primitive::kPrimInt: |
| 381 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 382 | break; |
| 383 | case Primitive::kPrimLong: |
| 384 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 385 | break; |
| 386 | case Primitive::kPrimNot: |
| 387 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 388 | break; |
| 389 | default: |
| 390 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 391 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | // Explicitly instantiate all DoIPutQuick functions. |
| 397 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 398 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 399 | const Instruction* inst, \ |
| 400 | uint16_t inst_data) |
| 401 | |
| 402 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 403 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 404 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 405 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 406 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 407 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 408 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 409 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 410 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 411 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 412 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 413 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 414 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 415 | |
| 416 | uint32_t FindNextInstructionFollowingException(Thread* self, |
| 417 | ShadowFrame& shadow_frame, |
| 418 | uint32_t dex_pc, |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 419 | const instrumentation::Instrumentation* instrumentation) { |
| 420 | self->VerifyStack(); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 421 | StackHandleScope<3> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 422 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 423 | if (instrumentation->HasExceptionCaughtListeners() |
| 424 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 425 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 426 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 427 | bool clear_exception = false; |
| 428 | uint32_t found_dex_pc; |
| 429 | { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 430 | Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass())); |
| 431 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod())); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 432 | found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc, |
| 433 | &clear_exception); |
| 434 | } |
| 435 | if (found_dex_pc == DexFile::kDexNoIndex) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 436 | // Exception is not caught by the current method. We will unwind to the |
| 437 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 438 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 439 | shadow_frame.GetMethod(), dex_pc); |
| 440 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 441 | // 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] | 442 | if (clear_exception) { |
| 443 | self->ClearException(); |
| 444 | } |
| 445 | } |
| 446 | return found_dex_pc; |
| 447 | } |
| 448 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 449 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 450 | LOG(FATAL) << "Unexpected instruction: " |
| 451 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 452 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 455 | // Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 456 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 457 | size_t dest_reg, size_t src_reg) |
| 458 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 459 | // If both register locations contains the same value, the register probably holds a reference. |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 460 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 461 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 462 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 463 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 464 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 465 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 466 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 470 | void AbortTransaction(Thread* self, const char* fmt, ...) { |
| 471 | CHECK(Runtime::Current()->IsActiveTransaction()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 472 | // Constructs abort message. |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 473 | va_list args; |
| 474 | va_start(args, fmt); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 475 | std::string abort_msg; |
| 476 | StringAppendV(&abort_msg, fmt, args); |
| 477 | // Throws an exception so we can abort the transaction and rollback every change. |
| 478 | Runtime::Current()->AbortTransactionAndThrowInternalError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 479 | va_end(args); |
| 480 | } |
| 481 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 482 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 483 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 484 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 485 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 486 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 487 | const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 488 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 489 | if (LIKELY(code_item != NULL)) { |
| 490 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 491 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 492 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 493 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 494 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 497 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 498 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 499 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 500 | ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0, |
| 501 | memory)); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 502 | |
| 503 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 504 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 505 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 506 | // Slow path. |
| 507 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 508 | // register the shadow frame as under construction and allow suspension again. |
| 509 | self->SetShadowFrameUnderConstruction(new_shadow_frame); |
| 510 | self->EndAssertNoThreadSuspension(old_cause); |
| 511 | |
| 512 | // 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] | 513 | // to get the exact type of each reference argument. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 514 | const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 515 | uint32_t shorty_len = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 516 | const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 517 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 518 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 519 | // code. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 520 | uint32_t arg[5]; // only used in invoke-XXX. |
| 521 | uint32_t vregC; // only used in invoke-XXX-range. |
| 522 | if (is_range) { |
| 523 | vregC = inst->VRegC_3rc(); |
| 524 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 525 | inst->GetVarArgs(arg, inst_data); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 526 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 527 | |
| 528 | // Handle receiver apart since it's not part of the shorty. |
| 529 | size_t dest_reg = first_dest_reg; |
| 530 | size_t arg_offset = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 531 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 532 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 533 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 534 | ++dest_reg; |
| 535 | ++arg_offset; |
| 536 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 537 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 538 | DCHECK_LT(shorty_pos + 1, shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 539 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 540 | switch (shorty[shorty_pos + 1]) { |
| 541 | case 'L': { |
| 542 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 543 | if (do_assignability_check && o != NULL) { |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 544 | Class* arg_type = |
| 545 | new_shadow_frame->GetMethod()->GetClassFromTypeIndex( |
| 546 | params->GetTypeItem(shorty_pos).type_idx_, true); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 547 | if (arg_type == NULL) { |
| 548 | CHECK(self->IsExceptionPending()); |
| 549 | return false; |
| 550 | } |
| 551 | if (!o->VerifierInstanceOf(arg_type)) { |
| 552 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 553 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 554 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 555 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 556 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 557 | o->GetClass()->GetDescriptor(&temp1), |
| 558 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 559 | return false; |
| 560 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 561 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 562 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 563 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 564 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 565 | case 'J': case 'D': { |
| 566 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 567 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 568 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 569 | ++dest_reg; |
| 570 | ++arg_offset; |
| 571 | break; |
| 572 | } |
| 573 | default: |
| 574 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 575 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 576 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 577 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 578 | // We're done with the construction. |
| 579 | self->ClearShadowFrameUnderConstruction(); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 580 | } else { |
| 581 | // Fast path: no extra checks. |
| 582 | if (is_range) { |
| 583 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 584 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 585 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 586 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 587 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 588 | } else { |
| 589 | DCHECK_LE(num_ins, 5U); |
| 590 | uint16_t regList = inst->Fetch16(2); |
| 591 | uint16_t count = num_ins; |
| 592 | if (count == 5) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 593 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, |
| 594 | (inst_data >> 8) & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 595 | --count; |
| 596 | } |
| 597 | for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 598 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 599 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 600 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 601 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 604 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 605 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 606 | if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) { |
| 607 | LOG(FATAL) << "Attempt to invoke non-executable method: " |
| 608 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 609 | UNREACHABLE(); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 610 | } |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 611 | if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 612 | !new_shadow_frame->GetMethod()->IsNative() && |
| 613 | !new_shadow_frame->GetMethod()->IsProxyMethod() && |
| 614 | new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() |
| 615 | == artInterpreterToCompiledCodeBridge) { |
| 616 | LOG(FATAL) << "Attempt to call compiled code when -Xint: " |
| 617 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 618 | UNREACHABLE(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 619 | } |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 620 | // Force the use of interpreter when it is required by the debugger. |
| 621 | mirror::EntryPointFromInterpreter* entry; |
| 622 | if (UNLIKELY(Dbg::IsForcedInterpreterNeededForCalling(self, new_shadow_frame->GetMethod()))) { |
| 623 | entry = &art::artInterpreterToInterpreterBridge; |
| 624 | } else { |
| 625 | entry = new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter(); |
| 626 | } |
| 627 | entry(self, code_item, new_shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 628 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 629 | UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 630 | } |
| 631 | return !self->IsExceptionPending(); |
| 632 | } |
| 633 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 634 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 635 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 636 | Thread* self, JValue* result) { |
| 637 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 638 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 639 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 640 | if (!is_range) { |
| 641 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 642 | CHECK_LE(length, 5); |
| 643 | } |
| 644 | if (UNLIKELY(length < 0)) { |
| 645 | ThrowNegativeArraySizeException(length); |
| 646 | return false; |
| 647 | } |
| 648 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 649 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 650 | self, false, do_access_check); |
| 651 | if (UNLIKELY(arrayClass == NULL)) { |
| 652 | DCHECK(self->IsExceptionPending()); |
| 653 | return false; |
| 654 | } |
| 655 | CHECK(arrayClass->IsArrayClass()); |
| 656 | Class* componentClass = arrayClass->GetComponentType(); |
| 657 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 658 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 659 | ThrowRuntimeException("Bad filled array request for type %s", |
| 660 | PrettyDescriptor(componentClass).c_str()); |
| 661 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 662 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 663 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 664 | PrettyDescriptor(componentClass).c_str()); |
| 665 | } |
| 666 | return false; |
| 667 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 668 | Object* newArray = Array::Alloc<true>(self, arrayClass, length, |
| 669 | arrayClass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 670 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 671 | if (UNLIKELY(newArray == NULL)) { |
| 672 | DCHECK(self->IsExceptionPending()); |
| 673 | return false; |
| 674 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 675 | uint32_t arg[5]; // only used in filled-new-array. |
| 676 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 677 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 678 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 679 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 680 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 681 | } |
| 682 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 683 | for (int32_t i = 0; i < length; ++i) { |
| 684 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 685 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 686 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 687 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 688 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
| 692 | result->SetL(newArray); |
| 693 | return true; |
| 694 | } |
| 695 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 696 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 697 | template<typename T> |
| 698 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 699 | NO_THREAD_SAFETY_ANALYSIS { |
| 700 | Runtime* runtime = Runtime::Current(); |
| 701 | for (int32_t i = 0; i < count; ++i) { |
| 702 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 707 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 708 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 709 | DCHECK(array != nullptr); |
| 710 | DCHECK_LE(count, array->GetLength()); |
| 711 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 712 | switch (primitive_component_type) { |
| 713 | case Primitive::kPrimBoolean: |
| 714 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 715 | break; |
| 716 | case Primitive::kPrimByte: |
| 717 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 718 | break; |
| 719 | case Primitive::kPrimChar: |
| 720 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 721 | break; |
| 722 | case Primitive::kPrimShort: |
| 723 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 724 | break; |
| 725 | case Primitive::kPrimInt: |
| 726 | case Primitive::kPrimFloat: |
| 727 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 728 | break; |
| 729 | case Primitive::kPrimLong: |
| 730 | case Primitive::kPrimDouble: |
| 731 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 732 | break; |
| 733 | default: |
| 734 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 735 | << " in fill-array-data"; |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 740 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 741 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 742 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 743 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 744 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 745 | const Instruction* inst, uint16_t inst_data, \ |
| 746 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 747 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 748 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 749 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 750 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 751 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 752 | |
| 753 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 754 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 755 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 756 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 757 | const ShadowFrame& shadow_frame, \ |
| 758 | Thread* self, JValue* result) |
| 759 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 760 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 761 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 762 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 763 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 764 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 765 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 766 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 767 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 768 | |
| 769 | } // namespace interpreter |
| 770 | } // namespace art |