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