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