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