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 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | #include "instrumentation.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 22 | #include "interpreter.h" |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 23 | #include "interpreter_intrinsics.h" |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 24 | #include "transaction.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 25 | |
| 26 | #include <math.h> |
| 27 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include <atomic> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 29 | #include <iostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 30 | #include <sstream> |
| 31 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 35 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 37 | #include "base/enums.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 38 | #include "base/locks.h" |
Mathieu Chartier | 765b2a0 | 2019-05-02 11:04:13 -0700 | [diff] [blame] | 39 | #include "base/logging.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 40 | #include "base/macros.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 41 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 42 | #include "class_root-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 43 | #include "common_dex_operations.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 44 | #include "common_throws.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 45 | #include "dex/dex_file-inl.h" |
| 46 | #include "dex/dex_instruction-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 47 | #include "entrypoints/entrypoint_utils-inl.h" |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 48 | #include "handle_scope-inl.h" |
David Srbecky | 960327b | 2018-10-25 10:11:59 +0000 | [diff] [blame] | 49 | #include "interpreter_switch_impl.h" |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 50 | #include "jit/jit-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 51 | #include "mirror/call_site.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 52 | #include "mirror/class-inl.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 53 | #include "mirror/dex_cache.h" |
| 54 | #include "mirror/method.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 55 | #include "mirror/method_handles_lookup.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 56 | #include "mirror/object-inl.h" |
| 57 | #include "mirror/object_array-inl.h" |
Douglas Leung | 4965c02 | 2014-06-11 11:41:11 -0700 | [diff] [blame] | 58 | #include "mirror/string-inl.h" |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 59 | #include "obj_ptr.h" |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 60 | #include "stack.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 61 | #include "thread.h" |
Hans Boehm | 1b3ec0f | 2022-01-26 16:53:07 +0000 | [diff] [blame] | 62 | #include "thread-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 63 | #include "unstarted_runtime.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 64 | #include "verifier/method_verifier.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 65 | #include "well_known_classes.h" |
| 66 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 67 | namespace art { |
| 68 | namespace interpreter { |
| 69 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 70 | void ThrowNullPointerExceptionFromInterpreter() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 71 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | da843e1 | 2014-05-28 19:28:31 +0200 | [diff] [blame] | 72 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 73 | template <bool kMonitorCounting> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 74 | static inline void DoMonitorEnter(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref) |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 75 | NO_THREAD_SAFETY_ANALYSIS |
| 76 | REQUIRES(!Roles::uninterruptible_) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 77 | DCHECK(!ref.IsNull()); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 78 | StackHandleScope<1> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 79 | Handle<mirror::Object> h_ref(hs.NewHandle(ref)); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 80 | h_ref->MonitorEnter(self); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 81 | DCHECK(self->HoldsLock(h_ref.Get())); |
| 82 | if (UNLIKELY(self->IsExceptionPending())) { |
| 83 | bool unlocked = h_ref->MonitorExit(self); |
| 84 | DCHECK(unlocked); |
| 85 | return; |
| 86 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 87 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 88 | frame->GetLockCountData().AddMonitor(self, h_ref.Get()); |
| 89 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 92 | template <bool kMonitorCounting> |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 93 | static inline void DoMonitorExit(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref) |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 94 | NO_THREAD_SAFETY_ANALYSIS |
| 95 | REQUIRES(!Roles::uninterruptible_) { |
| 96 | StackHandleScope<1> hs(self); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 97 | Handle<mirror::Object> h_ref(hs.NewHandle(ref)); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 98 | h_ref->MonitorExit(self); |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 99 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 100 | frame->GetLockCountData().RemoveMonitorOrThrow(self, h_ref.Get()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | template <bool kMonitorCounting> |
| 105 | static inline bool DoMonitorCheckOnExit(Thread* self, ShadowFrame* frame) |
| 106 | NO_THREAD_SAFETY_ANALYSIS |
| 107 | REQUIRES(!Roles::uninterruptible_) { |
| 108 | if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) { |
| 109 | return frame->GetLockCountData().CheckAllMonitorsReleasedOrThrow(self); |
| 110 | } |
| 111 | return true; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 114 | void AbortTransactionF(Thread* self, const char* fmt, ...) |
| 115 | __attribute__((__format__(__printf__, 2, 3))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 116 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 117 | |
| 118 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 119 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 120 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 121 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 123 | |
buzbee | 78f1bdc | 2017-03-01 10:55:57 -0800 | [diff] [blame] | 124 | // Invokes the given method. This is part of the invocation support and is used by DoInvoke, |
| 125 | // DoFastInvoke and DoInvokeVirtualQuick functions. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 126 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 127 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 128 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 129 | const Instruction* inst, uint16_t inst_data, JValue* result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 130 | |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 131 | // Called by the switch interpreter to know if we can stay in it. |
| 132 | bool ShouldStayInSwitchInterpreter(ArtMethod* method) |
David Srbecky | 9581e61 | 2018-10-30 14:29:43 +0000 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_); |
David Srbecky | 960327b | 2018-10-25 10:11:59 +0000 | [diff] [blame] | 134 | |
| 135 | // Throws exception if we are getting close to the end of the stack. |
| 136 | NO_INLINE bool CheckStackOverflow(Thread* self, size_t frame_size) |
| 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 138 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 139 | |
| 140 | // Sends the normal method exit event. |
| 141 | // Returns true if the events succeeded and false if there is a pending exception. |
| 142 | template <typename T> bool SendMethodExitEvents( |
| 143 | Thread* self, |
| 144 | const instrumentation::Instrumentation* instrumentation, |
| 145 | ShadowFrame& frame, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 146 | ArtMethod* method, |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 147 | T& result) REQUIRES_SHARED(Locks::mutator_lock_); |
| 148 | |
| 149 | static inline ALWAYS_INLINE WARN_UNUSED bool |
| 150 | NeedsMethodExitEvent(const instrumentation::Instrumentation* ins) |
| 151 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 152 | return ins->HasMethodExitListeners() || ins->HasWatchedFramePopListeners(); |
| 153 | } |
| 154 | |
| 155 | // NO_INLINE so we won't bloat the interpreter with this very cold lock-release code. |
| 156 | template <bool kMonitorCounting> |
| 157 | static NO_INLINE void UnlockHeldMonitors(Thread* self, ShadowFrame* shadow_frame) |
| 158 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 159 | DCHECK(shadow_frame->GetForcePopFrame() || |
| 160 | Runtime::Current()->IsTransactionAborted()); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 161 | // Unlock all monitors. |
| 162 | if (kMonitorCounting && shadow_frame->GetMethod()->MustCountLocks()) { |
| 163 | // Get the monitors from the shadow-frame monitor-count data. |
| 164 | shadow_frame->GetLockCountData().VisitMonitors( |
| 165 | [&](mirror::Object** obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 166 | // Since we don't use the 'obj' pointer after the DoMonitorExit everything should be fine |
| 167 | // WRT suspension. |
| 168 | DoMonitorExit<kMonitorCounting>(self, shadow_frame, *obj); |
| 169 | }); |
| 170 | } else { |
| 171 | std::vector<verifier::MethodVerifier::DexLockInfo> locks; |
| 172 | verifier::MethodVerifier::FindLocksAtDexPc(shadow_frame->GetMethod(), |
| 173 | shadow_frame->GetDexPC(), |
| 174 | &locks, |
| 175 | Runtime::Current()->GetTargetSdkVersion()); |
| 176 | for (const auto& reg : locks) { |
| 177 | if (UNLIKELY(reg.dex_registers.empty())) { |
| 178 | LOG(ERROR) << "Unable to determine reference locked by " |
| 179 | << shadow_frame->GetMethod()->PrettyMethod() << " at pc " |
| 180 | << shadow_frame->GetDexPC(); |
| 181 | } else { |
| 182 | DoMonitorExit<kMonitorCounting>( |
| 183 | self, shadow_frame, shadow_frame->GetVRegReference(*reg.dex_registers.begin())); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | enum class MonitorState { |
| 190 | kNoMonitorsLocked, |
| 191 | kCountingMonitors, |
| 192 | kNormalMonitors, |
| 193 | }; |
| 194 | |
| 195 | template<MonitorState kMonitorState> |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 196 | static inline ALWAYS_INLINE void PerformNonStandardReturn( |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 197 | Thread* self, |
| 198 | ShadowFrame& frame, |
| 199 | JValue& result, |
| 200 | const instrumentation::Instrumentation* instrumentation, |
Mythri Alle | 18fba4c | 2021-10-27 10:00:55 +0000 | [diff] [blame] | 201 | uint16_t num_dex_inst) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 202 | static constexpr bool kMonitorCounting = (kMonitorState == MonitorState::kCountingMonitors); |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 203 | ObjPtr<mirror::Object> thiz(frame.GetThisObject(num_dex_inst)); |
| 204 | StackHandleScope<1u> hs(self); |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 205 | if (UNLIKELY(self->IsExceptionPending())) { |
| 206 | LOG(WARNING) << "Suppressing exception for non-standard method exit: " |
| 207 | << self->GetException()->Dump(); |
| 208 | self->ClearException(); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 209 | } |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 210 | if (kMonitorState != MonitorState::kNoMonitorsLocked) { |
| 211 | UnlockHeldMonitors<kMonitorCounting>(self, &frame); |
| 212 | } |
| 213 | DoMonitorCheckOnExit<kMonitorCounting>(self, &frame); |
| 214 | result = JValue(); |
| 215 | if (UNLIKELY(NeedsMethodExitEvent(instrumentation))) { |
Mythri Alle | 18fba4c | 2021-10-27 10:00:55 +0000 | [diff] [blame] | 216 | SendMethodExitEvents(self, instrumentation, frame, frame.GetMethod(), result); |
Vladimir Marko | 0685b98 | 2021-03-25 11:59:22 +0000 | [diff] [blame] | 217 | } |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 220 | // Handles all invoke-XXX/range instructions except for invoke-polymorphic[/range]. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 221 | // Returns true on success, otherwise throws an exception and returns false. |
David Srbecky | dfe5544 | 2021-03-18 11:46:27 +0000 | [diff] [blame] | 222 | template<InvokeType type, bool is_range, bool do_access_check, bool is_mterp> |
David Srbecky | 6849c06 | 2018-09-21 09:42:24 +0100 | [diff] [blame] | 223 | static ALWAYS_INLINE bool DoInvoke(Thread* self, |
| 224 | ShadowFrame& shadow_frame, |
| 225 | const Instruction* inst, |
| 226 | uint16_t inst_data, |
| 227 | JValue* result) |
| 228 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 229 | // Make sure to check for async exceptions before anything else. |
Nicolas Geoffray | e4f983c | 2021-07-12 15:53:27 +0100 | [diff] [blame] | 230 | if (UNLIKELY(self->ObserveAsyncException())) { |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 233 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 234 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
David Srbecky | c853eed | 2018-09-13 14:33:22 +0100 | [diff] [blame] | 235 | ArtMethod* sf_method = shadow_frame.GetMethod(); |
| 236 | |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 237 | // Try to find the method in small thread-local cache first (only used when |
| 238 | // nterp is not used as mterp and nterp use the cache in an incompatible way). |
David Srbecky | c853eed | 2018-09-13 14:33:22 +0100 | [diff] [blame] | 239 | InterpreterCache* tls_cache = self->GetInterpreterCache(); |
| 240 | size_t tls_value; |
| 241 | ArtMethod* resolved_method; |
Hans Boehm | 3d2f148 | 2022-01-17 01:32:55 +0000 | [diff] [blame] | 242 | if (!IsNterpSupported() && LIKELY(tls_cache->Get(inst, &tls_value))) { |
David Srbecky | c853eed | 2018-09-13 14:33:22 +0100 | [diff] [blame] | 243 | resolved_method = reinterpret_cast<ArtMethod*>(tls_value); |
| 244 | } else { |
| 245 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 246 | constexpr ClassLinker::ResolveMode resolve_mode = |
| 247 | do_access_check ? ClassLinker::ResolveMode::kCheckICCEAndIAE |
| 248 | : ClassLinker::ResolveMode::kNoChecks; |
| 249 | resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, sf_method, type); |
| 250 | if (UNLIKELY(resolved_method == nullptr)) { |
| 251 | CHECK(self->IsExceptionPending()); |
| 252 | result->SetJ(0); |
| 253 | return false; |
| 254 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 255 | if (!IsNterpSupported()) { |
Hans Boehm | 3d2f148 | 2022-01-17 01:32:55 +0000 | [diff] [blame] | 256 | tls_cache->Set(inst, reinterpret_cast<size_t>(resolved_method)); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 257 | } |
David Srbecky | c853eed | 2018-09-13 14:33:22 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Null pointer check and virtual method resolution. |
Roland Levillain | 2ae376f | 2018-01-30 11:35:11 +0000 | [diff] [blame] | 261 | ObjPtr<mirror::Object> receiver = |
| 262 | (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC); |
David Srbecky | 08cb738 | 2018-10-30 09:27:59 +0000 | [diff] [blame] | 263 | ArtMethod* called_method; |
David Srbecky | dfe5544 | 2021-03-18 11:46:27 +0000 | [diff] [blame] | 264 | called_method = FindMethodToCall<type, do_access_check>( |
| 265 | method_idx, resolved_method, &receiver, sf_method, self); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 266 | if (UNLIKELY(called_method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 267 | CHECK(self->IsExceptionPending()); |
| 268 | result->SetJ(0); |
| 269 | return false; |
David Srbecky | 1f5ab4e | 2018-10-15 11:46:46 +0100 | [diff] [blame] | 270 | } |
| 271 | if (UNLIKELY(!called_method->IsInvokable())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 272 | called_method->ThrowInvocationTimeError(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 273 | result->SetJ(0); |
| 274 | return false; |
David Srbecky | 1f5ab4e | 2018-10-15 11:46:46 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | jit::Jit* jit = Runtime::Current()->GetJit(); |
David Srbecky | 1f5ab4e | 2018-10-15 11:46:46 +0100 | [diff] [blame] | 278 | if (is_mterp && !is_range && called_method->IsIntrinsic()) { |
David Srbecky | 6c44448 | 2018-10-17 15:15:19 +0100 | [diff] [blame] | 279 | if (MterpHandleIntrinsic(&shadow_frame, called_method, inst, inst_data, |
| 280 | shadow_frame.GetResultRegister())) { |
| 281 | if (jit != nullptr && sf_method != nullptr) { |
| 282 | jit->NotifyInterpreterToCompiledCodeTransition(self, sf_method); |
David Srbecky | 6849c06 | 2018-09-21 09:42:24 +0100 | [diff] [blame] | 283 | } |
David Srbecky | 6c44448 | 2018-10-17 15:15:19 +0100 | [diff] [blame] | 284 | return !self->IsExceptionPending(); |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 285 | } |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 286 | } |
David Srbecky | 1f5ab4e | 2018-10-15 11:46:46 +0100 | [diff] [blame] | 287 | |
| 288 | return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data, |
| 289 | result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 290 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 291 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 292 | static inline ObjPtr<mirror::MethodHandle> ResolveMethodHandle(Thread* self, |
| 293 | uint32_t method_handle_index, |
| 294 | ArtMethod* referrer) |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 295 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 296 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 297 | return class_linker->ResolveMethodHandle(self, method_handle_index, referrer); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 300 | static inline ObjPtr<mirror::MethodType> ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 301 | dex::ProtoIndex method_type_index, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 302 | ArtMethod* referrer) |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 303 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 304 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 305 | return class_linker->ResolveMethodType(self, method_type_index, referrer); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 306 | } |
| 307 | |
Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 308 | #define DECLARE_SIGNATURE_POLYMORPHIC_HANDLER(Name, ...) \ |
| 309 | bool Do ## Name(Thread* self, \ |
| 310 | ShadowFrame& shadow_frame, \ |
| 311 | const Instruction* inst, \ |
| 312 | uint16_t inst_data, \ |
| 313 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_); |
| 314 | #include "intrinsics_list.h" |
| 315 | INTRINSICS_LIST(DECLARE_SIGNATURE_POLYMORPHIC_HANDLER) |
| 316 | #undef INTRINSICS_LIST |
| 317 | #undef DECLARE_SIGNATURE_POLYMORPHIC_HANDLER |
| 318 | |
| 319 | // Performs a invoke-polymorphic or invoke-polymorphic-range. |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 320 | template<bool is_range> |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 321 | bool DoInvokePolymorphic(Thread* self, |
| 322 | ShadowFrame& shadow_frame, |
| 323 | const Instruction* inst, |
| 324 | uint16_t inst_data, |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 325 | JValue* result) |
| 326 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 327 | |
| 328 | bool DoInvokeCustom(Thread* self, |
| 329 | ShadowFrame& shadow_frame, |
| 330 | uint32_t call_site_idx, |
| 331 | const InstructionOperands* operands, |
| 332 | JValue* result) |
| 333 | REQUIRES_SHARED(Locks::mutator_lock_); |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 334 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 335 | // Performs a custom invoke (invoke-custom/invoke-custom-range). |
| 336 | template<bool is_range> |
| 337 | bool DoInvokeCustom(Thread* self, |
| 338 | ShadowFrame& shadow_frame, |
| 339 | const Instruction* inst, |
| 340 | uint16_t inst_data, |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 341 | JValue* result) |
| 342 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 343 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 344 | if (is_range) { |
| 345 | RangeInstructionOperands operands(inst->VRegC_3rc(), inst->VRegA_3rc()); |
| 346 | return DoInvokeCustom(self, shadow_frame, call_site_idx, &operands, result); |
| 347 | } else { |
| 348 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 349 | inst->GetVarArgs(args, inst_data); |
| 350 | VarArgsInstructionOperands operands(args, inst->VRegA_35c()); |
| 351 | return DoInvokeCustom(self, shadow_frame, call_site_idx, &operands, result); |
| 352 | } |
| 353 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 354 | |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 355 | template<Primitive::Type field_type> |
| 356 | ALWAYS_INLINE static JValue GetFieldValue(const ShadowFrame& shadow_frame, uint32_t vreg) |
| 357 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 358 | JValue field_value; |
| 359 | switch (field_type) { |
| 360 | case Primitive::kPrimBoolean: |
| 361 | field_value.SetZ(static_cast<uint8_t>(shadow_frame.GetVReg(vreg))); |
| 362 | break; |
| 363 | case Primitive::kPrimByte: |
| 364 | field_value.SetB(static_cast<int8_t>(shadow_frame.GetVReg(vreg))); |
| 365 | break; |
| 366 | case Primitive::kPrimChar: |
| 367 | field_value.SetC(static_cast<uint16_t>(shadow_frame.GetVReg(vreg))); |
| 368 | break; |
| 369 | case Primitive::kPrimShort: |
| 370 | field_value.SetS(static_cast<int16_t>(shadow_frame.GetVReg(vreg))); |
| 371 | break; |
| 372 | case Primitive::kPrimInt: |
| 373 | field_value.SetI(shadow_frame.GetVReg(vreg)); |
| 374 | break; |
| 375 | case Primitive::kPrimLong: |
| 376 | field_value.SetJ(shadow_frame.GetVRegLong(vreg)); |
| 377 | break; |
| 378 | case Primitive::kPrimNot: |
| 379 | field_value.SetL(shadow_frame.GetVRegReference(vreg)); |
| 380 | break; |
| 381 | default: |
| 382 | LOG(FATAL) << "Unreachable: " << field_type; |
| 383 | UNREACHABLE(); |
| 384 | } |
| 385 | return field_value; |
| 386 | } |
| 387 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 388 | // Handles iget-XXX and sget-XXX instructions. |
| 389 | // Returns true on success, otherwise throws an exception and returns false. |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 390 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 391 | bool transaction_active = false> |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 392 | ALWAYS_INLINE bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 393 | uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 394 | const bool is_static = (find_type == StaticObjectRead) || (find_type == StaticPrimitiveRead); |
| 395 | const uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 396 | ArtField* f = |
| 397 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 398 | Primitive::ComponentSize(field_type)); |
| 399 | if (UNLIKELY(f == nullptr)) { |
| 400 | CHECK(self->IsExceptionPending()); |
| 401 | return false; |
| 402 | } |
| 403 | ObjPtr<mirror::Object> obj; |
| 404 | if (is_static) { |
| 405 | obj = f->GetDeclaringClass(); |
| 406 | if (transaction_active) { |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 407 | if (Runtime::Current()->GetTransaction()->ReadConstraint(obj)) { |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 408 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Can't read static fields of " |
| 409 | + obj->PrettyTypeOf() + " since it does not belong to clinit's class."); |
| 410 | return false; |
| 411 | } |
| 412 | } |
| 413 | } else { |
| 414 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 415 | if (UNLIKELY(obj == nullptr)) { |
| 416 | ThrowNullPointerExceptionForFieldAccess(f, true); |
| 417 | return false; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | JValue result; |
| 422 | if (UNLIKELY(!DoFieldGetCommon<field_type>(self, shadow_frame, obj, f, &result))) { |
| 423 | // Instrumentation threw an error! |
| 424 | CHECK(self->IsExceptionPending()); |
| 425 | return false; |
| 426 | } |
| 427 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 428 | switch (field_type) { |
| 429 | case Primitive::kPrimBoolean: |
| 430 | shadow_frame.SetVReg(vregA, result.GetZ()); |
| 431 | break; |
| 432 | case Primitive::kPrimByte: |
| 433 | shadow_frame.SetVReg(vregA, result.GetB()); |
| 434 | break; |
| 435 | case Primitive::kPrimChar: |
| 436 | shadow_frame.SetVReg(vregA, result.GetC()); |
| 437 | break; |
| 438 | case Primitive::kPrimShort: |
| 439 | shadow_frame.SetVReg(vregA, result.GetS()); |
| 440 | break; |
| 441 | case Primitive::kPrimInt: |
| 442 | shadow_frame.SetVReg(vregA, result.GetI()); |
| 443 | break; |
| 444 | case Primitive::kPrimLong: |
| 445 | shadow_frame.SetVRegLong(vregA, result.GetJ()); |
| 446 | break; |
| 447 | case Primitive::kPrimNot: |
| 448 | shadow_frame.SetVRegReference(vregA, result.GetL()); |
| 449 | break; |
| 450 | default: |
| 451 | LOG(FATAL) << "Unreachable: " << field_type; |
| 452 | UNREACHABLE(); |
| 453 | } |
| 454 | return true; |
| 455 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 456 | |
Vladimir Marko | 4d7b689 | 2020-01-16 17:06:35 +0000 | [diff] [blame] | 457 | static inline bool CheckWriteConstraint(Thread* self, ObjPtr<mirror::Object> obj) |
| 458 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 459 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 460 | if (runtime->GetTransaction()->WriteConstraint(obj)) { |
Vladimir Marko | 4d7b689 | 2020-01-16 17:06:35 +0000 | [diff] [blame] | 461 | DCHECK(runtime->GetHeap()->ObjectIsInBootImageSpace(obj) || obj->IsClass()); |
| 462 | const char* base_msg = runtime->GetHeap()->ObjectIsInBootImageSpace(obj) |
| 463 | ? "Can't set fields of boot image " |
| 464 | : "Can't set fields of "; |
| 465 | runtime->AbortTransactionAndThrowAbortError(self, base_msg + obj->PrettyTypeOf()); |
| 466 | return false; |
| 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | |
Vladimir Marko | 149cdda | 2019-11-12 15:02:51 +0000 | [diff] [blame] | 471 | static inline bool CheckWriteValueConstraint(Thread* self, ObjPtr<mirror::Object> value) |
| 472 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 473 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 24c080f | 2021-07-06 14:04:30 +0100 | [diff] [blame] | 474 | if (runtime->GetTransaction()->WriteValueConstraint(value)) { |
Vladimir Marko | 149cdda | 2019-11-12 15:02:51 +0000 | [diff] [blame] | 475 | DCHECK(value != nullptr); |
| 476 | std::string msg = value->IsClass() |
| 477 | ? "Can't store reference to class " + value->AsClass()->PrettyDescriptor() |
| 478 | : "Can't store reference to instance of " + value->GetClass()->PrettyDescriptor(); |
| 479 | runtime->AbortTransactionAndThrowAbortError(self, msg); |
| 480 | return false; |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 485 | // Handles iput-XXX and sput-XXX instructions. |
| 486 | // Returns true on success, otherwise throws an exception and returns false. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 487 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 488 | bool transaction_active> |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 489 | ALWAYS_INLINE bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, |
| 490 | const Instruction* inst, uint16_t inst_data) |
| 491 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 492 | const bool do_assignability_check = do_access_check; |
| 493 | bool is_static = (find_type == StaticObjectWrite) || (find_type == StaticPrimitiveWrite); |
| 494 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 495 | ArtField* f = |
| 496 | FindFieldFromCode<find_type, do_access_check>(field_idx, shadow_frame.GetMethod(), self, |
| 497 | Primitive::ComponentSize(field_type)); |
| 498 | if (UNLIKELY(f == nullptr)) { |
| 499 | CHECK(self->IsExceptionPending()); |
| 500 | return false; |
| 501 | } |
| 502 | ObjPtr<mirror::Object> obj; |
| 503 | if (is_static) { |
| 504 | obj = f->GetDeclaringClass(); |
| 505 | } else { |
| 506 | obj = shadow_frame.GetVRegReference(inst->VRegB_22c(inst_data)); |
| 507 | if (UNLIKELY(obj == nullptr)) { |
| 508 | ThrowNullPointerExceptionForFieldAccess(f, false); |
| 509 | return false; |
| 510 | } |
| 511 | } |
Vladimir Marko | 4d7b689 | 2020-01-16 17:06:35 +0000 | [diff] [blame] | 512 | if (transaction_active && !CheckWriteConstraint(self, obj)) { |
| 513 | return false; |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | uint32_t vregA = is_static ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data); |
| 517 | JValue value = GetFieldValue<field_type>(shadow_frame, vregA); |
Vladimir Marko | 149cdda | 2019-11-12 15:02:51 +0000 | [diff] [blame] | 518 | |
| 519 | if (transaction_active && |
| 520 | field_type == Primitive::kPrimNot && |
| 521 | !CheckWriteValueConstraint(self, value.GetL())) { |
| 522 | return false; |
| 523 | } |
| 524 | |
David Srbecky | f56393c | 2019-08-02 15:49:27 +0100 | [diff] [blame] | 525 | return DoFieldPutCommon<field_type, do_assignability_check, transaction_active>(self, |
| 526 | shadow_frame, |
| 527 | obj, |
| 528 | f, |
| 529 | value); |
| 530 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 531 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 532 | // Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the |
| 533 | // java.lang.String class is initialized. |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 534 | static inline ObjPtr<mirror::String> ResolveString(Thread* self, |
| 535 | ShadowFrame& shadow_frame, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 536 | dex::StringIndex string_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 537 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 538 | ObjPtr<mirror::Class> java_lang_string_class = GetClassRoot<mirror::String>(); |
Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 539 | if (UNLIKELY(!java_lang_string_class->IsVisiblyInitialized())) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 540 | StackHandleScope<1> hs(self); |
| 541 | Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class)); |
Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 542 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized( |
| 543 | self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 544 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 545 | return nullptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 546 | } |
Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 547 | DCHECK(h_class->IsInitializing()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 548 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 549 | ArtMethod* method = shadow_frame.GetMethod(); |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 550 | ObjPtr<mirror::String> string_ptr = |
| 551 | Runtime::Current()->GetClassLinker()->ResolveString(string_idx, method); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 552 | return string_ptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 555 | // Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions. |
| 556 | // 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] | 557 | static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 558 | int32_t dividend, int32_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 559 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 560 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 561 | if (UNLIKELY(divisor == 0)) { |
| 562 | ThrowArithmeticExceptionDivideByZero(); |
| 563 | return false; |
| 564 | } |
| 565 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 566 | shadow_frame.SetVReg(result_reg, kMinInt); |
| 567 | } else { |
| 568 | shadow_frame.SetVReg(result_reg, dividend / divisor); |
| 569 | } |
| 570 | return true; |
| 571 | } |
| 572 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 573 | // Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions. |
| 574 | // 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] | 575 | static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 576 | int32_t dividend, int32_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 577 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 578 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 579 | if (UNLIKELY(divisor == 0)) { |
| 580 | ThrowArithmeticExceptionDivideByZero(); |
| 581 | return false; |
| 582 | } |
| 583 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 584 | shadow_frame.SetVReg(result_reg, 0); |
| 585 | } else { |
| 586 | shadow_frame.SetVReg(result_reg, dividend % divisor); |
| 587 | } |
| 588 | return true; |
| 589 | } |
| 590 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 591 | // Handles div-long and div-long-2addr instructions. |
| 592 | // 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] | 593 | static inline bool DoLongDivide(ShadowFrame& shadow_frame, |
| 594 | size_t result_reg, |
| 595 | int64_t dividend, |
| 596 | int64_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 597 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 598 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 599 | if (UNLIKELY(divisor == 0)) { |
| 600 | ThrowArithmeticExceptionDivideByZero(); |
| 601 | return false; |
| 602 | } |
| 603 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 604 | shadow_frame.SetVRegLong(result_reg, kMinLong); |
| 605 | } else { |
| 606 | shadow_frame.SetVRegLong(result_reg, dividend / divisor); |
| 607 | } |
| 608 | return true; |
| 609 | } |
| 610 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 611 | // Handles rem-long and rem-long-2addr instructions. |
| 612 | // 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] | 613 | static inline bool DoLongRemainder(ShadowFrame& shadow_frame, |
| 614 | size_t result_reg, |
| 615 | int64_t dividend, |
| 616 | int64_t divisor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 617 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 618 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 619 | if (UNLIKELY(divisor == 0)) { |
| 620 | ThrowArithmeticExceptionDivideByZero(); |
| 621 | return false; |
| 622 | } |
| 623 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 624 | shadow_frame.SetVRegLong(result_reg, 0); |
| 625 | } else { |
| 626 | shadow_frame.SetVRegLong(result_reg, dividend % divisor); |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 631 | // Handles filled-new-array and filled-new-array-range instructions. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 632 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 633 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 634 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 635 | Thread* self, JValue* result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 636 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 637 | // Handles packed-switch instruction. |
| 638 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 639 | static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 640 | uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 641 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 642 | DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH); |
| 643 | 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] | 644 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 645 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 646 | uint16_t size = switch_data[1]; |
David Brazdil | 2ef645b | 2015-06-17 18:20:52 +0100 | [diff] [blame] | 647 | if (size == 0) { |
| 648 | // Empty packed switch, move forward by 3 (size of PACKED_SWITCH). |
| 649 | return 3; |
| 650 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 651 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 652 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 653 | int32_t first_key = keys[0]; |
| 654 | const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 655 | DCHECK_ALIGNED(targets, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 656 | int32_t index = test_val - first_key; |
| 657 | if (index >= 0 && index < size) { |
| 658 | return targets[index]; |
| 659 | } else { |
| 660 | // No corresponding value: move forward by 3 (size of PACKED_SWITCH). |
| 661 | return 3; |
| 662 | } |
| 663 | } |
| 664 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 665 | // Handles sparse-switch instruction. |
| 666 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 667 | static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 668 | uint16_t inst_data) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 669 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 670 | DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH); |
| 671 | 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] | 672 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 673 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 674 | uint16_t size = switch_data[1]; |
Jeff Hao | 935e01a | 2015-03-20 19:44:35 -0700 | [diff] [blame] | 675 | // Return length of SPARSE_SWITCH if size is 0. |
| 676 | if (size == 0) { |
| 677 | return 3; |
| 678 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 679 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 680 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 681 | const int32_t* entries = keys + size; |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 682 | DCHECK_ALIGNED(entries, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 683 | int lo = 0; |
| 684 | int hi = size - 1; |
| 685 | while (lo <= hi) { |
| 686 | int mid = (lo + hi) / 2; |
| 687 | int32_t foundVal = keys[mid]; |
| 688 | if (test_val < foundVal) { |
| 689 | hi = mid - 1; |
| 690 | } else if (test_val > foundVal) { |
| 691 | lo = mid + 1; |
| 692 | } else { |
| 693 | return entries[mid]; |
| 694 | } |
| 695 | } |
| 696 | // No corresponding value: move forward by 3 (size of SPARSE_SWITCH). |
| 697 | return 3; |
| 698 | } |
| 699 | |
Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 700 | // We execute any instrumentation events triggered by throwing and/or handing the pending exception |
| 701 | // and change the shadow_frames dex_pc to the appropriate exception handler if the current method |
| 702 | // has one. If the exception has been handled and the shadow_frame is now pointing to a catch clause |
| 703 | // we return true. If the current method is unable to handle the exception we return false. |
| 704 | // This function accepts a null Instrumentation* as a way to cause instrumentation events not to be |
| 705 | // reported. |
| 706 | // TODO We might wish to reconsider how we cause some events to be ignored. |
| 707 | bool MoveToExceptionHandler(Thread* self, |
| 708 | ShadowFrame& shadow_frame, |
| 709 | const instrumentation::Instrumentation* instrumentation) |
| 710 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 711 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 712 | NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) |
| 713 | __attribute__((cold)) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 714 | REQUIRES_SHARED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 715 | |
Bill Buzbee | d47fd90 | 2016-07-07 14:42:43 +0000 | [diff] [blame] | 716 | // Set true if you want TraceExecution invocation before each bytecode execution. |
| 717 | constexpr bool kTraceExecutionEnabled = false; |
Serguei Katkov | 9fb0ac7 | 2016-02-20 12:55:24 +0600 | [diff] [blame] | 718 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 719 | static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst, |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 720 | const uint32_t dex_pc) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 721 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Bill Buzbee | d47fd90 | 2016-07-07 14:42:43 +0000 | [diff] [blame] | 722 | if (kTraceExecutionEnabled) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 723 | #define TRACE_LOG std::cerr |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 724 | std::ostringstream oss; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 725 | oss << shadow_frame.GetMethod()->PrettyMethod() |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 726 | << android::base::StringPrintf("\n0x%x: ", dex_pc) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 727 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 728 | for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 729 | uint32_t raw_value = shadow_frame.GetVReg(i); |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 730 | ObjPtr<mirror::Object> ref_value = shadow_frame.GetVRegReference(i); |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 731 | oss << android::base::StringPrintf(" vreg%u=0x%08X", i, raw_value); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 732 | if (ref_value != nullptr) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 733 | if (ref_value->GetClass()->IsStringClass() && |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 734 | !ref_value->AsString()->IsValueNull()) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 735 | oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\""; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 736 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 737 | oss << "/" << ref_value->PrettyTypeOf(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 741 | TRACE_LOG << oss.str() << "\n"; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 742 | #undef TRACE_LOG |
| 743 | } |
| 744 | } |
| 745 | |
Sebastien Hertz | 1eda226 | 2013-09-09 16:53:14 +0200 | [diff] [blame] | 746 | static inline bool IsBackwardBranch(int32_t branch_offset) { |
| 747 | return branch_offset <= 0; |
| 748 | } |
| 749 | |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 750 | // 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] | 751 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| 752 | ArtMethod* caller, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 753 | ShadowFrame* shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 754 | uint16_t arg_offset, |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 755 | JValue* result); |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 756 | |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 757 | static inline bool IsStringInit(const DexFile* dex_file, uint32_t method_idx) |
| 758 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 759 | const dex::MethodId& method_id = dex_file->GetMethodId(method_idx); |
Mingyao Yang | 2ee1790 | 2017-08-30 11:37:08 -0700 | [diff] [blame] | 760 | const char* class_name = dex_file->StringByTypeIdx(method_id.class_idx_); |
| 761 | const char* method_name = dex_file->GetMethodName(method_id); |
| 762 | // Instead of calling ResolveMethod() which has suspend point and can trigger |
| 763 | // GC, look up the method symbolically. |
| 764 | // Compare method's class name and method name against string init. |
| 765 | // It's ok since it's not allowed to create your own java/lang/String. |
| 766 | // TODO: verify that assumption. |
| 767 | if ((strcmp(class_name, "Ljava/lang/String;") == 0) && |
| 768 | (strcmp(method_name, "<init>") == 0)) { |
| 769 | return true; |
| 770 | } |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | static inline bool IsStringInit(const Instruction* instr, ArtMethod* caller) |
| 775 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 776 | if (instr->Opcode() == Instruction::INVOKE_DIRECT || |
| 777 | instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) { |
| 778 | uint16_t callee_method_idx = (instr->Opcode() == Instruction::INVOKE_DIRECT_RANGE) ? |
| 779 | instr->VRegB_3rc() : instr->VRegB_35c(); |
| 780 | return IsStringInit(caller->GetDexFile(), callee_method_idx); |
| 781 | } |
| 782 | return false; |
| 783 | } |
| 784 | |
Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 785 | // Set string value created from StringFactory.newStringFromXXX() into all aliases of |
| 786 | // StringFactory.newEmptyString(). |
| 787 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 788 | uint16_t this_obj_vreg, |
| 789 | JValue result); |
| 790 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 791 | } // namespace interpreter |
| 792 | } // namespace art |
| 793 | |
| 794 | #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |