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