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 | #ifndef ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |
| 18 | #define ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |
| 19 | |
| 20 | #include "interpreter.h" |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 21 | #include "interpreter_intrinsics.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 22 | |
| 23 | #include <math.h> |
| 24 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include <atomic> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 26 | #include <iostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | #include <sstream> |
| 28 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
| 30 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 31 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 32 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 34 | #include "base/enums.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 35 | #include "base/macros.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 36 | #include "base/mutex.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 37 | #include "class_linker-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 38 | #include "common_dex_operations.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 39 | #include "common_throws.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 40 | #include "dex/dex_file-inl.h" |
| 41 | #include "dex/dex_instruction-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 42 | #include "entrypoints/entrypoint_utils-inl.h" |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 43 | #include "handle_scope-inl.h" |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 44 | #include "jit/jit.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 45 | #include "mirror/call_site.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 46 | #include "mirror/class-inl.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 47 | #include "mirror/dex_cache.h" |
| 48 | #include "mirror/method.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 49 | #include "mirror/method_handles_lookup.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 50 | #include "mirror/object-inl.h" |
| 51 | #include "mirror/object_array-inl.h" |
Douglas Leung | 4965c02 | 2014-06-11 11:41:11 -0700 | [diff] [blame] | 52 | #include "mirror/string-inl.h" |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 53 | #include "obj_ptr.h" |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 54 | #include "stack.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 55 | #include "thread.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 56 | #include "unstarted_runtime.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 57 | #include "well_known_classes.h" |
| 58 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 59 | namespace art { |
| 60 | namespace interpreter { |
| 61 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 62 | void ThrowNullPointerExceptionFromInterpreter() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | da843e1 | 2014-05-28 19:28:31 +0200 | [diff] [blame] | 64 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 65 | template <bool kMonitorCounting> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 66 | static inline void DoMonitorEnter(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref) |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 67 | NO_THREAD_SAFETY_ANALYSIS |
| 68 | REQUIRES(!Roles::uninterruptible_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 69 | DCHECK(!ref.IsNull()); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 70 | StackHandleScope<1> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 71 | Handle<mirror::Object> h_ref(hs.NewHandle(ref)); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 72 | h_ref->MonitorEnter(self); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 73 | DCHECK(self->HoldsLock(h_ref.Get())); |
| 74 | if (UNLIKELY(self->IsExceptionPending())) { |
| 75 | bool unlocked = h_ref->MonitorExit(self); |
| 76 | DCHECK(unlocked); |
| 77 | return; |
| 78 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 79 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 80 | frame->GetLockCountData().AddMonitor(self, h_ref.Get()); |
| 81 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 84 | template <bool kMonitorCounting> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 85 | static inline void DoMonitorExit(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref) |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 86 | NO_THREAD_SAFETY_ANALYSIS |
| 87 | REQUIRES(!Roles::uninterruptible_) { |
| 88 | StackHandleScope<1> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 89 | Handle<mirror::Object> h_ref(hs.NewHandle(ref)); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 90 | h_ref->MonitorExit(self); |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 91 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 92 | frame->GetLockCountData().RemoveMonitorOrThrow(self, h_ref.Get()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | template <bool kMonitorCounting> |
| 97 | static inline bool DoMonitorCheckOnExit(Thread* self, ShadowFrame* frame) |
| 98 | NO_THREAD_SAFETY_ANALYSIS |
| 99 | REQUIRES(!Roles::uninterruptible_) { |
| 100 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 101 | return frame->GetLockCountData().CheckAllMonitorsReleasedOrThrow(self); |
| 102 | } |
| 103 | return true; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 106 | void AbortTransactionF(Thread* self, const char* fmt, ...) |
| 107 | __attribute__((__format__(__printf__, 2, 3))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 108 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 109 | |
| 110 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 111 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 112 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 113 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 115 | |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 116 | // Invokes the given method. This is part of the invocation support and is used by DoInvoke, |
| 117 | // DoFastInvoke and DoInvokeVirtualQuick functions. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 118 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 119 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 120 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 121 | const Instruction* inst, uint16_t inst_data, JValue* result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 122 | |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 123 | // Handles streamlined non-range invoke static, direct and virtual instructions originating in |
| 124 | // mterp. Access checks and instrumentation other than jit profiling are not supported, but does |
| 125 | // support interpreter intrinsics if applicable. |
| 126 | // Returns true on success, otherwise throws an exception and returns false. |
| 127 | template<InvokeType type> |
| 128 | static inline bool DoFastInvoke(Thread* self, |
| 129 | ShadowFrame& shadow_frame, |
| 130 | const Instruction* inst, |
| 131 | uint16_t inst_data, |
| 132 | JValue* result) { |
| 133 | const uint32_t method_idx = inst->VRegB_35c(); |
| 134 | const uint32_t vregC = inst->VRegC_35c(); |
| 135 | ObjPtr<mirror::Object> receiver = (type == kStatic) |
| 136 | ? nullptr |
| 137 | : shadow_frame.GetVRegReference(vregC); |
| 138 | ArtMethod* sf_method = shadow_frame.GetMethod(); |
| 139 | ArtMethod* const called_method = FindMethodFromCode<type, false>( |
| 140 | method_idx, &receiver, sf_method, self); |
| 141 | // The shadow frame should already be pushed, so we don't need to update it. |
| 142 | if (UNLIKELY(called_method == nullptr)) { |
| 143 | CHECK(self->IsExceptionPending()); |
| 144 | result->SetJ(0); |
| 145 | return false; |
| 146 | } else if (UNLIKELY(!called_method->IsInvokable())) { |
| 147 | called_method->ThrowInvocationTimeError(); |
| 148 | result->SetJ(0); |
| 149 | return false; |
| 150 | } else { |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 151 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Nicolas Geoffray | cecc488 | 2017-08-09 19:14:01 +0100 | [diff] [blame] | 152 | if (jit != nullptr && type == kVirtual) { |
| 153 | jit->InvokeVirtualOrInterface(receiver, sf_method, shadow_frame.GetDexPC(), called_method); |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 154 | } |
Nicolas Geoffray | b9bec2e | 2017-05-24 15:59:18 +0100 | [diff] [blame] | 155 | if (called_method->IsIntrinsic()) { |
| 156 | if (MterpHandleIntrinsic(&shadow_frame, called_method, inst, inst_data, |
| 157 | shadow_frame.GetResultRegister())) { |
| 158 | return !self->IsExceptionPending(); |
| 159 | } |
| 160 | } |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 161 | return DoCall<false, false>(called_method, self, shadow_frame, inst, inst_data, result); |
| 162 | } |
| 163 | } |
| 164 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 165 | // Handles all invoke-XXX/range instructions except for invoke-polymorphic[/range]. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 166 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 167 | template<InvokeType type, bool is_range, bool do_access_check> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 168 | static inline bool DoInvoke(Thread* self, |
| 169 | ShadowFrame& shadow_frame, |
| 170 | const Instruction* inst, |
| 171 | uint16_t inst_data, |
| 172 | JValue* result) { |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 173 | // Make sure to check for async exceptions before anything else. |
| 174 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 175 | return false; |
| 176 | } |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 177 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 178 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
Roland Levillain | 2ae376f | 2018-01-30 11:35:11 +0000 | [diff] [blame] | 179 | ObjPtr<mirror::Object> receiver = |
| 180 | (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 181 | ArtMethod* sf_method = shadow_frame.GetMethod(); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 182 | ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>( |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 183 | method_idx, &receiver, sf_method, self); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 184 | // The shadow frame should already be pushed, so we don't need to update it. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 185 | if (UNLIKELY(called_method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 186 | CHECK(self->IsExceptionPending()); |
| 187 | result->SetJ(0); |
| 188 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 189 | } else if (UNLIKELY(!called_method->IsInvokable())) { |
| 190 | called_method->ThrowInvocationTimeError(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 191 | result->SetJ(0); |
| 192 | return false; |
| 193 | } else { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 194 | jit::Jit* jit = Runtime::Current()->GetJit(); |
Nicolas Geoffray | cecc488 | 2017-08-09 19:14:01 +0100 | [diff] [blame] | 195 | if (jit != nullptr && (type == kVirtual || type == kInterface)) { |
| 196 | jit->InvokeVirtualOrInterface(receiver, sf_method, shadow_frame.GetDexPC(), called_method); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 197 | } |
| 198 | // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT. |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 199 | if (type == kVirtual || type == kInterface) { |
| 200 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 201 | if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) { |
| 202 | instrumentation->InvokeVirtualOrInterface( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 203 | self, receiver.Ptr(), sf_method, shadow_frame.GetDexPC(), called_method); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 206 | return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data, |
| 207 | result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 208 | } |
| 209 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 210 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 211 | static inline ObjPtr<mirror::MethodHandle> ResolveMethodHandle(Thread* self, |
| 212 | uint32_t method_handle_index, |
| 213 | ArtMethod* referrer) |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 214 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 215 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 216 | return class_linker->ResolveMethodHandle(self, method_handle_index, referrer); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 219 | static inline ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self, |
Orion Hodson | 16b2adf | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 220 | dex::ProtoIndex method_type_index, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 221 | ArtMethod* referrer) |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 222 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 223 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 224 | return class_linker->ResolveMethodType(self, method_type_index, referrer); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 227 | #define DECLARE_SIGNATURE_POLYMORPHIC_HANDLER(Name, ...) \ |
| 228 | bool Do ## Name(Thread* self, \ |
| 229 | ShadowFrame& shadow_frame, \ |
| 230 | const Instruction* inst, \ |
| 231 | uint16_t inst_data, \ |
| 232 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_); |
| 233 | #include "intrinsics_list.h" |
| 234 | INTRINSICS_LIST(DECLARE_SIGNATURE_POLYMORPHIC_HANDLER) |
| 235 | #undef INTRINSICS_LIST |
| 236 | #undef DECLARE_SIGNATURE_POLYMORPHIC_HANDLER |
| 237 | |
| 238 | // Performs a invoke-polymorphic or invoke-polymorphic-range. |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 239 | template<bool is_range> |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 240 | bool DoInvokePolymorphic(Thread* self, |
| 241 | ShadowFrame& shadow_frame, |
| 242 | const Instruction* inst, |
| 243 | uint16_t inst_data, |
Orion Hodson | 20e7a6e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 244 | JValue* result) |
| 245 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 246 | |
| 247 | bool DoInvokeCustom(Thread* self, |
| 248 | ShadowFrame& shadow_frame, |
| 249 | uint32_t call_site_idx, |
| 250 | const InstructionOperands* operands, |
| 251 | JValue* result) |
| 252 | REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 253 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 254 | // Performs a custom invoke (invoke-custom/invoke-custom-range). |
| 255 | template<bool is_range> |
| 256 | bool DoInvokeCustom(Thread* self, |
| 257 | ShadowFrame& shadow_frame, |
| 258 | const Instruction* inst, |
| 259 | uint16_t inst_data, |
Orion Hodson | 20e7a6e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 260 | JValue* result) |
| 261 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 262 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 263 | if (is_range) { |
| 264 | RangeInstructionOperands operands(inst->VRegC_3rc(), inst->VRegA_3rc()); |
| 265 | return DoInvokeCustom(self, shadow_frame, call_site_idx, &operands, result); |
| 266 | } else { |
| 267 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 268 | inst->GetVarArgs(args, inst_data); |
| 269 | VarArgsInstructionOperands operands(args, inst->VRegA_35c()); |
| 270 | return DoInvokeCustom(self, shadow_frame, call_site_idx, &operands, result); |
| 271 | } |
| 272 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 273 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 274 | // Handles invoke-virtual-quick and invoke-virtual-quick-range instructions. |
| 275 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 276 | template<bool is_range> |
| 277 | static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame, |
| 278 | const Instruction* inst, uint16_t inst_data, |
| 279 | JValue* result) { |
| 280 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 281 | ObjPtr<mirror::Object> const receiver = shadow_frame.GetVRegReference(vregC); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 282 | if (UNLIKELY(receiver == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 283 | // We lost the reference to the method index so we cannot get a more |
| 284 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 285 | ThrowNullPointerExceptionFromDexPC(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 289 | CHECK(receiver->GetClass()->ShouldHaveEmbeddedVTable()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 290 | ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry( |
Chang Xing | fef27c2 | 2017-07-11 09:57:44 -0700 | [diff] [blame] | 291 | vtable_idx, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 292 | if (UNLIKELY(called_method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 293 | CHECK(self->IsExceptionPending()); |
| 294 | result->SetJ(0); |
| 295 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 296 | } else if (UNLIKELY(!called_method->IsInvokable())) { |
| 297 | called_method->ThrowInvocationTimeError(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 298 | result->SetJ(0); |
| 299 | return false; |
| 300 | } else { |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 301 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 302 | if (jit != nullptr) { |
| 303 | jit->InvokeVirtualOrInterface( |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 304 | receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 305 | jit->AddSamples(self, shadow_frame.GetMethod(), 1, /*with_backedges*/false); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 306 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 307 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 308 | // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT. |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 309 | if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) { |
| 310 | instrumentation->InvokeVirtualOrInterface( |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 311 | self, receiver.Ptr(), shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 312 | } |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 313 | // No need to check since we've been quickened. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 314 | return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 315 | } |
| 316 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 317 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 318 | // Handles iget-XXX and sget-XXX instructions. |
| 319 | // Returns true on success, otherwise throws an exception and returns false. |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 320 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 321 | bool transaction_active = false> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 322 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 323 | uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 324 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 325 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 326 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 327 | template<Primitive::Type field_type> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 328 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 329 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 330 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 331 | // Handles iput-XXX and sput-XXX instructions. |
| 332 | // Returns true on success, otherwise throws an exception and returns false. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 333 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 334 | bool transaction_active> |
| 335 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 336 | uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 337 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 338 | // Handles iput-quick, iput-wide-quick and iput-object-quick instructions. |
| 339 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 340 | template<Primitive::Type field_type, bool transaction_active> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 341 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 342 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 343 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 344 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 345 | // Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the |
| 346 | // java.lang.String class is initialized. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 347 | static inline ObjPtr<mirror::String> ResolveString(Thread* self, |
| 348 | ShadowFrame& shadow_frame, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 349 | dex::StringIndex string_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 350 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 351 | ObjPtr<mirror::Class> java_lang_string_class = mirror::String::GetJavaLangString(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 352 | if (UNLIKELY(!java_lang_string_class->IsInitialized())) { |
| 353 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 354 | StackHandleScope<1> hs(self); |
| 355 | Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 356 | if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 357 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 358 | return nullptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 359 | } |
| 360 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 361 | ArtMethod* method = shadow_frame.GetMethod(); |
Vladimir Marko | ec78622 | 2016-12-20 16:24:13 +0000 | [diff] [blame] | 362 | ObjPtr<mirror::String> string_ptr = method->GetDexCache()->GetResolvedString(string_idx); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 363 | if (UNLIKELY(string_ptr == nullptr)) { |
Ian Rogers | 6786a58 | 2014-10-28 12:49:06 -0700 | [diff] [blame] | 364 | StackHandleScope<1> hs(self); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 365 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 366 | string_ptr = Runtime::Current()->GetClassLinker()->ResolveString(string_idx, dex_cache); |
Ian Rogers | 6786a58 | 2014-10-28 12:49:06 -0700 | [diff] [blame] | 367 | } |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 368 | return string_ptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 371 | // Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions. |
| 372 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 373 | static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 374 | int32_t dividend, int32_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 375 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 376 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 377 | if (UNLIKELY(divisor == 0)) { |
| 378 | ThrowArithmeticExceptionDivideByZero(); |
| 379 | return false; |
| 380 | } |
| 381 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 382 | shadow_frame.SetVReg(result_reg, kMinInt); |
| 383 | } else { |
| 384 | shadow_frame.SetVReg(result_reg, dividend / divisor); |
| 385 | } |
| 386 | return true; |
| 387 | } |
| 388 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 389 | // Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions. |
| 390 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 391 | static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 392 | int32_t dividend, int32_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 393 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 394 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 395 | if (UNLIKELY(divisor == 0)) { |
| 396 | ThrowArithmeticExceptionDivideByZero(); |
| 397 | return false; |
| 398 | } |
| 399 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 400 | shadow_frame.SetVReg(result_reg, 0); |
| 401 | } else { |
| 402 | shadow_frame.SetVReg(result_reg, dividend % divisor); |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 407 | // Handles div-long and div-long-2addr instructions. |
| 408 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 409 | static inline bool DoLongDivide(ShadowFrame& shadow_frame, |
| 410 | size_t result_reg, |
| 411 | int64_t dividend, |
| 412 | int64_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 413 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 414 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 415 | if (UNLIKELY(divisor == 0)) { |
| 416 | ThrowArithmeticExceptionDivideByZero(); |
| 417 | return false; |
| 418 | } |
| 419 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 420 | shadow_frame.SetVRegLong(result_reg, kMinLong); |
| 421 | } else { |
| 422 | shadow_frame.SetVRegLong(result_reg, dividend / divisor); |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 427 | // Handles rem-long and rem-long-2addr instructions. |
| 428 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 429 | static inline bool DoLongRemainder(ShadowFrame& shadow_frame, |
| 430 | size_t result_reg, |
| 431 | int64_t dividend, |
| 432 | int64_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 433 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 434 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 435 | if (UNLIKELY(divisor == 0)) { |
| 436 | ThrowArithmeticExceptionDivideByZero(); |
| 437 | return false; |
| 438 | } |
| 439 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 440 | shadow_frame.SetVRegLong(result_reg, 0); |
| 441 | } else { |
| 442 | shadow_frame.SetVRegLong(result_reg, dividend % divisor); |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 447 | // Handles filled-new-array and filled-new-array-range instructions. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 448 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 449 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 450 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 451 | Thread* self, JValue* result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 452 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 453 | // Handles packed-switch instruction. |
| 454 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 455 | static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 456 | uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 457 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 458 | DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH); |
| 459 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 460 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 461 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 462 | uint16_t size = switch_data[1]; |
David Brazdil | 2ef645b | 2015-06-17 18:20:52 +0100 | [diff] [blame] | 463 | if (size == 0) { |
| 464 | // Empty packed switch, move forward by 3 (size of PACKED_SWITCH). |
| 465 | return 3; |
| 466 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 467 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 468 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 469 | int32_t first_key = keys[0]; |
| 470 | const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 471 | DCHECK_ALIGNED(targets, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 472 | int32_t index = test_val - first_key; |
| 473 | if (index >= 0 && index < size) { |
| 474 | return targets[index]; |
| 475 | } else { |
| 476 | // No corresponding value: move forward by 3 (size of PACKED_SWITCH). |
| 477 | return 3; |
| 478 | } |
| 479 | } |
| 480 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 481 | // Handles sparse-switch instruction. |
| 482 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 483 | static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 484 | uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 485 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 486 | DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH); |
| 487 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 488 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 489 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 490 | uint16_t size = switch_data[1]; |
Jeff Hao | 935e01a | 2015-03-20 19:44:35 -0700 | [diff] [blame] | 491 | // Return length of SPARSE_SWITCH if size is 0. |
| 492 | if (size == 0) { |
| 493 | return 3; |
| 494 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 495 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 496 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 497 | const int32_t* entries = keys + size; |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 498 | DCHECK_ALIGNED(entries, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 499 | int lo = 0; |
| 500 | int hi = size - 1; |
| 501 | while (lo <= hi) { |
| 502 | int mid = (lo + hi) / 2; |
| 503 | int32_t foundVal = keys[mid]; |
| 504 | if (test_val < foundVal) { |
| 505 | hi = mid - 1; |
| 506 | } else if (test_val > foundVal) { |
| 507 | lo = mid + 1; |
| 508 | } else { |
| 509 | return entries[mid]; |
| 510 | } |
| 511 | } |
| 512 | // No corresponding value: move forward by 3 (size of SPARSE_SWITCH). |
| 513 | return 3; |
| 514 | } |
| 515 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 516 | // We execute any instrumentation events triggered by throwing and/or handing the pending exception |
| 517 | // and change the shadow_frames dex_pc to the appropriate exception handler if the current method |
| 518 | // has one. If the exception has been handled and the shadow_frame is now pointing to a catch clause |
| 519 | // we return true. If the current method is unable to handle the exception we return false. |
| 520 | // This function accepts a null Instrumentation* as a way to cause instrumentation events not to be |
| 521 | // reported. |
| 522 | // TODO We might wish to reconsider how we cause some events to be ignored. |
| 523 | bool MoveToExceptionHandler(Thread* self, |
| 524 | ShadowFrame& shadow_frame, |
| 525 | const instrumentation::Instrumentation* instrumentation) |
| 526 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 527 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 528 | NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) |
| 529 | __attribute__((cold)) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 530 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 531 | |
Bill Buzbee | d47fd90 | 2016-07-07 14:42:43 +0000 | [diff] [blame] | 532 | // Set true if you want TraceExecution invocation before each bytecode execution. |
| 533 | constexpr bool kTraceExecutionEnabled = false; |
Serguei Katkov | 9fb0ac7 | 2016-02-20 12:55:24 +0600 | [diff] [blame] | 534 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 535 | static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst, |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 536 | const uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 537 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Bill Buzbee | d47fd90 | 2016-07-07 14:42:43 +0000 | [diff] [blame] | 538 | if (kTraceExecutionEnabled) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 539 | #define TRACE_LOG std::cerr |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 540 | std::ostringstream oss; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 541 | oss << shadow_frame.GetMethod()->PrettyMethod() |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 542 | << android::base::StringPrintf("\n0x%x: ", dex_pc) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 543 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 544 | for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 545 | uint32_t raw_value = shadow_frame.GetVReg(i); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 546 | ObjPtr<mirror::Object> ref_value = shadow_frame.GetVRegReference(i); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 547 | oss << android::base::StringPrintf(" vreg%u=0x%08X", i, raw_value); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 548 | if (ref_value != nullptr) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 549 | if (ref_value->GetClass()->IsStringClass() && |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 550 | !ref_value->AsString()->IsValueNull()) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 551 | oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\""; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 552 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 553 | oss << "/" << ref_value->PrettyTypeOf(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 557 | TRACE_LOG << oss.str() << "\n"; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 558 | #undef TRACE_LOG |
| 559 | } |
| 560 | } |
| 561 | |
Sebastien Hertz | 1eda226 | 2013-09-09 16:53:14 +0200 | [diff] [blame] | 562 | static inline bool IsBackwardBranch(int32_t branch_offset) { |
| 563 | return branch_offset <= 0; |
| 564 | } |
| 565 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 566 | // The arg_offset is the offset to the first input register in the frame. |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 567 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| 568 | ArtMethod* caller, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 569 | ShadowFrame* shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 570 | uint16_t arg_offset, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 571 | JValue* result); |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 572 | |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 573 | static inline bool IsStringInit(const DexFile* dex_file, uint32_t method_idx) |
| 574 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 575 | const DexFile::MethodId& method_id = dex_file->GetMethodId(method_idx); |
| 576 | const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_); |
| 577 | const char* method_name = dex_file->GetMethodName(method_id); |
| 578 | // Instead of calling ResolveMethod() which has suspend point and can trigger |
| 579 | // GC, look up the method symbolically. |
| 580 | // Compare method's class name and method name against string init. |
| 581 | // It's ok since it's not allowed to create your own java/lang/String. |
| 582 | // TODO: verify that assumption. |
| 583 | if ((strcmp(class_name, "Ljava/lang/String;") == 0) && |
| 584 | (strcmp(method_name, "<init>") == 0)) { |
| 585 | return true; |
| 586 | } |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | static inline bool IsStringInit(const Instruction* instr, ArtMethod* caller) |
| 591 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 592 | if (instr->Opcode() == Instruction::INVOKE_DIRECT || |
| 593 | instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) { |
| 594 | uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ? |
| 595 | instr->VRegB_3rc() : instr->VRegB_35c(); |
| 596 | return IsStringInit(caller->GetDexFile(), callee_method_idx); |
| 597 | } |
| 598 | return false; |
| 599 | } |
| 600 | |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 601 | // Set string value created from StringFactory.newStringFromXXX() into all aliases of |
| 602 | // StringFactory.newEmptyString(). |
| 603 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 604 | uint16_t this_obj_vreg, |
| 605 | JValue result); |
| 606 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 607 | // Explicitly instantiate all DoInvoke functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 608 | #define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \ |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 609 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 610 | bool DoInvoke<_type, _is_range, _do_check>(Thread* self, \ |
| 611 | ShadowFrame& shadow_frame, \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 612 | const Instruction* inst, uint16_t inst_data, \ |
| 613 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 614 | |
| 615 | #define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \ |
| 616 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \ |
| 617 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \ |
| 618 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \ |
| 619 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true); |
| 620 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 621 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range. |
| 622 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range. |
| 623 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range. |
| 624 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range. |
| 625 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 626 | #undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL |
| 627 | #undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL |
| 628 | |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 629 | // Explicitly instantiate all DoFastInvoke functions. |
| 630 | #define EXPLICIT_DO_FAST_INVOKE_TEMPLATE_DECL(_type) \ |
| 631 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 632 | bool DoFastInvoke<_type>(Thread* self, \ |
| 633 | ShadowFrame& shadow_frame, \ |
| 634 | const Instruction* inst, uint16_t inst_data, \ |
| 635 | JValue* result) |
| 636 | |
| 637 | EXPLICIT_DO_FAST_INVOKE_TEMPLATE_DECL(kStatic); // invoke-static |
| 638 | EXPLICIT_DO_FAST_INVOKE_TEMPLATE_DECL(kDirect); // invoke-direct |
| 639 | EXPLICIT_DO_FAST_INVOKE_TEMPLATE_DECL(kVirtual); // invoke-virtual |
| 640 | #undef EXPLICIT_DO_FAST_INVOKE_TEMPLATE_DECL |
| 641 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 642 | // Explicitly instantiate all DoInvokeVirtualQuick functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 643 | #define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \ |
Roland Levillain | 2ae376f | 2018-01-30 11:35:11 +0000 | [diff] [blame] | 644 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 645 | bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \ |
| 646 | const Instruction* inst, uint16_t inst_data, \ |
| 647 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 648 | |
| 649 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick. |
| 650 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range. |
| 651 | #undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK |
| 652 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 653 | } // namespace interpreter |
| 654 | } // namespace art |
| 655 | |
| 656 | #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |