blob: 133ddb07211f19aaacf90b36dbb4f88dce4af070 [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
20#include "art_field.h"
21#include "art_method.h"
22#include "class_linker.h"
23#include "interpreter/unstarted_runtime.h"
24#include "runtime.h"
25#include "stack.h"
26#include "thread.h"
27
28namespace art {
29
30namespace interpreter {
31 void ArtInterpreterToInterpreterBridge(Thread* self,
32 const DexFile::CodeItem* code_item,
33 ShadowFrame* shadow_frame,
34 JValue* result)
35 REQUIRES_SHARED(Locks::mutator_lock_);
36
37 void ArtInterpreterToCompiledCodeBridge(Thread* self,
38 ArtMethod* caller,
Orion Hodson811bd5f2016-12-07 11:35:37 +000039 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070040 uint16_t arg_offset,
Orion Hodson811bd5f2016-12-07 11:35:37 +000041 JValue* result);
42} // namespace interpreter
43
44inline void PerformCall(Thread* self,
45 const DexFile::CodeItem* code_item,
46 ArtMethod* caller_method,
47 const size_t first_dest_reg,
48 ShadowFrame* callee_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -070049 JValue* result,
50 bool use_interpreter_entrypoint)
Orion Hodson811bd5f2016-12-07 11:35:37 +000051 REQUIRES_SHARED(Locks::mutator_lock_) {
52 if (LIKELY(Runtime::Current()->IsStarted())) {
Jeff Hao5ea84132017-05-05 16:59:29 -070053 if (use_interpreter_entrypoint) {
Orion Hodson811bd5f2016-12-07 11:35:37 +000054 interpreter::ArtInterpreterToInterpreterBridge(self, code_item, callee_frame, result);
55 } else {
56 interpreter::ArtInterpreterToCompiledCodeBridge(
Jeff Hao5ea84132017-05-05 16:59:29 -070057 self, caller_method, callee_frame, first_dest_reg, result);
Orion Hodson811bd5f2016-12-07 11:35:37 +000058 }
59 } else {
60 interpreter::UnstartedRuntime::Invoke(self, code_item, callee_frame, result, first_dest_reg);
61 }
62}
63
64template<Primitive::Type field_type>
65static ALWAYS_INLINE void DoFieldGetCommon(Thread* self,
66 const ShadowFrame& shadow_frame,
67 ObjPtr<mirror::Object> obj,
68 ArtField* field,
69 JValue* result)
70 REQUIRES_SHARED(Locks::mutator_lock_) {
71 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
72
73 // Report this field access to instrumentation if needed.
74 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
75 if (UNLIKELY(instrumentation->HasFieldReadListeners())) {
76 StackHandleScope<1> hs(self);
77 // Wrap in handle wrapper in case the listener does thread suspension.
78 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
79 ObjPtr<mirror::Object> this_object;
80 if (!field->IsStatic()) {
81 this_object = obj;
82 }
83 instrumentation->FieldReadEvent(self,
84 this_object.Ptr(),
85 shadow_frame.GetMethod(),
86 shadow_frame.GetDexPC(),
87 field);
88 }
89
90 switch (field_type) {
91 case Primitive::kPrimBoolean:
92 result->SetZ(field->GetBoolean(obj));
93 break;
94 case Primitive::kPrimByte:
95 result->SetB(field->GetByte(obj));
96 break;
97 case Primitive::kPrimChar:
98 result->SetC(field->GetChar(obj));
99 break;
100 case Primitive::kPrimShort:
101 result->SetS(field->GetShort(obj));
102 break;
103 case Primitive::kPrimInt:
104 result->SetI(field->GetInt(obj));
105 break;
106 case Primitive::kPrimLong:
107 result->SetJ(field->GetLong(obj));
108 break;
109 case Primitive::kPrimNot:
110 result->SetL(field->GetObject(obj));
111 break;
112 case Primitive::kPrimVoid:
113 LOG(FATAL) << "Unreachable " << field_type;
114 break;
115 }
116}
117
118template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active>
119ALWAYS_INLINE bool DoFieldPutCommon(Thread* self,
120 const ShadowFrame& shadow_frame,
121 ObjPtr<mirror::Object> obj,
122 ArtField* field,
123 const JValue& value)
124 REQUIRES_SHARED(Locks::mutator_lock_) {
125 field->GetDeclaringClass()->AssertInitializedOrInitializingInThread(self);
126
127 // Report this field access to instrumentation if needed. Since we only have the offset of
128 // the field from the base of the object, we need to look for it first.
129 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
130 if (UNLIKELY(instrumentation->HasFieldWriteListeners())) {
131 StackHandleScope<1> hs(self);
132 // Wrap in handle wrapper in case the listener does thread suspension.
133 HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj));
134 ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj;
135 instrumentation->FieldWriteEvent(self, this_object.Ptr(),
136 shadow_frame.GetMethod(),
137 shadow_frame.GetDexPC(),
138 field,
139 value);
140 }
141
142 switch (field_type) {
143 case Primitive::kPrimBoolean:
144 field->SetBoolean<transaction_active>(obj, value.GetZ());
145 break;
146 case Primitive::kPrimByte:
147 field->SetByte<transaction_active>(obj, value.GetB());
148 break;
149 case Primitive::kPrimChar:
150 field->SetChar<transaction_active>(obj, value.GetC());
151 break;
152 case Primitive::kPrimShort:
153 field->SetShort<transaction_active>(obj, value.GetS());
154 break;
155 case Primitive::kPrimInt:
156 field->SetInt<transaction_active>(obj, value.GetI());
157 break;
158 case Primitive::kPrimLong:
159 field->SetLong<transaction_active>(obj, value.GetJ());
160 break;
161 case Primitive::kPrimNot: {
162 ObjPtr<mirror::Object> reg = value.GetL();
163 if (do_assignability_check && reg != nullptr) {
164 // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the
165 // object in the destructor.
166 ObjPtr<mirror::Class> field_class;
167 {
168 StackHandleScope<2> hs(self);
169 HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(&reg));
170 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj));
171 field_class = field->GetType<true>();
172 }
173 if (!reg->VerifierInstanceOf(field_class.Ptr())) {
174 // This should never happen.
175 std::string temp1, temp2, temp3;
176 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
177 "Put '%s' that is not instance of field '%s' in '%s'",
178 reg->GetClass()->GetDescriptor(&temp1),
179 field_class->GetDescriptor(&temp2),
180 field->GetDeclaringClass()->GetDescriptor(&temp3));
181 return false;
182 }
183 }
184 field->SetObj<transaction_active>(obj, reg);
185 break;
186 }
187 case Primitive::kPrimVoid: {
188 LOG(FATAL) << "Unreachable " << field_type;
189 break;
190 }
191 }
192 return true;
193}
194
195} // namespace art
196
197#endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_