blob: a7491069fae3c20e8e54d2a7b44c2077920fd3c0 [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"
Orion Hodson811bd5f2016-12-07 11:35:37 +000038#include "runtime.h"
39#include "stack.h"
40#include "thread.h"
41
42namespace art {
43
44namespace interpreter {
45 void ArtInterpreterToInterpreterBridge(Thread* self,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080046 const dex::CodeItem* code_item,
Orion Hodson811bd5f2016-12-07 11:35:37 +000047 ShadowFrame* shadow_frame,
48 JValue* result)
49 REQUIRES_SHARED(Locks::mutator_lock_);
50
51 void ArtInterpreterToCompiledCodeBridge(Thread* self,
52 ArtMethod* caller,
Orion Hodson811bd5f2016-12-07 11:35:37 +000053 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070054 uint16_t arg_offset,
Orion Hodson811bd5f2016-12-07 11:35:37 +000055 JValue* result);
56} // namespace interpreter
57
58inline void PerformCall(Thread* self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -080059 const CodeItemDataAccessor& accessor,
Orion Hodson811bd5f2016-12-07 11:35:37 +000060 ArtMethod* caller_method,
61 const size_t first_dest_reg,
62 ShadowFrame* callee_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070063 JValue* result,
64 bool use_interpreter_entrypoint)
Orion Hodson811bd5f2016-12-07 11:35:37 +000065 REQUIRES_SHARED(Locks::mutator_lock_) {
66 if (LIKELY(Runtime::Current()->IsStarted())) {
Jeff Hao5ea84132017-05-05 16:59:29 -070067 if (use_interpreter_entrypoint) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080068 interpreter::ArtInterpreterToInterpreterBridge(self, accessor, callee_frame, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000069 } else {
70 interpreter::ArtInterpreterToCompiledCodeBridge(
Jeff Hao5ea84132017-05-05 16:59:29 -070071 self, caller_method, callee_frame, first_dest_reg, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000072 }
73 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -080074 interpreter::UnstartedRuntime::Invoke(self, accessor, callee_frame, result, first_dest_reg);
Orion Hodson811bd5f2016-12-07 11:35:37 +000075 }
76}
77
Andreas Gampe580667b2017-10-23 11:20:39 -070078template <typename T>
79inline void DCheckStaticState(Thread* self, T* entity) REQUIRES_SHARED(Locks::mutator_lock_) {
80 if (kIsDebugBuild) {
81 ObjPtr<mirror::Class> klass = entity->GetDeclaringClass();
82 if (entity->IsStatic()) {
83 klass->AssertInitializedOrInitializingInThread(self);
84 } else {
85 CHECK(klass->IsInitializing() || klass->IsErroneousResolved());
86 }
87 }
88}
89
Orion Hodson811bd5f2016-12-07 11:35:37 +000090template<Primitive::Type field_type>
Alex Light084fa372017-06-16 08:58:34 -070091static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self,
Orion Hodson811bd5f2016-12-07 11:35:37 +000092 const ShadowFrame& shadow_frame,
93 ObjPtr<mirror::Object> obj,
94 ArtField* field,
95 JValue* result)
96 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -070097 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +000098
99 // Report this field access to instrumentation if needed.
100 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
101 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
102 StackHandleScope<1> hs(self);
103 // Wrap in handle wrapper in case the listener does thread suspension.
104 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
105 ObjPtr<mirror::Object> this_object;
106 if (!field->IsStatic()) {
107 this_object = obj;
108 }
109 instrumentation->FieldReadEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100110 this_object,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000111 shadow_frame.GetMethod(),
112 shadow_frame.GetDexPC(),
113 field);
Alex Light084fa372017-06-16 08:58:34 -0700114 if (UNLIKELY(self->IsExceptionPending())) {
115 return false;
116 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000117 }
118
119 switch (field_type) {
120 case Primitive::kPrimBoolean:
121 result->SetZ(field->GetBoolean(obj));
122 break;
123 case Primitive::kPrimByte:
124 result->SetB(field->GetByte(obj));
125 break;
126 case Primitive::kPrimChar:
127 result->SetC(field->GetChar(obj));
128 break;
129 case Primitive::kPrimShort:
130 result->SetS(field->GetShort(obj));
131 break;
132 case Primitive::kPrimInt:
133 result->SetI(field->GetInt(obj));
134 break;
135 case Primitive::kPrimLong:
136 result->SetJ(field->GetLong(obj));
137 break;
138 case Primitive::kPrimNot:
139 result->SetL(field->GetObject(obj));
140 break;
141 case Primitive::kPrimVoid:
142 LOG(FATAL) << "Unreachable " << field_type;
143 break;
144 }
Alex Light084fa372017-06-16 08:58:34 -0700145 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000146}
147
148template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
149ALWAYS_INLINE bool DoFieldPutCommon(Thread* self,
150 const ShadowFrame& shadow_frame,
151 ObjPtr<mirror::Object> obj,
152 ArtField* field,
Alex Light084fa372017-06-16 08:58:34 -0700153 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000154 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe580667b2017-10-23 11:20:39 -0700155 DCheckStaticState(self, field);
Orion Hodson811bd5f2016-12-07 11:35:37 +0000156
157 // Report this field access to instrumentation if needed. Since we only have the offset of
158 // the field from the base of the object, we need to look for it first.
159 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
160 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Alex Light084fa372017-06-16 08:58:34 -0700161 StackHandleScope<2> hs(self);
162 // Save this and return value (if needed) in case the instrumentation causes a suspend.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000163 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
164 ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj;
Alex Light084fa372017-06-16 08:58:34 -0700165 mirror::Object* fake_root = nullptr;
166 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
167 field_type == Primitive::kPrimNot ? value.GetGCRoot() : &fake_root));
168 instrumentation->FieldWriteEvent(self,
Vladimir Marko19711d42019-04-12 14:05:34 +0100169 this_object,
Orion Hodson811bd5f2016-12-07 11:35:37 +0000170 shadow_frame.GetMethod(),
171 shadow_frame.GetDexPC(),
172 field,
173 value);
Alex Light084fa372017-06-16 08:58:34 -0700174 if (UNLIKELY(self->IsExceptionPending())) {
175 return false;
176 }
Alex Light0aa7a5a2018-10-10 15:58:14 +0000177 if (shadow_frame.GetForcePopFrame()) {
178 // We need to check this here since we expect that the FieldWriteEvent happens before the
179 // actual field write. If one pops the stack we should not modify the field. The next
180 // instruction will force a pop. Return true.
181 DCHECK(Runtime::Current()->AreNonStandardExitsEnabled());
182 DCHECK(interpreter::PrevFrameWillRetry(self, shadow_frame));
183 return true;
184 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000185 }
186
187 switch (field_type) {
188 case Primitive::kPrimBoolean:
189 field->SetBoolean<transaction_active>(obj, value.GetZ());
190 break;
191 case Primitive::kPrimByte:
192 field->SetByte<transaction_active>(obj, value.GetB());
193 break;
194 case Primitive::kPrimChar:
195 field->SetChar<transaction_active>(obj, value.GetC());
196 break;
197 case Primitive::kPrimShort:
198 field->SetShort<transaction_active>(obj, value.GetS());
199 break;
200 case Primitive::kPrimInt:
201 field->SetInt<transaction_active>(obj, value.GetI());
202 break;
203 case Primitive::kPrimLong:
204 field->SetLong<transaction_active>(obj, value.GetJ());
205 break;
206 case Primitive::kPrimNot: {
207 ObjPtr<mirror::Object> reg = value.GetL();
208 if (do_assignability_check && reg != nullptr) {
209 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
210 // object in the destructor.
211 ObjPtr<mirror::Class> field_class;
212 {
213 StackHandleScope<2> hs(self);
214 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
215 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000216 field_class = field->ResolveType();
Orion Hodson811bd5f2016-12-07 11:35:37 +0000217 }
Orion Hodson9fa1bab2018-05-23 15:21:35 +0100218 // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577).
219 if (UNLIKELY(field_class.IsNull())) {
220 Thread::Current()->AssertPendingException();
221 return false;
222 }
Vladimir Marko19711d42019-04-12 14:05:34 +0100223 if (UNLIKELY(!reg->VerifierInstanceOf(field_class))) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000224 // This should never happen.
225 std::string temp1, temp2, temp3;
226 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
227 "Put '%s' that is not instance of field '%s' in '%s'",
228 reg->GetClass()->GetDescriptor(&temp1),
229 field_class->GetDescriptor(&temp2),
230 field->GetDeclaringClass()->GetDescriptor(&temp3));
231 return false;
232 }
233 }
234 field->SetObj<transaction_active>(obj, reg);
235 break;
236 }
237 case Primitive::kPrimVoid: {
238 LOG(FATAL) << "Unreachable " << field_type;
239 break;
240 }
241 }
Chang Xingbd208d82017-07-12 14:53:17 -0700242 if (transaction_active) {
243 if (UNLIKELY(self->IsExceptionPending())) {
244 return false;
245 }
246 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000247 return true;
248}
249
250} // namespace art
251
252#endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_