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 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 541 | static mirror::Class* GetClassFromTypeIdx(mirror::ArtMethod* method, uint16_t type_idx) |
| 542 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 543 | mirror::Class* type = method->GetDexCacheResolvedType(type_idx); |
| 544 | if (type == nullptr) { |
| 545 | type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 546 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
| 547 | } |
| 548 | return type; |
| 549 | } |
| 550 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 551 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 552 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 553 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 554 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 555 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 556 | 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] | 557 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 558 | if (LIKELY(code_item != NULL)) { |
| 559 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 560 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 561 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 562 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 563 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 566 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 567 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 568 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 569 | ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0, |
| 570 | memory)); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 571 | |
| 572 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 573 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 574 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 575 | // Slow path. |
| 576 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 577 | // register the shadow frame as under construction and allow suspension again. |
| 578 | self->SetShadowFrameUnderConstruction(new_shadow_frame); |
| 579 | self->EndAssertNoThreadSuspension(old_cause); |
| 580 | |
| 581 | // 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] | 582 | // to get the exact type of each reference argument. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 583 | const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 584 | uint32_t shorty_len = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 585 | const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 586 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 587 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 588 | // code. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 589 | uint32_t arg[5]; // only used in invoke-XXX. |
| 590 | uint32_t vregC; // only used in invoke-XXX-range. |
| 591 | if (is_range) { |
| 592 | vregC = inst->VRegC_3rc(); |
| 593 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 594 | inst->GetVarArgs(arg, inst_data); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 595 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 596 | |
| 597 | // Handle receiver apart since it's not part of the shorty. |
| 598 | size_t dest_reg = first_dest_reg; |
| 599 | size_t arg_offset = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 600 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 601 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 602 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 603 | ++dest_reg; |
| 604 | ++arg_offset; |
| 605 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 606 | 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] | 607 | DCHECK_LT(shorty_pos + 1, shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 608 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 609 | switch (shorty[shorty_pos + 1]) { |
| 610 | case 'L': { |
| 611 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 612 | if (do_assignability_check && o != NULL) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 613 | Class* arg_type = GetClassFromTypeIdx(new_shadow_frame->GetMethod(), |
| 614 | params->GetTypeItem(shorty_pos).type_idx_); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 615 | if (arg_type == NULL) { |
| 616 | CHECK(self->IsExceptionPending()); |
| 617 | return false; |
| 618 | } |
| 619 | if (!o->VerifierInstanceOf(arg_type)) { |
| 620 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 621 | std::string temp1, temp2; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 622 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 623 | "Ljava/lang/VirtualMachineError;", |
| 624 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 625 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 626 | o->GetClass()->GetDescriptor(&temp1), |
| 627 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 628 | return false; |
| 629 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 630 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 631 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 632 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 633 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 634 | case 'J': case 'D': { |
| 635 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 636 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 637 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 638 | ++dest_reg; |
| 639 | ++arg_offset; |
| 640 | break; |
| 641 | } |
| 642 | default: |
| 643 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 644 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 645 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 646 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 647 | // We're done with the construction. |
| 648 | self->ClearShadowFrameUnderConstruction(); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 649 | } else { |
| 650 | // Fast path: no extra checks. |
| 651 | if (is_range) { |
| 652 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 653 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 654 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 655 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 656 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 657 | } else { |
| 658 | DCHECK_LE(num_ins, 5U); |
| 659 | uint16_t regList = inst->Fetch16(2); |
| 660 | uint16_t count = num_ins; |
| 661 | if (count == 5) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 662 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, |
| 663 | (inst_data >> 8) & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 664 | --count; |
| 665 | } |
| 666 | 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] | 667 | 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] | 668 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 669 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 670 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 671 | } |
| 672 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 673 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 674 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 675 | if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) { |
| 676 | LOG(FATAL) << "Attempt to invoke non-executable method: " |
| 677 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 678 | UNREACHABLE(); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 679 | } |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 680 | if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 681 | !new_shadow_frame->GetMethod()->IsNative() && |
| 682 | !new_shadow_frame->GetMethod()->IsProxyMethod() && |
| 683 | new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() |
| 684 | == artInterpreterToCompiledCodeBridge) { |
| 685 | LOG(FATAL) << "Attempt to call compiled code when -Xint: " |
| 686 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 687 | UNREACHABLE(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 688 | } |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 689 | (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item, |
| 690 | new_shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 691 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 692 | UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 693 | } |
| 694 | return !self->IsExceptionPending(); |
| 695 | } |
| 696 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 697 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 698 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 699 | Thread* self, JValue* result) { |
| 700 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 701 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 702 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 703 | if (!is_range) { |
| 704 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 705 | CHECK_LE(length, 5); |
| 706 | } |
| 707 | if (UNLIKELY(length < 0)) { |
| 708 | ThrowNegativeArraySizeException(length); |
| 709 | return false; |
| 710 | } |
| 711 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 712 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 713 | self, false, do_access_check); |
| 714 | if (UNLIKELY(arrayClass == NULL)) { |
| 715 | DCHECK(self->IsExceptionPending()); |
| 716 | return false; |
| 717 | } |
| 718 | CHECK(arrayClass->IsArrayClass()); |
| 719 | Class* componentClass = arrayClass->GetComponentType(); |
| 720 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 721 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 722 | ThrowRuntimeException("Bad filled array request for type %s", |
| 723 | PrettyDescriptor(componentClass).c_str()); |
| 724 | } else { |
| 725 | self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(), |
| 726 | "Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 727 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 728 | PrettyDescriptor(componentClass).c_str()); |
| 729 | } |
| 730 | return false; |
| 731 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 732 | Object* newArray = Array::Alloc<true>(self, arrayClass, length, |
| 733 | arrayClass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 734 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 735 | if (UNLIKELY(newArray == NULL)) { |
| 736 | DCHECK(self->IsExceptionPending()); |
| 737 | return false; |
| 738 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 739 | uint32_t arg[5]; // only used in filled-new-array. |
| 740 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 741 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 742 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 743 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 744 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 745 | } |
| 746 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 747 | for (int32_t i = 0; i < length; ++i) { |
| 748 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 749 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 750 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 751 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 752 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
| 756 | result->SetL(newArray); |
| 757 | return true; |
| 758 | } |
| 759 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 760 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 761 | template<typename T> |
| 762 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 763 | NO_THREAD_SAFETY_ANALYSIS { |
| 764 | Runtime* runtime = Runtime::Current(); |
| 765 | for (int32_t i = 0; i < count; ++i) { |
| 766 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 771 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 772 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 773 | DCHECK(array != nullptr); |
| 774 | DCHECK_LE(count, array->GetLength()); |
| 775 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 776 | switch (primitive_component_type) { |
| 777 | case Primitive::kPrimBoolean: |
| 778 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 779 | break; |
| 780 | case Primitive::kPrimByte: |
| 781 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 782 | break; |
| 783 | case Primitive::kPrimChar: |
| 784 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 785 | break; |
| 786 | case Primitive::kPrimShort: |
| 787 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 788 | break; |
| 789 | case Primitive::kPrimInt: |
| 790 | case Primitive::kPrimFloat: |
| 791 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 792 | break; |
| 793 | case Primitive::kPrimLong: |
| 794 | case Primitive::kPrimDouble: |
| 795 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 796 | break; |
| 797 | default: |
| 798 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 799 | << " in fill-array-data"; |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 804 | // Helper function to deal with class loading in an unstarted runtime. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 805 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 806 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 807 | const std::string& method_name, bool initialize_class, |
| 808 | bool abort_if_not_found) |
| 809 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 810 | CHECK(className.Get() != nullptr); |
| 811 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 812 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 813 | |
| 814 | Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 815 | if (found == nullptr && abort_if_not_found) { |
| 816 | if (!self->IsExceptionPending()) { |
| 817 | AbortTransaction(self, "%s failed in un-started runtime for class: %s", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 818 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 819 | } |
| 820 | return; |
| 821 | } |
| 822 | if (found != nullptr && initialize_class) { |
| 823 | StackHandleScope<1> hs(self); |
| 824 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 825 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 826 | CHECK(self->IsExceptionPending()); |
| 827 | return; |
| 828 | } |
| 829 | } |
| 830 | result->SetL(found); |
| 831 | } |
| 832 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 833 | static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 834 | ShadowFrame* shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 835 | JValue* result, size_t arg_offset) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 836 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 837 | // problems in core libraries. |
| 838 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 839 | if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") { |
| 840 | // TODO: Support for the other variants that take more arguments should also be added. |
| 841 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 842 | StackHandleScope<1> hs(self); |
| 843 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 844 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name, |
| 845 | true, true); |
| 846 | } else if (name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") { |
| 847 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 848 | StackHandleScope<1> hs(self); |
| 849 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 850 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name, |
| 851 | false, true); |
| 852 | } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") { |
| 853 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 854 | mirror::ClassLoader* class_loader = |
| 855 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 856 | StackHandleScope<2> hs(self); |
| 857 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 858 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 859 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 860 | } else if (name == "java.lang.Class java.lang.Void.lookupType()") { |
| 861 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 862 | } else if (name == "java.lang.Object java.lang.Class.newInstance()") { |
| 863 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 864 | ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V"); |
| 865 | CHECK(c != NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 866 | StackHandleScope<1> hs(self); |
| 867 | Handle<Object> obj(hs.NewHandle(klass->AllocObject(self))); |
| 868 | CHECK(obj.Get() != NULL); |
| 869 | EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL); |
| 870 | result->SetL(obj.Get()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 871 | } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { |
| 872 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 873 | // going the reflective Dex way. |
| 874 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 875 | String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 876 | ArtField* found = NULL; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 877 | ObjectArray<ArtField>* fields = klass->GetIFields(); |
| 878 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 879 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 880 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 881 | found = f; |
| 882 | } |
| 883 | } |
| 884 | if (found == NULL) { |
| 885 | fields = klass->GetSFields(); |
| 886 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 887 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 888 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 889 | found = f; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | CHECK(found != NULL) |
| 894 | << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 895 | << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 896 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 897 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 898 | Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 899 | StackHandleScope<1> hs(self); |
| 900 | Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self))); |
| 901 | CHECK(field.Get() != NULL); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 902 | ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); |
| 903 | uint32_t args[1]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 904 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 905 | EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL); |
| 906 | result->SetL(field.Get()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 907 | } else if (name == "int java.lang.Object.hashCode()") { |
| 908 | Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 909 | result->SetI(obj->IdentityHashCode()); |
| 910 | } 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] | 911 | mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod(); |
| 912 | result->SetL(method->GetNameAsString(self)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 913 | } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || |
| 914 | name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") { |
| 915 | // Special case array copying without initializing System. |
| 916 | Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 917 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 918 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 919 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 920 | if (!ctype->IsPrimitive()) { |
| 921 | ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); |
| 922 | ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); |
| 923 | for (jint i = 0; i < length; ++i) { |
| 924 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 925 | } |
| 926 | } else if (ctype->IsPrimitiveChar()) { |
| 927 | CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 928 | CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 929 | for (jint i = 0; i < length; ++i) { |
| 930 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 931 | } |
| 932 | } else if (ctype->IsPrimitiveInt()) { |
| 933 | IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 934 | IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 935 | for (jint i = 0; i < length; ++i) { |
| 936 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 937 | } |
| 938 | } else { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 939 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 940 | "Unimplemented System.arraycopy for type '%s'", |
| 941 | PrettyDescriptor(ctype).c_str()); |
| 942 | } |
| 943 | } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") { |
| 944 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 945 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") { |
| 946 | // Allocate non-threadlocal buffer. |
| 947 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
| 948 | } else { |
| 949 | self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 950 | "Unimplemented ThreadLocal.get"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 951 | } |
| 952 | } else { |
| 953 | // Not special, continue with regular interpreter execution. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 954 | artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 958 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 959 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 960 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 961 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 962 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 963 | const Instruction* inst, uint16_t inst_data, \ |
| 964 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 965 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 966 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 967 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 968 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 969 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 970 | |
| 971 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 972 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 973 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 974 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 975 | const ShadowFrame& shadow_frame, \ |
| 976 | Thread* self, JValue* result) |
| 977 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 978 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 979 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 980 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 981 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 982 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 983 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 984 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 985 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 986 | |
| 987 | } // namespace interpreter |
| 988 | } // namespace art |