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 | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 23 | #include "base/locks.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 24 | #include "base/macros.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" |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 27 | #include "dex/dex_file_structs.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 28 | #include "dex/primitive.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 29 | #include "handle_scope-inl.h" |
| 30 | #include "instrumentation.h" |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 31 | #include "interpreter/interpreter.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 32 | #include "interpreter/shadow_frame.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 33 | #include "interpreter/unstarted_runtime.h" |
Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 34 | #include "jvalue-inl.h" |
Andreas Gampe | 0a0935f | 2017-10-23 11:59:49 -0700 | [diff] [blame] | 35 | #include "mirror/class.h" |
| 36 | #include "mirror/object.h" |
| 37 | #include "obj_ptr-inl.h" |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 38 | #include "runtime.h" |
| 39 | #include "stack.h" |
| 40 | #include "thread.h" |
| 41 | |
| 42 | namespace art { |
| 43 | |
| 44 | namespace interpreter { |
| 45 | void ArtInterpreterToInterpreterBridge(Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 46 | const dex::CodeItem* code_item, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 47 | ShadowFrame* shadow_frame, |
| 48 | JValue* result) |
| 49 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 50 | |
| 51 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| 52 | ArtMethod* caller, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 53 | ShadowFrame* shadow_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 54 | uint16_t arg_offset, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 55 | JValue* result); |
| 56 | } // namespace interpreter |
| 57 | |
| 58 | inline void PerformCall(Thread* self, |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 59 | const CodeItemDataAccessor& accessor, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 60 | ArtMethod* caller_method, |
| 61 | const size_t first_dest_reg, |
| 62 | ShadowFrame* callee_frame, |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 63 | JValue* result, |
| 64 | bool use_interpreter_entrypoint) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 65 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 66 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 67 | if (use_interpreter_entrypoint) { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 68 | interpreter::ArtInterpreterToInterpreterBridge(self, accessor, callee_frame, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 69 | } else { |
| 70 | interpreter::ArtInterpreterToCompiledCodeBridge( |
Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 71 | self, caller_method, callee_frame, first_dest_reg, result); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 72 | } |
| 73 | } else { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 74 | interpreter::UnstartedRuntime::Invoke(self, accessor, callee_frame, result, first_dest_reg); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 78 | template <typename T> |
| 79 | inline 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 Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 90 | template<Primitive::Type field_type> |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 91 | static ALWAYS_INLINE bool DoFieldGetCommon(Thread* self, |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 92 | const ShadowFrame& shadow_frame, |
| 93 | ObjPtr<mirror::Object> obj, |
| 94 | ArtField* field, |
| 95 | JValue* result) |
| 96 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 97 | DCheckStaticState(self, field); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 98 | |
| 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, |
| 110 | this_object.Ptr(), |
| 111 | shadow_frame.GetMethod(), |
| 112 | shadow_frame.GetDexPC(), |
| 113 | field); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 114 | if (UNLIKELY(self->IsExceptionPending())) { |
| 115 | return false; |
| 116 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 117 | } |
| 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 Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 145 | return true; |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | template<Primitive::Type field_type, bool do_assignability_check, bool transaction_active> |
| 149 | ALWAYS_INLINE bool DoFieldPutCommon(Thread* self, |
| 150 | const ShadowFrame& shadow_frame, |
| 151 | ObjPtr<mirror::Object> obj, |
| 152 | ArtField* field, |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 153 | JValue& value) |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 580667b | 2017-10-23 11:20:39 -0700 | [diff] [blame] | 155 | DCheckStaticState(self, field); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 156 | |
| 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 Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 161 | StackHandleScope<2> hs(self); |
| 162 | // 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] | 163 | HandleWrapperObjPtr<mirror::Object> h(hs.NewHandleWrapper(&obj)); |
| 164 | ObjPtr<mirror::Object> this_object = field->IsStatic() ? nullptr : obj; |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 165 | 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, |
| 169 | this_object.Ptr(), |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 170 | shadow_frame.GetMethod(), |
| 171 | shadow_frame.GetDexPC(), |
| 172 | field, |
| 173 | value); |
Alex Light | 084fa37 | 2017-06-16 08:58:34 -0700 | [diff] [blame] | 174 | if (UNLIKELY(self->IsExceptionPending())) { |
| 175 | return false; |
| 176 | } |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 177 | 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 Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 185 | } |
| 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(®)); |
| 215 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&obj)); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 216 | field_class = field->ResolveType(); |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 217 | } |
Orion Hodson | 9fa1bab | 2018-05-23 15:21:35 +0100 | [diff] [blame] | 218 | // 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 | } |
| 223 | if (UNLIKELY(!reg->VerifierInstanceOf(field_class.Ptr()))) { |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 224 | // 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 Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 242 | if (transaction_active) { |
| 243 | if (UNLIKELY(self->IsExceptionPending())) { |
| 244 | return false; |
| 245 | } |
| 246 | } |
Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | } // namespace art |
| 251 | |
| 252 | #endif // ART_RUNTIME_COMMON_DEX_OPERATIONS_H_ |