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 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 19 | #include "mirror/array-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | namespace interpreter { |
| 23 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 24 | void ThrowNullPointerExceptionFromInterpreter(const ShadowFrame& shadow_frame) { |
| 25 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 26 | } |
| 27 | |
| 28 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 29 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 30 | uint16_t inst_data) { |
| 31 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 32 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 33 | 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] | 34 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 35 | if (UNLIKELY(f == nullptr)) { |
| 36 | CHECK(self->IsExceptionPending()); |
| 37 | return false; |
| 38 | } |
| 39 | Object* obj; |
| 40 | if (is_static) { |
| 41 | obj = f->GetDeclaringClass(); |
| 42 | } else { |
| 43 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 44 | if (UNLIKELY(obj == nullptr)) { |
| 45 | ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), f, true); |
| 46 | return false; |
| 47 | } |
| 48 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 49 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 50 | // Report this field access to instrumentation if needed. |
| 51 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 52 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 53 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 54 | instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(), |
| 55 | shadow_frame.GetDexPC(), f); |
| 56 | } |
| 57 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 58 | switch (field_type) { |
| 59 | case Primitive::kPrimBoolean: |
| 60 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 61 | break; |
| 62 | case Primitive::kPrimByte: |
| 63 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 64 | break; |
| 65 | case Primitive::kPrimChar: |
| 66 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 67 | break; |
| 68 | case Primitive::kPrimShort: |
| 69 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 70 | break; |
| 71 | case Primitive::kPrimInt: |
| 72 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 73 | break; |
| 74 | case Primitive::kPrimLong: |
| 75 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 76 | break; |
| 77 | case Primitive::kPrimNot: |
| 78 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj)); |
| 79 | break; |
| 80 | default: |
| 81 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 82 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // Explicitly instantiate all DoFieldGet functions. |
| 88 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 89 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 90 | ShadowFrame& shadow_frame, \ |
| 91 | const Instruction* inst, \ |
| 92 | uint16_t inst_data) |
| 93 | |
| 94 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 95 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 96 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 97 | |
| 98 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 99 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 100 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 101 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 102 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 103 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 104 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 105 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 106 | |
| 107 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 108 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 109 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 110 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 111 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 112 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 113 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 114 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 115 | |
| 116 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 117 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 118 | |
| 119 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 120 | // Returns true on success, otherwise throws an exception and returns false. |
| 121 | template<Primitive::Type field_type> |
| 122 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 123 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 124 | if (UNLIKELY(obj == nullptr)) { |
| 125 | // We lost the reference to the field index so we cannot get a more |
| 126 | // precised exception message. |
| 127 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 128 | return false; |
| 129 | } |
| 130 | MemberOffset field_offset(inst->VRegC_22c()); |
| 131 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 132 | // the field from the base of the object, we need to look for it first. |
| 133 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 134 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 135 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 136 | field_offset.Uint32Value()); |
| 137 | DCHECK(f != nullptr); |
| 138 | DCHECK(!f->IsStatic()); |
| 139 | instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 140 | shadow_frame.GetDexPC(), f); |
| 141 | } |
| 142 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 143 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 144 | switch (field_type) { |
| 145 | case Primitive::kPrimInt: |
| 146 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 147 | break; |
| 148 | case Primitive::kPrimLong: |
| 149 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 150 | break; |
| 151 | case Primitive::kPrimNot: |
| 152 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 153 | break; |
| 154 | default: |
| 155 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 156 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | // Explicitly instantiate all DoIGetQuick functions. |
| 162 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 163 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 164 | uint16_t inst_data) |
| 165 | |
| 166 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 167 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 168 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
| 169 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 170 | |
| 171 | template<Primitive::Type field_type> |
| 172 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
| 173 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 174 | JValue field_value; |
| 175 | switch (field_type) { |
| 176 | case Primitive::kPrimBoolean: |
| 177 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 178 | break; |
| 179 | case Primitive::kPrimByte: |
| 180 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 181 | break; |
| 182 | case Primitive::kPrimChar: |
| 183 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 184 | break; |
| 185 | case Primitive::kPrimShort: |
| 186 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 187 | break; |
| 188 | case Primitive::kPrimInt: |
| 189 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 190 | break; |
| 191 | case Primitive::kPrimLong: |
| 192 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 193 | break; |
| 194 | case Primitive::kPrimNot: |
| 195 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 196 | break; |
| 197 | default: |
| 198 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 199 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 200 | } |
| 201 | return field_value; |
| 202 | } |
| 203 | |
| 204 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 205 | bool transaction_active> |
| 206 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 207 | uint16_t inst_data) { |
| 208 | bool do_assignability_check = do_access_check; |
| 209 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 210 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 211 | 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] | 212 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 213 | if (UNLIKELY(f == nullptr)) { |
| 214 | CHECK(self->IsExceptionPending()); |
| 215 | return false; |
| 216 | } |
| 217 | Object* obj; |
| 218 | if (is_static) { |
| 219 | obj = f->GetDeclaringClass(); |
| 220 | } else { |
| 221 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 222 | if (UNLIKELY(obj == nullptr)) { |
| 223 | ThrowNullPointerExceptionForFieldAccess(shadow_frame.GetCurrentLocationForThrow(), |
| 224 | f, false); |
| 225 | return false; |
| 226 | } |
| 227 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 228 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 229 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 230 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 231 | // the field from the base of the object, we need to look for it first. |
| 232 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 233 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 234 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 235 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 236 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 237 | shadow_frame.GetDexPC(), f, field_value); |
| 238 | } |
| 239 | switch (field_type) { |
| 240 | case Primitive::kPrimBoolean: |
| 241 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 242 | break; |
| 243 | case Primitive::kPrimByte: |
| 244 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 245 | break; |
| 246 | case Primitive::kPrimChar: |
| 247 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 248 | break; |
| 249 | case Primitive::kPrimShort: |
| 250 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 251 | break; |
| 252 | case Primitive::kPrimInt: |
| 253 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 254 | break; |
| 255 | case Primitive::kPrimLong: |
| 256 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 257 | break; |
| 258 | case Primitive::kPrimNot: { |
| 259 | Object* reg = shadow_frame.GetVRegReference(vregA); |
| 260 | if (do_assignability_check && reg != nullptr) { |
| 261 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 262 | // object in the destructor. |
| 263 | Class* field_class; |
| 264 | { |
| 265 | StackHandleScope<3> hs(self); |
| 266 | HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f)); |
| 267 | HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 268 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 269 | field_class = h_f->GetType(true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 270 | } |
| 271 | if (!reg->VerifierInstanceOf(field_class)) { |
| 272 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 273 | std::string temp1, temp2, temp3; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 274 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 275 | "Ljava/lang/VirtualMachineError;", |
| 276 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 277 | reg->GetClass()->GetDescriptor(&temp1), |
| 278 | field_class->GetDescriptor(&temp2), |
| 279 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | f->SetObj<transaction_active>(obj, reg); |
| 284 | break; |
| 285 | } |
| 286 | default: |
| 287 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 288 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | // Explicitly instantiate all DoFieldPut functions. |
| 294 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 295 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 296 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 297 | |
| 298 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 299 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 300 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 301 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 302 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 303 | |
| 304 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 305 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 306 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 307 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 308 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 309 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 310 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 311 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 312 | |
| 313 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 314 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 315 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 316 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 317 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 318 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 319 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 320 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 321 | |
| 322 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 323 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 324 | |
| 325 | template<Primitive::Type field_type, bool transaction_active> |
| 326 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 327 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 328 | if (UNLIKELY(obj == nullptr)) { |
| 329 | // We lost the reference to the field index so we cannot get a more |
| 330 | // precised exception message. |
| 331 | ThrowNullPointerExceptionFromDexPC(shadow_frame.GetCurrentLocationForThrow()); |
| 332 | return false; |
| 333 | } |
| 334 | MemberOffset field_offset(inst->VRegC_22c()); |
| 335 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 336 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 337 | // the field from the base of the object, we need to look for it first. |
| 338 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 339 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 340 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 341 | field_offset.Uint32Value()); |
| 342 | DCHECK(f != nullptr); |
| 343 | DCHECK(!f->IsStatic()); |
| 344 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 345 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 346 | shadow_frame.GetDexPC(), f, field_value); |
| 347 | } |
| 348 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 349 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 350 | case Primitive::kPrimBoolean: |
| 351 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 352 | break; |
| 353 | case Primitive::kPrimByte: |
| 354 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 355 | break; |
| 356 | case Primitive::kPrimChar: |
| 357 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 358 | break; |
| 359 | case Primitive::kPrimShort: |
| 360 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 361 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 362 | case Primitive::kPrimInt: |
| 363 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 364 | break; |
| 365 | case Primitive::kPrimLong: |
| 366 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 367 | break; |
| 368 | case Primitive::kPrimNot: |
| 369 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 370 | break; |
| 371 | default: |
| 372 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 373 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | // Explicitly instantiate all DoIPutQuick functions. |
| 379 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 380 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 381 | const Instruction* inst, \ |
| 382 | uint16_t inst_data) |
| 383 | |
| 384 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 385 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 386 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 387 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 388 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 389 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 390 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 391 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 392 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 393 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 394 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 395 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 396 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 397 | |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 398 | /** |
| 399 | * Finds the location where this exception will be caught. We search until we reach either the top |
| 400 | * frame or a native frame, in which cases this exception is considered uncaught. |
| 401 | */ |
| 402 | class CatchLocationFinder : public StackVisitor { |
| 403 | public: |
| 404 | explicit CatchLocationFinder(Thread* self, Handle<mirror::Throwable>* exception) |
| 405 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 406 | : StackVisitor(self, nullptr), self_(self), handle_scope_(self), exception_(exception), |
| 407 | catch_method_(handle_scope_.NewHandle<mirror::ArtMethod>(nullptr)), |
| 408 | catch_dex_pc_(DexFile::kDexNoIndex), clear_exception_(false) { |
| 409 | } |
| 410 | |
| 411 | bool VisitFrame() OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 412 | mirror::ArtMethod* method = GetMethod(); |
| 413 | if (method == nullptr) { |
| 414 | return true; |
| 415 | } |
| 416 | if (method->IsRuntimeMethod()) { |
| 417 | // Ignore callee save method. |
| 418 | DCHECK(method->IsCalleeSaveMethod()); |
| 419 | return true; |
| 420 | } |
| 421 | if (method->IsNative()) { |
| 422 | return false; // End stack walk. |
| 423 | } |
| 424 | DCHECK(!method->IsNative()); |
| 425 | uint32_t dex_pc = GetDexPc(); |
| 426 | if (dex_pc != DexFile::kDexNoIndex) { |
| 427 | uint32_t found_dex_pc; |
| 428 | { |
| 429 | StackHandleScope<3> hs(self_); |
| 430 | Handle<mirror::Class> exception_class(hs.NewHandle((*exception_)->GetClass())); |
| 431 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(method)); |
| 432 | found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc, |
| 433 | &clear_exception_); |
| 434 | } |
| 435 | if (found_dex_pc != DexFile::kDexNoIndex) { |
| 436 | catch_method_.Assign(method); |
| 437 | catch_dex_pc_ = found_dex_pc; |
| 438 | return false; // End stack walk. |
| 439 | } |
| 440 | } |
| 441 | return true; // Continue stack walk. |
| 442 | } |
| 443 | |
| 444 | ArtMethod* GetCatchMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 445 | return catch_method_.Get(); |
| 446 | } |
| 447 | |
| 448 | uint32_t GetCatchDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 449 | return catch_dex_pc_; |
| 450 | } |
| 451 | |
| 452 | bool NeedClearException() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 453 | return clear_exception_; |
| 454 | } |
| 455 | |
| 456 | private: |
| 457 | Thread* const self_; |
| 458 | StackHandleScope<1> handle_scope_; |
| 459 | Handle<mirror::Throwable>* exception_; |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 460 | MutableHandle<mirror::ArtMethod> catch_method_; |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 461 | uint32_t catch_dex_pc_; |
| 462 | bool clear_exception_; |
| 463 | |
| 464 | |
| 465 | DISALLOW_COPY_AND_ASSIGN(CatchLocationFinder); |
| 466 | }; |
| 467 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 468 | uint32_t FindNextInstructionFollowingException(Thread* self, |
| 469 | ShadowFrame& shadow_frame, |
| 470 | uint32_t dex_pc, |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 471 | const instrumentation::Instrumentation* instrumentation) { |
| 472 | self->VerifyStack(); |
| 473 | ThrowLocation throw_location; |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 474 | StackHandleScope<3> hs(self); |
| 475 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location))); |
| 476 | if (!self->IsExceptionReportedToInstrumentation() && instrumentation->HasExceptionCaughtListeners()) { |
| 477 | CatchLocationFinder clf(self, &exception); |
| 478 | clf.WalkStack(false); |
| 479 | instrumentation->ExceptionCaughtEvent(self, throw_location, clf.GetCatchMethod(), |
| 480 | clf.GetCatchDexPc(), exception.Get()); |
| 481 | self->SetExceptionReportedToInstrumentation(true); |
| 482 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 483 | bool clear_exception = false; |
| 484 | uint32_t found_dex_pc; |
| 485 | { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 486 | Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass())); |
| 487 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod())); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 488 | found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc, |
| 489 | &clear_exception); |
| 490 | } |
| 491 | if (found_dex_pc == DexFile::kDexNoIndex) { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 492 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 493 | shadow_frame.GetMethod(), dex_pc); |
| 494 | } else { |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 495 | if (self->IsExceptionReportedToInstrumentation()) { |
| 496 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
| 497 | shadow_frame.GetMethod(), dex_pc); |
| 498 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 499 | if (clear_exception) { |
| 500 | self->ClearException(); |
| 501 | } |
| 502 | } |
| 503 | return found_dex_pc; |
| 504 | } |
| 505 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 506 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 507 | LOG(FATAL) << "Unexpected instruction: " |
| 508 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 509 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 512 | static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 513 | ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 514 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 515 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 516 | // 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] | 517 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 518 | size_t dest_reg, size_t src_reg) |
| 519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 520 | // 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] | 521 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 522 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 523 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 524 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 525 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 526 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 527 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 531 | void AbortTransaction(Thread* self, const char* fmt, ...) { |
| 532 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 533 | // Throw an exception so we can abort the transaction and undo every change. |
| 534 | va_list args; |
| 535 | va_start(args, fmt); |
| 536 | self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt, |
| 537 | args); |
| 538 | va_end(args); |
| 539 | } |
| 540 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 541 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 542 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 543 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 544 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 545 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 546 | 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] | 547 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 548 | if (LIKELY(code_item != NULL)) { |
| 549 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 550 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 551 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 552 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 553 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 556 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 557 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 558 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 559 | ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0, |
| 560 | memory)); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 561 | |
| 562 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 563 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 564 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 565 | // Slow path. |
| 566 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 567 | // register the shadow frame as under construction and allow suspension again. |
| 568 | self->SetShadowFrameUnderConstruction(new_shadow_frame); |
| 569 | self->EndAssertNoThreadSuspension(old_cause); |
| 570 | |
| 571 | // 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] | 572 | // to get the exact type of each reference argument. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 573 | const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 574 | uint32_t shorty_len = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 575 | const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 576 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 577 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 578 | // code. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 579 | uint32_t arg[5]; // only used in invoke-XXX. |
| 580 | uint32_t vregC; // only used in invoke-XXX-range. |
| 581 | if (is_range) { |
| 582 | vregC = inst->VRegC_3rc(); |
| 583 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 584 | inst->GetVarArgs(arg, inst_data); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 585 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 586 | |
| 587 | // Handle receiver apart since it's not part of the shorty. |
| 588 | size_t dest_reg = first_dest_reg; |
| 589 | size_t arg_offset = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 590 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 591 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 592 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 593 | ++dest_reg; |
| 594 | ++arg_offset; |
| 595 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 596 | 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] | 597 | DCHECK_LT(shorty_pos + 1, shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 598 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 599 | switch (shorty[shorty_pos + 1]) { |
| 600 | case 'L': { |
| 601 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 602 | if (do_assignability_check && o != NULL) { |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame^] | 603 | Class* arg_type = |
| 604 | new_shadow_frame->GetMethod()->GetClassFromTypeIndex( |
| 605 | params->GetTypeItem(shorty_pos).type_idx_, true); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 606 | if (arg_type == NULL) { |
| 607 | CHECK(self->IsExceptionPending()); |
| 608 | return false; |
| 609 | } |
| 610 | if (!o->VerifierInstanceOf(arg_type)) { |
| 611 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 612 | std::string temp1, temp2; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 613 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 614 | "Ljava/lang/VirtualMachineError;", |
| 615 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 616 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 617 | o->GetClass()->GetDescriptor(&temp1), |
| 618 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 619 | return false; |
| 620 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 621 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 622 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 623 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 624 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 625 | case 'J': case 'D': { |
| 626 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 627 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 628 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 629 | ++dest_reg; |
| 630 | ++arg_offset; |
| 631 | break; |
| 632 | } |
| 633 | default: |
| 634 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 635 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 636 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 637 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 638 | // We're done with the construction. |
| 639 | self->ClearShadowFrameUnderConstruction(); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 640 | } else { |
| 641 | // Fast path: no extra checks. |
| 642 | if (is_range) { |
| 643 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 644 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 645 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 646 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 647 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 648 | } else { |
| 649 | DCHECK_LE(num_ins, 5U); |
| 650 | uint16_t regList = inst->Fetch16(2); |
| 651 | uint16_t count = num_ins; |
| 652 | if (count == 5) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 653 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, |
| 654 | (inst_data >> 8) & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 655 | --count; |
| 656 | } |
| 657 | 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] | 658 | 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] | 659 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 660 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 661 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 664 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 665 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 666 | if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) { |
| 667 | LOG(FATAL) << "Attempt to invoke non-executable method: " |
| 668 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 669 | UNREACHABLE(); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 670 | } |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 671 | if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 672 | !new_shadow_frame->GetMethod()->IsNative() && |
| 673 | !new_shadow_frame->GetMethod()->IsProxyMethod() && |
| 674 | new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() |
| 675 | == artInterpreterToCompiledCodeBridge) { |
| 676 | LOG(FATAL) << "Attempt to call compiled code when -Xint: " |
| 677 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 678 | UNREACHABLE(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 679 | } |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 680 | (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item, |
| 681 | new_shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 682 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 683 | UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 684 | } |
| 685 | return !self->IsExceptionPending(); |
| 686 | } |
| 687 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 688 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 689 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 690 | Thread* self, JValue* result) { |
| 691 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 692 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 693 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 694 | if (!is_range) { |
| 695 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 696 | CHECK_LE(length, 5); |
| 697 | } |
| 698 | if (UNLIKELY(length < 0)) { |
| 699 | ThrowNegativeArraySizeException(length); |
| 700 | return false; |
| 701 | } |
| 702 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 703 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 704 | self, false, do_access_check); |
| 705 | if (UNLIKELY(arrayClass == NULL)) { |
| 706 | DCHECK(self->IsExceptionPending()); |
| 707 | return false; |
| 708 | } |
| 709 | CHECK(arrayClass->IsArrayClass()); |
| 710 | Class* componentClass = arrayClass->GetComponentType(); |
| 711 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 712 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 713 | ThrowRuntimeException("Bad filled array request for type %s", |
| 714 | PrettyDescriptor(componentClass).c_str()); |
| 715 | } else { |
| 716 | self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(), |
| 717 | "Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 718 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 719 | PrettyDescriptor(componentClass).c_str()); |
| 720 | } |
| 721 | return false; |
| 722 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 723 | Object* newArray = Array::Alloc<true>(self, arrayClass, length, |
| 724 | arrayClass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 725 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 726 | if (UNLIKELY(newArray == NULL)) { |
| 727 | DCHECK(self->IsExceptionPending()); |
| 728 | return false; |
| 729 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 730 | uint32_t arg[5]; // only used in filled-new-array. |
| 731 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 732 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 733 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 734 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 735 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 736 | } |
| 737 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 738 | for (int32_t i = 0; i < length; ++i) { |
| 739 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 740 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 741 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 742 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 743 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 744 | } |
| 745 | } |
| 746 | |
| 747 | result->SetL(newArray); |
| 748 | return true; |
| 749 | } |
| 750 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 751 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 752 | template<typename T> |
| 753 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 754 | NO_THREAD_SAFETY_ANALYSIS { |
| 755 | Runtime* runtime = Runtime::Current(); |
| 756 | for (int32_t i = 0; i < count; ++i) { |
| 757 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 762 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 763 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 764 | DCHECK(array != nullptr); |
| 765 | DCHECK_LE(count, array->GetLength()); |
| 766 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 767 | switch (primitive_component_type) { |
| 768 | case Primitive::kPrimBoolean: |
| 769 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 770 | break; |
| 771 | case Primitive::kPrimByte: |
| 772 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 773 | break; |
| 774 | case Primitive::kPrimChar: |
| 775 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 776 | break; |
| 777 | case Primitive::kPrimShort: |
| 778 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 779 | break; |
| 780 | case Primitive::kPrimInt: |
| 781 | case Primitive::kPrimFloat: |
| 782 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 783 | break; |
| 784 | case Primitive::kPrimLong: |
| 785 | case Primitive::kPrimDouble: |
| 786 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 787 | break; |
| 788 | default: |
| 789 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 790 | << " in fill-array-data"; |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 795 | // Helper function to deal with class loading in an unstarted runtime. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 796 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 797 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 798 | const std::string& method_name, bool initialize_class, |
| 799 | bool abort_if_not_found) |
| 800 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 801 | CHECK(className.Get() != nullptr); |
| 802 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 803 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 804 | |
| 805 | Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 806 | if (found == nullptr && abort_if_not_found) { |
| 807 | if (!self->IsExceptionPending()) { |
| 808 | AbortTransaction(self, "%s failed in un-started runtime for class: %s", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 809 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 810 | } |
| 811 | return; |
| 812 | } |
| 813 | if (found != nullptr && initialize_class) { |
| 814 | StackHandleScope<1> hs(self); |
| 815 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 816 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 817 | CHECK(self->IsExceptionPending()); |
| 818 | return; |
| 819 | } |
| 820 | } |
| 821 | result->SetL(found); |
| 822 | } |
| 823 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 824 | static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 825 | ShadowFrame* shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 826 | JValue* result, size_t arg_offset) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 827 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 828 | // problems in core libraries. |
| 829 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 830 | if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") { |
| 831 | // TODO: Support for the other variants that take more arguments should also be added. |
| 832 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 833 | StackHandleScope<1> hs(self); |
| 834 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 835 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name, |
| 836 | true, true); |
| 837 | } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") { |
| 838 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 839 | StackHandleScope<1> hs(self); |
| 840 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 841 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name, |
| 842 | false, true); |
| 843 | } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") { |
| 844 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 845 | mirror::ClassLoader* class_loader = |
| 846 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 847 | StackHandleScope<2> hs(self); |
| 848 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 849 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 850 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 851 | } else if (name == "java.lang.Class java.lang.Void.lookupType()") { |
| 852 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 853 | } else if (name == "java.lang.Object java.lang.Class.newInstance()") { |
| 854 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 855 | ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V"); |
| 856 | CHECK(c != NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 857 | StackHandleScope<1> hs(self); |
| 858 | Handle<Object> obj(hs.NewHandle(klass->AllocObject(self))); |
| 859 | CHECK(obj.Get() != NULL); |
| 860 | EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL); |
| 861 | result->SetL(obj.Get()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 862 | } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { |
| 863 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 864 | // going the reflective Dex way. |
| 865 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 866 | String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 867 | ArtField* found = NULL; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 868 | ObjectArray<ArtField>* fields = klass->GetIFields(); |
| 869 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 870 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 871 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 872 | found = f; |
| 873 | } |
| 874 | } |
| 875 | if (found == NULL) { |
| 876 | fields = klass->GetSFields(); |
| 877 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 878 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 879 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 880 | found = f; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | CHECK(found != NULL) |
| 885 | << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 886 | << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 887 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 888 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 889 | Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 890 | StackHandleScope<1> hs(self); |
| 891 | Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self))); |
| 892 | CHECK(field.Get() != NULL); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 893 | ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); |
| 894 | uint32_t args[1]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 895 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 896 | EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL); |
| 897 | result->SetL(field.Get()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 898 | } else if (name == "int java.lang.Object.hashCode()") { |
| 899 | Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 900 | result->SetI(obj->IdentityHashCode()); |
| 901 | } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") { |
Ian Rogers | 6b14d55 | 2014-10-28 21:50:58 -0700 | [diff] [blame] | 902 | mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod(); |
| 903 | result->SetL(method->GetNameAsString(self)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 904 | } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || |
| 905 | name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") { |
| 906 | // Special case array copying without initializing System. |
| 907 | Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 908 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 909 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 910 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 911 | if (!ctype->IsPrimitive()) { |
| 912 | ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); |
| 913 | ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); |
| 914 | for (jint i = 0; i < length; ++i) { |
| 915 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 916 | } |
| 917 | } else if (ctype->IsPrimitiveChar()) { |
| 918 | CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 919 | CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 920 | for (jint i = 0; i < length; ++i) { |
| 921 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 922 | } |
| 923 | } else if (ctype->IsPrimitiveInt()) { |
| 924 | IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 925 | IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 926 | for (jint i = 0; i < length; ++i) { |
| 927 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 928 | } |
| 929 | } else { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 930 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 931 | "Unimplemented System.arraycopy for type '%s'", |
| 932 | PrettyDescriptor(ctype).c_str()); |
| 933 | } |
| 934 | } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") { |
| 935 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 936 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") { |
| 937 | // Allocate non-threadlocal buffer. |
| 938 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
| 939 | } else { |
| 940 | self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 941 | "Unimplemented ThreadLocal.get"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 942 | } |
| 943 | } else { |
| 944 | // Not special, continue with regular interpreter execution. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 945 | artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 949 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 950 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 951 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 952 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 953 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 954 | const Instruction* inst, uint16_t inst_data, \ |
| 955 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 956 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 957 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 958 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 959 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 960 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 961 | |
| 962 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 963 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 964 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 965 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 966 | const ShadowFrame& shadow_frame, \ |
| 967 | Thread* self, JValue* result) |
| 968 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 969 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 970 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 971 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 972 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 973 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 974 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 975 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 976 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 977 | |
| 978 | } // namespace interpreter |
| 979 | } // namespace art |