diff options
| -rw-r--r-- | runtime/mirror/class.cc | 76 | ||||
| -rw-r--r-- | runtime/mirror/class.h | 37 | ||||
| -rw-r--r-- | runtime/verifier/verifier_deps.cc | 5 |
3 files changed, 64 insertions, 54 deletions
diff --git a/runtime/mirror/class.cc b/runtime/mirror/class.cc index fcd3714b99..c3f7ad75cd 100644 --- a/runtime/mirror/class.cc +++ b/runtime/mirror/class.cc @@ -21,6 +21,7 @@ #include "art_field-inl.h" #include "art_method-inl.h" #include "base/logging.h" // For VLOG. +#include "base/stringpiece.h" #include "base/utils.h" #include "class-inl.h" #include "class_ext.h" @@ -389,14 +390,14 @@ void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { new_reference_offsets); } -bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { +bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) { size_t i = 0; size_t min_length = std::min(descriptor1.size(), descriptor2.size()); while (i < min_length && descriptor1[i] == descriptor2[i]) { ++i; } - if (descriptor1.find('/', i) != StringPiece::npos || - descriptor2.find('/', i) != StringPiece::npos) { + if (descriptor1.find('/', i) != std::string_view::npos || + descriptor2.find('/', i) != std::string_view::npos) { return false; } else { return true; @@ -435,7 +436,7 @@ bool Class::IsThrowableClass() { template <typename SignatureType> static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass, - const StringPiece& name, + std::string_view name, const SignatureType& signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_) { @@ -478,13 +479,15 @@ static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass, return nullptr; } -ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, - const StringPiece& signature, +ArtMethod* Class::FindInterfaceMethod(std::string_view name, + std::string_view signature, PointerSize pointer_size) { - return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); + // TODO: Change Signature::operator==() to accept std::string_view instead of StringPiece. + StringPiece sp_signature(signature.data(), signature.size()); + return FindInterfaceMethodWithSignature(this, name, sp_signature, pointer_size); } -ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, +ArtMethod* Class::FindInterfaceMethod(std::string_view name, const Signature& signature, PointerSize pointer_size) { return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); @@ -496,7 +499,7 @@ ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache, // We always search by name and signature, ignoring the type index in the MethodId. const DexFile& dex_file = *dex_cache->GetDexFile(); const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); - StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_); + std::string_view name = dex_file.StringDataByIdx(method_id.name_idx_); const Signature signature = dex_file.GetMethodSignature(method_id); return FindInterfaceMethod(name, signature, pointer_size); } @@ -537,7 +540,7 @@ static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass, template <typename SignatureType> static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass, - const StringPiece& name, + std::string_view name, const SignatureType& signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_) { @@ -591,13 +594,15 @@ static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass, } -ArtMethod* Class::FindClassMethod(const StringPiece& name, - const StringPiece& signature, +ArtMethod* Class::FindClassMethod(std::string_view name, + std::string_view signature, PointerSize pointer_size) { - return FindClassMethodWithSignature(this, name, signature, pointer_size); + // TODO: Change Signature::operator==() to accept std::string_view instead of StringPiece. + StringPiece sp_signature(signature.data(), signature.size()); + return FindClassMethodWithSignature(this, name, sp_signature, pointer_size); } -ArtMethod* Class::FindClassMethod(const StringPiece& name, +ArtMethod* Class::FindClassMethod(std::string_view name, const Signature& signature, PointerSize pointer_size) { return FindClassMethodWithSignature(this, name, signature, pointer_size); @@ -624,7 +629,7 @@ ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache, const DexFile& dex_file = *dex_cache->GetDexFile(); const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); const Signature signature = dex_file.GetMethodSignature(method_id); - StringPiece name; // Delay strlen() until actually needed. + std::string_view name; // Delay strlen() until actually needed. // If we do not have a dex_cache match, try to find the declared method in this class now. if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) { DCHECK(name.empty()); @@ -701,20 +706,21 @@ ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache, return uninherited_method; // Return the `uninherited_method` if any. } -ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) { +ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) { + // TODO: Change Signature::operator==() to accept std::string_view instead of StringPiece. + StringPiece sp_signature(signature.data(), signature.size()); // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy(). DCHECK(!IsProxyClass()); - StringPiece name("<init>"); + std::string_view name("<init>"); for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) { - if (method.GetName() == name && method.GetSignature() == signature) { + if (method.GetName() == name && method.GetSignature() == sp_signature) { return &method; } } return nullptr; } -ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name, - PointerSize pointer_size) { +ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) { for (auto& method : GetDirectMethods(pointer_size)) { ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); if (name == np_method->GetName()) { @@ -724,8 +730,7 @@ ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name, return nullptr; } -ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, - PointerSize pointer_size) { +ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) { for (auto& method : GetVirtualMethods(pointer_size)) { ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); if (name == np_method->GetName()) { @@ -813,8 +818,8 @@ ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) { // Custom binary search to avoid double comparisons from std::binary_search. static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, - const StringPiece& name, - const StringPiece& type) + std::string_view name, + std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_) { if (fields == nullptr) { return nullptr; @@ -827,9 +832,12 @@ static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, ArtField& field = fields->At(mid); // Fields are sorted by class, then name, then type descriptor. This is verified in dex file // verifier. There can be multiple fields with the same in the same class name due to proguard. - int result = StringPiece(field.GetName()).Compare(name); + // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as + // unsigned; for modified-UTF-8 without embedded nulls this is consistent with the + // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering. + int result = std::string_view(field.GetName()).compare(name); if (result == 0) { - result = StringPiece(field.GetTypeDescriptor()).Compare(type); + result = std::string_view(field.GetTypeDescriptor()).compare(type); } if (result < 0) { low = mid + 1; @@ -853,7 +861,7 @@ static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, return ret; } -ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { +ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) { // Binary search by name. Interfaces are not relevant because they can't contain instance fields. return FindFieldByNameAndType(GetIFieldsPtr(), name, type); } @@ -869,7 +877,7 @@ ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t return nullptr; } -ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { +ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) { // Is the field in this class, or any of its superclasses? // Interfaces are not relevant because they can't contain instance fields. for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { @@ -893,8 +901,8 @@ ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_fiel return nullptr; } -ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { - DCHECK(type != nullptr); +ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) { + DCHECK(!type.empty()); return FindFieldByNameAndType(GetSFieldsPtr(), name, type); } @@ -911,8 +919,8 @@ ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t de ArtField* Class::FindStaticField(Thread* self, ObjPtr<Class> klass, - const StringPiece& name, - const StringPiece& type) { + std::string_view name, + std::string_view type) { // Is the field in this class (or its interfaces), or any of its // superclasses (or their interfaces)? for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { @@ -962,8 +970,8 @@ ArtField* Class::FindStaticField(Thread* self, ArtField* Class::FindField(Thread* self, ObjPtr<Class> klass, - const StringPiece& name, - const StringPiece& type) { + std::string_view name, + std::string_view type) { // Find a field using the JLS field resolution order for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { // Is the field in this class? diff --git a/runtime/mirror/class.h b/runtime/mirror/class.h index c9c542d175..bde9b03c0f 100644 --- a/runtime/mirror/class.h +++ b/runtime/mirror/class.h @@ -17,6 +17,8 @@ #ifndef ART_RUNTIME_MIRROR_CLASS_H_ #define ART_RUNTIME_MIRROR_CLASS_H_ +#include <string_view> + #include "base/bit_utils.h" #include "base/casts.h" #include "base/stride_iterator.h" @@ -53,7 +55,6 @@ template <typename Iter> class IterationRange; template<typename T> class LengthPrefixedArray; enum class PointerSize : size_t; class Signature; -class StringPiece; template<size_t kNumReferences> class PACKED(4) StackHandleScope; class Thread; @@ -556,7 +557,7 @@ class MANAGED Class final : public Object { // Returns true if this class is in the same packages as that class. bool IsInSamePackage(ObjPtr<Class> that) REQUIRES_SHARED(Locks::mutator_lock_); - static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2); + static bool IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2); // Returns true if this class can access that class. bool CanAccess(ObjPtr<Class> that) REQUIRES_SHARED(Locks::mutator_lock_); @@ -861,12 +862,12 @@ class MANAGED Class final : public Object { // in an interface without superinterfaces, see JLS 9.2, can be inherited, see JLS 9.4.1). // TODO: Implement search for a unique maximally-specific non-abstract superinterface method. - ArtMethod* FindInterfaceMethod(const StringPiece& name, - const StringPiece& signature, + ArtMethod* FindInterfaceMethod(std::string_view name, + std::string_view signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); - ArtMethod* FindInterfaceMethod(const StringPiece& name, + ArtMethod* FindInterfaceMethod(std::string_view name, const Signature& signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); @@ -900,12 +901,12 @@ class MANAGED Class final : public Object { // does not satisfy the request. Special consideration should be given to the case where this // function returns a method that's not inherited (found in step 2, returned in step 4). - ArtMethod* FindClassMethod(const StringPiece& name, - const StringPiece& signature, + ArtMethod* FindClassMethod(std::string_view name, + std::string_view signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); - ArtMethod* FindClassMethod(const StringPiece& name, + ArtMethod* FindClassMethod(std::string_view name, const Signature& signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); @@ -915,13 +916,13 @@ class MANAGED Class final : public Object { PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); - ArtMethod* FindConstructor(const StringPiece& signature, PointerSize pointer_size) + ArtMethod* FindConstructor(std::string_view signature, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); - ArtMethod* FindDeclaredVirtualMethodByName(const StringPiece& name, PointerSize pointer_size) + ArtMethod* FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); - ArtMethod* FindDeclaredDirectMethodByName(const StringPiece& name, PointerSize pointer_size) + ArtMethod* FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); ArtMethod* FindClassInitializer(PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_); @@ -1036,12 +1037,12 @@ class MANAGED Class final : public Object { // Find a static or instance field using the JLS resolution order static ArtField* FindField(Thread* self, ObjPtr<Class> klass, - const StringPiece& name, - const StringPiece& type) + std::string_view name, + std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_); // Finds the given instance field in this class or a superclass. - ArtField* FindInstanceField(const StringPiece& name, const StringPiece& type) + ArtField* FindInstanceField(std::string_view name, std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_); // Finds the given instance field in this class or a superclass, only searches classes that @@ -1049,7 +1050,7 @@ class MANAGED Class final : public Object { ArtField* FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) REQUIRES_SHARED(Locks::mutator_lock_); - ArtField* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) + ArtField* FindDeclaredInstanceField(std::string_view name, std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_); ArtField* FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) @@ -1058,8 +1059,8 @@ class MANAGED Class final : public Object { // Finds the given static field in this class or a superclass. static ArtField* FindStaticField(Thread* self, ObjPtr<Class> klass, - const StringPiece& name, - const StringPiece& type) + std::string_view name, + std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_); // Finds the given static field in this class or superclass, only searches classes that @@ -1070,7 +1071,7 @@ class MANAGED Class final : public Object { uint32_t dex_field_idx) REQUIRES_SHARED(Locks::mutator_lock_); - ArtField* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) + ArtField* FindDeclaredStaticField(std::string_view name, std::string_view type) REQUIRES_SHARED(Locks::mutator_lock_); ArtField* FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) diff --git a/runtime/verifier/verifier_deps.cc b/runtime/verifier/verifier_deps.cc index bdcadd9fa6..b758159923 100644 --- a/runtime/verifier/verifier_deps.cc +++ b/runtime/verifier/verifier_deps.cc @@ -966,8 +966,9 @@ bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader, ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); for (const auto& entry : fields) { const dex::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex()); - StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); - StringPiece type(dex_file.StringDataByIdx(dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); + std::string_view name(dex_file.StringDataByIdx(field_id.name_idx_)); + std::string_view type( + dex_file.StringDataByIdx(dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); // Only use field_id.class_idx_ when the entry is unresolved, which is rare. // Otherwise, we might end up resolving an application class, which is expensive. std::string expected_decl_klass = entry.IsResolved() |