1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_RUNTIME_ART_METHOD_INL_H_
#define ART_RUNTIME_ART_METHOD_INL_H_
#include "art_method.h"
#include "art_field.h"
#include "base/callee_save_type.h"
#include "base/logging.h"
#include "class_linker-inl.h"
#include "common_throws.h"
#include "dex_file.h"
#include "dex_file_annotations.h"
#include "dex_file-inl.h"
#include "gc_root-inl.h"
#include "invoke_type.h"
#include "jit/profiling_info.h"
#include "mirror/class-inl.h"
#include "mirror/dex_cache-inl.h"
#include "mirror/object-inl.h"
#include "mirror/object_array.h"
#include "mirror/string.h"
#include "oat.h"
#include "obj_ptr-inl.h"
#include "primitive.h"
#include "quick/quick_method_frame_info.h"
#include "read_barrier-inl.h"
#include "runtime-inl.h"
#include "scoped_thread_state_change-inl.h"
#include "thread-current-inl.h"
#include "utils.h"
namespace art {
template <ReadBarrierOption kReadBarrierOption>
inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
GcRootSource gc_root_source(this);
return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
}
template <ReadBarrierOption kReadBarrierOption>
inline mirror::Class* ArtMethod::GetDeclaringClass() {
mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
if (kIsDebugBuild) {
if (!IsRuntimeMethod()) {
CHECK(result != nullptr) << this;
if (kCheckDeclaringClassState) {
if (!(result->IsIdxLoaded() || result->IsErroneous())) {
LOG(FATAL_WITHOUT_ABORT) << "Class status: " << result->GetStatus();
LOG(FATAL) << result->PrettyClass();
}
}
} else {
CHECK(result == nullptr) << this;
}
}
return result;
}
inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
}
inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
mirror::Class* desired_class) {
GcRoot<mirror::Class> expected_root(expected_class);
GcRoot<mirror::Class> desired_root(desired_class);
return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
CompareExchangeStrongSequentiallyConsistent(
expected_root, desired_root);
}
inline uint16_t ArtMethod::GetMethodIndex() {
DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
return method_index_;
}
inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
return method_index_;
}
inline uint32_t ArtMethod::GetDexMethodIndex() {
if (kCheckDeclaringClassState) {
CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
GetDeclaringClass()->IsErroneous());
}
return GetDexMethodIndexUnchecked();
}
inline mirror::MethodDexCacheType* ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
return GetNativePointer<mirror::MethodDexCacheType*>(DexCacheResolvedMethodsOffset(pointer_size),
pointer_size);
}
inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
PointerSize pointer_size) {
// NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
// without accessing the DexCache and we don't want to do that in release build.
DCHECK_LT(method_index, GetInterfaceMethodIfProxy(pointer_size)->GetDexFile()->NumMethodIds());
uint32_t slot_idx = method_index % mirror::DexCache::kDexCacheMethodCacheSize;
DCHECK_LT(slot_idx, GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
mirror::MethodDexCachePair pair = mirror::DexCache::GetNativePairPtrSize(
GetDexCacheResolvedMethods(pointer_size), slot_idx, pointer_size);
ArtMethod* method = pair.GetObjectForIndex(method_index);
if (LIKELY(method != nullptr)) {
auto* declaring_class = method->GetDeclaringClass();
if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
return method;
}
}
return nullptr;
}
inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
ArtMethod* new_method,
PointerSize pointer_size) {
// NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
// without accessing the DexCache and we don't want to do that in release build.
DCHECK_LT(method_index, GetInterfaceMethodIfProxy(pointer_size)->GetDexFile()->NumMethodIds());
DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
uint32_t slot_idx = method_index % mirror::DexCache::kDexCacheMethodCacheSize;
DCHECK_LT(slot_idx, GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
mirror::MethodDexCachePair pair(new_method, method_index);
mirror::DexCache::SetNativePairPtrSize(
GetDexCacheResolvedMethods(pointer_size), slot_idx, pair, pointer_size);
}
inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
return GetDexCacheResolvedMethods(pointer_size) != nullptr;
}
inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
return GetDexCacheResolvedMethods(pointer_size) ==
other->GetDexCacheResolvedMethods(pointer_size);
}
inline bool ArtMethod::HasSameDexCacheResolvedMethods(mirror::MethodDexCacheType* other_cache,
PointerSize pointer_size) {
return GetDexCacheResolvedMethods(pointer_size) == other_cache;
}
inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx, bool resolve) {
// TODO: Refactor this function into two functions, Resolve...() and Lookup...()
// so that we can properly annotate it with no-suspension possible / suspension possible.
ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
if (UNLIKELY(type == nullptr)) {
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
if (resolve) {
type = class_linker->ResolveType(type_idx, this);
CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
} else {
type = class_linker->LookupResolvedType(
*dex_cache->GetDexFile(), type_idx, dex_cache, GetClassLoader());
}
}
return type.Ptr();
}
inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
switch (type) {
case kStatic:
return !IsStatic();
case kDirect:
return !IsDirect() || IsStatic();
case kVirtual: {
// We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
// method.
mirror::Class* methods_class = GetDeclaringClass();
return IsDirect() || (methods_class->IsInterface() && !IsCopied());
}
case kSuper:
// Constructors and static methods are called with invoke-direct.
return IsConstructor() || IsStatic();
case kInterface: {
mirror::Class* methods_class = GetDeclaringClass();
return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
}
default:
LOG(FATAL) << "Unreachable - invocation type: " << type;
UNREACHABLE();
}
}
inline bool ArtMethod::IsCalleeSaveMethod() {
if (!IsRuntimeMethod()) {
return false;
}
Runtime* runtime = Runtime::Current();
bool result = false;
for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
result = true;
break;
}
}
return result;
}
inline bool ArtMethod::IsResolutionMethod() {
bool result = this == Runtime::Current()->GetResolutionMethod();
// Check that if we do think it is phony it looks like the resolution method.
DCHECK(!result || IsRuntimeMethod());
return result;
}
inline bool ArtMethod::IsImtUnimplementedMethod() {
bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
// Check that if we do think it is phony it looks like the imt unimplemented method.
DCHECK(!result || IsRuntimeMethod());
return result;
}
inline const DexFile* ArtMethod::GetDexFile() {
// It is safe to avoid the read barrier here since the dex file is constant, so if we read the
// from-space dex file pointer it will be equal to the to-space copy.
return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
}
inline const char* ArtMethod::GetDeclaringClassDescriptor() {
uint32_t dex_method_idx = GetDexMethodIndex();
if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
return "<runtime method>";
}
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
}
inline const char* ArtMethod::GetShorty() {
uint32_t unused_length;
return GetShorty(&unused_length);
}
inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
}
inline const Signature ArtMethod::GetSignature() {
uint32_t dex_method_idx = GetDexMethodIndex();
if (dex_method_idx != DexFile::kDexNoIndex) {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
}
return Signature::NoSignature();
}
inline const char* ArtMethod::GetName() {
uint32_t dex_method_idx = GetDexMethodIndex();
if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
}
Runtime* const runtime = Runtime::Current();
if (this == runtime->GetResolutionMethod()) {
return "<runtime internal resolution method>";
} else if (this == runtime->GetImtConflictMethod()) {
return "<runtime internal imt conflict method>";
} else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
return "<runtime internal callee-save all registers method>";
} else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
return "<runtime internal callee-save reference registers method>";
} else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
return "<runtime internal callee-save reference and argument registers method>";
} else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
return "<runtime internal save-every-register method>";
} else {
return "<unknown runtime internal method>";
}
}
inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
return GetDexFile()->GetCodeItem(GetCodeItemOffset());
}
inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
DCHECK(!IsProxyMethod());
return GetClassFromTypeIndex(type_idx, /* resolve */ false) != nullptr;
}
inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
DCHECK(!IsProxyMethod());
if (dex_pc == DexFile::kDexNoIndex) {
return IsNative() ? -2 : -1;
}
return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
}
inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
}
inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
dex_file->GetMethodId(GetDexMethodIndex()));
return dex_file->GetProtoParameters(proto);
}
inline const char* ArtMethod::GetDeclaringClassSourceFile() {
DCHECK(!IsProxyMethod());
return GetDeclaringClass()->GetSourceFile();
}
inline uint16_t ArtMethod::GetClassDefIndex() {
DCHECK(!IsProxyMethod());
if (LIKELY(!IsObsolete())) {
return GetDeclaringClass()->GetDexClassDefIndex();
} else {
return FindObsoleteDexClassDefIndex();
}
}
inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
DCHECK(!IsProxyMethod());
return GetDexFile()->GetClassDef(GetClassDefIndex());
}
inline const char* ArtMethod::GetReturnTypeDescriptor() {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
}
inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
return Primitive::GetType(GetReturnTypeDescriptor()[0]);
}
inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
}
inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
DCHECK(!IsProxyMethod());
return GetDeclaringClass()->GetClassLoader();
}
template <ReadBarrierOption kReadBarrierOption>
inline mirror::DexCache* ArtMethod::GetDexCache() {
if (LIKELY(!IsObsolete())) {
mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
} else {
DCHECK(!IsProxyMethod());
return GetObsoleteDexCache();
}
}
inline bool ArtMethod::IsProxyMethod() {
// Avoid read barrier since the from-space version of the class will have the correct proxy class
// flags since they are constant for the lifetime of the class.
return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
}
inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
if (LIKELY(!IsProxyMethod())) {
return this;
}
uint32_t method_index = GetDexMethodIndex();
uint32_t slot_idx = method_index % mirror::DexCache::kDexCacheMethodCacheSize;
mirror::MethodDexCachePair pair = mirror::DexCache::GetNativePairPtrSize(
GetDexCacheResolvedMethods(pointer_size), slot_idx, pointer_size);
ArtMethod* interface_method = pair.GetObjectForIndex(method_index);
if (LIKELY(interface_method != nullptr)) {
DCHECK_EQ(interface_method, Runtime::Current()->GetClassLinker()->FindMethodForProxy(this));
} else {
interface_method = Runtime::Current()->GetClassLinker()->FindMethodForProxy(this);
DCHECK(interface_method != nullptr);
}
return interface_method;
}
inline void ArtMethod::SetDexCacheResolvedMethods(mirror::MethodDexCacheType* new_dex_cache_methods,
PointerSize pointer_size) {
SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
new_dex_cache_methods,
pointer_size);
}
inline mirror::Class* ArtMethod::GetReturnType(bool resolve) {
DCHECK(!IsProxyMethod());
const DexFile* dex_file = GetDexFile();
const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
return GetClassFromTypeIndex(return_type_idx, resolve);
}
inline bool ArtMethod::HasSingleImplementation() {
if (IsFinal() || GetDeclaringClass()->IsFinal()) {
// We don't set kAccSingleImplementation for these cases since intrinsic
// can use the flag also.
return true;
}
return (GetAccessFlags() & kAccSingleImplementation) != 0;
}
inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
DCHECK(IsUint<8>(intrinsic));
// Currently we only do intrinsics for static/final methods or methods of final
// classes. We don't set kHasSingleImplementation for those methods.
DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
"Potential conflict with kAccSingleImplementation";
uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
kAccIntrinsic |
(intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
if (kIsDebugBuild) {
uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
bool is_constructor = IsConstructor();
bool is_synchronized = IsSynchronized();
bool skip_access_checks = SkipAccessChecks();
bool is_fast_native = IsFastNative();
bool is_copied = IsCopied();
bool is_miranda = IsMiranda();
bool is_default = IsDefault();
bool is_default_conflict = IsDefaultConflicting();
bool is_compilable = IsCompilable();
bool must_count_locks = MustCountLocks();
SetAccessFlags(new_value);
DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
DCHECK_EQ(is_constructor, IsConstructor());
DCHECK_EQ(is_synchronized, IsSynchronized());
DCHECK_EQ(skip_access_checks, SkipAccessChecks());
DCHECK_EQ(is_fast_native, IsFastNative());
DCHECK_EQ(is_copied, IsCopied());
DCHECK_EQ(is_miranda, IsMiranda());
DCHECK_EQ(is_default, IsDefault());
DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
DCHECK_EQ(is_compilable, IsCompilable());
DCHECK_EQ(must_count_locks, MustCountLocks());
} else {
SetAccessFlags(new_value);
}
}
template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
if (LIKELY(!declaring_class_.IsNull())) {
visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
if (UNLIKELY(klass->IsProxyClass())) {
// For normal methods, dex cache shortcuts will be visited through the declaring class.
// However, for proxies we need to keep the interface method alive, so we visit its roots.
ArtMethod* interface_method = GetInterfaceMethodIfProxy(pointer_size);
DCHECK(interface_method != nullptr);
interface_method->VisitRoots(visitor, pointer_size);
}
}
}
template <typename Visitor>
inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
PointerSize pointer_size) {
mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
mirror::Class* new_class = visitor(old_class);
if (old_class != new_class) {
SetDeclaringClass(new_class);
}
mirror::MethodDexCacheType* old_methods = GetDexCacheResolvedMethods(pointer_size);
mirror::MethodDexCacheType* new_methods = visitor(old_methods);
if (old_methods != new_methods) {
SetDexCacheResolvedMethods(new_methods, pointer_size);
}
}
template <ReadBarrierOption kReadBarrierOption, typename Visitor>
inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
if (IsNative<kReadBarrierOption>()) {
const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
const void* new_native_code = visitor(old_native_code);
if (old_native_code != new_native_code) {
SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
}
} else {
DCHECK(GetDataPtrSize(pointer_size) == nullptr);
}
const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
const void* new_code = visitor(old_code);
if (old_code != new_code) {
SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
}
}
} // namespace art
#endif // ART_RUNTIME_ART_METHOD_INL_H_
|