Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 1 | /* |
| 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 Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 20 | #include "android-base/logging.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 21 | #include "art_field.h" |
| 22 | #include "art_method.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 23 | #include "base/macros.h" |
| 24 | #include "base/mutex.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 25 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/code_item_accessors.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "dex/primitive.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 28 | #include "handle_scope-inl.h" |
| 29 | #include "instrumentation.h" |
| 30 | #include "interpreter/shadow_frame.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 31 | #include "interpreter/unstarted_runtime.h" |
Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 32 | #include "jvalue-inl.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 33 | #include "mirror/class.h" |
| 34 | #include "mirror/object.h" |
| 35 | #include "obj_ptr-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 36 | #include "runtime.h" |
| 37 | #include "stack.h" |
| 38 | #include "thread.h" |
| 39 | |
| 40 | namespace art { |
| 41 | |
| 42 | namespace interpreter { |
| 43 | void ArtInterpreterToInterpreterBridge(Thread* self, |
| 44 | const DexFile::CodeItem* code_item, |
| 45 | ShadowFrame* shadow_frame, |
| 46 | JValue* result) |
| 47 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 48 | |
| 49 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| 50 | ArtMethod* caller, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 51 | ShadowFrame* shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 52 | uint16_t arg_offset, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 53 | JValue* result); |
| 54 | } // namespace interpreter |
| 55 | |
| 56 | inline void PerformCall(Thread* self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 57 | const CodeItemDataAccessor& accessor, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 58 | ArtMethod* caller_method, |
| 59 | const size_t first_dest_reg, |
| 60 | ShadowFrame* callee_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 61 | JValue* result, |
| 62 | bool use_interpreter_entrypoint) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 64 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 65 | if (use_interpreter_entrypoint) { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 66 | interpreter::ArtInterpreterToInterpreterBridge(self, accessor, callee_frame, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 67 | } else { |
| 68 | interpreter::ArtInterpreterToCompiledCodeBridge( |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 69 | self, caller_method, callee_frame, first_dest_reg, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 70 | } |
| 71 | } else { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 72 | interpreter::UnstartedRuntime::Invoke(self, accessor, callee_frame, result, first_dest_reg); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 76 | template <typename T> |
| 77 | inline void DCheckStaticState(Thread* self, T* entity) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 78 | if (kIsDebugBuild) { |
| 79 | ObjPtr<mirror::Class> klass = entity->GetDeclaringClass(); |
| 80 | if (entity->IsStatic()) { |
| 81 | klass->AssertInitializedOrInitializingInThread(self); |
| 82 | } else { |
| 83 | CHECK(klass->IsInitializing() || klass->IsErroneousResolved()); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 88 | template<Primitive::Type field_type> |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 89 | static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 90 | const ShadowFrame& shadow_frame, |
| 91 | ObjPtr<mirror::Object> obj, |
| 92 | ArtField* field, |
| 93 | JValue* result) |
| 94 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 95 | DCheckStaticState(self, field); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 96 | |
| 97 | // Report this field access to instrumentation if needed. |
| 98 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 99 | if (UNLIKELY(instrumentation->HasFieldReadListeners())) { |
| 100 | StackHandleScope<1> hs(self); |
| 101 | // Wrap in handle wrapper in case the listener does thread suspension. |
| 102 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
| 103 | ObjPtr<mirror::Object> this_object; |
| 104 | if (!field->IsStatic()) { |
| 105 | this_object = obj; |
| 106 | } |
| 107 | instrumentation->FieldReadEvent(self, |
| 108 | this_object.Ptr(), |
| 109 | shadow_frame.GetMethod(), |
| 110 | shadow_frame.GetDexPC(), |
| 111 | field); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 112 | if (UNLIKELY(self->IsExceptionPending())) { |
| 113 | return false; |
| 114 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | switch (field_type) { |
| 118 | case Primitive::kPrimBoolean: |
| 119 | result->SetZ(field->GetBoolean(obj)); |
| 120 | break; |
| 121 | case Primitive::kPrimByte: |
| 122 | result->SetB(field->GetByte(obj)); |
| 123 | break; |
| 124 | case Primitive::kPrimChar: |
| 125 | result->SetC(field->GetChar(obj)); |
| 126 | break; |
| 127 | case Primitive::kPrimShort: |
| 128 | result->SetS(field->GetShort(obj)); |
| 129 | break; |
| 130 | case Primitive::kPrimInt: |
| 131 | result->SetI(field->GetInt(obj)); |
| 132 | break; |
| 133 | case Primitive::kPrimLong: |
| 134 | result->SetJ(field->GetLong(obj)); |
| 135 | break; |
| 136 | case Primitive::kPrimNot: |
| 137 | result->SetL(field->GetObject(obj)); |
| 138 | break; |
| 139 | case Primitive::kPrimVoid: |
| 140 | LOG(FATAL) << "Unreachable " << field_type; |
| 141 | break; |
| 142 | } |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 143 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active> |
| 147 | ALWAYS_INLINE bool DoFieldPutCommon(Thread* self, |
| 148 | const ShadowFrame& shadow_frame, |
| 149 | ObjPtr<mirror::Object> obj, |
| 150 | ArtField* field, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 151 | JValue& value) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 152 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 153 | DCheckStaticState(self, field); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 154 | |
| 155 | // Report this field access to instrumentation if needed. Since we only have the offset of |
| 156 | // the field from the base of the object, we need to look for it first. |
| 157 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 158 | if (UNLIKELY(instrumentation->HasFieldWriteListeners())) { |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 159 | StackHandleScope<2> hs(self); |
| 160 | // Save this and return value (if needed) in case the instrumentation causes a suspend. |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 161 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
| 162 | ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj; |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 163 | mirror::Object* fake_root = nullptr; |
| 164 | HandleWrapper<mirror::Object> ret(hs.NewHandleWrapper<mirror::Object>( |
| 165 | field_type == Primitive::kPrimNot ? value.GetGCRoot() : &fake_root)); |
| 166 | instrumentation->FieldWriteEvent(self, |
| 167 | this_object.Ptr(), |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 168 | shadow_frame.GetMethod(), |
| 169 | shadow_frame.GetDexPC(), |
| 170 | field, |
| 171 | value); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 172 | if (UNLIKELY(self->IsExceptionPending())) { |
| 173 | return false; |
| 174 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | switch (field_type) { |
| 178 | case Primitive::kPrimBoolean: |
| 179 | field->SetBoolean<transaction_active>(obj, value.GetZ()); |
| 180 | break; |
| 181 | case Primitive::kPrimByte: |
| 182 | field->SetByte<transaction_active>(obj, value.GetB()); |
| 183 | break; |
| 184 | case Primitive::kPrimChar: |
| 185 | field->SetChar<transaction_active>(obj, value.GetC()); |
| 186 | break; |
| 187 | case Primitive::kPrimShort: |
| 188 | field->SetShort<transaction_active>(obj, value.GetS()); |
| 189 | break; |
| 190 | case Primitive::kPrimInt: |
| 191 | field->SetInt<transaction_active>(obj, value.GetI()); |
| 192 | break; |
| 193 | case Primitive::kPrimLong: |
| 194 | field->SetLong<transaction_active>(obj, value.GetJ()); |
| 195 | break; |
| 196 | case Primitive::kPrimNot: { |
| 197 | ObjPtr<mirror::Object> reg = value.GetL(); |
| 198 | if (do_assignability_check && reg != nullptr) { |
| 199 | // FieldHelper::GetType can resolve classes, use a handle wrapper which will restore the |
| 200 | // object in the destructor. |
| 201 | ObjPtr<mirror::Class> field_class; |
| 202 | { |
| 203 | StackHandleScope<2> hs(self); |
| 204 | HandleWrapperObjPtr<mirror::Object> h_reg(hs.NewHandleWrapper(®)); |
| 205 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 206 | field_class = field->ResolveType(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 207 | } |
Orion Hodson | 9fa1bab | 2018-05-23 15:21:35 +0100 | [diff] [blame] | 208 | // ArtField::ResolveType() may fail as evidenced with a dexing bug (b/78788577). |
| 209 | if (UNLIKELY(field_class.IsNull())) { |
| 210 | Thread::Current()->AssertPendingException(); |
| 211 | return false; |
| 212 | } |
| 213 | if (UNLIKELY(!reg->VerifierInstanceOf(field_class.Ptr()))) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 214 | // This should never happen. |
| 215 | std::string temp1, temp2, temp3; |
| 216 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 217 | "Put '%s' that is not instance of field '%s' in '%s'", |
| 218 | reg->GetClass()->GetDescriptor(&temp1), |
| 219 | field_class->GetDescriptor(&temp2), |
| 220 | field->GetDeclaringClass()->GetDescriptor(&temp3)); |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 | field->SetObj<transaction_active>(obj, reg); |
| 225 | break; |
| 226 | } |
| 227 | case Primitive::kPrimVoid: { |
| 228 | LOG(FATAL) << "Unreachable " << field_type; |
| 229 | break; |
| 230 | } |
| 231 | } |
Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 232 | if (transaction_active) { |
| 233 | if (UNLIKELY(self->IsExceptionPending())) { |
| 234 | return false; |
| 235 | } |
| 236 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | |
| 240 | } // namespace art |
| 241 | |
| 242 | #endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_ |