David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [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 | #include "verifier_deps.h" |
| 18 | |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 19 | #include <cstring> |
| 20 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 21 | #include "base/stl_util.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 22 | #include "compiler_callbacks.h" |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 23 | #include "leb128.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 25 | #include "obj_ptr-inl.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 26 | #include "runtime.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace verifier { |
| 30 | |
| 31 | VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 32 | for (const DexFile* dex_file : dex_files) { |
| 33 | DCHECK(GetDexFileDeps(*dex_file) == nullptr); |
| 34 | std::unique_ptr<DexFileDeps> deps(new DexFileDeps()); |
| 35 | dex_deps_.emplace(dex_file, std::move(deps)); |
| 36 | } |
| 37 | } |
| 38 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 39 | void VerifierDeps::MergeWith(const VerifierDeps& other, |
| 40 | const std::vector<const DexFile*>& dex_files) { |
| 41 | DCHECK(dex_deps_.size() == other.dex_deps_.size()); |
| 42 | for (const DexFile* dex_file : dex_files) { |
| 43 | DexFileDeps* my_deps = GetDexFileDeps(*dex_file); |
| 44 | const DexFileDeps& other_deps = *other.GetDexFileDeps(*dex_file); |
| 45 | // We currently collect extra strings only on the main `VerifierDeps`, |
| 46 | // which should be the one passed as `this` in this method. |
| 47 | DCHECK(other_deps.strings_.empty()); |
| 48 | MergeSets(my_deps->assignable_types_, other_deps.assignable_types_); |
| 49 | MergeSets(my_deps->unassignable_types_, other_deps.unassignable_types_); |
| 50 | MergeSets(my_deps->classes_, other_deps.classes_); |
| 51 | MergeSets(my_deps->fields_, other_deps.fields_); |
| 52 | MergeSets(my_deps->direct_methods_, other_deps.direct_methods_); |
| 53 | MergeSets(my_deps->virtual_methods_, other_deps.virtual_methods_); |
| 54 | MergeSets(my_deps->interface_methods_, other_deps.interface_methods_); |
| 55 | for (dex::TypeIndex entry : other_deps.unverified_classes_) { |
| 56 | my_deps->unverified_classes_.push_back(entry); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 61 | VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) { |
| 62 | auto it = dex_deps_.find(&dex_file); |
| 63 | return (it == dex_deps_.end()) ? nullptr : it->second.get(); |
| 64 | } |
| 65 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 66 | const VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) const { |
| 67 | auto it = dex_deps_.find(&dex_file); |
| 68 | return (it == dex_deps_.end()) ? nullptr : it->second.get(); |
| 69 | } |
| 70 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 71 | template <typename T> |
| 72 | uint16_t VerifierDeps::GetAccessFlags(T* element) { |
| 73 | static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant"); |
| 74 | if (element == nullptr) { |
| 75 | return VerifierDeps::kUnresolvedMarker; |
| 76 | } else { |
| 77 | uint16_t access_flags = Low16Bits(element->GetAccessFlags()); |
| 78 | CHECK_NE(access_flags, VerifierDeps::kUnresolvedMarker); |
| 79 | return access_flags; |
| 80 | } |
| 81 | } |
| 82 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 83 | dex::StringIndex VerifierDeps::GetClassDescriptorStringId(const DexFile& dex_file, |
| 84 | ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 85 | DCHECK(klass != nullptr); |
| 86 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 87 | // Array and proxy classes do not have a dex cache. |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 88 | if (!klass->IsArrayClass() && !klass->IsProxyClass()) { |
| 89 | DCHECK(dex_cache != nullptr) << klass->PrettyClass(); |
| 90 | if (dex_cache->GetDexFile() == &dex_file) { |
| 91 | // FindStringId is slow, try to go through the class def if we have one. |
| 92 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
| 93 | DCHECK(class_def != nullptr) << klass->PrettyClass(); |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 94 | const DexFile::TypeId& type_id = dex_file.GetTypeId(class_def->class_idx_); |
Mathieu Chartier | fc2dd61 | 2016-11-21 15:05:23 -0800 | [diff] [blame] | 95 | if (kIsDebugBuild) { |
| 96 | std::string temp; |
| 97 | CHECK_EQ(GetIdFromString(dex_file, klass->GetDescriptor(&temp)), type_id.descriptor_idx_); |
| 98 | } |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 99 | return type_id.descriptor_idx_; |
| 100 | } |
| 101 | } |
| 102 | std::string temp; |
| 103 | return GetIdFromString(dex_file, klass->GetDescriptor(&temp)); |
| 104 | } |
| 105 | |
| 106 | // Try to find the string descriptor of the class. type_idx is a best guess of a matching string id. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 107 | static dex::StringIndex TryGetClassDescriptorStringId(const DexFile& dex_file, |
| 108 | dex::TypeIndex type_idx, |
| 109 | ObjPtr<mirror::Class> klass) |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 110 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 111 | if (!klass->IsArrayClass()) { |
| 112 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
| 113 | const DexFile& klass_dex = klass->GetDexFile(); |
| 114 | const DexFile::TypeId& klass_type_id = klass_dex.GetTypeId(klass->GetClassDef()->class_idx_); |
| 115 | if (strcmp(dex_file.GetTypeDescriptor(type_id), |
| 116 | klass_dex.GetTypeDescriptor(klass_type_id)) == 0) { |
| 117 | return type_id.descriptor_idx_; |
| 118 | } |
| 119 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 120 | return dex::StringIndex::Invalid(); |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 123 | dex::StringIndex VerifierDeps::GetMethodDeclaringClassStringId(const DexFile& dex_file, |
| 124 | uint32_t dex_method_index, |
| 125 | ArtMethod* method) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 126 | static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant"); |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 127 | if (method == nullptr) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 128 | return dex::StringIndex(VerifierDeps::kUnresolvedMarker); |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 129 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 130 | const dex::StringIndex string_id = TryGetClassDescriptorStringId( |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 131 | dex_file, |
| 132 | dex_file.GetMethodId(dex_method_index).class_idx_, |
| 133 | method->GetDeclaringClass()); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 134 | if (string_id.IsValid()) { |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 135 | // Got lucky using the original dex file, return based on the input dex file. |
| 136 | DCHECK_EQ(GetClassDescriptorStringId(dex_file, method->GetDeclaringClass()), string_id); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 137 | return string_id; |
| 138 | } |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 139 | return GetClassDescriptorStringId(dex_file, method->GetDeclaringClass()); |
| 140 | } |
| 141 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 142 | dex::StringIndex VerifierDeps::GetFieldDeclaringClassStringId(const DexFile& dex_file, |
| 143 | uint32_t dex_field_idx, |
| 144 | ArtField* field) { |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 145 | static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant"); |
| 146 | if (field == nullptr) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 147 | return dex::StringIndex(VerifierDeps::kUnresolvedMarker); |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 148 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 149 | const dex::StringIndex string_id = TryGetClassDescriptorStringId( |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 150 | dex_file, |
| 151 | dex_file.GetFieldId(dex_field_idx).class_idx_, |
| 152 | field->GetDeclaringClass()); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 153 | if (string_id.IsValid()) { |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 154 | // Got lucky using the original dex file, return based on the input dex file. |
| 155 | DCHECK_EQ(GetClassDescriptorStringId(dex_file, field->GetDeclaringClass()), string_id); |
| 156 | return string_id; |
| 157 | } |
| 158 | return GetClassDescriptorStringId(dex_file, field->GetDeclaringClass()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 161 | static inline VerifierDeps* GetMainVerifierDeps() { |
| 162 | // The main VerifierDeps is the one set in the compiler callbacks, which at the |
| 163 | // end of verification will have all the per-thread VerifierDeps merged into it. |
| 164 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
| 165 | if (callbacks == nullptr) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | return callbacks->GetVerifierDeps(); |
| 169 | } |
| 170 | |
| 171 | static inline VerifierDeps* GetThreadLocalVerifierDeps() { |
| 172 | // During AOT, each thread has its own VerifierDeps, to avoid lock contention. At the end |
| 173 | // of full verification, these VerifierDeps will be merged into the main one. |
| 174 | if (!Runtime::Current()->IsAotCompiler()) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | return Thread::Current()->GetVerifierDeps(); |
| 178 | } |
| 179 | |
| 180 | static bool FindExistingStringId(const std::vector<std::string>& strings, |
| 181 | const std::string& str, |
| 182 | uint32_t* found_id) { |
| 183 | uint32_t num_extra_ids = strings.size(); |
| 184 | for (size_t i = 0; i < num_extra_ids; ++i) { |
| 185 | if (strings[i] == str) { |
| 186 | *found_id = i; |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 193 | dex::StringIndex VerifierDeps::GetIdFromString(const DexFile& dex_file, const std::string& str) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 194 | const DexFile::StringId* string_id = dex_file.FindStringId(str.c_str()); |
| 195 | if (string_id != nullptr) { |
| 196 | // String is in the DEX file. Return its ID. |
| 197 | return dex_file.GetIndexForStringId(*string_id); |
| 198 | } |
| 199 | |
| 200 | // String is not in the DEX file. Assign a new ID to it which is higher than |
| 201 | // the number of strings in the DEX file. |
| 202 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 203 | // We use the main `VerifierDeps` for adding new strings to simplify |
| 204 | // synchronization/merging of these entries between threads. |
| 205 | VerifierDeps* singleton = GetMainVerifierDeps(); |
| 206 | DexFileDeps* deps = singleton->GetDexFileDeps(dex_file); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 207 | DCHECK(deps != nullptr); |
| 208 | |
| 209 | uint32_t num_ids_in_dex = dex_file.NumStringIds(); |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 210 | uint32_t found_id; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 211 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 212 | { |
| 213 | ReaderMutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 214 | if (FindExistingStringId(deps->strings_, str, &found_id)) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 215 | return dex::StringIndex(num_ids_in_dex + found_id); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 216 | } |
| 217 | } |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 218 | { |
| 219 | WriterMutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 220 | if (FindExistingStringId(deps->strings_, str, &found_id)) { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 221 | return dex::StringIndex(num_ids_in_dex + found_id); |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 222 | } |
| 223 | deps->strings_.push_back(str); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 224 | dex::StringIndex new_id(num_ids_in_dex + deps->strings_.size() - 1); |
| 225 | CHECK_GE(new_id.index_, num_ids_in_dex); // check for overflows |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 226 | DCHECK_EQ(str, singleton->GetStringFromId(dex_file, new_id)); |
| 227 | return new_id; |
| 228 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 231 | std::string VerifierDeps::GetStringFromId(const DexFile& dex_file, dex::StringIndex string_id) |
| 232 | const { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 233 | uint32_t num_ids_in_dex = dex_file.NumStringIds(); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 234 | if (string_id.index_ < num_ids_in_dex) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 235 | return std::string(dex_file.StringDataByIdx(string_id)); |
| 236 | } else { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 237 | const DexFileDeps* deps = GetDexFileDeps(dex_file); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 238 | DCHECK(deps != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 239 | string_id.index_ -= num_ids_in_dex; |
| 240 | CHECK_LT(string_id.index_, deps->strings_.size()); |
| 241 | return deps->strings_[string_id.index_]; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 245 | bool VerifierDeps::IsInClassPath(ObjPtr<mirror::Class> klass) const { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 246 | DCHECK(klass != nullptr); |
| 247 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 248 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 249 | if (dex_cache == nullptr) { |
| 250 | // This is a synthesized class, in this case always an array. They are not |
| 251 | // defined in the compiled DEX files and therefore are part of the classpath. |
| 252 | // We could avoid recording dependencies on arrays with component types in |
| 253 | // the compiled DEX files but we choose to record them anyway so as to |
| 254 | // record the access flags VM sets for array classes. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 255 | DCHECK(klass->IsArrayClass()) << klass->PrettyDescriptor(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
| 259 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 260 | DCHECK(dex_file != nullptr); |
| 261 | |
| 262 | // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex |
| 263 | // file was not registered as being compiled and we assume `klass` is in the |
| 264 | // classpath. |
| 265 | return (GetDexFileDeps(*dex_file) == nullptr); |
| 266 | } |
| 267 | |
| 268 | void VerifierDeps::AddClassResolution(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 269 | dex::TypeIndex type_idx, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 270 | mirror::Class* klass) { |
| 271 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 272 | if (dex_deps == nullptr) { |
| 273 | // This invocation is from verification of a dex file which is not being compiled. |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (klass != nullptr && !IsInClassPath(klass)) { |
| 278 | // Class resolved into one of the DEX files which are being compiled. |
| 279 | // This is not a classpath dependency. |
| 280 | return; |
| 281 | } |
| 282 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 283 | dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass))); |
| 284 | } |
| 285 | |
| 286 | void VerifierDeps::AddFieldResolution(const DexFile& dex_file, |
| 287 | uint32_t field_idx, |
| 288 | ArtField* field) { |
| 289 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 290 | if (dex_deps == nullptr) { |
| 291 | // This invocation is from verification of a dex file which is not being compiled. |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) { |
| 296 | // Field resolved into one of the DEX files which are being compiled. |
| 297 | // This is not a classpath dependency. |
| 298 | return; |
| 299 | } |
| 300 | |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 301 | dex_deps->fields_.emplace(FieldResolution(field_idx, |
| 302 | GetAccessFlags(field), |
| 303 | GetFieldDeclaringClassStringId(dex_file, |
| 304 | field_idx, |
| 305 | field))); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void VerifierDeps::AddMethodResolution(const DexFile& dex_file, |
| 309 | uint32_t method_idx, |
| 310 | MethodResolutionKind resolution_kind, |
| 311 | ArtMethod* method) { |
| 312 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 313 | if (dex_deps == nullptr) { |
| 314 | // This invocation is from verification of a dex file which is not being compiled. |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) { |
| 319 | // Method resolved into one of the DEX files which are being compiled. |
| 320 | // This is not a classpath dependency. |
| 321 | return; |
| 322 | } |
| 323 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 324 | MethodResolution method_tuple(method_idx, |
| 325 | GetAccessFlags(method), |
Mathieu Chartier | 32b5030 | 2016-11-17 13:08:35 -0800 | [diff] [blame] | 326 | GetMethodDeclaringClassStringId(dex_file, method_idx, method)); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 327 | if (resolution_kind == kDirectMethodResolution) { |
| 328 | dex_deps->direct_methods_.emplace(method_tuple); |
| 329 | } else if (resolution_kind == kVirtualMethodResolution) { |
| 330 | dex_deps->virtual_methods_.emplace(method_tuple); |
| 331 | } else { |
| 332 | DCHECK_EQ(resolution_kind, kInterfaceMethodResolution); |
| 333 | dex_deps->interface_methods_.emplace(method_tuple); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void VerifierDeps::AddAssignability(const DexFile& dex_file, |
| 338 | mirror::Class* destination, |
| 339 | mirror::Class* source, |
| 340 | bool is_strict, |
| 341 | bool is_assignable) { |
| 342 | // Test that the method is only called on reference types. |
| 343 | // Note that concurrent verification of `destination` and `source` may have |
| 344 | // set their status to erroneous. However, the tests performed below rely |
| 345 | // merely on no issues with linking (valid access flags, superclass and |
| 346 | // implemented interfaces). If the class at any point reached the IsResolved |
| 347 | // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass. |
| 348 | DCHECK(destination != nullptr && !destination->IsPrimitive()); |
| 349 | DCHECK(source != nullptr && !source->IsPrimitive()); |
| 350 | |
| 351 | if (destination == source || |
| 352 | destination->IsObjectClass() || |
| 353 | (!is_strict && destination->IsInterface())) { |
| 354 | // Cases when `destination` is trivially assignable from `source`. |
| 355 | DCHECK(is_assignable); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source)); |
| 360 | |
| 361 | if (destination->IsArrayClass() && source->IsArrayClass()) { |
| 362 | // Both types are arrays. Break down to component types and add recursively. |
| 363 | // This helps filter out destinations from compiled DEX files (see below) |
| 364 | // and deduplicate entries with the same canonical component type. |
| 365 | mirror::Class* destination_component = destination->GetComponentType(); |
| 366 | mirror::Class* source_component = source->GetComponentType(); |
| 367 | |
| 368 | // Only perform the optimization if both types are resolved which guarantees |
| 369 | // that they linked successfully, as required at the top of this method. |
| 370 | if (destination_component->IsResolved() && source_component->IsResolved()) { |
| 371 | AddAssignability(dex_file, |
| 372 | destination_component, |
| 373 | source_component, |
| 374 | /* is_strict */ true, |
| 375 | is_assignable); |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 381 | if (dex_deps == nullptr) { |
| 382 | // This invocation is from verification of a DEX file which is not being compiled. |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | if (!IsInClassPath(destination) && !IsInClassPath(source)) { |
| 387 | // Both `destination` and `source` are defined in the compiled DEX files. |
| 388 | // No need to record a dependency. |
| 389 | return; |
| 390 | } |
| 391 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 392 | // Get string IDs for both descriptors and store in the appropriate set. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 393 | dex::StringIndex destination_id = GetClassDescriptorStringId(dex_file, destination); |
| 394 | dex::StringIndex source_id = GetClassDescriptorStringId(dex_file, source); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 395 | |
| 396 | if (is_assignable) { |
| 397 | dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 398 | } else { |
| 399 | dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 400 | } |
| 401 | } |
| 402 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 403 | void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 404 | dex::TypeIndex type_idx, |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 405 | MethodVerifier::FailureKind failure_kind) { |
| 406 | if (failure_kind == MethodVerifier::kNoFailure) { |
| 407 | // We only record classes that did not fully verify at compile time. |
| 408 | return; |
| 409 | } |
| 410 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 411 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 412 | if (thread_deps != nullptr) { |
| 413 | DexFileDeps* dex_deps = thread_deps->GetDexFileDeps(dex_file); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 414 | dex_deps->unverified_classes_.push_back(type_idx); |
| 415 | } |
| 416 | } |
| 417 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 418 | void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 419 | dex::TypeIndex type_idx, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 420 | mirror::Class* klass) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 421 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 422 | if (thread_deps != nullptr) { |
| 423 | thread_deps->AddClassResolution(dex_file, type_idx, klass); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file, |
| 428 | uint32_t field_idx, |
| 429 | ArtField* field) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 430 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 431 | if (thread_deps != nullptr) { |
| 432 | thread_deps->AddFieldResolution(dex_file, field_idx, field); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file, |
| 437 | uint32_t method_idx, |
| 438 | MethodResolutionKind resolution_kind, |
| 439 | ArtMethod* method) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 440 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 441 | if (thread_deps != nullptr) { |
| 442 | thread_deps->AddMethodResolution(dex_file, method_idx, resolution_kind, method); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file, |
| 447 | mirror::Class* destination, |
| 448 | mirror::Class* source, |
| 449 | bool is_strict, |
| 450 | bool is_assignable) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 451 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 452 | if (thread_deps != nullptr) { |
| 453 | thread_deps->AddAssignability(dex_file, destination, source, is_strict, is_assignable); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 457 | namespace { |
| 458 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 459 | static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) { |
| 460 | CHECK_LT(*in, end); |
| 461 | return DecodeUnsignedLeb128(in); |
| 462 | } |
| 463 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 464 | template<typename T> inline uint32_t Encode(T in); |
| 465 | |
| 466 | template<> inline uint32_t Encode<uint16_t>(uint16_t in) { |
| 467 | return in; |
| 468 | } |
| 469 | template<> inline uint32_t Encode<uint32_t>(uint32_t in) { |
| 470 | return in; |
| 471 | } |
| 472 | template<> inline uint32_t Encode<dex::TypeIndex>(dex::TypeIndex in) { |
| 473 | return in.index_; |
| 474 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 475 | template<> inline uint32_t Encode<dex::StringIndex>(dex::StringIndex in) { |
| 476 | return in.index_; |
| 477 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 478 | |
| 479 | template<typename T> inline T Decode(uint32_t in); |
| 480 | |
| 481 | template<> inline uint16_t Decode<uint16_t>(uint32_t in) { |
| 482 | return dchecked_integral_cast<uint16_t>(in); |
| 483 | } |
| 484 | template<> inline uint32_t Decode<uint32_t>(uint32_t in) { |
| 485 | return in; |
| 486 | } |
| 487 | template<> inline dex::TypeIndex Decode<dex::TypeIndex>(uint32_t in) { |
| 488 | return dex::TypeIndex(in); |
| 489 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 490 | template<> inline dex::StringIndex Decode<dex::StringIndex>(uint32_t in) { |
| 491 | return dex::StringIndex(in); |
| 492 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 493 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 494 | template<typename T1, typename T2> |
| 495 | static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 496 | EncodeUnsignedLeb128(out, Encode(std::get<0>(t))); |
| 497 | EncodeUnsignedLeb128(out, Encode(std::get<1>(t))); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | template<typename T1, typename T2> |
| 501 | static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 502 | T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 503 | T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end)); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 504 | *t = std::make_tuple(v1, v2); |
| 505 | } |
| 506 | |
| 507 | template<typename T1, typename T2, typename T3> |
| 508 | static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 509 | EncodeUnsignedLeb128(out, Encode(std::get<0>(t))); |
| 510 | EncodeUnsignedLeb128(out, Encode(std::get<1>(t))); |
| 511 | EncodeUnsignedLeb128(out, Encode(std::get<2>(t))); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | template<typename T1, typename T2, typename T3> |
| 515 | static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 516 | T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 517 | T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end)); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame^] | 518 | T3 v3 = Decode<T3>(DecodeUint32WithOverflowCheck(in, end)); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 519 | *t = std::make_tuple(v1, v2, v3); |
| 520 | } |
| 521 | |
| 522 | template<typename T> |
| 523 | static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) { |
| 524 | EncodeUnsignedLeb128(out, set.size()); |
| 525 | for (const T& entry : set) { |
| 526 | EncodeTuple(out, entry); |
| 527 | } |
| 528 | } |
| 529 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 530 | template <typename T> |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 531 | static inline void EncodeUint16Vector(std::vector<uint8_t>* out, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 532 | const std::vector<T>& vector) { |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 533 | EncodeUnsignedLeb128(out, vector.size()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 534 | for (const T& entry : vector) { |
| 535 | EncodeUnsignedLeb128(out, Encode(entry)); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 539 | template<typename T> |
| 540 | static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) { |
| 541 | DCHECK(set->empty()); |
| 542 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 543 | for (size_t i = 0; i < num_entries; ++i) { |
| 544 | T tuple; |
| 545 | DecodeTuple(in, end, &tuple); |
| 546 | set->emplace(tuple); |
| 547 | } |
| 548 | } |
| 549 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 550 | template<typename T> |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 551 | static inline void DecodeUint16Vector(const uint8_t** in, |
| 552 | const uint8_t* end, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 553 | std::vector<T>* vector) { |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 554 | DCHECK(vector->empty()); |
| 555 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 556 | vector->reserve(num_entries); |
| 557 | for (size_t i = 0; i < num_entries; ++i) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 558 | vector->push_back( |
| 559 | Decode<T>(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end)))); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 563 | static inline void EncodeStringVector(std::vector<uint8_t>* out, |
| 564 | const std::vector<std::string>& strings) { |
| 565 | EncodeUnsignedLeb128(out, strings.size()); |
| 566 | for (const std::string& str : strings) { |
| 567 | const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str()); |
| 568 | size_t length = str.length() + 1; |
| 569 | out->insert(out->end(), data, data + length); |
| 570 | DCHECK_EQ(0u, out->back()); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static inline void DecodeStringVector(const uint8_t** in, |
| 575 | const uint8_t* end, |
| 576 | std::vector<std::string>* strings) { |
| 577 | DCHECK(strings->empty()); |
| 578 | size_t num_strings = DecodeUint32WithOverflowCheck(in, end); |
| 579 | strings->reserve(num_strings); |
| 580 | for (size_t i = 0; i < num_strings; ++i) { |
| 581 | CHECK_LT(*in, end); |
| 582 | const char* string_start = reinterpret_cast<const char*>(*in); |
| 583 | strings->emplace_back(std::string(string_start)); |
| 584 | *in += strings->back().length() + 1; |
| 585 | } |
| 586 | } |
| 587 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 588 | } // namespace |
| 589 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 590 | void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files, |
| 591 | std::vector<uint8_t>* buffer) const { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 592 | for (const DexFile* dex_file : dex_files) { |
| 593 | const DexFileDeps& deps = *GetDexFileDeps(*dex_file); |
| 594 | EncodeStringVector(buffer, deps.strings_); |
| 595 | EncodeSet(buffer, deps.assignable_types_); |
| 596 | EncodeSet(buffer, deps.unassignable_types_); |
| 597 | EncodeSet(buffer, deps.classes_); |
| 598 | EncodeSet(buffer, deps.fields_); |
| 599 | EncodeSet(buffer, deps.direct_methods_); |
| 600 | EncodeSet(buffer, deps.virtual_methods_); |
| 601 | EncodeSet(buffer, deps.interface_methods_); |
| 602 | EncodeUint16Vector(buffer, deps.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 606 | VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files, |
| 607 | ArrayRef<const uint8_t> data) |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 608 | : VerifierDeps(dex_files) { |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 609 | if (data.empty()) { |
| 610 | // Return eagerly, as the first thing we expect from VerifierDeps data is |
| 611 | // the number of created strings, even if there is no dependency. |
| 612 | // Currently, only the boot image does not have any VerifierDeps data. |
| 613 | return; |
| 614 | } |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 615 | const uint8_t* data_start = data.data(); |
| 616 | const uint8_t* data_end = data_start + data.size(); |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 617 | for (const DexFile* dex_file : dex_files) { |
| 618 | DexFileDeps* deps = GetDexFileDeps(*dex_file); |
| 619 | DecodeStringVector(&data_start, data_end, &deps->strings_); |
| 620 | DecodeSet(&data_start, data_end, &deps->assignable_types_); |
| 621 | DecodeSet(&data_start, data_end, &deps->unassignable_types_); |
| 622 | DecodeSet(&data_start, data_end, &deps->classes_); |
| 623 | DecodeSet(&data_start, data_end, &deps->fields_); |
| 624 | DecodeSet(&data_start, data_end, &deps->direct_methods_); |
| 625 | DecodeSet(&data_start, data_end, &deps->virtual_methods_); |
| 626 | DecodeSet(&data_start, data_end, &deps->interface_methods_); |
| 627 | DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 628 | } |
| 629 | CHECK_LE(data_start, data_end); |
| 630 | } |
| 631 | |
| 632 | bool VerifierDeps::Equals(const VerifierDeps& rhs) const { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 633 | if (dex_deps_.size() != rhs.dex_deps_.size()) { |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | auto lhs_it = dex_deps_.begin(); |
| 638 | auto rhs_it = rhs.dex_deps_.begin(); |
| 639 | |
| 640 | for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) { |
| 641 | const DexFile* lhs_dex_file = lhs_it->first; |
| 642 | const DexFile* rhs_dex_file = rhs_it->first; |
| 643 | if (lhs_dex_file != rhs_dex_file) { |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | DexFileDeps* lhs_deps = lhs_it->second.get(); |
| 648 | DexFileDeps* rhs_deps = rhs_it->second.get(); |
| 649 | if (!lhs_deps->Equals(*rhs_deps)) { |
| 650 | return false; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end())); |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const { |
| 659 | return (strings_ == rhs.strings_) && |
| 660 | (assignable_types_ == rhs.assignable_types_) && |
| 661 | (unassignable_types_ == rhs.unassignable_types_) && |
| 662 | (classes_ == rhs.classes_) && |
| 663 | (fields_ == rhs.fields_) && |
| 664 | (direct_methods_ == rhs.direct_methods_) && |
| 665 | (virtual_methods_ == rhs.virtual_methods_) && |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 666 | (interface_methods_ == rhs.interface_methods_) && |
| 667 | (unverified_classes_ == rhs.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 668 | } |
| 669 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 670 | void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const { |
| 671 | for (const auto& dep : dex_deps_) { |
| 672 | const DexFile& dex_file = *dep.first; |
| 673 | vios->Stream() |
| 674 | << "Dependencies of " |
| 675 | << dex_file.GetLocation() |
| 676 | << ":\n"; |
| 677 | |
| 678 | ScopedIndentation indent(vios); |
| 679 | |
| 680 | for (const std::string& str : dep.second->strings_) { |
| 681 | vios->Stream() << "Extra string: " << str << "\n"; |
| 682 | } |
| 683 | |
| 684 | for (const TypeAssignability& entry : dep.second->assignable_types_) { |
| 685 | vios->Stream() |
| 686 | << GetStringFromId(dex_file, entry.GetSource()) |
| 687 | << " must be assignable to " |
| 688 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 689 | << "\n"; |
| 690 | } |
| 691 | |
| 692 | for (const TypeAssignability& entry : dep.second->unassignable_types_) { |
| 693 | vios->Stream() |
| 694 | << GetStringFromId(dex_file, entry.GetSource()) |
| 695 | << " must not be assignable to " |
| 696 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 697 | << "\n"; |
| 698 | } |
| 699 | |
| 700 | for (const ClassResolution& entry : dep.second->classes_) { |
| 701 | vios->Stream() |
| 702 | << dex_file.StringByTypeIdx(entry.GetDexTypeIndex()) |
| 703 | << (entry.IsResolved() ? " must be resolved " : "must not be resolved ") |
| 704 | << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 705 | << "\n"; |
| 706 | } |
| 707 | |
| 708 | for (const FieldResolution& entry : dep.second->fields_) { |
| 709 | const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex()); |
| 710 | vios->Stream() |
| 711 | << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->" |
| 712 | << dex_file.GetFieldName(field_id) << ":" |
| 713 | << dex_file.GetFieldTypeDescriptor(field_id) |
| 714 | << " is expected to be "; |
| 715 | if (!entry.IsResolved()) { |
| 716 | vios->Stream() << "unresolved\n"; |
| 717 | } else { |
| 718 | vios->Stream() |
| 719 | << "in class " |
| 720 | << GetStringFromId(dex_file, entry.GetDeclaringClassIndex()) |
| 721 | << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 722 | << "\n"; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | for (const auto& entry : |
| 727 | { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_), |
| 728 | std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_), |
| 729 | std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) { |
| 730 | for (const MethodResolution& method : entry.second) { |
| 731 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); |
| 732 | vios->Stream() |
| 733 | << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->" |
| 734 | << dex_file.GetMethodName(method_id) |
| 735 | << dex_file.GetMethodSignature(method_id).ToString() |
| 736 | << " is expected to be "; |
| 737 | if (!method.IsResolved()) { |
| 738 | vios->Stream() << "unresolved\n"; |
| 739 | } else { |
| 740 | vios->Stream() |
| 741 | << "in class " |
| 742 | << GetStringFromId(dex_file, method.GetDeclaringClassIndex()) |
| 743 | << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec |
| 744 | << ", and be of kind " << entry.first |
| 745 | << "\n"; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 750 | for (dex::TypeIndex type_index : dep.second->unverified_classes_) { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 751 | vios->Stream() |
| 752 | << dex_file.StringByTypeIdx(type_index) |
| 753 | << " is expected to be verified at runtime\n"; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
Nicolas Geoffray | 6bb7f1b | 2016-11-03 10:52:49 +0000 | [diff] [blame] | 758 | bool VerifierDeps::ValidateDependencies(Handle<mirror::ClassLoader> class_loader, |
| 759 | Thread* self) const { |
Nicolas Geoffray | 8904b6f | 2016-10-28 19:50:34 +0100 | [diff] [blame] | 760 | for (const auto& entry : dex_deps_) { |
| 761 | if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) { |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | return true; |
| 766 | } |
| 767 | |
| 768 | // TODO: share that helper with other parts of the compiler that have |
| 769 | // the same lookup pattern. |
| 770 | static mirror::Class* FindClassAndClearException(ClassLinker* class_linker, |
| 771 | Thread* self, |
| 772 | const char* name, |
| 773 | Handle<mirror::ClassLoader> class_loader) |
| 774 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 775 | mirror::Class* result = class_linker->FindClass(self, name, class_loader); |
| 776 | if (result == nullptr) { |
| 777 | DCHECK(self->IsExceptionPending()); |
| 778 | self->ClearException(); |
| 779 | } |
| 780 | return result; |
| 781 | } |
| 782 | |
| 783 | bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader, |
| 784 | const DexFile& dex_file, |
| 785 | const std::set<TypeAssignability>& assignables, |
| 786 | bool expected_assignability, |
| 787 | Thread* self) const { |
| 788 | StackHandleScope<2> hs(self); |
| 789 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 790 | MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr)); |
| 791 | MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr)); |
| 792 | |
| 793 | for (const auto& entry : assignables) { |
| 794 | const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination()); |
| 795 | destination.Assign( |
| 796 | FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader)); |
| 797 | const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource()); |
| 798 | source.Assign( |
| 799 | FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader)); |
| 800 | |
| 801 | if (destination.Get() == nullptr) { |
| 802 | LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc; |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | if (source.Get() == nullptr) { |
| 807 | LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc; |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | DCHECK(destination->IsResolved() && source->IsResolved()); |
| 812 | if (destination->IsAssignableFrom(source.Get()) != expected_assignability) { |
| 813 | LOG(INFO) << "VerifierDeps: Class " |
| 814 | << destination_desc |
| 815 | << (expected_assignability ? " not " : " ") |
| 816 | << "assignable from " |
| 817 | << source_desc; |
| 818 | return false; |
| 819 | } |
| 820 | } |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader, |
| 825 | const DexFile& dex_file, |
| 826 | const std::set<ClassResolution>& classes, |
| 827 | Thread* self) const { |
| 828 | StackHandleScope<1> hs(self); |
| 829 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 830 | MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr)); |
| 831 | for (const auto& entry : classes) { |
| 832 | const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex()); |
| 833 | cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader)); |
| 834 | |
| 835 | if (entry.IsResolved()) { |
| 836 | if (cls.Get() == nullptr) { |
| 837 | LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor; |
| 838 | return false; |
| 839 | } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) { |
| 840 | LOG(INFO) << "VerifierDeps: Unexpected access flags on class " |
| 841 | << descriptor |
| 842 | << std::hex |
| 843 | << " (expected=" |
| 844 | << entry.GetAccessFlags() |
| 845 | << ", actual=" |
| 846 | << GetAccessFlags(cls.Get()) << ")" |
| 847 | << std::dec; |
| 848 | return false; |
| 849 | } |
| 850 | } else if (cls.Get() != nullptr) { |
| 851 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor; |
| 852 | return false; |
| 853 | } |
| 854 | } |
| 855 | return true; |
| 856 | } |
| 857 | |
| 858 | static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) { |
| 859 | const DexFile::FieldId& field_id = dex_file.GetFieldId(index); |
| 860 | return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id)) |
| 861 | + "->" |
| 862 | + dex_file.GetFieldName(field_id) |
| 863 | + ":" |
| 864 | + dex_file.GetFieldTypeDescriptor(field_id); |
| 865 | } |
| 866 | |
| 867 | bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader, |
| 868 | const DexFile& dex_file, |
| 869 | const std::set<FieldResolution>& fields, |
| 870 | Thread* self) const { |
| 871 | // Check recorded fields are resolved the same way, have the same recorded class, |
| 872 | // and have the same recorded flags. |
| 873 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 874 | StackHandleScope<1> hs(self); |
| 875 | Handle<mirror::DexCache> dex_cache( |
| 876 | hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false))); |
| 877 | for (const auto& entry : fields) { |
| 878 | ArtField* field = class_linker->ResolveFieldJLS( |
| 879 | dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader); |
| 880 | |
| 881 | if (field == nullptr) { |
| 882 | DCHECK(self->IsExceptionPending()); |
| 883 | self->ClearException(); |
| 884 | } |
| 885 | |
| 886 | if (entry.IsResolved()) { |
| 887 | std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex()); |
| 888 | std::string temp; |
| 889 | if (field == nullptr) { |
| 890 | LOG(INFO) << "VerifierDeps: Could not resolve field " |
| 891 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()); |
| 892 | return false; |
| 893 | } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) { |
| 894 | LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution " |
| 895 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()) |
| 896 | << " (expected=" << expected_decl_klass |
| 897 | << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")"; |
| 898 | return false; |
| 899 | } else if (entry.GetAccessFlags() != GetAccessFlags(field)) { |
| 900 | LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field " |
| 901 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()) |
| 902 | << std::hex << " (expected=" << entry.GetAccessFlags() |
| 903 | << ", actual=" << GetAccessFlags(field) << ")" << std::dec; |
| 904 | return false; |
| 905 | } |
| 906 | } else if (field != nullptr) { |
| 907 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field " |
| 908 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()); |
| 909 | return false; |
| 910 | } |
| 911 | } |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) { |
| 916 | const DexFile::MethodId& method_id = dex_file.GetMethodId(index); |
| 917 | return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id)) |
| 918 | + "->" |
| 919 | + dex_file.GetMethodName(method_id) |
| 920 | + dex_file.GetMethodSignature(method_id).ToString(); |
| 921 | } |
| 922 | |
| 923 | bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader, |
| 924 | const DexFile& dex_file, |
| 925 | const std::set<MethodResolution>& methods, |
| 926 | MethodResolutionKind kind, |
| 927 | Thread* self) const { |
| 928 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 929 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
| 930 | |
| 931 | for (const auto& entry : methods) { |
| 932 | const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex()); |
| 933 | |
| 934 | const char* name = dex_file.GetMethodName(method_id); |
| 935 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 936 | const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id); |
| 937 | |
| 938 | mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader); |
| 939 | if (cls == nullptr) { |
| 940 | LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor; |
| 941 | return false; |
| 942 | } |
| 943 | DCHECK(cls->IsResolved()); |
| 944 | ArtMethod* method = nullptr; |
| 945 | if (kind == kDirectMethodResolution) { |
| 946 | method = cls->FindDirectMethod(name, signature, pointer_size); |
| 947 | } else if (kind == kVirtualMethodResolution) { |
| 948 | method = cls->FindVirtualMethod(name, signature, pointer_size); |
| 949 | } else { |
| 950 | DCHECK_EQ(kind, kInterfaceMethodResolution); |
| 951 | method = cls->FindInterfaceMethod(name, signature, pointer_size); |
| 952 | } |
| 953 | |
| 954 | if (entry.IsResolved()) { |
| 955 | std::string temp; |
| 956 | std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex()); |
| 957 | if (method == nullptr) { |
| 958 | LOG(INFO) << "VerifierDeps: Could not resolve " |
| 959 | << kind |
| 960 | << " method " |
| 961 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()); |
| 962 | return false; |
| 963 | } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) { |
| 964 | LOG(INFO) << "VerifierDeps: Unexpected declaring class for " |
| 965 | << kind |
| 966 | << " method resolution " |
| 967 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()) |
| 968 | << " (expected=" |
| 969 | << expected_decl_klass |
| 970 | << ", actual=" |
| 971 | << method->GetDeclaringClass()->GetDescriptor(&temp) |
| 972 | << ")"; |
| 973 | return false; |
| 974 | } else if (entry.GetAccessFlags() != GetAccessFlags(method)) { |
| 975 | LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved " |
| 976 | << kind |
| 977 | << " method resolution " |
| 978 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()) |
| 979 | << std::hex |
| 980 | << " (expected=" |
| 981 | << entry.GetAccessFlags() |
| 982 | << ", actual=" |
| 983 | << GetAccessFlags(method) << ")" |
| 984 | << std::dec; |
| 985 | return false; |
| 986 | } |
| 987 | } else if (method != nullptr) { |
| 988 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of " |
| 989 | << kind |
| 990 | << " method " |
| 991 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()); |
| 992 | return false; |
| 993 | } |
| 994 | } |
| 995 | return true; |
| 996 | } |
| 997 | |
| 998 | bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader, |
| 999 | const DexFile& dex_file, |
| 1000 | const DexFileDeps& deps, |
| 1001 | Thread* self) const { |
| 1002 | bool result = VerifyAssignability( |
| 1003 | class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self); |
| 1004 | result = result && VerifyAssignability( |
| 1005 | class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self); |
| 1006 | |
| 1007 | result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self); |
| 1008 | result = result && VerifyFields(class_loader, dex_file, deps.fields_, self); |
| 1009 | |
| 1010 | result = result && VerifyMethods( |
| 1011 | class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self); |
| 1012 | result = result && VerifyMethods( |
| 1013 | class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self); |
| 1014 | result = result && VerifyMethods( |
| 1015 | class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self); |
| 1016 | |
| 1017 | return result; |
| 1018 | } |
| 1019 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 1020 | } // namespace verifier |
| 1021 | } // namespace art |