blob: 882e3ce4c7f6d8f1f3be6a94ab37d003a8a6f070 [file] [log] [blame]
Orion Hodson811bd5f2016-12-07 11:35:37 +00001/*
2 * Copyright (C) 2016 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_COMMON_DEX_OPERATIONS_H_
18#define ART_RUNTIME_COMMON_DEX_OPERATIONS_H_
19
Andreas Gampe0a0935f2017-10-23 11:59:49 -070020#include "android-base/logging.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000021#include "art_field.h"
22#include "art_method.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080023#include "base/locks.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070024#include "base/macros.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000025#include "class_linker.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/code_item_accessors.h"
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080027#include "dex/dex_file_structs.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "dex/primitive.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070029#include "handle_scope-inl.h"
30#include "instrumentation.h"
Alex Light0aa7a5a2018-10-10 15:58:14 +000031#include "interpreter/interpreter.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070032#include "interpreter/shadow_frame.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000033#include "interpreter/unstarted_runtime.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070034#include "jvalue-inl.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070035#include "mirror/class.h"
36#include "mirror/object.h"
37#include "obj_ptr-inl.h"
Alex Light55eccdf2019-10-07 13:51:13 +000038#include "reflective_handle.h"
39#include "reflective_handle_scope.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000040#include "runtime.h"
41#include "stack.h"
42#include "thread.h"
43
44namespace art {
45
46namespace interpreter {
47 void ArtInterpreterToInterpreterBridge(Thread* self,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080048 const dex::CodeItem* code_item,
Orion Hodson811bd5f2016-12-07 11:35:37 +000049 ShadowFrame* shadow_frame,
50 JValue* result)
51 REQUIRES_SHARED(Locks::mutator_lock_);
52
53 void ArtInterpreterToCompiledCodeBridge(Thread* self,
54 ArtMethod* caller,
Orion Hodson811bd5f2016-12-07 11:35:37 +000055 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070056 uint16_t arg_offset,
Orion Hodson811bd5f2016-12-07 11:35:37 +000057 JValue* result);
58} // namespace interpreter
59
60inline void PerformCall(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080061 const CodeItemDataAccessor& accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +000062 ArtMethod* caller_method,
63 const size_t first_dest_reg,
64 ShadowFrame* callee_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070065 JValue* result,
66 bool use_interpreter_entrypoint)
Orion Hodson811bd5f2016-12-07 11:35:37 +000067 REQUIRES_SHARED(Locks::mutator_lock_) {
68 if (LIKELY(Runtime::Current()->IsStarted())) {
Jeff Hao5ea84132017-05-05 16:59:29 -070069 if (use_interpreter_entrypoint) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080070 interpreter::ArtInterpreterToInterpreterBridge(self, accessor, callee_frame, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000071 } else {
72 interpreter::ArtInterpreterToCompiledCodeBridge(
Jeff Hao5ea84132017-05-05 16:59:29 -070073 self, caller_method, callee_frame, first_dest_reg, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000074 }
75 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080076 interpreter::UnstartedRuntime::Invoke(self, accessor, callee_frame, result, first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +000077 }
78}
79
Andreas Gampe580667b2017-10-23 11:20:39 -070080template <typename T>
81inline void DCheckStaticState(Thread* self, T* entity) REQUIRES_SHARED(Locks::mutator_lock_) {
82 if (kIsDebugBuild) {
83 ObjPtr<mirror::Class> klass = entity->GetDeclaringClass();
84 if (entity->IsStatic()) {
85 klass->AssertInitializedOrInitializingInThread(self);
86 } else {
87 CHECK(klass->IsInitializing() || klass->IsErroneousResolved());
88 }
89 }
90}
91
Orion Hodson811bd5f2016-12-07 11:35:37 +000092template<Primitive::Type field_type>
Alex Light084fa372017-06-16 08:58:34 -070093static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self,
Orion Hodson811bd5f2016-12-07 11:35:37 +000094 const ShadowFrame& shadow_frame,
95 ObjPtr<mirror::Object> obj,
96 ArtField* field,
97 JValue* result)
98 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -070099 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000100
101 // Report this field access to instrumentation if needed.
102 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
103 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
104 StackHandleScope<1> hs(self);
Alex Light55eccdf2019-10-07 13:51:13 +0000105 StackArtFieldHandleScope<1> rhs(self);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000106 // Wrap in handle wrapper in case the listener does thread suspension.
107 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Alex Light55eccdf2019-10-07 13:51:13 +0000108 ReflectiveHandleWrapper<ArtField> fh(rhs.NewReflectiveHandleWrapper(&field));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000109 ObjPtr<mirror::Object> this_object;
110 if (!field->IsStatic()) {
111 this_object = obj;
112 }
113 instrumentation->FieldReadEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100114 this_object,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000115 shadow_frame.GetMethod(),
116 shadow_frame.GetDexPC(),
117 field);
Alex Light084fa372017-06-16 08:58:34 -0700118 if (UNLIKELY(self->IsExceptionPending())) {
119 return false;
120 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000121 }
122
123 switch (field_type) {
124 case Primitive::kPrimBoolean:
125 result->SetZ(field->GetBoolean(obj));
126 break;
127 case Primitive::kPrimByte:
128 result->SetB(field->GetByte(obj));
129 break;
130 case Primitive::kPrimChar:
131 result->SetC(field->GetChar(obj));
132 break;
133 case Primitive::kPrimShort:
134 result->SetS(field->GetShort(obj));
135 break;
136 case Primitive::kPrimInt:
137 result->SetI(field->GetInt(obj));
138 break;
139 case Primitive::kPrimLong:
140 result->SetJ(field->GetLong(obj));
141 break;
142 case Primitive::kPrimNot:
143 result->SetL(field->GetObject(obj));
144 break;
145 case Primitive::kPrimVoid:
146 LOG(FATAL) << "Unreachable " << field_type;
147 break;
148 }
Alex Light084fa372017-06-16 08:58:34 -0700149 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000150}
151
152template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
153ALWAYS_INLINE bool DoFieldPutCommon(Thread* self,
154 const ShadowFrame& shadow_frame,
155 ObjPtr<mirror::Object> obj,
156 ArtField* field,
Alex Light084fa372017-06-16 08:58:34 -0700157 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000158 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -0700159 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000160
161 // Report this field access to instrumentation if needed. Since we only have the offset of
162 // the field from the base of the object, we need to look for it first.
163 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
164 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Alex Light084fa372017-06-16 08:58:34 -0700165 StackHandleScope<2> hs(self);
Alex Light55eccdf2019-10-07 13:51:13 +0000166 StackArtFieldHandleScope<1> rhs(self);
Alex Light084fa372017-06-16 08:58:34 -0700167 // Save this and return value (if needed) in case the instrumentation causes a suspend.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000168 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
Alex Light55eccdf2019-10-07 13:51:13 +0000169 ReflectiveHandleWrapper<ArtField> fh(rhs.NewReflectiveHandleWrapper(&field));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000170 ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj;
Alex Light084fa372017-06-16 08:58:34 -0700171 mirror::Object* fake_root = nullptr;
172 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
173 field_type == Primitive::kPrimNot ? value.GetGCRoot() : &fake_root));
174 instrumentation->FieldWriteEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100175 this_object,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000176 shadow_frame.GetMethod(),
177 shadow_frame.GetDexPC(),
178 field,
179 value);
Alex Light084fa372017-06-16 08:58:34 -0700180 if (UNLIKELY(self->IsExceptionPending())) {
181 return false;
182 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000183 if (shadow_frame.GetForcePopFrame()) {
184 // We need to check this here since we expect that the FieldWriteEvent happens before the
185 // actual field write. If one pops the stack we should not modify the field. The next
186 // instruction will force a pop. Return true.
187 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000188 return true;
189 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000190 }
191
192 switch (field_type) {
193 case Primitive::kPrimBoolean:
194 field->SetBoolean<transaction_active>(obj, value.GetZ());
195 break;
196 case Primitive::kPrimByte:
197 field->SetByte<transaction_active>(obj, value.GetB());
198 break;
199 case Primitive::kPrimChar:
200 field->SetChar<transaction_active>(obj, value.GetC());
201 break;
202 case Primitive::kPrimShort:
203 field->SetShort<transaction_active>(obj, value.GetS());
204 break;
205 case Primitive::kPrimInt:
206 field->SetInt<transaction_active>(obj, value.GetI());
207 break;
208 case Primitive::kPrimLong:
209 field->SetLong<transaction_active>(obj, value.GetJ());
210 break;
211 case Primitive::kPrimNot: {
212 ObjPtr<mirror::Object> reg = value.GetL();
213 if (do_assignability_check && reg != nullptr) {
214 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
215 // object in the destructor.
216 ObjPtr<mirror::Class> field_class;
217 {
218 StackHandleScope<2> hs(self);
Alex Light55eccdf2019-10-07 13:51:13 +0000219 StackArtFieldHandleScope<1> rhs(self);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000220 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
221 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Alex Light55eccdf2019-10-07 13:51:13 +0000222 ReflectiveHandleWrapper<ArtField> fh(rhs.NewReflectiveHandleWrapper(&field));
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000223 field_class = field->ResolveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000224 }
Orion Hodson9fa1bab2018-05-23 15:21:35 +0100225 // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
226 if (UNLIKELY(field_class.IsNull())) {
227 Thread::Current()->AssertPendingException();
228 return false;
229 }
Vladimir Marko19711d42019-04-12 14:05:34 +0100230 if (UNLIKELY(!reg->VerifierInstanceOf(field_class))) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000231 // This should never happen.
232 std::string temp1, temp2, temp3;
233 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
234 "Put '%s' that is not instance of field '%s' in '%s'",
235 reg->GetClass()->GetDescriptor(&temp1),
236 field_class->GetDescriptor(&temp2),
237 field->GetDeclaringClass()->GetDescriptor(&temp3));
238 return false;
239 }
240 }
241 field->SetObj<transaction_active>(obj, reg);
242 break;
243 }
244 case Primitive::kPrimVoid: {
245 LOG(FATAL) << "Unreachable " << field_type;
246 break;
247 }
248 }
Chang Xingbd208d82017-07-12 14:53:17 -0700249 if (transaction_active) {
250 if (UNLIKELY(self->IsExceptionPending())) {
251 return false;
252 }
253 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000254 return true;
255}
256
257} // namespace art
258
259#endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_