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 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 21 | #include "base/enums.h" |
Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 22 | #include "debugger.h" |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 23 | #include "entrypoints/runtime_asm_entrypoints.h" |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 24 | #include "jit/jit.h" |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 25 | #include "jvalue.h" |
| 26 | #include "method_handles.h" |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 27 | #include "method_handles-inl.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 28 | #include "mirror/array-inl.h" |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 29 | #include "mirror/class.h" |
| 30 | #include "mirror/method_handle_impl.h" |
| 31 | #include "reflection.h" |
| 32 | #include "reflection-inl.h" |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 33 | #include "stack.h" |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 34 | #include "unstarted_runtime.h" |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 35 | #include "verifier/method_verifier.h" |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 36 | #include "well_known_classes.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | namespace interpreter { |
| 40 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 41 | void ThrowNullPointerExceptionFromInterpreter() { |
| 42 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
| 46 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 47 | uint16_t inst_data) { |
| 48 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 49 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 50 | ArtField* f = |
| 51 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 52 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 53 | if (UNLIKELY(f == nullptr)) { |
| 54 | CHECK(self->IsExceptionPending()); |
| 55 | return false; |
| 56 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 57 | ObjPtr<mirror::Object> obj; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 58 | if (is_static) { |
| 59 | obj = f->GetDeclaringClass(); |
| 60 | } else { |
| 61 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 62 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 63 | ThrowNullPointerExceptionForFieldAccess(f, true); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 67 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 68 | // Report this field access to instrumentation if needed. |
| 69 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 70 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
Mathieu Chartier | 0715c0b | 2016-10-03 22:49:46 -0700 | [diff] [blame] | 71 | StackHandleScope<1> hs(self); |
| 72 | // Wrap in handle wrapper in case the listener does thread suspension. |
| 73 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 74 | ObjPtr<mirror::Object> this_object; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 75 | if (!f->IsStatic()) { |
| 76 | this_object = obj; |
| 77 | } |
| 78 | instrumentation->FieldReadEvent(self, |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 79 | this_object.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 80 | shadow_frame.GetMethod(), |
| 81 | shadow_frame.GetDexPC(), |
| 82 | f); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 83 | } |
| 84 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 85 | switch (field_type) { |
| 86 | case Primitive::kPrimBoolean: |
| 87 | shadow_frame.SetVReg(vregA, f->GetBoolean(obj)); |
| 88 | break; |
| 89 | case Primitive::kPrimByte: |
| 90 | shadow_frame.SetVReg(vregA, f->GetByte(obj)); |
| 91 | break; |
| 92 | case Primitive::kPrimChar: |
| 93 | shadow_frame.SetVReg(vregA, f->GetChar(obj)); |
| 94 | break; |
| 95 | case Primitive::kPrimShort: |
| 96 | shadow_frame.SetVReg(vregA, f->GetShort(obj)); |
| 97 | break; |
| 98 | case Primitive::kPrimInt: |
| 99 | shadow_frame.SetVReg(vregA, f->GetInt(obj)); |
| 100 | break; |
| 101 | case Primitive::kPrimLong: |
| 102 | shadow_frame.SetVRegLong(vregA, f->GetLong(obj)); |
| 103 | break; |
| 104 | case Primitive::kPrimNot: |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 105 | shadow_frame.SetVRegReference(vregA, f->GetObject(obj).Ptr()); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 109 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | // Explicitly instantiate all DoFieldGet functions. |
| 115 | #define EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, _do_check) \ |
| 116 | template bool DoFieldGet<_find_type, _field_type, _do_check>(Thread* self, \ |
| 117 | ShadowFrame& shadow_frame, \ |
| 118 | const Instruction* inst, \ |
| 119 | uint16_t inst_data) |
| 120 | |
| 121 | #define EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 122 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, false); \ |
| 123 | EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL(_find_type, _field_type, true); |
| 124 | |
| 125 | // iget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 126 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimBoolean) |
| 127 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimByte) |
| 128 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimChar) |
| 129 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimShort) |
| 130 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimInt) |
| 131 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstancePrimitiveRead, Primitive::kPrimLong) |
| 132 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(InstanceObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 133 | |
| 134 | // sget-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 135 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimBoolean) |
| 136 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimByte) |
| 137 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimChar) |
| 138 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimShort) |
| 139 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimInt) |
| 140 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticPrimitiveRead, Primitive::kPrimLong) |
| 141 | EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL(StaticObjectRead, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 142 | |
| 143 | #undef EXPLICIT_DO_FIELD_GET_ALL_TEMPLATE_DECL |
| 144 | #undef EXPLICIT_DO_FIELD_GET_TEMPLATE_DECL |
| 145 | |
| 146 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 147 | // Returns true on success, otherwise throws an exception and returns false. |
| 148 | template<Primitive::Type field_type> |
| 149 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 150 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 151 | if (UNLIKELY(obj == nullptr)) { |
| 152 | // We lost the reference to the field index so we cannot get a more |
| 153 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 154 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | MemberOffset field_offset(inst->VRegC_22c()); |
| 158 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 159 | // the field from the base of the object, we need to look for it first. |
| 160 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 161 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 162 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 163 | field_offset.Uint32Value()); |
| 164 | DCHECK(f != nullptr); |
| 165 | DCHECK(!f->IsStatic()); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 166 | instrumentation->FieldReadEvent(Thread::Current(), |
| 167 | obj.Ptr(), |
| 168 | shadow_frame.GetMethod(), |
| 169 | shadow_frame.GetDexPC(), |
| 170 | f); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 171 | } |
| 172 | // Note: iget-x-quick instructions are only for non-volatile fields. |
| 173 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 174 | switch (field_type) { |
| 175 | case Primitive::kPrimInt: |
| 176 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetField32(field_offset))); |
| 177 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 178 | case Primitive::kPrimBoolean: |
| 179 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldBoolean(field_offset))); |
| 180 | break; |
| 181 | case Primitive::kPrimByte: |
| 182 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldByte(field_offset))); |
| 183 | break; |
| 184 | case Primitive::kPrimChar: |
| 185 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldChar(field_offset))); |
| 186 | break; |
| 187 | case Primitive::kPrimShort: |
| 188 | shadow_frame.SetVReg(vregA, static_cast<int32_t>(obj->GetFieldShort(field_offset))); |
| 189 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 190 | case Primitive::kPrimLong: |
| 191 | shadow_frame.SetVRegLong(vregA, static_cast<int64_t>(obj->GetField64(field_offset))); |
| 192 | break; |
| 193 | case Primitive::kPrimNot: |
| 194 | shadow_frame.SetVRegReference(vregA, obj->GetFieldObject<mirror::Object>(field_offset)); |
| 195 | break; |
| 196 | default: |
| 197 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 198 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | // Explicitly instantiate all DoIGetQuick functions. |
| 204 | #define EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(_field_type) \ |
| 205 | template bool DoIGetQuick<_field_type>(ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 206 | uint16_t inst_data) |
| 207 | |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 208 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimInt); // iget-quick. |
| 209 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimBoolean); // iget-boolean-quick. |
| 210 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimByte); // iget-byte-quick. |
| 211 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimChar); // iget-char-quick. |
| 212 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimShort); // iget-short-quick. |
| 213 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimLong); // iget-wide-quick. |
| 214 | EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL(Primitive::kPrimNot); // iget-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 215 | #undef EXPLICIT_DO_IGET_QUICK_TEMPLATE_DECL |
| 216 | |
| 217 | template<Primitive::Type field_type> |
| 218 | static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 219 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 220 | JValue field_value; |
| 221 | switch (field_type) { |
| 222 | case Primitive::kPrimBoolean: |
| 223 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 224 | break; |
| 225 | case Primitive::kPrimByte: |
| 226 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 227 | break; |
| 228 | case Primitive::kPrimChar: |
| 229 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 230 | break; |
| 231 | case Primitive::kPrimShort: |
| 232 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 233 | break; |
| 234 | case Primitive::kPrimInt: |
| 235 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 236 | break; |
| 237 | case Primitive::kPrimLong: |
| 238 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 239 | break; |
| 240 | case Primitive::kPrimNot: |
| 241 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 242 | break; |
| 243 | default: |
| 244 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 245 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 246 | } |
| 247 | return field_value; |
| 248 | } |
| 249 | |
| 250 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 251 | bool transaction_active> |
| 252 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
| 253 | uint16_t inst_data) { |
| 254 | bool do_assignability_check = do_access_check; |
| 255 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 256 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 257 | ArtField* f = |
| 258 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 259 | Primitive::ComponentSize(field_type)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 260 | if (UNLIKELY(f == nullptr)) { |
| 261 | CHECK(self->IsExceptionPending()); |
| 262 | return false; |
| 263 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 264 | ObjPtr<mirror::Object> obj; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 265 | if (is_static) { |
| 266 | obj = f->GetDeclaringClass(); |
| 267 | } else { |
| 268 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 269 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 270 | ThrowNullPointerExceptionForFieldAccess(f, false); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | } |
Sebastien Hertz | 1edbd8e | 2014-07-16 20:00:11 +0200 | [diff] [blame] | 274 | f->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 275 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 276 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 277 | // the field from the base of the object, we need to look for it first. |
| 278 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 279 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
Mathieu Chartier | 0715c0b | 2016-10-03 22:49:46 -0700 | [diff] [blame] | 280 | StackHandleScope<1> hs(self); |
| 281 | // Wrap in handle wrapper in case the listener does thread suspension. |
| 282 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 283 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 284 | ObjPtr<mirror::Object> this_object = f->IsStatic() ? nullptr : obj; |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 285 | instrumentation->FieldWriteEvent(self, this_object.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 286 | shadow_frame.GetMethod(), |
| 287 | shadow_frame.GetDexPC(), |
| 288 | f, |
| 289 | field_value); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 290 | } |
| 291 | switch (field_type) { |
| 292 | case Primitive::kPrimBoolean: |
| 293 | f->SetBoolean<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 294 | break; |
| 295 | case Primitive::kPrimByte: |
| 296 | f->SetByte<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 297 | break; |
| 298 | case Primitive::kPrimChar: |
| 299 | f->SetChar<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 300 | break; |
| 301 | case Primitive::kPrimShort: |
| 302 | f->SetShort<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 303 | break; |
| 304 | case Primitive::kPrimInt: |
| 305 | f->SetInt<transaction_active>(obj, shadow_frame.GetVReg(vregA)); |
| 306 | break; |
| 307 | case Primitive::kPrimLong: |
| 308 | f->SetLong<transaction_active>(obj, shadow_frame.GetVRegLong(vregA)); |
| 309 | break; |
| 310 | case Primitive::kPrimNot: { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 311 | ObjPtr<mirror::Object> reg = shadow_frame.GetVRegReference(vregA); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 312 | if (do_assignability_check && reg != nullptr) { |
| 313 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 314 | // object in the destructor. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 315 | ObjPtr<mirror::Class> field_class; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 316 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 317 | StackHandleScope<2> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 318 | HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 319 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 320 | field_class = f->GetType<true>(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 321 | } |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 322 | if (!reg->VerifierInstanceOf(field_class.Ptr())) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 323 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 324 | std::string temp1, temp2, temp3; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 325 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 326 | "Put '%s' that is not instance of field '%s' in '%s'", |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 327 | reg->GetClass()->GetDescriptor(&temp1), |
| 328 | field_class->GetDescriptor(&temp2), |
| 329 | f->GetDeclaringClass()->GetDescriptor(&temp3)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | } |
| 333 | f->SetObj<transaction_active>(obj, reg); |
| 334 | break; |
| 335 | } |
| 336 | default: |
| 337 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 338 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 339 | } |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | // Explicitly instantiate all DoFieldPut functions. |
| 344 | #define EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, _do_check, _transaction_active) \ |
| 345 | template bool DoFieldPut<_find_type, _field_type, _do_check, _transaction_active>(Thread* self, \ |
| 346 | const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
| 347 | |
| 348 | #define EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(_find_type, _field_type) \ |
| 349 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, false); \ |
| 350 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, false); \ |
| 351 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, false, true); \ |
| 352 | EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL(_find_type, _field_type, true, true); |
| 353 | |
| 354 | // iput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 355 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimBoolean) |
| 356 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimByte) |
| 357 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimChar) |
| 358 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimShort) |
| 359 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimInt) |
| 360 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstancePrimitiveWrite, Primitive::kPrimLong) |
| 361 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(InstanceObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 362 | |
| 363 | // sput-XXX |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 364 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimBoolean) |
| 365 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimByte) |
| 366 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimChar) |
| 367 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimShort) |
| 368 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimInt) |
| 369 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticPrimitiveWrite, Primitive::kPrimLong) |
| 370 | EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL(StaticObjectWrite, Primitive::kPrimNot) |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 371 | |
| 372 | #undef EXPLICIT_DO_FIELD_PUT_ALL_TEMPLATE_DECL |
| 373 | #undef EXPLICIT_DO_FIELD_PUT_TEMPLATE_DECL |
| 374 | |
| 375 | template<Primitive::Type field_type, bool transaction_active> |
| 376 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 377 | ObjPtr<mirror::Object> obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 378 | if (UNLIKELY(obj == nullptr)) { |
| 379 | // We lost the reference to the field index so we cannot get a more |
| 380 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 381 | ThrowNullPointerExceptionFromDexPC(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | MemberOffset field_offset(inst->VRegC_22c()); |
| 385 | const uint32_t vregA = inst->VRegA_22c(inst_data); |
| 386 | // Report this field modification to instrumentation if needed. Since we only have the offset of |
| 387 | // the field from the base of the object, we need to look for it first. |
| 388 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 389 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
| 390 | ArtField* f = ArtField::FindInstanceFieldWithOffset(obj->GetClass(), |
| 391 | field_offset.Uint32Value()); |
| 392 | DCHECK(f != nullptr); |
| 393 | DCHECK(!f->IsStatic()); |
| 394 | JValue field_value = GetFieldValue<field_type>(shadow_frame, vregA); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 395 | instrumentation->FieldWriteEvent(Thread::Current(), |
| 396 | obj.Ptr(), |
| 397 | shadow_frame.GetMethod(), |
| 398 | shadow_frame.GetDexPC(), |
| 399 | f, |
| 400 | field_value); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 401 | } |
| 402 | // Note: iput-x-quick instructions are only for non-volatile fields. |
| 403 | switch (field_type) { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 404 | case Primitive::kPrimBoolean: |
| 405 | obj->SetFieldBoolean<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 406 | break; |
| 407 | case Primitive::kPrimByte: |
| 408 | obj->SetFieldByte<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 409 | break; |
| 410 | case Primitive::kPrimChar: |
| 411 | obj->SetFieldChar<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 412 | break; |
| 413 | case Primitive::kPrimShort: |
| 414 | obj->SetFieldShort<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 415 | break; |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 416 | case Primitive::kPrimInt: |
| 417 | obj->SetField32<transaction_active>(field_offset, shadow_frame.GetVReg(vregA)); |
| 418 | break; |
| 419 | case Primitive::kPrimLong: |
| 420 | obj->SetField64<transaction_active>(field_offset, shadow_frame.GetVRegLong(vregA)); |
| 421 | break; |
| 422 | case Primitive::kPrimNot: |
| 423 | obj->SetFieldObject<transaction_active>(field_offset, shadow_frame.GetVRegReference(vregA)); |
| 424 | break; |
| 425 | default: |
| 426 | LOG(FATAL) << "Unreachable: " << field_type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 427 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | // Explicitly instantiate all DoIPutQuick functions. |
| 433 | #define EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, _transaction_active) \ |
| 434 | template bool DoIPutQuick<_field_type, _transaction_active>(const ShadowFrame& shadow_frame, \ |
| 435 | const Instruction* inst, \ |
| 436 | uint16_t inst_data) |
| 437 | |
| 438 | #define EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(_field_type) \ |
| 439 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, false); \ |
| 440 | EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL(_field_type, true); |
| 441 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 442 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimInt) // iput-quick. |
| 443 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimBoolean) // iput-boolean-quick. |
| 444 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimByte) // iput-byte-quick. |
| 445 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimChar) // iput-char-quick. |
| 446 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimShort) // iput-short-quick. |
| 447 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimLong) // iput-wide-quick. |
| 448 | EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL(Primitive::kPrimNot) // iput-object-quick. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 449 | #undef EXPLICIT_DO_IPUT_QUICK_ALL_TEMPLATE_DECL |
| 450 | #undef EXPLICIT_DO_IPUT_QUICK_TEMPLATE_DECL |
| 451 | |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 452 | // We accept a null Instrumentation* meaning we must not report anything to the instrumentation. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 453 | uint32_t FindNextInstructionFollowingException( |
| 454 | Thread* self, ShadowFrame& shadow_frame, uint32_t dex_pc, |
| 455 | const instrumentation::Instrumentation* instrumentation) { |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 456 | self->VerifyStack(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 457 | StackHandleScope<2> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 458 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 459 | if (instrumentation != nullptr && instrumentation->HasExceptionCaughtListeners() |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 460 | && self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 461 | instrumentation->ExceptionCaughtEvent(self, exception.Get()); |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 462 | } |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 463 | bool clear_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
| 465 | hs.NewHandle(exception->GetClass()), dex_pc, &clear_exception); |
Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 466 | if (found_dex_pc == DexFile::kDexNoIndex && instrumentation != nullptr) { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 467 | // Exception is not caught by the current method. We will unwind to the |
| 468 | // caller. Notify any instrumentation listener. |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 469 | instrumentation->MethodUnwindEvent(self, shadow_frame.GetThisObject(), |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 470 | shadow_frame.GetMethod(), dex_pc); |
| 471 | } else { |
Nicolas Geoffray | 7642cfc | 2015-02-26 10:56:09 +0000 | [diff] [blame] | 472 | // 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] | 473 | if (clear_exception) { |
| 474 | self->ClearException(); |
| 475 | } |
| 476 | } |
| 477 | return found_dex_pc; |
| 478 | } |
| 479 | |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 480 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 481 | LOG(FATAL) << "Unexpected instruction: " |
| 482 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 483 | UNREACHABLE(); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 486 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 487 | va_list args; |
| 488 | va_start(args, fmt); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 489 | AbortTransactionV(self, fmt, args); |
| 490 | va_end(args); |
| 491 | } |
| 492 | |
| 493 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 494 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 495 | // Constructs abort message. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 496 | std::string abort_msg; |
| 497 | StringAppendV(&abort_msg, fmt, args); |
| 498 | // 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] | 499 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 502 | // START DECLARATIONS : |
| 503 | // |
| 504 | // These additional declarations are required because clang complains |
| 505 | // about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions. |
| 506 | // |
| 507 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 508 | template <bool is_range, bool do_assignability_check> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 509 | REQUIRES_SHARED(Locks::mutator_lock_) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 510 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 511 | Thread* self, |
| 512 | ShadowFrame& shadow_frame, |
| 513 | JValue* result, |
| 514 | uint16_t number_of_inputs, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 515 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 516 | uint32_t vregC) ALWAYS_INLINE; |
| 517 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 518 | template <bool is_range> REQUIRES_SHARED(Locks::mutator_lock_) |
| 519 | static inline bool DoCallPolymorphic(ArtMethod* called_method, |
| 520 | Handle<mirror::MethodType> callsite_type, |
| 521 | Handle<mirror::MethodType> target_type, |
| 522 | Thread* self, |
| 523 | ShadowFrame& shadow_frame, |
| 524 | JValue* result, |
| 525 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 526 | uint32_t vregC) ALWAYS_INLINE; |
| 527 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 528 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 529 | static inline bool DoCallTransform(ArtMethod* called_method, |
| 530 | Handle<mirror::MethodType> callsite_type, |
| 531 | Thread* self, |
| 532 | ShadowFrame& shadow_frame, |
| 533 | Handle<mirror::MethodHandleImpl> receiver, |
| 534 | JValue* result) ALWAYS_INLINE; |
| 535 | |
| 536 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 537 | inline void PerformCall(Thread* self, |
| 538 | const DexFile::CodeItem* code_item, |
| 539 | ArtMethod* caller_method, |
| 540 | const size_t first_dest_reg, |
| 541 | ShadowFrame* callee_frame, |
| 542 | JValue* result) ALWAYS_INLINE; |
| 543 | |
| 544 | template <bool is_range> |
| 545 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 546 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 547 | ShadowFrame* callee_frame, |
| 548 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 549 | const size_t first_src_reg, |
| 550 | const size_t first_dest_reg, |
| 551 | const size_t num_regs) ALWAYS_INLINE; |
| 552 | |
| 553 | // END DECLARATIONS. |
| 554 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 555 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 556 | ArtMethod* caller, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 557 | const DexFile::CodeItem* code_item, |
| 558 | ShadowFrame* shadow_frame, |
| 559 | JValue* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 560 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 561 | ArtMethod* method = shadow_frame->GetMethod(); |
| 562 | // Ensure static methods are initialized. |
| 563 | if (method->IsStatic()) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 564 | ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass(); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 565 | if (UNLIKELY(!declaringClass->IsInitialized())) { |
| 566 | self->PushShadowFrame(shadow_frame); |
| 567 | StackHandleScope<1> hs(self); |
| 568 | Handle<mirror::Class> h_class(hs.NewHandle(declaringClass)); |
| 569 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, |
| 570 | true))) { |
| 571 | self->PopShadowFrame(); |
| 572 | DCHECK(self->IsExceptionPending()); |
| 573 | return; |
| 574 | } |
| 575 | self->PopShadowFrame(); |
| 576 | CHECK(h_class->IsInitializing()); |
| 577 | // Reload from shadow frame in case the method moved, this is faster than adding a handle. |
| 578 | method = shadow_frame->GetMethod(); |
| 579 | } |
| 580 | } |
| 581 | uint16_t arg_offset = (code_item == nullptr) |
| 582 | ? 0 |
| 583 | : code_item->registers_size_ - code_item->ins_size_; |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 584 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 585 | if (jit != nullptr && caller != nullptr) { |
| 586 | jit->NotifyInterpreterToCompiledCodeTransition(self, caller); |
| 587 | } |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 588 | method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset), |
| 589 | (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 590 | result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()); |
Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 593 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 594 | uint16_t this_obj_vreg, |
| 595 | JValue result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 596 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 597 | ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg); |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 598 | if (existing == nullptr) { |
| 599 | // If it's null, we come from compiled code that was deoptimized. Nothing to do, |
| 600 | // as the compiler verified there was no alias. |
| 601 | // Set the new string result of the StringFactory. |
| 602 | shadow_frame->SetVRegReference(this_obj_vreg, result.GetL()); |
| 603 | return; |
| 604 | } |
| 605 | // Set the string init result into all aliases. |
| 606 | for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) { |
| 607 | if (shadow_frame->GetVRegReference(i) == existing) { |
| 608 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 609 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 610 | shadow_frame->SetVRegReference(i, result.GetL()); |
| 611 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| 612 | reinterpret_cast<mirror::Object*>(shadow_frame->GetVReg(i))); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 617 | template<bool is_range, bool do_access_check> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 618 | inline bool DoInvokePolymorphic(Thread* self, |
| 619 | ShadowFrame& shadow_frame, |
| 620 | const Instruction* inst, |
| 621 | uint16_t inst_data, |
| 622 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 623 | // Invoke-polymorphic instructions always take a receiver. i.e, they are never static. |
| 624 | const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc(); |
| 625 | |
| 626 | // The method_idx here is the name of the signature polymorphic method that |
| 627 | // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact) |
| 628 | // and not the method that we'll dispatch to in the end. |
| 629 | // |
| 630 | // TODO(narayan) We'll have to check in the verifier that this is in fact a |
| 631 | // signature polymorphic method so that we disallow calls via invoke-polymorphic |
| 632 | // to non sig-poly methods. This would also have the side effect of verifying |
| 633 | // that vRegC really is a reference type. |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 634 | StackHandleScope<6> hs(self); |
| 635 | Handle<mirror::MethodHandleImpl> method_handle(hs.NewHandle( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 636 | ObjPtr<mirror::MethodHandleImpl>::DownCast( |
| 637 | MakeObjPtr(shadow_frame.GetVRegReference(vRegC))))); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 638 | if (UNLIKELY(method_handle.Get() == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 639 | const int method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc(); |
| 640 | // Note that the invoke type is kVirtual here because a call to a signature |
| 641 | // polymorphic method is shaped like a virtual call at the bytecode level. |
| 642 | ThrowNullPointerExceptionForMethodAccess(method_idx, InvokeType::kVirtual); |
| 643 | |
| 644 | result->SetJ(0); |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | // The vRegH value gives the index of the proto_id associated with this |
| 649 | // signature polymorphic callsite. |
| 650 | const uint32_t callsite_proto_id = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 651 | |
| 652 | // Call through to the classlinker and ask it to resolve the static type associated |
| 653 | // with the callsite. This information is stored in the dex cache so it's |
| 654 | // guaranteed to be fast after the first resolution. |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 655 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 656 | Handle<mirror::Class> caller_class(hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass())); |
| 657 | Handle<mirror::MethodType> callsite_type(hs.NewHandle(class_linker->ResolveMethodType( |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 658 | caller_class->GetDexFile(), callsite_proto_id, |
| 659 | hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()), |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 660 | hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader())))); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 661 | |
| 662 | // This implies we couldn't resolve one or more types in this method handle. |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 663 | if (UNLIKELY(callsite_type.Get() == nullptr)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 664 | CHECK(self->IsExceptionPending()); |
| 665 | result->SetJ(0); |
| 666 | return false; |
| 667 | } |
| 668 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 669 | // Get the method we're actually invoking along with the kind of |
| 670 | // invoke that is desired. We don't need to perform access checks at this |
| 671 | // point because they would have been performed on our behalf at the point |
| 672 | // of creation of the method handle. |
| 673 | ArtMethod* called_method = method_handle->GetTargetMethod(); |
| 674 | const MethodHandleKind handle_kind = method_handle->GetHandleKind(); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 675 | Handle<mirror::MethodType> handle_type(hs.NewHandle(method_handle->GetMethodType())); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 676 | CHECK(called_method != nullptr); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 677 | CHECK(handle_type.Get() != nullptr); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 678 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 679 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; |
| 680 | uint32_t receiver_vregC = 0; |
| 681 | if (is_range) { |
| 682 | receiver_vregC = (inst->VRegC_4rcc() + 1); |
| 683 | } else { |
| 684 | inst->GetVarArgs(arg, inst_data); |
| 685 | arg[0] = arg[1]; |
| 686 | arg[1] = arg[2]; |
| 687 | arg[2] = arg[3]; |
| 688 | arg[3] = arg[4]; |
| 689 | arg[4] = 0; |
| 690 | receiver_vregC = arg[0]; |
| 691 | } |
| 692 | |
| 693 | if (IsInvoke(handle_kind)) { |
| 694 | if (handle_kind == kInvokeVirtual || handle_kind == kInvokeInterface) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 695 | ObjPtr<mirror::Object> receiver = shadow_frame.GetVRegReference(receiver_vregC); |
| 696 | ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass(); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 697 | // Verify that _vRegC is an object reference and of the type expected by |
| 698 | // the receiver. |
| 699 | called_method = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface( |
| 700 | called_method, kRuntimePointerSize); |
| 701 | if (!VerifyObjectIsClass(receiver, declaring_class)) { |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 702 | return false; |
| 703 | } |
| 704 | } else if (handle_kind == kInvokeDirect) { |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 705 | if (called_method->IsConstructor()) { |
| 706 | // TODO(narayan) : We need to handle the case where the target method is a |
| 707 | // constructor here. |
| 708 | UNIMPLEMENTED(FATAL) << "Direct invokes for constructors are not implemented yet."; |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | // Nothing special to do in the case where we're not dealing with a |
| 713 | // constructor. It's a private method, and we've already access checked at |
| 714 | // the point of creating the handle. |
| 715 | } else if (handle_kind == kInvokeSuper) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 716 | ObjPtr<mirror::Class> declaring_class = called_method->GetDeclaringClass(); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 717 | |
| 718 | // Note that we're not dynamically dispatching on the type of the receiver |
| 719 | // here. We use the static type of the "receiver" object that we've |
| 720 | // recorded in the method handle's type, which will be the same as the |
| 721 | // special caller that was specified at the point of lookup. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 722 | ObjPtr<mirror::Class> referrer_class = handle_type->GetPTypes()->Get(0); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 723 | if (!declaring_class->IsInterface()) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 724 | ObjPtr<mirror::Class> super_class = referrer_class->GetSuperClass(); |
Narayan Kamath | 9bdaeeb | 2016-10-20 10:57:45 +0100 | [diff] [blame] | 725 | uint16_t vtable_index = called_method->GetMethodIndex(); |
| 726 | DCHECK(super_class != nullptr); |
| 727 | DCHECK(super_class->HasVTable()); |
| 728 | // Note that super_class is a super of referrer_class and called_method |
| 729 | // will always be declared by super_class (or one of its super classes). |
| 730 | DCHECK_LT(vtable_index, super_class->GetVTableLength()); |
| 731 | called_method = super_class->GetVTableEntry(vtable_index, kRuntimePointerSize); |
| 732 | } else { |
| 733 | called_method = referrer_class->FindVirtualMethodForInterfaceSuper( |
| 734 | called_method, kRuntimePointerSize); |
| 735 | } |
| 736 | |
| 737 | CHECK(called_method != nullptr); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 738 | } |
| 739 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 740 | if (handle_kind == kInvokeTransform) { |
| 741 | return DoCallTransform(called_method, |
| 742 | callsite_type, |
| 743 | self, |
| 744 | shadow_frame, |
| 745 | method_handle /* receiver */, |
| 746 | result); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 747 | } else { |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 748 | return DoCallPolymorphic<is_range>(called_method, |
| 749 | callsite_type, |
| 750 | handle_type, |
| 751 | self, |
| 752 | shadow_frame, |
| 753 | result, |
| 754 | arg, |
| 755 | receiver_vregC); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 756 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 757 | } else { |
| 758 | // TODO(narayan): Implement field getters and setters. |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 759 | UNIMPLEMENTED(FATAL) << "Field references in method handles are not implemented yet."; |
| 760 | return false; |
| 761 | } |
| 762 | } |
| 763 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 764 | // Calculate the number of ins for a proxy or native method, where we |
| 765 | // can't just look at the code item. |
| 766 | static inline size_t GetInsForProxyOrNativeMethod(ArtMethod* method) |
| 767 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 768 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
| 769 | |
| 770 | method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| 771 | size_t num_ins = 0; |
| 772 | // Separate accounting for the receiver, which isn't a part of the |
| 773 | // shorty. |
| 774 | if (!method->IsStatic()) { |
| 775 | ++num_ins; |
| 776 | } |
| 777 | |
| 778 | uint32_t shorty_len = 0; |
| 779 | const char* shorty = method->GetShorty(&shorty_len); |
| 780 | for (size_t i = 1; i < shorty_len; ++i) { |
| 781 | const char c = shorty[i]; |
| 782 | ++num_ins; |
| 783 | if (c == 'J' || c == 'D') { |
| 784 | ++num_ins; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return num_ins; |
| 789 | } |
| 790 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 791 | |
| 792 | inline void PerformCall(Thread* self, |
| 793 | const DexFile::CodeItem* code_item, |
| 794 | ArtMethod* caller_method, |
| 795 | const size_t first_dest_reg, |
| 796 | ShadowFrame* callee_frame, |
| 797 | JValue* result) { |
| 798 | if (LIKELY(Runtime::Current()->IsStarted())) { |
| 799 | ArtMethod* target = callee_frame->GetMethod(); |
| 800 | if (ClassLinker::ShouldUseInterpreterEntrypoint( |
| 801 | target, |
| 802 | target->GetEntryPointFromQuickCompiledCode())) { |
| 803 | ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result); |
| 804 | } else { |
| 805 | ArtInterpreterToCompiledCodeBridge( |
| 806 | self, caller_method, code_item, callee_frame, result); |
| 807 | } |
| 808 | } else { |
| 809 | UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | template <bool is_range> |
| 814 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 815 | ShadowFrame* callee_frame, |
| 816 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 817 | const size_t first_src_reg, |
| 818 | const size_t first_dest_reg, |
| 819 | const size_t num_regs) { |
| 820 | if (is_range) { |
| 821 | const size_t dest_reg_bound = first_dest_reg + num_regs; |
| 822 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound; |
| 823 | ++dest_reg, ++src_reg) { |
| 824 | AssignRegister(callee_frame, caller_frame, dest_reg, src_reg); |
| 825 | } |
| 826 | } else { |
| 827 | DCHECK_LE(num_regs, arraysize(arg)); |
| 828 | |
| 829 | for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) { |
| 830 | AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // Returns true iff. the callsite type for a polymorphic invoke is transformer |
| 836 | // like, i.e that it has a single input argument whose type is |
| 837 | // dalvik.system.EmulatedStackFrame. |
| 838 | static inline bool IsCallerTransformer(Handle<mirror::MethodType> callsite_type) |
| 839 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 840 | ObjPtr<mirror::ObjectArray<mirror::Class>> param_types(callsite_type->GetPTypes()); |
| 841 | if (param_types->GetLength() == 1) { |
| 842 | ObjPtr<mirror::Class> param(param_types->GetWithoutChecks(0)); |
| 843 | return param == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_EmulatedStackFrame); |
| 844 | } |
| 845 | |
| 846 | return false; |
| 847 | } |
| 848 | |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 849 | template <bool is_range> |
| 850 | static inline bool DoCallPolymorphic(ArtMethod* called_method, |
| 851 | Handle<mirror::MethodType> callsite_type, |
| 852 | Handle<mirror::MethodType> target_type, |
| 853 | Thread* self, |
| 854 | ShadowFrame& shadow_frame, |
| 855 | JValue* result, |
| 856 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 857 | uint32_t first_src_reg) { |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 858 | // TODO(narayan): Wire in the String.init hacks. |
| 859 | |
| 860 | // Compute method information. |
| 861 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
| 862 | |
| 863 | // Number of registers for the callee's call frame. Note that for non-exact |
| 864 | // invokes, we always derive this information from the callee method. We |
| 865 | // cannot guarantee during verification that the number of registers encoded |
| 866 | // in the invoke is equal to the number of ins for the callee. This is because |
| 867 | // some transformations (such as boxing a long -> Long or wideining an |
| 868 | // int -> long will change that number. |
| 869 | uint16_t num_regs; |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 870 | size_t num_input_regs; |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 871 | size_t first_dest_reg; |
| 872 | if (LIKELY(code_item != nullptr)) { |
| 873 | num_regs = code_item->registers_size_; |
| 874 | first_dest_reg = num_regs - code_item->ins_size_; |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 875 | num_input_regs = code_item->ins_size_; |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 876 | // Parameter registers go at the end of the shadow frame. |
| 877 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 878 | } else { |
| 879 | // No local regs for proxy and native methods. |
| 880 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 881 | num_regs = num_input_regs = GetInsForProxyOrNativeMethod(called_method); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 882 | first_dest_reg = 0; |
| 883 | } |
| 884 | |
| 885 | // Allocate shadow frame on the stack. |
| 886 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 887 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| 888 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 889 | |
| 890 | // Thread might be suspended during PerformArgumentConversions due to the |
| 891 | // allocations performed during boxing. |
| 892 | { |
| 893 | ScopedStackedShadowFramePusher pusher( |
| 894 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 895 | if (callsite_type->IsExactMatch(target_type.Get())) { |
| 896 | // This is an exact invoke, we can take the fast path of just copying all |
| 897 | // registers without performing any argument conversions. |
| 898 | CopyRegisters<is_range>(shadow_frame, |
| 899 | new_shadow_frame, |
| 900 | arg, |
| 901 | first_src_reg, |
| 902 | first_dest_reg, |
| 903 | num_input_regs); |
| 904 | } else { |
| 905 | // This includes the case where we're entering this invoke-polymorphic |
| 906 | // from a transformer method. In that case, the callsite_type will contain |
| 907 | // a single argument of type dalvik.system.EmulatedStackFrame. In that |
| 908 | // case, we'll have to unmarshal the EmulatedStackFrame into the |
| 909 | // new_shadow_frame and perform argument conversions on it. |
| 910 | if (IsCallerTransformer(callsite_type)) { |
| 911 | // The emulated stack frame will be the first ahnd only argument |
| 912 | // when we're coming through from a transformer. |
| 913 | // |
| 914 | // TODO(narayan): This should be a mirror::EmulatedStackFrame after that |
| 915 | // type is introduced. |
| 916 | ObjPtr<mirror::Object> emulated_stack_frame( |
| 917 | shadow_frame.GetVRegReference(first_src_reg)); |
| 918 | if (!ConvertAndCopyArgumentsFromEmulatedStackFrame<is_range>(self, |
| 919 | emulated_stack_frame, |
| 920 | target_type, |
| 921 | first_dest_reg, |
| 922 | new_shadow_frame)) { |
| 923 | DCHECK(self->IsExceptionPending()); |
| 924 | result->SetL(0); |
| 925 | return false; |
| 926 | } |
| 927 | } else if (!ConvertAndCopyArgumentsFromCallerFrame<is_range>(self, |
| 928 | callsite_type, |
| 929 | target_type, |
| 930 | shadow_frame, |
| 931 | first_src_reg, |
| 932 | first_dest_reg, |
| 933 | arg, |
| 934 | new_shadow_frame)) { |
| 935 | DCHECK(self->IsExceptionPending()); |
| 936 | result->SetL(0); |
| 937 | return false; |
| 938 | } |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 942 | PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result); |
Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 943 | |
| 944 | // TODO(narayan): Perform return value conversions. |
| 945 | |
| 946 | return !self->IsExceptionPending(); |
| 947 | } |
| 948 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 949 | static inline bool DoCallTransform(ArtMethod* called_method, |
| 950 | Handle<mirror::MethodType> callsite_type, |
| 951 | Thread* self, |
| 952 | ShadowFrame& shadow_frame, |
| 953 | Handle<mirror::MethodHandleImpl> receiver, |
| 954 | JValue* result) { |
| 955 | // This can be fixed, because the method we're calling here |
| 956 | // (MethodHandle.transformInternal) doesn't have any locals and the signature |
| 957 | // is known : |
| 958 | // |
| 959 | // private MethodHandle.transformInternal(EmulatedStackFrame sf); |
| 960 | // |
| 961 | // This means we need only two vregs : |
| 962 | // - One for the receiver object. |
| 963 | // - One for the only method argument (an EmulatedStackFrame). |
| 964 | static constexpr size_t kNumRegsForTransform = 2; |
| 965 | |
| 966 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
| 967 | DCHECK(code_item != nullptr); |
| 968 | DCHECK_EQ(kNumRegsForTransform, code_item->registers_size_); |
| 969 | DCHECK_EQ(kNumRegsForTransform, code_item->ins_size_); |
| 970 | |
| 971 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| 972 | CREATE_SHADOW_FRAME(kNumRegsForTransform, &shadow_frame, called_method, /* dex pc */ 0); |
| 973 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| 974 | |
| 975 | // TODO(narayan): Perform argument conversions first (if this is an inexact invoke), and |
| 976 | // then construct an argument list object that's passed through to the |
| 977 | // method. Note that the ArgumentList reference is currently a nullptr. |
| 978 | // |
| 979 | // NOTE(narayan): If the caller is a transformer method (i.e, there is only |
| 980 | // one argument and its type is EmulatedStackFrame), we can directly pass that |
| 981 | // through without having to do any additional work. |
| 982 | UNUSED(callsite_type); |
| 983 | |
| 984 | new_shadow_frame->SetVRegReference(0, receiver.Get()); |
| 985 | // TODO(narayan): This is the EmulatedStackFrame, currently nullptr. |
| 986 | new_shadow_frame->SetVRegReference(1, nullptr); |
| 987 | |
| 988 | PerformCall(self, |
| 989 | code_item, |
| 990 | shadow_frame.GetMethod(), |
| 991 | 0 /* first dest reg */, |
| 992 | new_shadow_frame, |
| 993 | result); |
| 994 | |
| 995 | return !self->IsExceptionPending(); |
| 996 | } |
| 997 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 998 | template <bool is_range, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 999 | bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1000 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 1001 | Thread* self, |
| 1002 | ShadowFrame& shadow_frame, |
| 1003 | JValue* result, |
| 1004 | uint16_t number_of_inputs, |
Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1005 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1006 | uint32_t vregC) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1007 | bool string_init = false; |
| 1008 | // Replace calls to String.<init> with equivalent StringFactory call. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1009 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 1010 | && called_method->IsConstructor())) { |
Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 1011 | called_method = WellKnownClasses::StringInitToStringFactory(called_method); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1012 | string_init = true; |
| 1013 | } |
| 1014 | |
Alex Light | daf58c8 | 2016-03-16 23:00:49 +0000 | [diff] [blame] | 1015 | // Compute method information. |
| 1016 | const DexFile::CodeItem* code_item = called_method->GetCodeItem(); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1017 | |
| 1018 | // Number of registers for the callee's call frame. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1019 | uint16_t num_regs; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1020 | if (LIKELY(code_item != nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1021 | num_regs = code_item->registers_size_; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1022 | 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] | 1023 | } else { |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1024 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1025 | num_regs = number_of_inputs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1028 | // Hack for String init: |
| 1029 | // |
| 1030 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 1031 | // invoke-x StringFactory(a, b, c, ...) |
| 1032 | // by effectively dropping the first virtual register from the invoke. |
| 1033 | // |
| 1034 | // (at this point the ArtMethod has already been replaced, |
| 1035 | // so we just need to fix-up the arguments) |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 1036 | // |
| 1037 | // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased |
| 1038 | // to handle the compiler optimization of replacing `this` with null without |
| 1039 | // throwing NullPointerException. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1040 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1041 | if (UNLIKELY(string_init)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1042 | 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] | 1043 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1044 | // The new StringFactory call is static and has one fewer argument. |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1045 | if (code_item == nullptr) { |
| 1046 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1047 | num_regs--; |
| 1048 | } // 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] | 1049 | number_of_inputs--; |
| 1050 | |
| 1051 | // Rewrite the var-args, dropping the 0th argument ("this") |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1052 | for (uint32_t i = 1; i < arraysize(arg); ++i) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1053 | arg[i - 1] = arg[i]; |
| 1054 | } |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1055 | arg[arraysize(arg) - 1] = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1056 | |
| 1057 | // Rewrite the non-var-arg case |
| 1058 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 1059 | } |
| 1060 | |
| 1061 | // Parameter registers go at the end of the shadow frame. |
| 1062 | DCHECK_GE(num_regs, number_of_inputs); |
| 1063 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 1064 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1065 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1066 | // Allocate shadow frame on the stack. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1067 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1068 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 1069 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1070 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1071 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1072 | // 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] | 1073 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1074 | // Slow path. |
| 1075 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 1076 | // register the shadow frame as under construction and allow suspension again. |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 1077 | ScopedStackedShadowFramePusher pusher( |
Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 1078 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1079 | self->EndAssertNoThreadSuspension(old_cause); |
| 1080 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1081 | // ArtMethod here is needed to check type information of the call site against the callee. |
| 1082 | // Type information is retrieved from a DexFile/DexCache for that respective declared method. |
| 1083 | // |
| 1084 | // As a special case for proxy methods, which are not dex-backed, |
| 1085 | // we have to retrieve type information from the proxy's method |
| 1086 | // interface method instead (which is dex backed since proxies are never interfaces). |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1087 | ArtMethod* method = |
| 1088 | new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1089 | |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1090 | // 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] | 1091 | // to get the exact type of each reference argument. |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1092 | const DexFile::TypeList* params = method->GetParameterTypeList(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1093 | uint32_t shorty_len = 0; |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1094 | const char* shorty = method->GetShorty(&shorty_len); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1095 | |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1096 | // Handle receiver apart since it's not part of the shorty. |
| 1097 | size_t dest_reg = first_dest_reg; |
| 1098 | size_t arg_offset = 0; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1099 | |
Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1100 | if (!method->IsStatic()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1101 | size_t receiver_reg = is_range ? vregC : arg[0]; |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1102 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 1103 | ++dest_reg; |
| 1104 | ++arg_offset; |
Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1105 | DCHECK(!string_init); // All StringFactory methods are static. |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1106 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1107 | |
| 1108 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1109 | 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] | 1110 | // Skip the 0th 'shorty' type since it represents the return type. |
| 1111 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1112 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 1113 | switch (shorty[shorty_pos + 1]) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1114 | // Handle Object references. 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1115 | case 'L': { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1116 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1117 | if (do_assignability_check && o != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1118 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame^] | 1119 | const uint32_t type_idx = params->GetTypeItem(shorty_pos).type_idx_; |
| 1120 | ObjPtr<mirror::Class> arg_type = method->GetDexCacheResolvedType(type_idx, |
| 1121 | pointer_size); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1122 | if (arg_type == nullptr) { |
Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame^] | 1123 | StackHandleScope<1> hs(self); |
| 1124 | // Preserve o since it is used below and GetClassFromTypeIndex may cause thread |
| 1125 | // suspension. |
| 1126 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o); |
| 1127 | arg_type = method->GetClassFromTypeIndex(type_idx, true /* resolve */, pointer_size); |
| 1128 | if (arg_type == nullptr) { |
| 1129 | CHECK(self->IsExceptionPending()); |
| 1130 | return false; |
| 1131 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1132 | } |
| 1133 | if (!o->VerifierInstanceOf(arg_type)) { |
| 1134 | // This should never happen. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1135 | std::string temp1, temp2; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1136 | self->ThrowNewExceptionF("Ljava/lang/VirtualMachineError;", |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1137 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1138 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1139 | o->GetClass()->GetDescriptor(&temp1), |
| 1140 | arg_type->GetDescriptor(&temp2)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1143 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1144 | new_shadow_frame->SetVRegReference(dest_reg, o.Ptr()); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1145 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1146 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1147 | // Handle doubles and longs. 2 consecutive virtual register slots. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1148 | case 'J': case 'D': { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1149 | uint64_t wide_value = |
| 1150 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 1151 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1152 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1153 | // Skip the next virtual register slot since we already used it. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1154 | ++dest_reg; |
| 1155 | ++arg_offset; |
| 1156 | break; |
| 1157 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1158 | // Handle all other primitives that are always 1 virtual register slot. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1159 | default: |
| 1160 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 1161 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1162 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1163 | } |
| 1164 | } else { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1165 | if (is_range) { |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1166 | DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1167 | } |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1168 | |
| 1169 | CopyRegisters<is_range>(shadow_frame, |
| 1170 | new_shadow_frame, |
| 1171 | arg, |
| 1172 | vregC, |
| 1173 | first_dest_reg, |
| 1174 | number_of_inputs); |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1175 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1178 | PerformCall(self, code_item, shadow_frame.GetMethod(), first_dest_reg, new_shadow_frame, result); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1179 | |
| 1180 | if (string_init && !self->IsExceptionPending()) { |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 1181 | SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1184 | return !self->IsExceptionPending(); |
| 1185 | } |
| 1186 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1187 | template<bool is_range, bool do_assignability_check> |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1188 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 1189 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 1190 | // Argument word count. |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1191 | const uint16_t number_of_inputs = |
| 1192 | (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1193 | |
| 1194 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 1195 | // code. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1196 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX. |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1197 | uint32_t vregC = 0; |
| 1198 | if (is_range) { |
| 1199 | vregC = inst->VRegC_3rc(); |
| 1200 | } else { |
| 1201 | vregC = inst->VRegC_35c(); |
| 1202 | inst->GetVarArgs(arg, inst_data); |
| 1203 | } |
| 1204 | |
| 1205 | return DoCallCommon<is_range, do_assignability_check>( |
| 1206 | called_method, self, shadow_frame, |
| 1207 | result, number_of_inputs, arg, vregC); |
| 1208 | } |
| 1209 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1210 | template <bool is_range, bool do_access_check, bool transaction_active> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1211 | bool DoFilledNewArray(const Instruction* inst, |
| 1212 | const ShadowFrame& shadow_frame, |
| 1213 | Thread* self, |
| 1214 | JValue* result) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1215 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 1216 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 1217 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 1218 | if (!is_range) { |
| 1219 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 1220 | CHECK_LE(length, 5); |
| 1221 | } |
| 1222 | if (UNLIKELY(length < 0)) { |
| 1223 | ThrowNegativeArraySizeException(length); |
| 1224 | return false; |
| 1225 | } |
| 1226 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1227 | ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(type_idx, |
| 1228 | shadow_frame.GetMethod(), |
| 1229 | self, |
| 1230 | false, |
| 1231 | do_access_check); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1232 | if (UNLIKELY(array_class == nullptr)) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1233 | DCHECK(self->IsExceptionPending()); |
| 1234 | return false; |
| 1235 | } |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1236 | CHECK(array_class->IsArrayClass()); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1237 | ObjPtr<mirror::Class> component_class = array_class->GetComponentType(); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1238 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 1239 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 1240 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1241 | ThrowRuntimeException("Bad filled array request for type %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1242 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1243 | } else { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1244 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 1245 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1246 | component_class->PrettyDescriptor().c_str()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1247 | } |
| 1248 | return false; |
| 1249 | } |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1250 | ObjPtr<mirror::Object> new_array = mirror::Array::Alloc<true>( |
| 1251 | self, |
| 1252 | array_class, |
| 1253 | length, |
| 1254 | array_class->GetComponentSizeShift(), |
| 1255 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1256 | if (UNLIKELY(new_array == nullptr)) { |
| 1257 | self->AssertPendingOOMException(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1260 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 1261 | uint32_t vregC = 0; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1262 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1263 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1264 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1265 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1266 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1267 | for (int32_t i = 0; i < length; ++i) { |
| 1268 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 1269 | if (is_primitive_int_component) { |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1270 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 1271 | i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1272 | } else { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1273 | new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>( |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1274 | i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1278 | result->SetL(new_array); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1282 | // TODO: Use ObjPtr here. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1283 | template<typename T> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1284 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, |
| 1285 | int32_t count) |
| 1286 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1287 | Runtime* runtime = Runtime::Current(); |
| 1288 | for (int32_t i = 0; i < count; ++i) { |
| 1289 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1293 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1294 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1295 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 1296 | DCHECK(array != nullptr); |
| 1297 | DCHECK_LE(count, array->GetLength()); |
| 1298 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 1299 | switch (primitive_component_type) { |
| 1300 | case Primitive::kPrimBoolean: |
| 1301 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 1302 | break; |
| 1303 | case Primitive::kPrimByte: |
| 1304 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 1305 | break; |
| 1306 | case Primitive::kPrimChar: |
| 1307 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 1308 | break; |
| 1309 | case Primitive::kPrimShort: |
| 1310 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 1311 | break; |
| 1312 | case Primitive::kPrimInt: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1313 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 1314 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1315 | case Primitive::kPrimFloat: |
| 1316 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 1317 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1318 | case Primitive::kPrimLong: |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1319 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 1320 | break; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1321 | case Primitive::kPrimDouble: |
| 1322 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 1323 | break; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1324 | default: |
| 1325 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 1326 | << " in fill-array-data"; |
| 1327 | break; |
| 1328 | } |
| 1329 | } |
| 1330 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1331 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1332 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1333 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1334 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 1335 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1336 | const Instruction* inst, uint16_t inst_data, \ |
| 1337 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1338 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 1339 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 1340 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 1341 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 1342 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1343 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1344 | // Explicit DoInvokePolymorphic template function declarations. |
| 1345 | #define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 1346 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 1347 | bool DoInvokePolymorphic<_is_range, _do_assignability_check>( \ |
| 1348 | Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1349 | uint16_t inst_data, JValue* result) |
| 1350 | |
| 1351 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, false); |
| 1352 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false, true); |
| 1353 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, false); |
| 1354 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true, true); |
| 1355 | #undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL |
| 1356 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1357 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1358 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1359 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1360 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 1361 | const ShadowFrame& shadow_frame, \ |
| 1362 | Thread* self, JValue* result) |
| 1363 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 1364 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 1365 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 1366 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 1367 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 1368 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 1369 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 1370 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1371 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 1372 | |
| 1373 | } // namespace interpreter |
| 1374 | } // namespace art |