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 | |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 21 | #include "debugger.h" |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 22 | #include "entrypoints/runtime_asm_entrypoints.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 23 | #include "mirror/array-inl.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 24 | #include "unstarted_runtime.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 25 | #include "verifier/method_verifier.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace interpreter { |
| 29 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 30 | void ThrowNullPointerExceptionFromInterpreter() { |
| 31 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 35 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 36 | uint16_t inst_data) { |
| 37 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 38 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 39 | 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] | 40 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 41 | if (UNLIKELY(f == nullptr)) { |
| 42 | CHECK(self->IsExceptionPending()); |
| 43 | return false; |
| 44 | } |
| 45 | Object* obj; |
| 46 | if (is_static) { |
| 47 | obj = f->GetDeclaringClass(); |
| 48 | } else { |
| 49 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 50 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 51 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 55 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 56 | // Report this field access to instrumentation if needed. |
| 57 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 58 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 59 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 60 | instrumentation->FieldReadEvent(self, this_object, shadow_frame.GetMethod(), |
| 61 | shadow_frame.GetDexPC(), f); |
| 62 | } |
| 63 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 64 | switch (field_type) { |
| 65 | case Primitive::kPrimBoolean: |
| 66 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 67 | break; |
| 68 | case Primitive::kPrimByte: |
| 69 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 70 | break; |
| 71 | case Primitive::kPrimChar: |
| 72 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 73 | break; |
| 74 | case Primitive::kPrimShort: |
| 75 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 76 | break; |
| 77 | case Primitive::kPrimInt: |
| 78 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 79 | break; |
| 80 | case Primitive::kPrimLong: |
| 81 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 82 | break; |
| 83 | case Primitive::kPrimNot: |
| 84 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj)); |
| 85 | break; |
| 86 | default: |
| 87 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 88 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // Explicitly instantiate all DoFieldGet functions. |
| 94 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 95 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 96 | ShadowFrame& shadow_frame, \ |
| 97 | const Instruction* inst, \ |
| 98 | uint16_t inst_data) |
| 99 | |
| 100 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 101 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 102 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 103 | |
| 104 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 105 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 106 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 107 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 108 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 109 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 110 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 111 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 112 | |
| 113 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 114 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 115 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 116 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 117 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 118 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 119 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 120 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 121 | |
| 122 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 123 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 124 | |
| 125 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 126 | // Returns true on success, otherwise throws an exception and returns false. |
| 127 | template<Primitive::Type field_type> |
| 128 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 129 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 130 | if (UNLIKELY(obj == nullptr)) { |
| 131 | // We lost the reference to the field index so we cannot get a more |
| 132 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 133 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | MemberOffset field_offset(inst->VRegC_22c()); |
| 137 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 138 | // the field from the base of the object, we need to look for it first. |
| 139 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 140 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 141 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 142 | field_offset.Uint32Value()); |
| 143 | DCHECK(f != nullptr); |
| 144 | DCHECK(!f->IsStatic()); |
| 145 | instrumentation->FieldReadEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 146 | shadow_frame.GetDexPC(), f); |
| 147 | } |
| 148 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 149 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 150 | switch (field_type) { |
| 151 | case Primitive::kPrimInt: |
| 152 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 153 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 154 | case Primitive::kPrimBoolean: |
| 155 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 156 | break; |
| 157 | case Primitive::kPrimByte: |
| 158 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 159 | break; |
| 160 | case Primitive::kPrimChar: |
| 161 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 162 | break; |
| 163 | case Primitive::kPrimShort: |
| 164 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 165 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 166 | case Primitive::kPrimLong: |
| 167 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 168 | break; |
| 169 | case Primitive::kPrimNot: |
| 170 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 171 | break; |
| 172 | default: |
| 173 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 174 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | // Explicitly instantiate all DoIGetQuick functions. |
| 180 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 181 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 182 | uint16_t inst_data) |
| 183 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 184 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 185 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 186 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 187 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 188 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 189 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 190 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 191 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 192 | |
| 193 | template<Primitive::Type field_type> |
| 194 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 195 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 196 | JValue field_value; |
| 197 | switch (field_type) { |
| 198 | case Primitive::kPrimBoolean: |
| 199 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 200 | break; |
| 201 | case Primitive::kPrimByte: |
| 202 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 203 | break; |
| 204 | case Primitive::kPrimChar: |
| 205 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 206 | break; |
| 207 | case Primitive::kPrimShort: |
| 208 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 209 | break; |
| 210 | case Primitive::kPrimInt: |
| 211 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 212 | break; |
| 213 | case Primitive::kPrimLong: |
| 214 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 215 | break; |
| 216 | case Primitive::kPrimNot: |
| 217 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 218 | break; |
| 219 | default: |
| 220 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 221 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 222 | } |
| 223 | return field_value; |
| 224 | } |
| 225 | |
| 226 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 227 | bool transaction_active> |
| 228 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 229 | uint16_t inst_data) { |
| 230 | bool do_assignability_check = do_access_check; |
| 231 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 232 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 233 | 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] | 234 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 235 | if (UNLIKELY(f == nullptr)) { |
| 236 | CHECK(self->IsExceptionPending()); |
| 237 | return false; |
| 238 | } |
| 239 | Object* obj; |
| 240 | if (is_static) { |
| 241 | obj = f->GetDeclaringClass(); |
| 242 | } else { |
| 243 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 244 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 245 | ThrowNullPointerExceptionForFieldAccess(f, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 249 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 250 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 251 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 252 | // the field from the base of the object, we need to look for it first. |
| 253 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 254 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 255 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 256 | Object* this_object = f->IsStatic() ? nullptr : obj; |
| 257 | instrumentation->FieldWriteEvent(self, this_object, shadow_frame.GetMethod(), |
| 258 | shadow_frame.GetDexPC(), f, field_value); |
| 259 | } |
| 260 | switch (field_type) { |
| 261 | case Primitive::kPrimBoolean: |
| 262 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 263 | break; |
| 264 | case Primitive::kPrimByte: |
| 265 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 266 | break; |
| 267 | case Primitive::kPrimChar: |
| 268 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 269 | break; |
| 270 | case Primitive::kPrimShort: |
| 271 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 272 | break; |
| 273 | case Primitive::kPrimInt: |
| 274 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 275 | break; |
| 276 | case Primitive::kPrimLong: |
| 277 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 278 | break; |
| 279 | case Primitive::kPrimNot: { |
| 280 | Object* reg = shadow_frame.GetVRegReference(vregA); |
| 281 | if (do_assignability_check && reg != nullptr) { |
| 282 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 283 | // object in the destructor. |
| 284 | Class* field_class; |
| 285 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 286 | StackHandleScope<2> hs(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 287 | HandleWrapper<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 288 | HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 289 | field_class = f->GetType<true>(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 290 | } |
| 291 | if (!reg->VerifierInstanceOf(field_class)) { |
| 292 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 293 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 294 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 295 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 296 | reg->GetClass()->GetDescriptor(&temp1), |
| 297 | field_class->GetDescriptor(&temp2), |
| 298 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | f->SetObj<transaction_active>(obj, reg); |
| 303 | break; |
| 304 | } |
| 305 | default: |
| 306 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 307 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | // Explicitly instantiate all DoFieldPut functions. |
| 313 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 314 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 315 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 316 | |
| 317 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 318 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 319 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 320 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 321 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 322 | |
| 323 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 324 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 325 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 326 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 327 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 328 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 329 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 330 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 331 | |
| 332 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 333 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 334 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 335 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 336 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 337 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 338 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 339 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 340 | |
| 341 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 342 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 343 | |
| 344 | template<Primitive::Type field_type, bool transaction_active> |
| 345 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
| 346 | Object* obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 347 | if (UNLIKELY(obj == nullptr)) { |
| 348 | // We lost the reference to the field index so we cannot get a more |
| 349 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 350 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | MemberOffset field_offset(inst->VRegC_22c()); |
| 354 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 355 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 356 | // the field from the base of the object, we need to look for it first. |
| 357 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 358 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 359 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 360 | field_offset.Uint32Value()); |
| 361 | DCHECK(f != nullptr); |
| 362 | DCHECK(!f->IsStatic()); |
| 363 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
| 364 | instrumentation->FieldWriteEvent(Thread::Current(), obj, shadow_frame.GetMethod(), |
| 365 | shadow_frame.GetDexPC(), f, field_value); |
| 366 | } |
| 367 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 368 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 369 | case Primitive::kPrimBoolean: |
| 370 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 371 | break; |
| 372 | case Primitive::kPrimByte: |
| 373 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 374 | break; |
| 375 | case Primitive::kPrimChar: |
| 376 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 377 | break; |
| 378 | case Primitive::kPrimShort: |
| 379 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 380 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 381 | case Primitive::kPrimInt: |
| 382 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 383 | break; |
| 384 | case Primitive::kPrimLong: |
| 385 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 386 | break; |
| 387 | case Primitive::kPrimNot: |
| 388 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 389 | break; |
| 390 | default: |
| 391 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 392 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | // Explicitly instantiate all DoIPutQuick functions. |
| 398 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 399 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 400 | const Instruction* inst, \ |
| 401 | uint16_t inst_data) |
| 402 | |
| 403 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 404 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 405 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 406 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 407 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 408 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 409 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 410 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 411 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 412 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 413 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 414 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 415 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 416 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 417 | uint32_t FindNextInstructionFollowingException( |
| 418 | Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc, |
| 419 | const instrumentation::Instrumentation* instrumentation) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 420 | self->VerifyStack(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 421 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 422 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 423 | if (instrumentation->HasExceptionCaughtListeners() |
| 424 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 425 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 426 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 427 | bool clear_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 428 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
| 429 | hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 430 | if (found_dex_pc == DexFile::kDexNoIndex) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 431 | // Exception is not caught by the current method. We will unwind to the |
| 432 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 433 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 434 | shadow_frame.GetMethod(), dex_pc); |
| 435 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 436 | // 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] | 437 | if (clear_exception) { |
| 438 | self->ClearException(); |
| 439 | } |
| 440 | } |
| 441 | return found_dex_pc; |
| 442 | } |
| 443 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 444 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 445 | LOG(FATAL) << "Unexpected instruction: " |
| 446 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 447 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 450 | // 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] | 451 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 452 | size_t dest_reg, size_t src_reg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 453 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 454 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 455 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 456 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Igor Murashkin | c449e8b | 2015-06-10 15:56:42 -0700 | [diff] [blame] | 457 | |
| 458 | // If both register locations contains the same value, the register probably holds a reference. |
| 459 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 460 | // in the references array even after the original vreg was overwritten to a non-reference. |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 461 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 462 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 463 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 464 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 468 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 469 | va_list args; |
| 470 | va_start(args, fmt); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 471 | AbortTransactionV(self, fmt, args); |
| 472 | va_end(args); |
| 473 | } |
| 474 | |
| 475 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 476 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 477 | // Constructs abort message. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 478 | std::string abort_msg; |
| 479 | StringAppendV(&abort_msg, fmt, args); |
| 480 | // Throws an exception so we can abort the transaction and rollback every change. |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 481 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 484 | // Separate declaration is required solely for the attributes. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 485 | template<bool is_range, bool do_assignability_check> SHARED_REQUIRES(Locks::mutator_lock_) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 486 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 487 | Thread* self, |
| 488 | ShadowFrame& shadow_frame, |
| 489 | JValue* result, |
| 490 | uint16_t number_of_inputs, |
| 491 | uint32_t arg[Instruction::kMaxVarArgRegs], |
| 492 | uint32_t vregC) ALWAYS_INLINE; |
| 493 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 494 | SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 495 | static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) ALWAYS_INLINE; |
| 496 | |
| 497 | static inline bool NeedsInterpreter(Thread* self, ShadowFrame* new_shadow_frame) { |
| 498 | ArtMethod* target = new_shadow_frame->GetMethod(); |
| 499 | if (UNLIKELY(target->IsNative() || target->IsProxyMethod())) { |
| 500 | return false; |
| 501 | } |
| 502 | Runtime* runtime = Runtime::Current(); |
| 503 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 504 | return runtime->GetInstrumentation()->IsForcedInterpretOnly() || |
| 505 | // Doing this check avoids doing compiled/interpreter transitions. |
| 506 | class_linker->IsQuickToInterpreterBridge(target->GetEntryPointFromQuickCompiledCode()) || |
| 507 | // Force the use of interpreter when it is required by the debugger. |
| 508 | Dbg::IsForcedInterpreterNeededForCalling(self, target); |
| 509 | } |
| 510 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 511 | template<bool is_range, bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 512 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 513 | Thread* self, |
| 514 | ShadowFrame& shadow_frame, |
| 515 | JValue* result, |
| 516 | uint16_t number_of_inputs, |
| 517 | uint32_t arg[Instruction::kMaxVarArgRegs], |
| 518 | uint32_t vregC) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 519 | bool string_init = false; |
| 520 | // Replace calls to String.<init> with equivalent StringFactory call. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 521 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 522 | && called_method->IsConstructor())) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 523 | ScopedObjectAccessUnchecked soa(self); |
| 524 | jmethodID mid = soa.EncodeMethod(called_method); |
| 525 | called_method = soa.DecodeMethod(WellKnownClasses::StringInitToStringFactoryMethodID(mid)); |
| 526 | string_init = true; |
| 527 | } |
| 528 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 529 | // Compute method information. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 530 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 531 | |
| 532 | // Number of registers for the callee's call frame. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 533 | uint16_t num_regs; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 534 | if (LIKELY(code_item != nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 535 | num_regs = code_item->registers_size_; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 536 | DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 537 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 538 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 539 | num_regs = number_of_inputs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 542 | // Hack for String init: |
| 543 | // |
| 544 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 545 | // invoke-x StringFactory(a, b, c, ...) |
| 546 | // by effectively dropping the first virtual register from the invoke. |
| 547 | // |
| 548 | // (at this point the ArtMethod has already been replaced, |
| 549 | // so we just need to fix-up the arguments) |
| 550 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 551 | if (UNLIKELY(string_init)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 552 | DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 553 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 554 | // The new StringFactory call is static and has one fewer argument. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 555 | if (code_item == nullptr) { |
| 556 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 557 | num_regs--; |
| 558 | } // else ... don't need to change num_regs since it comes up from the string_init's code item |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 559 | number_of_inputs--; |
| 560 | |
| 561 | // Rewrite the var-args, dropping the 0th argument ("this") |
| 562 | for (uint32_t i = 1; i < Instruction::kMaxVarArgRegs; ++i) { |
| 563 | arg[i - 1] = arg[i]; |
| 564 | } |
| 565 | arg[Instruction::kMaxVarArgRegs - 1] = 0; |
| 566 | |
| 567 | // Rewrite the non-var-arg case |
| 568 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 569 | } |
| 570 | |
| 571 | // Parameter registers go at the end of the shadow frame. |
| 572 | DCHECK_GE(num_regs, number_of_inputs); |
| 573 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 574 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 575 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 576 | // Allocate shadow frame on the stack. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 577 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
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 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 582 | // Initialize new shadow frame by copying the registers from the callee shadow frame. |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 583 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 584 | // Slow path. |
| 585 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 586 | // register the shadow frame as under construction and allow suspension again. |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 587 | ScopedStackedShadowFramePusher pusher( |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 588 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 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 | |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 597 | // Handle receiver apart since it's not part of the shorty. |
| 598 | size_t dest_reg = first_dest_reg; |
| 599 | size_t arg_offset = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 600 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 601 | if (!new_shadow_frame->GetMethod()->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 602 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 603 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 604 | ++dest_reg; |
| 605 | ++arg_offset; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 606 | DCHECK(!string_init); // All StringFactory methods are static. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 607 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 608 | |
| 609 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 610 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 611 | // Skip the 0th 'shorty' type since it represents the return type. |
| 612 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 613 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 614 | switch (shorty[shorty_pos + 1]) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 615 | // Handle Object references. 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 616 | case 'L': { |
| 617 | Object* o = shadow_frame.GetVRegReference(src_reg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 618 | if (do_assignability_check && o != nullptr) { |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 619 | Class* arg_type = |
| 620 | new_shadow_frame->GetMethod()->GetClassFromTypeIndex( |
| 621 | params->GetTypeItem(shorty_pos).type_idx_, true); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 622 | if (arg_type == nullptr) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 623 | CHECK(self->IsExceptionPending()); |
| 624 | return false; |
| 625 | } |
| 626 | if (!o->VerifierInstanceOf(arg_type)) { |
| 627 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 628 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 629 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 630 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 631 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 632 | o->GetClass()->GetDescriptor(&temp1), |
| 633 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 634 | return false; |
| 635 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 636 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 637 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 638 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 639 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 640 | // Handle doubles and longs. 2 consecutive virtual register slots. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 641 | case 'J': case 'D': { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 642 | uint64_t wide_value = |
| 643 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 644 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 645 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 646 | // Skip the next virtual register slot since we already used it. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 647 | ++dest_reg; |
| 648 | ++arg_offset; |
| 649 | break; |
| 650 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 651 | // Handle all other primitives that are always 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 652 | default: |
| 653 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 654 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 655 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 656 | } |
| 657 | } else { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 658 | size_t arg_index = 0; |
| 659 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 660 | // Fast path: no extra checks. |
| 661 | if (is_range) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 662 | // TODO: Implement the range version of invoke-lambda |
| 663 | uint16_t first_src_reg = vregC; |
| 664 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 665 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 666 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 667 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 668 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 669 | } else { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 670 | DCHECK_LE(number_of_inputs, Instruction::kMaxVarArgRegs); |
| 671 | |
| 672 | for (; arg_index < number_of_inputs; ++arg_index) { |
| 673 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, arg[arg_index]); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 674 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 675 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 676 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 679 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 680 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 681 | if (NeedsInterpreter(self, new_shadow_frame)) { |
| 682 | artInterpreterToInterpreterBridge(self, code_item, new_shadow_frame, result); |
Nicolas Geoffray | 7070ccd | 2015-07-08 09:41:54 +0000 | [diff] [blame] | 683 | } else { |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 684 | artInterpreterToCompiledCodeBridge(self, code_item, new_shadow_frame, result); |
Nicolas Geoffray | 7070ccd | 2015-07-08 09:41:54 +0000 | [diff] [blame] | 685 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 686 | } else { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 687 | UnstartedRuntime::Invoke(self, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 688 | } |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 689 | |
| 690 | if (string_init && !self->IsExceptionPending()) { |
| 691 | // Set the new string result of the StringFactory. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 692 | shadow_frame.SetVRegReference(string_init_vreg_this, result->GetL()); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 693 | // Overwrite all potential copies of the original result of the new-instance of string with the |
| 694 | // new result of the StringFactory. Use the verifier to find this set of registers. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 695 | ArtMethod* method = shadow_frame.GetMethod(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 696 | MethodReference method_ref = method->ToMethodReference(); |
| 697 | SafeMap<uint32_t, std::set<uint32_t>> string_init_map; |
| 698 | SafeMap<uint32_t, std::set<uint32_t>>* string_init_map_ptr; |
| 699 | MethodRefToStringInitRegMap& method_to_string_init_map = Runtime::Current()->GetStringInitMap(); |
| 700 | auto it = method_to_string_init_map.find(method_ref); |
| 701 | if (it == method_to_string_init_map.end()) { |
| 702 | string_init_map = std::move(verifier::MethodVerifier::FindStringInitMap(method)); |
| 703 | method_to_string_init_map.Overwrite(method_ref, string_init_map); |
| 704 | string_init_map_ptr = &string_init_map; |
| 705 | } else { |
| 706 | string_init_map_ptr = &it->second; |
| 707 | } |
| 708 | if (string_init_map_ptr->size() != 0) { |
| 709 | uint32_t dex_pc = shadow_frame.GetDexPC(); |
| 710 | auto map_it = string_init_map_ptr->find(dex_pc); |
| 711 | if (map_it != string_init_map_ptr->end()) { |
| 712 | const std::set<uint32_t>& reg_set = map_it->second; |
| 713 | for (auto set_it = reg_set.begin(); set_it != reg_set.end(); ++set_it) { |
| 714 | shadow_frame.SetVRegReference(*set_it, result->GetL()); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 720 | return !self->IsExceptionPending(); |
| 721 | } |
| 722 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 723 | template<bool is_range, bool do_assignability_check> |
| 724 | bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 725 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 726 | const uint4_t num_additional_registers = inst->VRegB_25x(); |
| 727 | // Argument word count. |
| 728 | const uint16_t number_of_inputs = num_additional_registers + 1; |
| 729 | // The first input register is always present and is not encoded in the count. |
| 730 | |
| 731 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 732 | // code. |
| 733 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX. |
| 734 | uint32_t vregC = 0; // only used in invoke-XXX-range. |
| 735 | if (is_range) { |
| 736 | vregC = inst->VRegC_3rc(); |
| 737 | } else { |
| 738 | // TODO(iam): See if it's possible to remove inst_data dependency from 35x to avoid this path |
| 739 | UNUSED(inst_data); |
| 740 | inst->GetAllArgs25x(arg); |
| 741 | } |
| 742 | |
| 743 | // TODO: if there's an assignability check, throw instead? |
| 744 | DCHECK(called_method->IsStatic()); |
| 745 | |
| 746 | return DoCallCommon<is_range, do_assignability_check>( |
| 747 | called_method, self, shadow_frame, |
| 748 | result, number_of_inputs, arg, vregC); |
| 749 | } |
| 750 | |
| 751 | template<bool is_range, bool do_assignability_check> |
| 752 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 753 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 754 | // Argument word count. |
| 755 | const uint16_t number_of_inputs = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
| 756 | |
| 757 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 758 | // code. |
| 759 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in invoke-XXX. |
| 760 | uint32_t vregC = 0; |
| 761 | if (is_range) { |
| 762 | vregC = inst->VRegC_3rc(); |
| 763 | } else { |
| 764 | vregC = inst->VRegC_35c(); |
| 765 | inst->GetVarArgs(arg, inst_data); |
| 766 | } |
| 767 | |
| 768 | return DoCallCommon<is_range, do_assignability_check>( |
| 769 | called_method, self, shadow_frame, |
| 770 | result, number_of_inputs, arg, vregC); |
| 771 | } |
| 772 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 773 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 774 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 775 | Thread* self, JValue* result) { |
| 776 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 777 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 778 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 779 | if (!is_range) { |
| 780 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 781 | CHECK_LE(length, 5); |
| 782 | } |
| 783 | if (UNLIKELY(length < 0)) { |
| 784 | ThrowNegativeArraySizeException(length); |
| 785 | return false; |
| 786 | } |
| 787 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 788 | Class* array_class = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 789 | self, false, do_access_check); |
| 790 | if (UNLIKELY(array_class == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 791 | DCHECK(self->IsExceptionPending()); |
| 792 | return false; |
| 793 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 794 | CHECK(array_class->IsArrayClass()); |
| 795 | Class* component_class = array_class->GetComponentType(); |
| 796 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 797 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 798 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 799 | ThrowRuntimeException("Bad filled array request for type %s", |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 800 | PrettyDescriptor(component_class).c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 801 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 802 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 803 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 804 | PrettyDescriptor(component_class).c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 805 | } |
| 806 | return false; |
| 807 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 808 | Object* new_array = Array::Alloc<true>(self, array_class, length, |
| 809 | array_class->GetComponentSizeShift(), |
| 810 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 811 | if (UNLIKELY(new_array == nullptr)) { |
| 812 | self->AssertPendingOOMException(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 813 | return false; |
| 814 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 815 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 816 | uint32_t vregC = 0; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 817 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 818 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 819 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 820 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 821 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 822 | for (int32_t i = 0; i < length; ++i) { |
| 823 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 824 | if (is_primitive_int_component) { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 825 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 826 | i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 827 | } else { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 828 | new_array->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>( |
| 829 | i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 833 | result->SetL(new_array); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 834 | return true; |
| 835 | } |
| 836 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 837 | // TODO fix thread analysis: should be SHARED_REQUIRES(Locks::mutator_lock_). |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 838 | template<typename T> |
| 839 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 840 | NO_THREAD_SAFETY_ANALYSIS { |
| 841 | Runtime* runtime = Runtime::Current(); |
| 842 | for (int32_t i = 0; i < count; ++i) { |
| 843 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 848 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 849 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 850 | DCHECK(array != nullptr); |
| 851 | DCHECK_LE(count, array->GetLength()); |
| 852 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 853 | switch (primitive_component_type) { |
| 854 | case Primitive::kPrimBoolean: |
| 855 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 856 | break; |
| 857 | case Primitive::kPrimByte: |
| 858 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 859 | break; |
| 860 | case Primitive::kPrimChar: |
| 861 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 862 | break; |
| 863 | case Primitive::kPrimShort: |
| 864 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 865 | break; |
| 866 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 867 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 868 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 869 | case Primitive::kPrimFloat: |
| 870 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 871 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 872 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 873 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 874 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 875 | case Primitive::kPrimDouble: |
| 876 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 877 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 878 | default: |
| 879 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 880 | << " in fill-array-data"; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 885 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 886 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame^] | 887 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 888 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 889 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 890 | const Instruction* inst, uint16_t inst_data, \ |
| 891 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 892 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 893 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 894 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 895 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 896 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 897 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 898 | // Explicit DoLambdaCall template function declarations. |
| 899 | #define EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame^] | 900 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 901 | bool DoLambdaCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 902 | ShadowFrame& shadow_frame, \ |
| 903 | const Instruction* inst, \ |
| 904 | uint16_t inst_data, \ |
| 905 | JValue* result) |
| 906 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, false); |
| 907 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(false, true); |
| 908 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, false); |
| 909 | EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL(true, true); |
| 910 | #undef EXPLICIT_DO_LAMBDA_CALL_TEMPLATE_DECL |
| 911 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 912 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 913 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame^] | 914 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 915 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 916 | const ShadowFrame& shadow_frame, \ |
| 917 | Thread* self, JValue* result) |
| 918 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 919 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 920 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 921 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 922 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 923 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 924 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 925 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 926 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 927 | |
| 928 | } // namespace interpreter |
| 929 | } // namespace art |