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 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 26 | void ThrowNullPointerExceptionFromInterpreter() { |
| 27 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 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)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 47 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 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. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 129 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 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)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 241 | ThrowNullPointerExceptionForFieldAccess(f, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 245 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 246 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 247 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 248 | // the field from the base of the object, we need to look for it first. |
| 249 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 250 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 251 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 252 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 253 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 254 | shadow_frame.GetDexPC(), f, field_value); |
| 255 | } |
| 256 | switch (field_type) { |
| 257 | case Primitive::kPrimBoolean: |
| 258 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 259 | break; |
| 260 | case Primitive::kPrimByte: |
| 261 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 262 | break; |
| 263 | case Primitive::kPrimChar: |
| 264 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 265 | break; |
| 266 | case Primitive::kPrimShort: |
| 267 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 268 | break; |
| 269 | case Primitive::kPrimInt: |
| 270 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 271 | break; |
| 272 | case Primitive::kPrimLong: |
| 273 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 274 | break; |
| 275 | case Primitive::kPrimNot: { |
| 276 | Object* reg = shadow_frame.GetVRegReference(vregA); |
| 277 | if (do_assignability_check && reg != nullptr) { |
| 278 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 279 | // object in the destructor. |
| 280 | Class* field_class; |
| 281 | { |
| 282 | StackHandleScope<3> hs(self); |
| 283 | HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f)); |
| 284 | HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 285 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 286 | field_class = h_f->GetType(true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 287 | } |
| 288 | if (!reg->VerifierInstanceOf(field_class)) { |
| 289 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 290 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 291 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 292 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 293 | reg->GetClass()->GetDescriptor(&temp1), |
| 294 | field_class->GetDescriptor(&temp2), |
| 295 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | f->SetObj<transaction_active>(obj, reg); |
| 300 | break; |
| 301 | } |
| 302 | default: |
| 303 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 304 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | // Explicitly instantiate all DoFieldPut functions. |
| 310 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 311 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 312 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 313 | |
| 314 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 315 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 316 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 317 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 318 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 319 | |
| 320 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 321 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 322 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 323 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 324 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 325 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 326 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 327 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 328 | |
| 329 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 330 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 331 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 332 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 333 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 334 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 335 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 336 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 337 | |
| 338 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 339 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 340 | |
| 341 | template<Primitive::Type field_type, bool transaction_active> |
| 342 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 343 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 344 | if (UNLIKELY(obj == nullptr)) { |
| 345 | // We lost the reference to the field index so we cannot get a more |
| 346 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 347 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | MemberOffset field_offset(inst->VRegC_22c()); |
| 351 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 352 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 353 | // the field from the base of the object, we need to look for it first. |
| 354 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 355 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 356 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 357 | field_offset.Uint32Value()); |
| 358 | DCHECK(f != nullptr); |
| 359 | DCHECK(!f->IsStatic()); |
| 360 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 361 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 362 | shadow_frame.GetDexPC(), f, field_value); |
| 363 | } |
| 364 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 365 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 366 | case Primitive::kPrimBoolean: |
| 367 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 368 | break; |
| 369 | case Primitive::kPrimByte: |
| 370 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 371 | break; |
| 372 | case Primitive::kPrimChar: |
| 373 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 374 | break; |
| 375 | case Primitive::kPrimShort: |
| 376 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 377 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 378 | case Primitive::kPrimInt: |
| 379 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 380 | break; |
| 381 | case Primitive::kPrimLong: |
| 382 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 383 | break; |
| 384 | case Primitive::kPrimNot: |
| 385 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 386 | break; |
| 387 | default: |
| 388 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 389 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // Explicitly instantiate all DoIPutQuick functions. |
| 395 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 396 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 397 | const Instruction* inst, \ |
| 398 | uint16_t inst_data) |
| 399 | |
| 400 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 401 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 402 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 403 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 404 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 405 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 406 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 407 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 408 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 409 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 410 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 411 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 412 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 413 | |
| 414 | uint32_t FindNextInstructionFollowingException(Thread* self, |
| 415 | ShadowFrame& shadow_frame, |
| 416 | uint32_t dex_pc, |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 417 | const instrumentation::Instrumentation* instrumentation) { |
| 418 | self->VerifyStack(); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 419 | StackHandleScope<3> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 420 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 421 | if (instrumentation->HasExceptionCaughtListeners() |
| 422 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 423 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 424 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 425 | bool clear_exception = false; |
| 426 | uint32_t found_dex_pc; |
| 427 | { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 428 | Handle<mirror::Class> exception_class(hs.NewHandle(exception->GetClass())); |
| 429 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(shadow_frame.GetMethod())); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 430 | found_dex_pc = mirror::ArtMethod::FindCatchBlock(h_method, exception_class, dex_pc, |
| 431 | &clear_exception); |
| 432 | } |
| 433 | if (found_dex_pc == DexFile::kDexNoIndex) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 434 | // Exception is not caught by the current method. We will unwind to the |
| 435 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 436 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 437 | shadow_frame.GetMethod(), dex_pc); |
| 438 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 439 | // Exception is caught in the current method. We will jump to the found_dex_pc. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 440 | if (clear_exception) { |
| 441 | self->ClearException(); |
| 442 | } |
| 443 | } |
| 444 | return found_dex_pc; |
| 445 | } |
| 446 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 447 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 448 | LOG(FATAL) << "Unexpected instruction: " |
| 449 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 450 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 453 | static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 454 | ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 455 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 456 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 457 | // 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] | 458 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 459 | size_t dest_reg, size_t src_reg) |
| 460 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 461 | // 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] | 462 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 463 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 464 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 465 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 466 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 467 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 468 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 472 | void AbortTransaction(Thread* self, const char* fmt, ...) { |
| 473 | CHECK(Runtime::Current()->IsActiveTransaction()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 474 | // Constructs abort message. |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 475 | va_list args; |
| 476 | va_start(args, fmt); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 477 | std::string abort_msg; |
| 478 | StringAppendV(&abort_msg, fmt, args); |
| 479 | // Throws an exception so we can abort the transaction and rollback every change. |
| 480 | Runtime::Current()->AbortTransactionAndThrowInternalError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 481 | va_end(args); |
| 482 | } |
| 483 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 484 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 485 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 486 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 487 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 488 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 489 | 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] | 490 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 491 | if (LIKELY(code_item != NULL)) { |
| 492 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 493 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 494 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 495 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 496 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 499 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 500 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 501 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 502 | ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, called_method, 0, |
| 503 | memory)); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 504 | |
| 505 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 506 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 507 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 508 | // Slow path. |
| 509 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 510 | // register the shadow frame as under construction and allow suspension again. |
| 511 | self->SetShadowFrameUnderConstruction(new_shadow_frame); |
| 512 | self->EndAssertNoThreadSuspension(old_cause); |
| 513 | |
| 514 | // 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] | 515 | // to get the exact type of each reference argument. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 516 | const DexFile::TypeList* params = new_shadow_frame->GetMethod()->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 517 | uint32_t shorty_len = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 518 | const char* shorty = new_shadow_frame->GetMethod()->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 519 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 520 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 521 | // code. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 522 | uint32_t arg[5]; // only used in invoke-XXX. |
| 523 | uint32_t vregC; // only used in invoke-XXX-range. |
| 524 | if (is_range) { |
| 525 | vregC = inst->VRegC_3rc(); |
| 526 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 527 | inst->GetVarArgs(arg, inst_data); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 528 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 529 | |
| 530 | // Handle receiver apart since it's not part of the shorty. |
| 531 | size_t dest_reg = first_dest_reg; |
| 532 | size_t arg_offset = 0; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 533 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 534 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 535 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 536 | ++dest_reg; |
| 537 | ++arg_offset; |
| 538 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 539 | 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] | 540 | DCHECK_LT(shorty_pos + 1, shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 541 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 542 | switch (shorty[shorty_pos + 1]) { |
| 543 | case 'L': { |
| 544 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 545 | if (do_assignability_check && o != NULL) { |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 546 | Class* arg_type = |
| 547 | new_shadow_frame->GetMethod()->GetClassFromTypeIndex( |
| 548 | params->GetTypeItem(shorty_pos).type_idx_, true); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 549 | if (arg_type == NULL) { |
| 550 | CHECK(self->IsExceptionPending()); |
| 551 | return false; |
| 552 | } |
| 553 | if (!o->VerifierInstanceOf(arg_type)) { |
| 554 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 555 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 556 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 557 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 558 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 559 | o->GetClass()->GetDescriptor(&temp1), |
| 560 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 561 | return false; |
| 562 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 563 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 564 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 565 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 566 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 567 | case 'J': case 'D': { |
| 568 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 569 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 570 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 571 | ++dest_reg; |
| 572 | ++arg_offset; |
| 573 | break; |
| 574 | } |
| 575 | default: |
| 576 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 577 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 578 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 579 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 580 | // We're done with the construction. |
| 581 | self->ClearShadowFrameUnderConstruction(); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 582 | } else { |
| 583 | // Fast path: no extra checks. |
| 584 | if (is_range) { |
| 585 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 586 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 587 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 588 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 589 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 590 | } else { |
| 591 | DCHECK_LE(num_ins, 5U); |
| 592 | uint16_t regList = inst->Fetch16(2); |
| 593 | uint16_t count = num_ins; |
| 594 | if (count == 5) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 595 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, |
| 596 | (inst_data >> 8) & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 597 | --count; |
| 598 | } |
| 599 | 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] | 600 | 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] | 601 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 602 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 603 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 606 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 607 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 608 | if (kIsDebugBuild && new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() == nullptr) { |
| 609 | LOG(FATAL) << "Attempt to invoke non-executable method: " |
| 610 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 611 | UNREACHABLE(); |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 612 | } |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 613 | if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 614 | !new_shadow_frame->GetMethod()->IsNative() && |
| 615 | !new_shadow_frame->GetMethod()->IsProxyMethod() && |
| 616 | new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter() |
| 617 | == artInterpreterToCompiledCodeBridge) { |
| 618 | LOG(FATAL) << "Attempt to call compiled code when -Xint: " |
| 619 | << PrettyMethod(new_shadow_frame->GetMethod()); |
| 620 | UNREACHABLE(); |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 621 | } |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 622 | (new_shadow_frame->GetMethod()->GetEntryPointFromInterpreter())(self, code_item, |
| 623 | new_shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 624 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 625 | UnstartedRuntimeInvoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 626 | } |
| 627 | return !self->IsExceptionPending(); |
| 628 | } |
| 629 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 630 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 631 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 632 | Thread* self, JValue* result) { |
| 633 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 634 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 635 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 636 | if (!is_range) { |
| 637 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 638 | CHECK_LE(length, 5); |
| 639 | } |
| 640 | if (UNLIKELY(length < 0)) { |
| 641 | ThrowNegativeArraySizeException(length); |
| 642 | return false; |
| 643 | } |
| 644 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 645 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 646 | self, false, do_access_check); |
| 647 | if (UNLIKELY(arrayClass == NULL)) { |
| 648 | DCHECK(self->IsExceptionPending()); |
| 649 | return false; |
| 650 | } |
| 651 | CHECK(arrayClass->IsArrayClass()); |
| 652 | Class* componentClass = arrayClass->GetComponentType(); |
| 653 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 654 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 655 | ThrowRuntimeException("Bad filled array request for type %s", |
| 656 | PrettyDescriptor(componentClass).c_str()); |
| 657 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 658 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 659 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 660 | PrettyDescriptor(componentClass).c_str()); |
| 661 | } |
| 662 | return false; |
| 663 | } |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 664 | Object* newArray = Array::Alloc<true>(self, arrayClass, length, |
| 665 | arrayClass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 666 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 667 | if (UNLIKELY(newArray == NULL)) { |
| 668 | DCHECK(self->IsExceptionPending()); |
| 669 | return false; |
| 670 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 671 | uint32_t arg[5]; // only used in filled-new-array. |
| 672 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 673 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 674 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 675 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 676 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 677 | } |
| 678 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 679 | for (int32_t i = 0; i < length; ++i) { |
| 680 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 681 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 682 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 683 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 684 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
| 688 | result->SetL(newArray); |
| 689 | return true; |
| 690 | } |
| 691 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 692 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 693 | template<typename T> |
| 694 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 695 | NO_THREAD_SAFETY_ANALYSIS { |
| 696 | Runtime* runtime = Runtime::Current(); |
| 697 | for (int32_t i = 0; i < count; ++i) { |
| 698 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 703 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 704 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 705 | DCHECK(array != nullptr); |
| 706 | DCHECK_LE(count, array->GetLength()); |
| 707 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 708 | switch (primitive_component_type) { |
| 709 | case Primitive::kPrimBoolean: |
| 710 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 711 | break; |
| 712 | case Primitive::kPrimByte: |
| 713 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 714 | break; |
| 715 | case Primitive::kPrimChar: |
| 716 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 717 | break; |
| 718 | case Primitive::kPrimShort: |
| 719 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 720 | break; |
| 721 | case Primitive::kPrimInt: |
| 722 | case Primitive::kPrimFloat: |
| 723 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 724 | break; |
| 725 | case Primitive::kPrimLong: |
| 726 | case Primitive::kPrimDouble: |
| 727 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 728 | break; |
| 729 | default: |
| 730 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 731 | << " in fill-array-data"; |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 736 | // Helper function to deal with class loading in an unstarted runtime. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 737 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 738 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 739 | const std::string& method_name, bool initialize_class, |
| 740 | bool abort_if_not_found) |
| 741 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 742 | CHECK(className.Get() != nullptr); |
| 743 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 744 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 745 | |
| 746 | Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 747 | if (found == nullptr && abort_if_not_found) { |
| 748 | if (!self->IsExceptionPending()) { |
| 749 | AbortTransaction(self, "%s failed in un-started runtime for class: %s", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 750 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 751 | } |
| 752 | return; |
| 753 | } |
| 754 | if (found != nullptr && initialize_class) { |
| 755 | StackHandleScope<1> hs(self); |
| 756 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 757 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 758 | CHECK(self->IsExceptionPending()); |
| 759 | return; |
| 760 | } |
| 761 | } |
| 762 | result->SetL(found); |
| 763 | } |
| 764 | |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 765 | // Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that |
| 766 | // rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into |
| 767 | // ClassNotFoundException), so need to do the same. The only exception is if the exception is |
| 768 | // actually InternalError. This must not be wrapped, as it signals an initialization abort. |
| 769 | static void CheckExceptionGenerateClassNotFound(Thread* self) |
| 770 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 771 | if (self->IsExceptionPending()) { |
| 772 | // If it is not an InternalError, wrap it. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 773 | std::string type(PrettyTypeOf(self->GetException())); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 774 | if (type != "java.lang.InternalError") { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 775 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 776 | "ClassNotFoundException"); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 781 | static void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 782 | ShadowFrame* shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 783 | JValue* result, size_t arg_offset) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 784 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 785 | // problems in core libraries. |
| 786 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 787 | if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") { |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 788 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 789 | StackHandleScope<1> hs(self); |
| 790 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 791 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, name, |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 792 | true, false); |
| 793 | CheckExceptionGenerateClassNotFound(self); |
| 794 | } 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] | 795 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 796 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 797 | mirror::ClassLoader* class_loader = |
| 798 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 799 | StackHandleScope<2> hs(self); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 800 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 801 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 802 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, initialize_class, |
| 803 | false); |
| 804 | CheckExceptionGenerateClassNotFound(self); |
| 805 | } else if (name == "java.lang.Class java.lang.Class.classForName(java.lang.String, boolean, java.lang.ClassLoader)") { |
| 806 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 807 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 808 | mirror::ClassLoader* class_loader = |
| 809 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 810 | StackHandleScope<2> hs(self); |
| 811 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 812 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 813 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, initialize_class, |
| 814 | false); |
| 815 | CheckExceptionGenerateClassNotFound(self); |
Sebastien Hertz | 4e99b3d | 2014-06-24 14:35:40 +0200 | [diff] [blame] | 816 | } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") { |
| 817 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 818 | mirror::ClassLoader* class_loader = |
| 819 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 820 | StackHandleScope<2> hs(self); |
| 821 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 822 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 823 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, name, false, false); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 824 | // This might have an error pending. But semantics are to just return null. |
| 825 | if (self->IsExceptionPending()) { |
| 826 | // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 827 | std::string type(PrettyTypeOf(self->GetException())); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 828 | if (type != "java.lang.InternalError") { |
| 829 | self->ClearException(); |
| 830 | } |
| 831 | } |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 832 | } else if (name == "java.lang.Class java.lang.Void.lookupType()") { |
| 833 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 834 | } else if (name == "java.lang.Object java.lang.Class.newInstance()") { |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 835 | StackHandleScope<3> hs(self); // Class, constructor, object. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 836 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 837 | Handle<Class> h_klass(hs.NewHandle(klass)); |
| 838 | // There are two situations in which we'll abort this run. |
| 839 | // 1) If the class isn't yet initialized and initialization fails. |
| 840 | // 2) If we can't find the default constructor. We'll postpone the exception to runtime. |
| 841 | // Note that 2) could likely be handled here, but for safety abort the transaction. |
| 842 | bool ok = false; |
| 843 | if (Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) { |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 844 | Handle<ArtMethod> h_cons(hs.NewHandle(h_klass->FindDeclaredDirectMethod("<init>", "()V"))); |
| 845 | if (h_cons.Get() != nullptr) { |
| 846 | Handle<Object> h_obj(hs.NewHandle(klass->AllocObject(self))); |
| 847 | CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time. |
| 848 | EnterInterpreterFromInvoke(self, h_cons.Get(), h_obj.Get(), nullptr, nullptr); |
| 849 | if (!self->IsExceptionPending()) { |
| 850 | result->SetL(h_obj.Get()); |
| 851 | ok = true; |
| 852 | } |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 853 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 854 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 855 | "Could not find default constructor for '%s'", |
| 856 | PrettyClass(h_klass.Get()).c_str()); |
| 857 | } |
| 858 | } |
| 859 | if (!ok) { |
| 860 | std::string error_msg = StringPrintf("Failed in Class.newInstance for '%s' with %s", |
| 861 | PrettyClass(h_klass.Get()).c_str(), |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 862 | PrettyTypeOf(self->GetException()).c_str()); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 863 | self->ThrowNewWrappedException("Ljava/lang/InternalError;", error_msg.c_str()); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 864 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 865 | } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { |
| 866 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 867 | // going the reflective Dex way. |
| 868 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 869 | String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 870 | ArtField* found = NULL; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 871 | ObjectArray<ArtField>* fields = klass->GetIFields(); |
| 872 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 873 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 874 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 875 | found = f; |
| 876 | } |
| 877 | } |
| 878 | if (found == NULL) { |
| 879 | fields = klass->GetSFields(); |
| 880 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 881 | ArtField* f = fields->Get(i); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 882 | if (name2->Equals(f->GetName())) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 883 | found = f; |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | CHECK(found != NULL) |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 888 | << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" |
| 889 | << name2->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 890 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 891 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 892 | Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 893 | StackHandleScope<1> hs(self); |
| 894 | Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self))); |
| 895 | CHECK(field.Get() != NULL); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 896 | ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); |
| 897 | uint32_t args[1]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 898 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 899 | EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL); |
| 900 | result->SetL(field.Get()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 901 | } else if (name == "int java.lang.Object.hashCode()") { |
| 902 | Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 903 | result->SetI(obj->IdentityHashCode()); |
| 904 | } 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] | 905 | mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod(); |
| 906 | result->SetL(method->GetNameAsString(self)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 907 | } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || |
Andreas Gampe | e2be653 | 2015-03-06 17:11:47 -0800 | [diff] [blame] | 908 | name == "void java.lang.System.arraycopy(char[], int, char[], int, int)" || |
| 909 | name == "void java.lang.System.arraycopy(int[], int, int[], int, int)") { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 910 | // Special case array copying without initializing System. |
| 911 | Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 912 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 913 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 914 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 915 | if (!ctype->IsPrimitive()) { |
| 916 | ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); |
| 917 | ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); |
| 918 | for (jint i = 0; i < length; ++i) { |
| 919 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 920 | } |
| 921 | } else if (ctype->IsPrimitiveChar()) { |
| 922 | CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 923 | CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 924 | for (jint i = 0; i < length; ++i) { |
| 925 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 926 | } |
| 927 | } else if (ctype->IsPrimitiveInt()) { |
| 928 | IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 929 | IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 930 | for (jint i = 0; i < length; ++i) { |
| 931 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 932 | } |
| 933 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 934 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 935 | "Unimplemented System.arraycopy for type '%s'", |
| 936 | PrettyDescriptor(ctype).c_str()); |
| 937 | } |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 938 | } else if (name == "long java.lang.Double.doubleToRawLongBits(double)") { |
| 939 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 940 | result->SetJ(bit_cast<int64_t>(in)); |
| 941 | } else if (name == "double java.lang.Math.ceil(double)") { |
| 942 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 943 | double out; |
| 944 | // Special cases: |
| 945 | // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath. |
| 946 | // -1 < in < 0 -> out := -0. |
| 947 | if (-1.0 < in && in < 0) { |
| 948 | out = -0.0; |
| 949 | } else { |
| 950 | out = ceil(in); |
| 951 | } |
| 952 | result->SetD(out); |
| 953 | } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 954 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 955 | bool ok = false; |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 956 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") { |
| 957 | // Allocate non-threadlocal buffer. |
| 958 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 959 | ok = true; |
| 960 | } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") { |
| 961 | // Note: RealToString is implemented and used in a different fashion than IntegralToString. |
| 962 | // Conversion is done over an actual object of RealToString (the conversion method is an |
| 963 | // instance method). This means it is not as clear whether it is correct to return a new |
| 964 | // object each time. The caller needs to be inspected by hand to see whether it (incorrectly) |
| 965 | // stores the object for later use. |
| 966 | // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString. |
| 967 | if (shadow_frame->GetLink()->GetLink() != nullptr) { |
| 968 | std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod())); |
| 969 | if (caller2 == "java.lang.String java.lang.Double.toString(double)") { |
| 970 | // Allocate new object. |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 971 | StackHandleScope<2> hs(self); |
| 972 | Handle<Class> h_real_to_string_class(hs.NewHandle( |
| 973 | shadow_frame->GetLink()->GetMethod()->GetDeclaringClass())); |
| 974 | Handle<Object> h_real_to_string_obj(hs.NewHandle( |
| 975 | h_real_to_string_class->AllocObject(self))); |
| 976 | if (h_real_to_string_obj.Get() != nullptr) { |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 977 | mirror::ArtMethod* init_method = |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 978 | h_real_to_string_class->FindDirectMethod("<init>", "()V"); |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 979 | if (init_method == nullptr) { |
Andreas Gampe | 729699d | 2015-03-03 17:48:39 -0800 | [diff] [blame] | 980 | h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail); |
| 981 | } else { |
| 982 | JValue invoke_result; |
| 983 | EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr, |
| 984 | nullptr); |
| 985 | if (!self->IsExceptionPending()) { |
| 986 | result->SetL(h_real_to_string_obj.Get()); |
| 987 | ok = true; |
| 988 | } |
Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | if (!ok) { |
| 993 | // We'll abort, so clear exception. |
| 994 | self->ClearException(); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (!ok) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame^] | 1001 | self->ThrowNewException("Ljava/lang/InternalError;", "Unimplemented ThreadLocal.get"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1002 | } |
| 1003 | } else { |
| 1004 | // Not special, continue with regular interpreter execution. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1005 | artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1009 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1010 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 1011 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1012 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 1013 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1014 | const Instruction* inst, uint16_t inst_data, \ |
| 1015 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1016 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 1017 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 1018 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 1019 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 1020 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1021 | |
| 1022 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1023 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 1024 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 1025 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 1026 | const ShadowFrame& shadow_frame, \ |
| 1027 | Thread* self, JValue* result) |
| 1028 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 1029 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 1030 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 1031 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 1032 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 1033 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 1034 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 1035 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1036 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 1037 | |
| 1038 | } // namespace interpreter |
| 1039 | } // namespace art |