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