blob: aeb438f05fd7f13c75a4cc7ed24d4a3a8d4ca168 [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
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"
21
22#include <math.h>
23
Ian Rogerscf7f1912014-10-22 22:06:39 -070024#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070025#include <sstream>
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070026#include <atomic>
Ian Rogersc7dd2952014-10-21 23:31:19 -070027
Andreas Gampe46ee31b2016-12-14 10:11:49 -080028#include "android-base/stringprintf.h"
29
Mathieu Chartierc7853442015-03-27 14:35:38 -070030#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070032#include "base/enums.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020033#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080034#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020035#include "class_linker-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000036#include "common_dex_operations.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020037#include "common_throws.h"
38#include "dex_file-inl.h"
39#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040#include "entrypoints/entrypoint_utils-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070041#include "handle_scope-inl.h"
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +010042#include "jit/jit.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020043#include "mirror/class-inl.h"
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -070044#include "mirror/dex_cache.h"
45#include "mirror/method.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070048#include "mirror/string-inl.h"
Mathieu Chartieref41db72016-10-25 15:08:01 -070049#include "obj_ptr.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070050#include "stack.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020051#include "thread.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000052#include "unstarted_runtime.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020053#include "well_known_classes.h"
54
Sebastien Hertz8ece0502013-08-07 11:26:41 +020055namespace art {
56namespace interpreter {
57
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000058void ThrowNullPointerExceptionFromInterpreter()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070059 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020060
Andreas Gampe03ec9302015-08-27 17:41:47 -070061template <bool kMonitorCounting>
Mathieu Chartieref41db72016-10-25 15:08:01 -070062static inline void DoMonitorEnter(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref)
Mathieu Chartier2d096c92015-10-12 16:18:20 -070063 NO_THREAD_SAFETY_ANALYSIS
64 REQUIRES(!Roles::uninterruptible_) {
65 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -070066 Handle<mirror::Object> h_ref(hs.NewHandle(ref));
Mathieu Chartier2d096c92015-10-12 16:18:20 -070067 h_ref->MonitorEnter(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070068 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
69 frame->GetLockCountData().AddMonitor(self, h_ref.Get());
70 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +020071}
72
Andreas Gampe03ec9302015-08-27 17:41:47 -070073template <bool kMonitorCounting>
Mathieu Chartieref41db72016-10-25 15:08:01 -070074static inline void DoMonitorExit(Thread* self, ShadowFrame* frame, ObjPtr<mirror::Object> ref)
Mathieu Chartier2d096c92015-10-12 16:18:20 -070075 NO_THREAD_SAFETY_ANALYSIS
76 REQUIRES(!Roles::uninterruptible_) {
77 StackHandleScope<1> hs(self);
Mathieu Chartieref41db72016-10-25 15:08:01 -070078 Handle<mirror::Object> h_ref(hs.NewHandle(ref));
Mathieu Chartier2d096c92015-10-12 16:18:20 -070079 h_ref->MonitorExit(self);
Andreas Gampe56fdd0e2016-04-28 14:56:54 -070080 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
81 frame->GetLockCountData().RemoveMonitorOrThrow(self, h_ref.Get());
82 }
83}
84
85template <bool kMonitorCounting>
86static inline bool DoMonitorCheckOnExit(Thread* self, ShadowFrame* frame)
87 NO_THREAD_SAFETY_ANALYSIS
88 REQUIRES(!Roles::uninterruptible_) {
89 if (kMonitorCounting && frame->GetMethod()->MustCountLocks()) {
90 return frame->GetLockCountData().CheckAllMonitorsReleasedOrThrow(self);
91 }
92 return true;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020093}
94
Sebastien Hertz45b15972015-04-03 16:07:05 +020095void AbortTransactionF(Thread* self, const char* fmt, ...)
96 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070097 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020098
99void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700101
Mathieu Chartieref41db72016-10-25 15:08:01 -0700102void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104
Sebastien Hertzc6714852013-09-30 16:42:32 +0200105// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
106// DoInvokeVirtualQuick functions.
107// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200108template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800109bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200110 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200111
Narayan Kamath9823e782016-08-03 12:46:58 +0100112// Handles all invoke-XXX/range instructions except for invoke-polymorphic[/range].
Sebastien Hertzc6714852013-09-30 16:42:32 +0200113// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200114template<InvokeType type, bool is_range, bool do_access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -0700115static inline bool DoInvoke(Thread* self,
116 ShadowFrame& shadow_frame,
117 const Instruction* inst,
118 uint16_t inst_data,
119 JValue* result) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200120 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
121 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -0700122 ObjPtr<mirror::Object> receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800124 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700125 method_idx, &receiver, sf_method, self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700126 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800127 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200128 CHECK(self->IsExceptionPending());
129 result->SetJ(0);
130 return false;
Alex Light9139e002015-10-09 15:59:48 -0700131 } else if (UNLIKELY(!called_method->IsInvokable())) {
132 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200133 result->SetJ(0);
134 return false;
135 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100136 jit::Jit* jit = Runtime::Current()->GetJit();
137 if (jit != nullptr) {
138 if (type == kVirtual || type == kInterface) {
Mathieu Chartier268764d2016-09-13 12:09:38 -0700139 jit->InvokeVirtualOrInterface(receiver, sf_method, shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100140 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100141 jit->AddSamples(self, sf_method, 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100142 }
143 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100144 if (type == kVirtual || type == kInterface) {
145 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
146 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
147 instrumentation->InvokeVirtualOrInterface(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700148 self, receiver.Ptr(), sf_method, shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100149 }
150 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800151 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
152 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200153 }
154}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200155
Narayan Kamath9823e782016-08-03 12:46:58 +0100156// Performs a signature polymorphic invoke (invoke-polymorphic/invoke-polymorphic-range).
157template<bool is_range, bool do_access_check>
Orion Hodson811bd5f2016-12-07 11:35:37 +0000158bool DoInvokePolymorphic(Thread* self,
159 ShadowFrame& shadow_frame,
160 const Instruction* inst,
161 uint16_t inst_data,
Narayan Kamath9823e782016-08-03 12:46:58 +0100162 JValue* result);
163
Sebastien Hertzc6714852013-09-30 16:42:32 +0200164// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
165// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200166template<bool is_range>
167static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
168 const Instruction* inst, uint16_t inst_data,
169 JValue* result) {
170 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartieref41db72016-10-25 15:08:01 -0700171 ObjPtr<mirror::Object> const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200172 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200173 // We lost the reference to the method index so we cannot get a more
174 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000175 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200176 return false;
177 }
178 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Hiroshi Yamauchia10ae782016-10-05 18:13:23 -0700179 // Debug code for b/31357497. To be removed.
180 if (kUseReadBarrier) {
181 CHECK(receiver->GetClass() != nullptr)
182 << "Null class found in object " << receiver << " in region type "
183 << Runtime::Current()->GetHeap()->ConcurrentCopyingCollector()->
Mathieu Chartieref41db72016-10-25 15:08:01 -0700184 RegionSpace()->GetRegionType(receiver.Ptr());
Hiroshi Yamauchia10ae782016-10-05 18:13:23 -0700185 }
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000186 CHECK(receiver->GetClass()->ShouldHaveEmbeddedVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -0700188 vtable_idx, kRuntimePointerSize);
Ian Rogerse94652f2014-12-02 11:13:19 -0800189 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200190 CHECK(self->IsExceptionPending());
191 result->SetJ(0);
192 return false;
Alex Light9139e002015-10-09 15:59:48 -0700193 } else if (UNLIKELY(!called_method->IsInvokable())) {
194 called_method->ThrowInvocationTimeError();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200195 result->SetJ(0);
196 return false;
197 } else {
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100198 jit::Jit* jit = Runtime::Current()->GetJit();
199 if (jit != nullptr) {
200 jit->InvokeVirtualOrInterface(
Mathieu Chartier268764d2016-09-13 12:09:38 -0700201 receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100202 jit->AddSamples(self, shadow_frame.GetMethod(), 1, /*with_backedges*/false);
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100203 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100204 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
Nicolas Geoffray274fe4a2016-04-12 16:33:24 +0100205 // TODO: Remove the InvokeVirtualOrInterface instrumentation, as it was only used by the JIT.
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100206 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
207 instrumentation->InvokeVirtualOrInterface(
Mathieu Chartieref41db72016-10-25 15:08:01 -0700208 self, receiver.Ptr(), shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100209 }
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200210 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800211 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200212 }
213}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200214
Sebastien Hertzc6714852013-09-30 16:42:32 +0200215// Handles iget-XXX and sget-XXX instructions.
216// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200217template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700218bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200220
Sebastien Hertzc6714852013-09-30 16:42:32 +0200221// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
222// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200223template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700224bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200226
Sebastien Hertzc6714852013-09-30 16:42:32 +0200227// Handles iput-XXX and sput-XXX instructions.
228// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700229template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
230 bool transaction_active>
231bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 uint16_t inst_data) REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200233
Sebastien Hertzc6714852013-09-30 16:42:32 +0200234// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
235// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100236template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700237bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers54874942014-06-10 16:31:03 -0700239
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200240
Sebastien Hertzc6714852013-09-30 16:42:32 +0200241// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
242// java.lang.String class is initialized.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700243static inline ObjPtr<mirror::String> ResolveString(Thread* self,
244 ShadowFrame& shadow_frame,
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800245 dex::StringIndex string_idx)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700247 ObjPtr<mirror::Class> java_lang_string_class = mirror::String::GetJavaLangString();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200248 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
249 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700250 StackHandleScope<1> hs(self);
251 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700252 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200253 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800254 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200255 }
256 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 ArtMethod* method = shadow_frame.GetMethod();
Vladimir Marko05792b92015-08-03 11:56:49 +0100258 // MethodVerifier refuses methods with string_idx out of bounds.
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800259 DCHECK_LT(string_idx.index_ % mirror::DexCache::kDexCacheStringCacheSize,
Alex Lightdba61482016-12-21 08:20:29 -0800260 method->GetDexFile()->NumStringIds());
Mathieu Chartieref41db72016-10-25 15:08:01 -0700261 ObjPtr<mirror::String> string_ptr =
Alex Lightdba61482016-12-21 08:20:29 -0800262 mirror::StringDexCachePair::Lookup(method->GetDexCache()->GetStrings(),
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800263 string_idx.index_,
Narayan Kamathc38a6f82016-09-29 17:07:20 +0100264 mirror::DexCache::kDexCacheStringCacheSize).Read();
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700265 if (UNLIKELY(string_ptr == nullptr)) {
Ian Rogers6786a582014-10-28 12:49:06 -0700266 StackHandleScope<1> hs(self);
Alex Lightdba61482016-12-21 08:20:29 -0800267 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700268 string_ptr = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(),
269 string_idx,
270 dex_cache);
Ian Rogers6786a582014-10-28 12:49:06 -0700271 }
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700272 return string_ptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200273}
274
Sebastien Hertzc6714852013-09-30 16:42:32 +0200275// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
276// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200277static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
278 int32_t dividend, int32_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700280 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200281 if (UNLIKELY(divisor == 0)) {
282 ThrowArithmeticExceptionDivideByZero();
283 return false;
284 }
285 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
286 shadow_frame.SetVReg(result_reg, kMinInt);
287 } else {
288 shadow_frame.SetVReg(result_reg, dividend / divisor);
289 }
290 return true;
291}
292
Sebastien Hertzc6714852013-09-30 16:42:32 +0200293// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
294// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200295static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
296 int32_t dividend, int32_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700297 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700298 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200299 if (UNLIKELY(divisor == 0)) {
300 ThrowArithmeticExceptionDivideByZero();
301 return false;
302 }
303 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
304 shadow_frame.SetVReg(result_reg, 0);
305 } else {
306 shadow_frame.SetVReg(result_reg, dividend % divisor);
307 }
308 return true;
309}
310
Sebastien Hertzc6714852013-09-30 16:42:32 +0200311// Handles div-long and div-long-2addr instructions.
312// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700313static inline bool DoLongDivide(ShadowFrame& shadow_frame,
314 size_t result_reg,
315 int64_t dividend,
316 int64_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700317 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700318 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200319 if (UNLIKELY(divisor == 0)) {
320 ThrowArithmeticExceptionDivideByZero();
321 return false;
322 }
323 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
324 shadow_frame.SetVRegLong(result_reg, kMinLong);
325 } else {
326 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
327 }
328 return true;
329}
330
Sebastien Hertzc6714852013-09-30 16:42:32 +0200331// Handles rem-long and rem-long-2addr instructions.
332// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700333static inline bool DoLongRemainder(ShadowFrame& shadow_frame,
334 size_t result_reg,
335 int64_t dividend,
336 int64_t divisor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700338 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200339 if (UNLIKELY(divisor == 0)) {
340 ThrowArithmeticExceptionDivideByZero();
341 return false;
342 }
343 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
344 shadow_frame.SetVRegLong(result_reg, 0);
345 } else {
346 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
347 }
348 return true;
349}
350
Sebastien Hertzc6714852013-09-30 16:42:32 +0200351// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200352// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200354bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200355 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200356
Sebastien Hertzc6714852013-09-30 16:42:32 +0200357// Handles packed-switch instruction.
358// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200359static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
360 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700361 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200362 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
363 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200364 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200365 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
366 uint16_t size = switch_data[1];
David Brazdil2ef645b2015-06-17 18:20:52 +0100367 if (size == 0) {
368 // Empty packed switch, move forward by 3 (size of PACKED_SWITCH).
369 return 3;
370 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200371 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100372 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200373 int32_t first_key = keys[0];
374 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
Roland Levillain14d90572015-07-16 10:52:26 +0100375 DCHECK_ALIGNED(targets, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200376 int32_t index = test_val - first_key;
377 if (index >= 0 && index < size) {
378 return targets[index];
379 } else {
380 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
381 return 3;
382 }
383}
384
Sebastien Hertzc6714852013-09-30 16:42:32 +0200385// Handles sparse-switch instruction.
386// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200387static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
388 uint16_t inst_data)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700389 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200390 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
391 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200392 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200393 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
394 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700395 // Return length of SPARSE_SWITCH if size is 0.
396 if (size == 0) {
397 return 3;
398 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200399 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100400 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200401 const int32_t* entries = keys + size;
Roland Levillain14d90572015-07-16 10:52:26 +0100402 DCHECK_ALIGNED(entries, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200403 int lo = 0;
404 int hi = size - 1;
405 while (lo <= hi) {
406 int mid = (lo + hi) / 2;
407 int32_t foundVal = keys[mid];
408 if (test_val < foundVal) {
409 hi = mid - 1;
410 } else if (test_val > foundVal) {
411 lo = mid + 1;
412 } else {
413 return entries[mid];
414 }
415 }
416 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
417 return 3;
418}
419
Ian Rogers54874942014-06-10 16:31:03 -0700420uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200421 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700422 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200423
Andreas Gampe794ad762015-02-23 08:12:24 -0800424NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
425 __attribute__((cold))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700426 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200427
Bill Buzbeed47fd902016-07-07 14:42:43 +0000428// Set true if you want TraceExecution invocation before each bytecode execution.
429constexpr bool kTraceExecutionEnabled = false;
Serguei Katkov9fb0ac72016-02-20 12:55:24 +0600430
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200431static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800432 const uint32_t dex_pc)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700433 REQUIRES_SHARED(Locks::mutator_lock_) {
Bill Buzbeed47fd902016-07-07 14:42:43 +0000434 if (kTraceExecutionEnabled) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200435#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700436 std::ostringstream oss;
David Sehr709b0702016-10-13 09:12:37 -0700437 oss << shadow_frame.GetMethod()->PrettyMethod()
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800438 << android::base::StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800439 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200441 uint32_t raw_value = shadow_frame.GetVReg(i);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700442 ObjPtr<mirror::Object> ref_value = shadow_frame.GetVRegReference(i);
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800443 oss << android::base::StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700444 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200445 if (ref_value->GetClass()->IsStringClass() &&
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700446 !ref_value->AsString()->IsValueNull()) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700447 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200448 } else {
David Sehr709b0702016-10-13 09:12:37 -0700449 oss << "/" << ref_value->PrettyTypeOf();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200450 }
451 }
452 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700453 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200454#undef TRACE_LOG
455 }
456}
457
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200458static inline bool IsBackwardBranch(int32_t branch_offset) {
459 return branch_offset <= 0;
460}
461
Narayan Kamath208f8572016-08-03 12:46:58 +0100462// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
463static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
464 size_t dest_reg, size_t src_reg)
465 REQUIRES_SHARED(Locks::mutator_lock_) {
466 // Uint required, so that sign extension does not make this wrong on 64b systems
467 uint32_t src_value = shadow_frame.GetVReg(src_reg);
Mathieu Chartieref41db72016-10-25 15:08:01 -0700468 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
Narayan Kamath208f8572016-08-03 12:46:58 +0100469
470 // If both register locations contains the same value, the register probably holds a reference.
471 // Note: As an optimization, non-moving collectors leave a stale reference value
472 // in the references array even after the original vreg was overwritten to a non-reference.
Mathieu Chartieref41db72016-10-25 15:08:01 -0700473 if (src_value == reinterpret_cast<uintptr_t>(o.Ptr())) {
474 new_shadow_frame->SetVRegReference(dest_reg, o.Ptr());
Narayan Kamath208f8572016-08-03 12:46:58 +0100475 } else {
476 new_shadow_frame->SetVReg(dest_reg, src_value);
477 }
478}
479
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100480void ArtInterpreterToCompiledCodeBridge(Thread* self,
481 ArtMethod* caller,
482 const DexFile::CodeItem* code_item,
483 ShadowFrame* shadow_frame,
484 JValue* result);
Siva Chandra05d24152016-01-05 17:43:17 -0800485
Mingyao Yangffedec52016-05-19 10:48:40 -0700486// Set string value created from StringFactory.newStringFromXXX() into all aliases of
487// StringFactory.newEmptyString().
488void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
489 uint16_t this_obj_vreg,
490 JValue result);
491
Sebastien Hertzc6714852013-09-30 16:42:32 +0200492// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100493#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700494 template REQUIRES_SHARED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100495 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
496 const Instruction* inst, uint16_t inst_data, \
497 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200498
499#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
500 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
501 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
502 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
503 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
504
Andreas Gampec8ccf682014-09-29 20:07:43 -0700505EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
506EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
507EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
508EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
509EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200510#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
511#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
512
Sebastien Hertzc6714852013-09-30 16:42:32 +0200513// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100514#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700515 template REQUIRES_SHARED(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100516 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
517 const Instruction* inst, uint16_t inst_data, \
518 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200519
520EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
521EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
522#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
523
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200524} // namespace interpreter
525} // namespace art
526
527#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_