1 /* 2 * Copyright (C) 2011 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_ART_FIELD_INL_H_ 18 #define ART_RUNTIME_ART_FIELD_INL_H_ 19 20 #include "art_field.h" 21 22 #include <android-base/logging.h> 23 24 #include "class_linker.h" 25 #include "dex/dex_file-inl.h" 26 #include "dex/primitive.h" 27 #include "gc/accounting/card_table-inl.h" 28 #include "gc_root-inl.h" 29 #include "jvalue.h" 30 #include "mirror/dex_cache-inl.h" 31 #include "mirror/object-inl.h" 32 #include "scoped_thread_state_change-inl.h" 33 #include "thread-current-inl.h" 34 35 namespace art { 36 37 template<ReadBarrierOption kReadBarrierOption> 38 inline ObjPtr<mirror::Class> ArtField::GetDeclaringClass() { 39 GcRootSource gc_root_source(this); 40 ObjPtr<mirror::Class> result = declaring_class_.Read<kReadBarrierOption>(&gc_root_source); 41 DCHECK(result != nullptr); 42 DCHECK(result->IsLoaded() || result->IsErroneous()) << result->GetStatus(); 43 return result; 44 } 45 46 inline void ArtField::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) { 47 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class); 48 } 49 50 inline MemberOffset ArtField::GetOffsetDuringLinking() { 51 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); 52 return MemberOffset(offset_); 53 } 54 55 inline uint32_t ArtField::Get32(ObjPtr<mirror::Object> object) { 56 DCHECK(object != nullptr) << PrettyField(); 57 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 58 if (UNLIKELY(IsVolatile())) { 59 return object->GetField32Volatile(GetOffset()); 60 } 61 return object->GetField32(GetOffset()); 62 } 63 64 template<bool kTransactionActive> 65 inline void ArtField::Set32(ObjPtr<mirror::Object> object, uint32_t new_value) { 66 DCHECK(object != nullptr) << PrettyField(); 67 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 68 if (UNLIKELY(IsVolatile())) { 69 object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value); 70 } else { 71 object->SetField32<kTransactionActive>(GetOffset(), new_value); 72 } 73 } 74 75 inline uint64_t ArtField::Get64(ObjPtr<mirror::Object> object) { 76 DCHECK(object != nullptr) << PrettyField(); 77 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 78 if (UNLIKELY(IsVolatile())) { 79 return object->GetField64Volatile(GetOffset()); 80 } 81 return object->GetField64(GetOffset()); 82 } 83 84 template<bool kTransactionActive> 85 inline void ArtField::Set64(ObjPtr<mirror::Object> object, uint64_t new_value) { 86 DCHECK(object != nullptr) << PrettyField(); 87 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 88 if (UNLIKELY(IsVolatile())) { 89 object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value); 90 } else { 91 object->SetField64<kTransactionActive>(GetOffset(), new_value); 92 } 93 } 94 95 template<class MirrorType> 96 inline ObjPtr<MirrorType> ArtField::GetObj(ObjPtr<mirror::Object> object) { 97 DCHECK(object != nullptr) << PrettyField(); 98 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 99 if (UNLIKELY(IsVolatile())) { 100 return object->GetFieldObjectVolatile<MirrorType>(GetOffset()); 101 } 102 return object->GetFieldObject<MirrorType>(GetOffset()); 103 } 104 105 template<bool kTransactionActive> 106 inline void ArtField::SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value) { 107 DCHECK(object != nullptr) << PrettyField(); 108 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); 109 if (UNLIKELY(IsVolatile())) { 110 object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value); 111 } else { 112 object->SetFieldObject<kTransactionActive>(GetOffset(), new_value); 113 } 114 } 115 116 #define FIELD_GET(object, type) \ 117 DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(); \ 118 DCHECK((object) != nullptr) << PrettyField(); \ 119 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \ 120 if (UNLIKELY(IsVolatile())) { \ 121 return (object)->GetField ## type ## Volatile(GetOffset()); \ 122 } \ 123 return (object)->GetField ## type(GetOffset()); 124 125 #define FIELD_SET(object, type, value) \ 126 DCHECK((object) != nullptr) << PrettyField(); \ 127 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \ 128 if (UNLIKELY(IsVolatile())) { \ 129 (object)->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \ 130 } else { \ 131 (object)->SetField ## type<kTransactionActive>(GetOffset(), value); \ 132 } 133 134 inline uint8_t ArtField::GetBoolean(ObjPtr<mirror::Object> object) { 135 FIELD_GET(object, Boolean); 136 } 137 138 template<bool kTransactionActive> 139 inline void ArtField::SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) { 140 if (kIsDebugBuild) { 141 // For simplicity, this method is being called by the compiler entrypoint for 142 // both boolean and byte fields. 143 Primitive::Type type = GetTypeAsPrimitiveType(); 144 DCHECK(type == Primitive::kPrimBoolean || type == Primitive::kPrimByte) << PrettyField(); 145 } 146 FIELD_SET(object, Boolean, z); 147 } 148 149 inline int8_t ArtField::GetByte(ObjPtr<mirror::Object> object) { 150 FIELD_GET(object, Byte); 151 } 152 153 template<bool kTransactionActive> 154 inline void ArtField::SetByte(ObjPtr<mirror::Object> object, int8_t b) { 155 DCHECK_EQ(Primitive::kPrimByte, GetTypeAsPrimitiveType()) << PrettyField(); 156 FIELD_SET(object, Byte, b); 157 } 158 159 inline uint16_t ArtField::GetChar(ObjPtr<mirror::Object> object) { 160 FIELD_GET(object, Char); 161 } 162 163 template<bool kTransactionActive> 164 inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) { 165 if (kIsDebugBuild) { 166 // For simplicity, this method is being called by the compiler entrypoint for 167 // both char and short fields. 168 Primitive::Type type = GetTypeAsPrimitiveType(); 169 DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField(); 170 } 171 FIELD_SET(object, Char, c); 172 } 173 174 inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) { 175 FIELD_GET(object, Short); 176 } 177 178 template<bool kTransactionActive> 179 inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) { 180 DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField(); 181 FIELD_SET(object, Short, s); 182 } 183 184 #undef FIELD_GET 185 #undef FIELD_SET 186 187 inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) { 188 if (kIsDebugBuild) { 189 // For simplicity, this method is being called by the compiler entrypoint for 190 // both int and float fields. 191 Primitive::Type type = GetTypeAsPrimitiveType(); 192 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(); 193 } 194 return Get32(object); 195 } 196 197 template<bool kTransactionActive> 198 inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) { 199 if (kIsDebugBuild) { 200 // For simplicity, this method is being called by the compiler entrypoint for 201 // both int and float fields. 202 Primitive::Type type = GetTypeAsPrimitiveType(); 203 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(); 204 } 205 Set32<kTransactionActive>(object, i); 206 } 207 208 inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) { 209 if (kIsDebugBuild) { 210 // For simplicity, this method is being called by the compiler entrypoint for 211 // both long and double fields. 212 Primitive::Type type = GetTypeAsPrimitiveType(); 213 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(); 214 } 215 return Get64(object); 216 } 217 218 template<bool kTransactionActive> 219 inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) { 220 if (kIsDebugBuild) { 221 // For simplicity, this method is being called by the compiler entrypoint for 222 // both long and double fields. 223 Primitive::Type type = GetTypeAsPrimitiveType(); 224 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(); 225 } 226 Set64<kTransactionActive>(object, j); 227 } 228 229 inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) { 230 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(); 231 JValue bits; 232 bits.SetI(Get32(object)); 233 return bits.GetF(); 234 } 235 236 template<bool kTransactionActive> 237 inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) { 238 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField(); 239 JValue bits; 240 bits.SetF(f); 241 Set32<kTransactionActive>(object, bits.GetI()); 242 } 243 244 inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) { 245 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(); 246 JValue bits; 247 bits.SetJ(Get64(object)); 248 return bits.GetD(); 249 } 250 251 template<bool kTransactionActive> 252 inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) { 253 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField(); 254 JValue bits; 255 bits.SetD(d); 256 Set64<kTransactionActive>(object, bits.GetJ()); 257 } 258 259 inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) { 260 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(); 261 return GetObj(object); 262 } 263 264 template<bool kTransactionActive> 265 inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) { 266 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField(); 267 SetObj<kTransactionActive>(object, l); 268 } 269 270 inline const char* ArtField::GetName() REQUIRES_SHARED(Locks::mutator_lock_) { 271 uint32_t field_index = GetDexFieldIndex(); 272 if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) { 273 DCHECK(IsStatic()); 274 DCHECK_LT(field_index, 2U); 275 return field_index == 0 ? "interfaces" : "throws"; 276 } 277 const DexFile* dex_file = GetDexFile(); 278 return dex_file->GetFieldName(dex_file->GetFieldId(field_index)); 279 } 280 281 inline const char* ArtField::GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { 282 uint32_t field_index = GetDexFieldIndex(); 283 if (UNLIKELY(GetDeclaringClass()->IsProxyClass())) { 284 DCHECK(IsStatic()); 285 DCHECK_LT(field_index, 2U); 286 // 0 == Class[] interfaces; 1 == Class[][] throws; 287 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;"; 288 } 289 const DexFile* dex_file = GetDexFile(); 290 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); 291 return dex_file->GetFieldTypeDescriptor(field_id); 292 } 293 294 inline Primitive::Type ArtField::GetTypeAsPrimitiveType() 295 REQUIRES_SHARED(Locks::mutator_lock_) { 296 return Primitive::GetType(GetTypeDescriptor()[0]); 297 } 298 299 inline bool ArtField::IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_) { 300 return GetTypeAsPrimitiveType() != Primitive::kPrimNot; 301 } 302 303 inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() { 304 ScopedAssertNoThreadSuspension ants(__FUNCTION__); 305 const uint32_t field_index = GetDexFieldIndex(); 306 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(); 307 if (UNLIKELY(declaring_class->IsProxyClass())) { 308 return ProxyFindSystemClass(GetTypeDescriptor()); 309 } 310 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType( 311 declaring_class->GetDexFile().GetFieldId(field_index).type_idx_, declaring_class); 312 DCHECK(!Thread::Current()->IsExceptionPending()); 313 return type; 314 } 315 316 inline ObjPtr<mirror::Class> ArtField::ResolveType() { 317 const uint32_t field_index = GetDexFieldIndex(); 318 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(); 319 if (UNLIKELY(declaring_class->IsProxyClass())) { 320 return ProxyFindSystemClass(GetTypeDescriptor()); 321 } 322 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType( 323 declaring_class->GetDexFile().GetFieldId(field_index).type_idx_, declaring_class); 324 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending()); 325 return type; 326 } 327 328 inline size_t ArtField::FieldSize() REQUIRES_SHARED(Locks::mutator_lock_) { 329 return Primitive::ComponentSize(GetTypeAsPrimitiveType()); 330 } 331 332 inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { 333 return GetDeclaringClass()->GetDexCache(); 334 } 335 336 inline const DexFile* ArtField::GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_) { 337 return GetDexCache()->GetDexFile(); 338 } 339 340 inline ObjPtr<mirror::String> ArtField::GetStringName(Thread* self, bool resolve) { 341 auto dex_field_index = GetDexFieldIndex(); 342 CHECK_NE(dex_field_index, dex::kDexNoIndex); 343 ObjPtr<mirror::DexCache> dex_cache = GetDexCache(); 344 const DexFile::FieldId& field_id = dex_cache->GetDexFile()->GetFieldId(dex_field_index); 345 ObjPtr<mirror::String> name = dex_cache->GetResolvedString(field_id.name_idx_); 346 if (resolve && name == nullptr) { 347 name = ResolveGetStringName(self, field_id.name_idx_, dex_cache); 348 } 349 return name; 350 } 351 352 template <typename Visitor> 353 inline void ArtField::UpdateObjects(const Visitor& visitor) { 354 ObjPtr<mirror::Class> old_class = DeclaringClassRoot().Read<kWithoutReadBarrier>(); 355 ObjPtr<mirror::Class> new_class = visitor(old_class.Ptr()); 356 if (old_class != new_class) { 357 SetDeclaringClass(new_class); 358 } 359 } 360 361 // If kExactOffset is true then we only find the matching offset, not the field containing the 362 // offset. 363 template <bool kExactOffset> 364 static inline ArtField* FindFieldWithOffset( 365 const IterationRange<StrideIterator<ArtField>>& fields, 366 uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) { 367 for (ArtField& field : fields) { 368 if (kExactOffset) { 369 if (field.GetOffset().Uint32Value() == field_offset) { 370 return &field; 371 } 372 } else { 373 const uint32_t offset = field.GetOffset().Uint32Value(); 374 Primitive::Type type = field.GetTypeAsPrimitiveType(); 375 const size_t field_size = Primitive::ComponentSize(type); 376 DCHECK_GT(field_size, 0u); 377 if (offset <= field_offset && field_offset < offset + field_size) { 378 return &field; 379 } 380 } 381 } 382 return nullptr; 383 } 384 385 template <bool kExactOffset> 386 inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass, 387 uint32_t field_offset) { 388 DCHECK(klass != nullptr); 389 ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset); 390 if (field != nullptr) { 391 return field; 392 } 393 // We did not find field in the class: look into superclass. 394 return (klass->GetSuperClass() != nullptr) ? 395 FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr; 396 } 397 398 template <bool kExactOffset> 399 inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass, 400 uint32_t field_offset) { 401 DCHECK(klass != nullptr); 402 return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset); 403 } 404 405 } // namespace art 406 407 #endif // ART_RUNTIME_ART_FIELD_INL_H_ 408