blob: 37e074d552360eeeeb0d27873c320473e297924a [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 Gampe0a0935f2017-10-23 11:59:49 -070023#include "base/macros.h"
24#include "base/mutex.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"
David Sehr67bf42e2018-02-26 16:43:04 -080027#include "dex/primitive.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070028#include "handle_scope-inl.h"
29#include "instrumentation.h"
30#include "interpreter/shadow_frame.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000031#include "interpreter/unstarted_runtime.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070032#include "mirror/class.h"
33#include "mirror/object.h"
34#include "obj_ptr-inl.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000035#include "runtime.h"
36#include "stack.h"
37#include "thread.h"
38
39namespace art {
40
41namespace interpreter {
42 void ArtInterpreterToInterpreterBridge(Thread* self,
43 const DexFile::CodeItem* code_item,
44 ShadowFrame* shadow_frame,
45 JValue* result)
46 REQUIRES_SHARED(Locks::mutator_lock_);
47
48 void ArtInterpreterToCompiledCodeBridge(Thread* self,
49 ArtMethod* caller,
Orion Hodson811bd5f2016-12-07 11:35:37 +000050 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070051 uint16_t arg_offset,
Orion Hodson811bd5f2016-12-07 11:35:37 +000052 JValue* result);
53} // namespace interpreter
54
55inline void PerformCall(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080056 const CodeItemDataAccessor& accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +000057 ArtMethod* caller_method,
58 const size_t first_dest_reg,
59 ShadowFrame* callee_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070060 JValue* result,
61 bool use_interpreter_entrypoint)
Orion Hodson811bd5f2016-12-07 11:35:37 +000062 REQUIRES_SHARED(Locks::mutator_lock_) {
63 if (LIKELY(Runtime::Current()->IsStarted())) {
Jeff Hao5ea84132017-05-05 16:59:29 -070064 if (use_interpreter_entrypoint) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080065 interpreter::ArtInterpreterToInterpreterBridge(self, accessor, callee_frame, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000066 } else {
67 interpreter::ArtInterpreterToCompiledCodeBridge(
Jeff Hao5ea84132017-05-05 16:59:29 -070068 self, caller_method, callee_frame, first_dest_reg, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000069 }
70 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080071 interpreter::UnstartedRuntime::Invoke(self, accessor, callee_frame, result, first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +000072 }
73}
74
Andreas Gampe580667b2017-10-23 11:20:39 -070075template <typename T>
76inline void DCheckStaticState(Thread* self, T* entity) REQUIRES_SHARED(Locks::mutator_lock_) {
77 if (kIsDebugBuild) {
78 ObjPtr<mirror::Class> klass = entity->GetDeclaringClass();
79 if (entity->IsStatic()) {
80 klass->AssertInitializedOrInitializingInThread(self);
81 } else {
82 CHECK(klass->IsInitializing() || klass->IsErroneousResolved());
83 }
84 }
85}
86
Orion Hodson811bd5f2016-12-07 11:35:37 +000087template<Primitive::Type field_type>
Alex Light084fa372017-06-16 08:58:34 -070088static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self,
Orion Hodson811bd5f2016-12-07 11:35:37 +000089 const ShadowFrame& shadow_frame,
90 ObjPtr<mirror::Object> obj,
91 ArtField* field,
92 JValue* result)
93 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -070094 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +000095
96 // Report this field access to instrumentation if needed.
97 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
98 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
99 StackHandleScope<1> hs(self);
100 // Wrap in handle wrapper in case the listener does thread suspension.
101 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
102 ObjPtr<mirror::Object> this_object;
103 if (!field->IsStatic()) {
104 this_object = obj;
105 }
106 instrumentation->FieldReadEvent(self,
107 this_object.Ptr(),
108 shadow_frame.GetMethod(),
109 shadow_frame.GetDexPC(),
110 field);
Alex Light084fa372017-06-16 08:58:34 -0700111 if (UNLIKELY(self->IsExceptionPending())) {
112 return false;
113 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000114 }
115
116 switch (field_type) {
117 case Primitive::kPrimBoolean:
118 result->SetZ(field->GetBoolean(obj));
119 break;
120 case Primitive::kPrimByte:
121 result->SetB(field->GetByte(obj));
122 break;
123 case Primitive::kPrimChar:
124 result->SetC(field->GetChar(obj));
125 break;
126 case Primitive::kPrimShort:
127 result->SetS(field->GetShort(obj));
128 break;
129 case Primitive::kPrimInt:
130 result->SetI(field->GetInt(obj));
131 break;
132 case Primitive::kPrimLong:
133 result->SetJ(field->GetLong(obj));
134 break;
135 case Primitive::kPrimNot:
136 result->SetL(field->GetObject(obj));
137 break;
138 case Primitive::kPrimVoid:
139 LOG(FATAL) << "Unreachable " << field_type;
140 break;
141 }
Alex Light084fa372017-06-16 08:58:34 -0700142 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000143}
144
145template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
146ALWAYS_INLINE bool DoFieldPutCommon(Thread* self,
147 const ShadowFrame& shadow_frame,
148 ObjPtr<mirror::Object> obj,
149 ArtField* field,
Alex Light084fa372017-06-16 08:58:34 -0700150 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000151 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -0700152 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000153
154 // Report this field access to instrumentation if needed. Since we only have the offset of
155 // the field from the base of the object, we need to look for it first.
156 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
157 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Alex Light084fa372017-06-16 08:58:34 -0700158 StackHandleScope<2> hs(self);
159 // Save this and return value (if needed) in case the instrumentation causes a suspend.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000160 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
161 ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj;
Alex Light084fa372017-06-16 08:58:34 -0700162 mirror::Object* fake_root = nullptr;
163 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
164 field_type == Primitive::kPrimNot ? value.GetGCRoot() : &fake_root));
165 instrumentation->FieldWriteEvent(self,
166 this_object.Ptr(),
Orion Hodson811bd5f2016-12-07 11:35:37 +0000167 shadow_frame.GetMethod(),
168 shadow_frame.GetDexPC(),
169 field,
170 value);
Alex Light084fa372017-06-16 08:58:34 -0700171 if (UNLIKELY(self->IsExceptionPending())) {
172 return false;
173 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000174 }
175
176 switch (field_type) {
177 case Primitive::kPrimBoolean:
178 field->SetBoolean<transaction_active>(obj, value.GetZ());
179 break;
180 case Primitive::kPrimByte:
181 field->SetByte<transaction_active>(obj, value.GetB());
182 break;
183 case Primitive::kPrimChar:
184 field->SetChar<transaction_active>(obj, value.GetC());
185 break;
186 case Primitive::kPrimShort:
187 field->SetShort<transaction_active>(obj, value.GetS());
188 break;
189 case Primitive::kPrimInt:
190 field->SetInt<transaction_active>(obj, value.GetI());
191 break;
192 case Primitive::kPrimLong:
193 field->SetLong<transaction_active>(obj, value.GetJ());
194 break;
195 case Primitive::kPrimNot: {
196 ObjPtr<mirror::Object> reg = value.GetL();
197 if (do_assignability_check && reg != nullptr) {
198 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
199 // object in the destructor.
200 ObjPtr<mirror::Class> field_class;
201 {
202 StackHandleScope<2> hs(self);
203 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
204 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000205 field_class = field->ResolveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000206 }
207 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
208 // This should never happen.
209 std::string temp1, temp2, temp3;
210 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
211 "Put '%s' that is not instance of field '%s' in '%s'",
212 reg->GetClass()->GetDescriptor(&temp1),
213 field_class->GetDescriptor(&temp2),
214 field->GetDeclaringClass()->GetDescriptor(&temp3));
215 return false;
216 }
217 }
218 field->SetObj<transaction_active>(obj, reg);
219 break;
220 }
221 case Primitive::kPrimVoid: {
222 LOG(FATAL) << "Unreachable " << field_type;
223 break;
224 }
225 }
Chang Xingbd208d82017-07-12 14:53:17 -0700226 if (transaction_active) {
227 if (UNLIKELY(self->IsExceptionPending())) {
228 return false;
229 }
230 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000231 return true;
232}
233
234} // namespace art
235
236#endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_