blob: 4d5bd3fdce6764aa781ec9cacbc3a1d29afb6b54 [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"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070026#include "handle_scope-inl.h"
27#include "instrumentation.h"
28#include "interpreter/shadow_frame.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000029#include "interpreter/unstarted_runtime.h"
Andreas Gampe0a0935f2017-10-23 11:59:49 -070030#include "mirror/class.h"
31#include "mirror/object.h"
32#include "obj_ptr-inl.h"
33#include "primitive.h"
Orion Hodson811bd5f2016-12-07 11:35:37 +000034#include "runtime.h"
35#include "stack.h"
36#include "thread.h"
37
38namespace art {
39
40namespace interpreter {
41 void ArtInterpreterToInterpreterBridge(Thread* self,
42 const DexFile::CodeItem* code_item,
43 ShadowFrame* shadow_frame,
44 JValue* result)
45 REQUIRES_SHARED(Locks::mutator_lock_);
46
47 void ArtInterpreterToCompiledCodeBridge(Thread* self,
48 ArtMethod* caller,
Orion Hodson811bd5f2016-12-07 11:35:37 +000049 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070050 uint16_t arg_offset,
Orion Hodson811bd5f2016-12-07 11:35:37 +000051 JValue* result);
52} // namespace interpreter
53
54inline void PerformCall(Thread* self,
55 const DexFile::CodeItem* code_item,
56 ArtMethod* caller_method,
57 const size_t first_dest_reg,
58 ShadowFrame* callee_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070059 JValue* result,
60 bool use_interpreter_entrypoint)
Orion Hodson811bd5f2016-12-07 11:35:37 +000061 REQUIRES_SHARED(Locks::mutator_lock_) {
62 if (LIKELY(Runtime::Current()->IsStarted())) {
Jeff Hao5ea84132017-05-05 16:59:29 -070063 if (use_interpreter_entrypoint) {
Orion Hodson811bd5f2016-12-07 11:35:37 +000064 interpreter::ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
65 } else {
66 interpreter::ArtInterpreterToCompiledCodeBridge(
Jeff Hao5ea84132017-05-05 16:59:29 -070067 self, caller_method, callee_frame, first_dest_reg, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000068 }
69 } else {
70 interpreter::UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
71 }
72}
73
74template<Primitive::Type field_type>
Alex Light084fa372017-06-16 08:58:34 -070075static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self,
Orion Hodson811bd5f2016-12-07 11:35:37 +000076 const ShadowFrame& shadow_frame,
77 ObjPtr<mirror::Object> obj,
78 ArtField* field,
79 JValue* result)
80 REQUIRES_SHARED(Locks::mutator_lock_) {
81 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
82
83 // Report this field access to instrumentation if needed.
84 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
85 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
86 StackHandleScope<1> hs(self);
87 // Wrap in handle wrapper in case the listener does thread suspension.
88 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
89 ObjPtr<mirror::Object> this_object;
90 if (!field->IsStatic()) {
91 this_object = obj;
92 }
93 instrumentation->FieldReadEvent(self,
94 this_object.Ptr(),
95 shadow_frame.GetMethod(),
96 shadow_frame.GetDexPC(),
97 field);
Alex Light084fa372017-06-16 08:58:34 -070098 if (UNLIKELY(self->IsExceptionPending())) {
99 return false;
100 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000101 }
102
103 switch (field_type) {
104 case Primitive::kPrimBoolean:
105 result->SetZ(field->GetBoolean(obj));
106 break;
107 case Primitive::kPrimByte:
108 result->SetB(field->GetByte(obj));
109 break;
110 case Primitive::kPrimChar:
111 result->SetC(field->GetChar(obj));
112 break;
113 case Primitive::kPrimShort:
114 result->SetS(field->GetShort(obj));
115 break;
116 case Primitive::kPrimInt:
117 result->SetI(field->GetInt(obj));
118 break;
119 case Primitive::kPrimLong:
120 result->SetJ(field->GetLong(obj));
121 break;
122 case Primitive::kPrimNot:
123 result->SetL(field->GetObject(obj));
124 break;
125 case Primitive::kPrimVoid:
126 LOG(FATAL) << "Unreachable " << field_type;
127 break;
128 }
Alex Light084fa372017-06-16 08:58:34 -0700129 return true;
Orion Hodson811bd5f2016-12-07 11:35:37 +0000130}
131
132template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
133ALWAYS_INLINE bool DoFieldPutCommon(Thread* self,
134 const ShadowFrame& shadow_frame,
135 ObjPtr<mirror::Object> obj,
136 ArtField* field,
Alex Light084fa372017-06-16 08:58:34 -0700137 JValue& value)
Orion Hodson811bd5f2016-12-07 11:35:37 +0000138 REQUIRES_SHARED(Locks::mutator_lock_) {
139 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
140
141 // Report this field access to instrumentation if needed. Since we only have the offset of
142 // the field from the base of the object, we need to look for it first.
143 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
144 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
Alex Light084fa372017-06-16 08:58:34 -0700145 StackHandleScope<2> hs(self);
146 // Save this and return value (if needed) in case the instrumentation causes a suspend.
Orion Hodson811bd5f2016-12-07 11:35:37 +0000147 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
148 ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj;
Alex Light084fa372017-06-16 08:58:34 -0700149 mirror::Object* fake_root = nullptr;
150 HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>(
151 field_type == Primitive::kPrimNot ? value.GetGCRoot() : &fake_root));
152 instrumentation->FieldWriteEvent(self,
153 this_object.Ptr(),
Orion Hodson811bd5f2016-12-07 11:35:37 +0000154 shadow_frame.GetMethod(),
155 shadow_frame.GetDexPC(),
156 field,
157 value);
Alex Light084fa372017-06-16 08:58:34 -0700158 if (UNLIKELY(self->IsExceptionPending())) {
159 return false;
160 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000161 }
162
163 switch (field_type) {
164 case Primitive::kPrimBoolean:
165 field->SetBoolean<transaction_active>(obj, value.GetZ());
166 break;
167 case Primitive::kPrimByte:
168 field->SetByte<transaction_active>(obj, value.GetB());
169 break;
170 case Primitive::kPrimChar:
171 field->SetChar<transaction_active>(obj, value.GetC());
172 break;
173 case Primitive::kPrimShort:
174 field->SetShort<transaction_active>(obj, value.GetS());
175 break;
176 case Primitive::kPrimInt:
177 field->SetInt<transaction_active>(obj, value.GetI());
178 break;
179 case Primitive::kPrimLong:
180 field->SetLong<transaction_active>(obj, value.GetJ());
181 break;
182 case Primitive::kPrimNot: {
183 ObjPtr<mirror::Object> reg = value.GetL();
184 if (do_assignability_check && reg != nullptr) {
185 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
186 // object in the destructor.
187 ObjPtr<mirror::Class> field_class;
188 {
189 StackHandleScope<2> hs(self);
190 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
191 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
192 field_class = field->GetType<true>();
193 }
194 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
195 // This should never happen.
196 std::string temp1, temp2, temp3;
197 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
198 "Put '%s' that is not instance of field '%s' in '%s'",
199 reg->GetClass()->GetDescriptor(&temp1),
200 field_class->GetDescriptor(&temp2),
201 field->GetDeclaringClass()->GetDescriptor(&temp3));
202 return false;
203 }
204 }
205 field->SetObj<transaction_active>(obj, reg);
206 break;
207 }
208 case Primitive::kPrimVoid: {
209 LOG(FATAL) << "Unreachable " << field_type;
210 break;
211 }
212 }
Chang Xingbd208d82017-07-12 14:53:17 -0700213 if (transaction_active) {
214 if (UNLIKELY(self->IsExceptionPending())) {
215 return false;
216 }
217 }
Orion Hodson811bd5f2016-12-07 11:35:37 +0000218 return true;
219}
220
221} // namespace art
222
223#endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_