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. |
Nicolas Geoffray | d1665a0 | 2016-12-12 13:07:07 +0000 | [diff] [blame^] | 348 | DCHECK(destination != nullptr); |
| 349 | DCHECK(source != nullptr); |
| 350 | |
| 351 | if (destination->IsPrimitive() || source->IsPrimitive()) { |
| 352 | // Primitive types are trivially non-assignable to anything else. |
| 353 | // We do not need to record trivial assignability, as it will |
| 354 | // not change across releases. |
| 355 | return; |
| 356 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 357 | |
| 358 | if (destination == source || |
| 359 | destination->IsObjectClass() || |
| 360 | (!is_strict && destination->IsInterface())) { |
| 361 | // Cases when `destination` is trivially assignable from `source`. |
| 362 | DCHECK(is_assignable); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source)); |
| 367 | |
| 368 | if (destination->IsArrayClass() && source->IsArrayClass()) { |
| 369 | // Both types are arrays. Break down to component types and add recursively. |
| 370 | // This helps filter out destinations from compiled DEX files (see below) |
| 371 | // and deduplicate entries with the same canonical component type. |
| 372 | mirror::Class* destination_component = destination->GetComponentType(); |
| 373 | mirror::Class* source_component = source->GetComponentType(); |
| 374 | |
| 375 | // Only perform the optimization if both types are resolved which guarantees |
| 376 | // that they linked successfully, as required at the top of this method. |
| 377 | if (destination_component->IsResolved() && source_component->IsResolved()) { |
| 378 | AddAssignability(dex_file, |
| 379 | destination_component, |
| 380 | source_component, |
| 381 | /* is_strict */ true, |
| 382 | is_assignable); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 388 | if (dex_deps == nullptr) { |
| 389 | // This invocation is from verification of a DEX file which is not being compiled. |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | if (!IsInClassPath(destination) && !IsInClassPath(source)) { |
| 394 | // Both `destination` and `source` are defined in the compiled DEX files. |
| 395 | // No need to record a dependency. |
| 396 | return; |
| 397 | } |
| 398 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 399 | // Get string IDs for both descriptors and store in the appropriate set. |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 400 | dex::StringIndex destination_id = GetClassDescriptorStringId(dex_file, destination); |
| 401 | dex::StringIndex source_id = GetClassDescriptorStringId(dex_file, source); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 402 | |
| 403 | if (is_assignable) { |
| 404 | dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 405 | } else { |
| 406 | dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 407 | } |
| 408 | } |
| 409 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 410 | void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 411 | dex::TypeIndex type_idx, |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 412 | MethodVerifier::FailureKind failure_kind) { |
| 413 | if (failure_kind == MethodVerifier::kNoFailure) { |
| 414 | // We only record classes that did not fully verify at compile time. |
| 415 | return; |
| 416 | } |
| 417 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 418 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 419 | if (thread_deps != nullptr) { |
| 420 | DexFileDeps* dex_deps = thread_deps->GetDexFileDeps(dex_file); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 421 | dex_deps->unverified_classes_.push_back(type_idx); |
| 422 | } |
| 423 | } |
| 424 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 425 | void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 426 | dex::TypeIndex type_idx, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 427 | mirror::Class* klass) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 428 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 429 | if (thread_deps != nullptr) { |
| 430 | thread_deps->AddClassResolution(dex_file, type_idx, klass); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file, |
| 435 | uint32_t field_idx, |
| 436 | ArtField* field) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 437 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 438 | if (thread_deps != nullptr) { |
| 439 | thread_deps->AddFieldResolution(dex_file, field_idx, field); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file, |
| 444 | uint32_t method_idx, |
| 445 | MethodResolutionKind resolution_kind, |
| 446 | ArtMethod* method) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 447 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 448 | if (thread_deps != nullptr) { |
| 449 | thread_deps->AddMethodResolution(dex_file, method_idx, resolution_kind, method); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file, |
| 454 | mirror::Class* destination, |
| 455 | mirror::Class* source, |
| 456 | bool is_strict, |
| 457 | bool is_assignable) { |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 458 | VerifierDeps* thread_deps = GetThreadLocalVerifierDeps(); |
| 459 | if (thread_deps != nullptr) { |
| 460 | thread_deps->AddAssignability(dex_file, destination, source, is_strict, is_assignable); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 464 | namespace { |
| 465 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 466 | static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) { |
| 467 | CHECK_LT(*in, end); |
| 468 | return DecodeUnsignedLeb128(in); |
| 469 | } |
| 470 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 471 | template<typename T> inline uint32_t Encode(T in); |
| 472 | |
| 473 | template<> inline uint32_t Encode<uint16_t>(uint16_t in) { |
| 474 | return in; |
| 475 | } |
| 476 | template<> inline uint32_t Encode<uint32_t>(uint32_t in) { |
| 477 | return in; |
| 478 | } |
| 479 | template<> inline uint32_t Encode<dex::TypeIndex>(dex::TypeIndex in) { |
| 480 | return in.index_; |
| 481 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 482 | template<> inline uint32_t Encode<dex::StringIndex>(dex::StringIndex in) { |
| 483 | return in.index_; |
| 484 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 485 | |
| 486 | template<typename T> inline T Decode(uint32_t in); |
| 487 | |
| 488 | template<> inline uint16_t Decode<uint16_t>(uint32_t in) { |
| 489 | return dchecked_integral_cast<uint16_t>(in); |
| 490 | } |
| 491 | template<> inline uint32_t Decode<uint32_t>(uint32_t in) { |
| 492 | return in; |
| 493 | } |
| 494 | template<> inline dex::TypeIndex Decode<dex::TypeIndex>(uint32_t in) { |
| 495 | return dex::TypeIndex(in); |
| 496 | } |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 497 | template<> inline dex::StringIndex Decode<dex::StringIndex>(uint32_t in) { |
| 498 | return dex::StringIndex(in); |
| 499 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 500 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 501 | template<typename T1, typename T2> |
| 502 | 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] | 503 | EncodeUnsignedLeb128(out, Encode(std::get<0>(t))); |
| 504 | EncodeUnsignedLeb128(out, Encode(std::get<1>(t))); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | template<typename T1, typename T2> |
| 508 | 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] | 509 | T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 510 | T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end)); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 511 | *t = std::make_tuple(v1, v2); |
| 512 | } |
| 513 | |
| 514 | template<typename T1, typename T2, typename T3> |
| 515 | 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] | 516 | EncodeUnsignedLeb128(out, Encode(std::get<0>(t))); |
| 517 | EncodeUnsignedLeb128(out, Encode(std::get<1>(t))); |
| 518 | EncodeUnsignedLeb128(out, Encode(std::get<2>(t))); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | template<typename T1, typename T2, typename T3> |
| 522 | 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] | 523 | T1 v1 = Decode<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 524 | T2 v2 = Decode<T2>(DecodeUint32WithOverflowCheck(in, end)); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 525 | T3 v3 = Decode<T3>(DecodeUint32WithOverflowCheck(in, end)); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 526 | *t = std::make_tuple(v1, v2, v3); |
| 527 | } |
| 528 | |
| 529 | template<typename T> |
| 530 | static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) { |
| 531 | EncodeUnsignedLeb128(out, set.size()); |
| 532 | for (const T& entry : set) { |
| 533 | EncodeTuple(out, entry); |
| 534 | } |
| 535 | } |
| 536 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 537 | template <typename T> |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 538 | static inline void EncodeUint16Vector(std::vector<uint8_t>* out, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 539 | const std::vector<T>& vector) { |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 540 | EncodeUnsignedLeb128(out, vector.size()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 541 | for (const T& entry : vector) { |
| 542 | EncodeUnsignedLeb128(out, Encode(entry)); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 546 | template<typename T> |
| 547 | static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) { |
| 548 | DCHECK(set->empty()); |
| 549 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 550 | for (size_t i = 0; i < num_entries; ++i) { |
| 551 | T tuple; |
| 552 | DecodeTuple(in, end, &tuple); |
| 553 | set->emplace(tuple); |
| 554 | } |
| 555 | } |
| 556 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 557 | template<typename T> |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 558 | static inline void DecodeUint16Vector(const uint8_t** in, |
| 559 | const uint8_t* end, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 560 | std::vector<T>* vector) { |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 561 | DCHECK(vector->empty()); |
| 562 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 563 | vector->reserve(num_entries); |
| 564 | for (size_t i = 0; i < num_entries; ++i) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 565 | vector->push_back( |
| 566 | Decode<T>(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end)))); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 570 | static inline void EncodeStringVector(std::vector<uint8_t>* out, |
| 571 | const std::vector<std::string>& strings) { |
| 572 | EncodeUnsignedLeb128(out, strings.size()); |
| 573 | for (const std::string& str : strings) { |
| 574 | const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str()); |
| 575 | size_t length = str.length() + 1; |
| 576 | out->insert(out->end(), data, data + length); |
| 577 | DCHECK_EQ(0u, out->back()); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static inline void DecodeStringVector(const uint8_t** in, |
| 582 | const uint8_t* end, |
| 583 | std::vector<std::string>* strings) { |
| 584 | DCHECK(strings->empty()); |
| 585 | size_t num_strings = DecodeUint32WithOverflowCheck(in, end); |
| 586 | strings->reserve(num_strings); |
| 587 | for (size_t i = 0; i < num_strings; ++i) { |
| 588 | CHECK_LT(*in, end); |
| 589 | const char* string_start = reinterpret_cast<const char*>(*in); |
| 590 | strings->emplace_back(std::string(string_start)); |
| 591 | *in += strings->back().length() + 1; |
| 592 | } |
| 593 | } |
| 594 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 595 | } // namespace |
| 596 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 597 | void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files, |
| 598 | std::vector<uint8_t>* buffer) const { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 599 | for (const DexFile* dex_file : dex_files) { |
| 600 | const DexFileDeps& deps = *GetDexFileDeps(*dex_file); |
| 601 | EncodeStringVector(buffer, deps.strings_); |
| 602 | EncodeSet(buffer, deps.assignable_types_); |
| 603 | EncodeSet(buffer, deps.unassignable_types_); |
| 604 | EncodeSet(buffer, deps.classes_); |
| 605 | EncodeSet(buffer, deps.fields_); |
| 606 | EncodeSet(buffer, deps.direct_methods_); |
| 607 | EncodeSet(buffer, deps.virtual_methods_); |
| 608 | EncodeSet(buffer, deps.interface_methods_); |
| 609 | EncodeUint16Vector(buffer, deps.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 613 | VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files, |
| 614 | ArrayRef<const uint8_t> data) |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 615 | : VerifierDeps(dex_files) { |
Nicolas Geoffray | e70dd56 | 2016-10-30 21:03:35 +0000 | [diff] [blame] | 616 | if (data.empty()) { |
| 617 | // Return eagerly, as the first thing we expect from VerifierDeps data is |
| 618 | // the number of created strings, even if there is no dependency. |
| 619 | // Currently, only the boot image does not have any VerifierDeps data. |
| 620 | return; |
| 621 | } |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 622 | const uint8_t* data_start = data.data(); |
| 623 | const uint8_t* data_end = data_start + data.size(); |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 624 | for (const DexFile* dex_file : dex_files) { |
| 625 | DexFileDeps* deps = GetDexFileDeps(*dex_file); |
| 626 | DecodeStringVector(&data_start, data_end, &deps->strings_); |
| 627 | DecodeSet(&data_start, data_end, &deps->assignable_types_); |
| 628 | DecodeSet(&data_start, data_end, &deps->unassignable_types_); |
| 629 | DecodeSet(&data_start, data_end, &deps->classes_); |
| 630 | DecodeSet(&data_start, data_end, &deps->fields_); |
| 631 | DecodeSet(&data_start, data_end, &deps->direct_methods_); |
| 632 | DecodeSet(&data_start, data_end, &deps->virtual_methods_); |
| 633 | DecodeSet(&data_start, data_end, &deps->interface_methods_); |
| 634 | DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 635 | } |
| 636 | CHECK_LE(data_start, data_end); |
| 637 | } |
| 638 | |
| 639 | bool VerifierDeps::Equals(const VerifierDeps& rhs) const { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 640 | if (dex_deps_.size() != rhs.dex_deps_.size()) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | auto lhs_it = dex_deps_.begin(); |
| 645 | auto rhs_it = rhs.dex_deps_.begin(); |
| 646 | |
| 647 | for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) { |
| 648 | const DexFile* lhs_dex_file = lhs_it->first; |
| 649 | const DexFile* rhs_dex_file = rhs_it->first; |
| 650 | if (lhs_dex_file != rhs_dex_file) { |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | DexFileDeps* lhs_deps = lhs_it->second.get(); |
| 655 | DexFileDeps* rhs_deps = rhs_it->second.get(); |
| 656 | if (!lhs_deps->Equals(*rhs_deps)) { |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end())); |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const { |
| 666 | return (strings_ == rhs.strings_) && |
| 667 | (assignable_types_ == rhs.assignable_types_) && |
| 668 | (unassignable_types_ == rhs.unassignable_types_) && |
| 669 | (classes_ == rhs.classes_) && |
| 670 | (fields_ == rhs.fields_) && |
| 671 | (direct_methods_ == rhs.direct_methods_) && |
| 672 | (virtual_methods_ == rhs.virtual_methods_) && |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 673 | (interface_methods_ == rhs.interface_methods_) && |
| 674 | (unverified_classes_ == rhs.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 677 | void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const { |
| 678 | for (const auto& dep : dex_deps_) { |
| 679 | const DexFile& dex_file = *dep.first; |
| 680 | vios->Stream() |
| 681 | << "Dependencies of " |
| 682 | << dex_file.GetLocation() |
| 683 | << ":\n"; |
| 684 | |
| 685 | ScopedIndentation indent(vios); |
| 686 | |
| 687 | for (const std::string& str : dep.second->strings_) { |
| 688 | vios->Stream() << "Extra string: " << str << "\n"; |
| 689 | } |
| 690 | |
| 691 | for (const TypeAssignability& entry : dep.second->assignable_types_) { |
| 692 | vios->Stream() |
| 693 | << GetStringFromId(dex_file, entry.GetSource()) |
| 694 | << " must be assignable to " |
| 695 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 696 | << "\n"; |
| 697 | } |
| 698 | |
| 699 | for (const TypeAssignability& entry : dep.second->unassignable_types_) { |
| 700 | vios->Stream() |
| 701 | << GetStringFromId(dex_file, entry.GetSource()) |
| 702 | << " must not be assignable to " |
| 703 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 704 | << "\n"; |
| 705 | } |
| 706 | |
| 707 | for (const ClassResolution& entry : dep.second->classes_) { |
| 708 | vios->Stream() |
| 709 | << dex_file.StringByTypeIdx(entry.GetDexTypeIndex()) |
| 710 | << (entry.IsResolved() ? " must be resolved " : "must not be resolved ") |
| 711 | << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 712 | << "\n"; |
| 713 | } |
| 714 | |
| 715 | for (const FieldResolution& entry : dep.second->fields_) { |
| 716 | const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex()); |
| 717 | vios->Stream() |
| 718 | << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->" |
| 719 | << dex_file.GetFieldName(field_id) << ":" |
| 720 | << dex_file.GetFieldTypeDescriptor(field_id) |
| 721 | << " is expected to be "; |
| 722 | if (!entry.IsResolved()) { |
| 723 | vios->Stream() << "unresolved\n"; |
| 724 | } else { |
| 725 | vios->Stream() |
| 726 | << "in class " |
| 727 | << GetStringFromId(dex_file, entry.GetDeclaringClassIndex()) |
| 728 | << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 729 | << "\n"; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | for (const auto& entry : |
| 734 | { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_), |
| 735 | std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_), |
| 736 | std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) { |
| 737 | for (const MethodResolution& method : entry.second) { |
| 738 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); |
| 739 | vios->Stream() |
| 740 | << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->" |
| 741 | << dex_file.GetMethodName(method_id) |
| 742 | << dex_file.GetMethodSignature(method_id).ToString() |
| 743 | << " is expected to be "; |
| 744 | if (!method.IsResolved()) { |
| 745 | vios->Stream() << "unresolved\n"; |
| 746 | } else { |
| 747 | vios->Stream() |
| 748 | << "in class " |
| 749 | << GetStringFromId(dex_file, method.GetDeclaringClassIndex()) |
| 750 | << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec |
| 751 | << ", and be of kind " << entry.first |
| 752 | << "\n"; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 757 | for (dex::TypeIndex type_index : dep.second->unverified_classes_) { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 758 | vios->Stream() |
| 759 | << dex_file.StringByTypeIdx(type_index) |
| 760 | << " is expected to be verified at runtime\n"; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Nicolas Geoffray | 6bb7f1b | 2016-11-03 10:52:49 +0000 | [diff] [blame] | 765 | bool VerifierDeps::ValidateDependencies(Handle<mirror::ClassLoader> class_loader, |
| 766 | Thread* self) const { |
Nicolas Geoffray | 8904b6f | 2016-10-28 19:50:34 +0100 | [diff] [blame] | 767 | for (const auto& entry : dex_deps_) { |
| 768 | if (!VerifyDexFile(class_loader, *entry.first, *entry.second, self)) { |
| 769 | return false; |
| 770 | } |
| 771 | } |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | // TODO: share that helper with other parts of the compiler that have |
| 776 | // the same lookup pattern. |
| 777 | static mirror::Class* FindClassAndClearException(ClassLinker* class_linker, |
| 778 | Thread* self, |
| 779 | const char* name, |
| 780 | Handle<mirror::ClassLoader> class_loader) |
| 781 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 782 | mirror::Class* result = class_linker->FindClass(self, name, class_loader); |
| 783 | if (result == nullptr) { |
| 784 | DCHECK(self->IsExceptionPending()); |
| 785 | self->ClearException(); |
| 786 | } |
| 787 | return result; |
| 788 | } |
| 789 | |
| 790 | bool VerifierDeps::VerifyAssignability(Handle<mirror::ClassLoader> class_loader, |
| 791 | const DexFile& dex_file, |
| 792 | const std::set<TypeAssignability>& assignables, |
| 793 | bool expected_assignability, |
| 794 | Thread* self) const { |
| 795 | StackHandleScope<2> hs(self); |
| 796 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 797 | MutableHandle<mirror::Class> source(hs.NewHandle<mirror::Class>(nullptr)); |
| 798 | MutableHandle<mirror::Class> destination(hs.NewHandle<mirror::Class>(nullptr)); |
| 799 | |
| 800 | for (const auto& entry : assignables) { |
| 801 | const std::string& destination_desc = GetStringFromId(dex_file, entry.GetDestination()); |
| 802 | destination.Assign( |
| 803 | FindClassAndClearException(class_linker, self, destination_desc.c_str(), class_loader)); |
| 804 | const std::string& source_desc = GetStringFromId(dex_file, entry.GetSource()); |
| 805 | source.Assign( |
| 806 | FindClassAndClearException(class_linker, self, source_desc.c_str(), class_loader)); |
| 807 | |
| 808 | if (destination.Get() == nullptr) { |
| 809 | LOG(INFO) << "VerifiersDeps: Could not resolve class " << destination_desc; |
| 810 | return false; |
| 811 | } |
| 812 | |
| 813 | if (source.Get() == nullptr) { |
| 814 | LOG(INFO) << "VerifierDeps: Could not resolve class " << source_desc; |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | DCHECK(destination->IsResolved() && source->IsResolved()); |
| 819 | if (destination->IsAssignableFrom(source.Get()) != expected_assignability) { |
| 820 | LOG(INFO) << "VerifierDeps: Class " |
| 821 | << destination_desc |
| 822 | << (expected_assignability ? " not " : " ") |
| 823 | << "assignable from " |
| 824 | << source_desc; |
| 825 | return false; |
| 826 | } |
| 827 | } |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | bool VerifierDeps::VerifyClasses(Handle<mirror::ClassLoader> class_loader, |
| 832 | const DexFile& dex_file, |
| 833 | const std::set<ClassResolution>& classes, |
| 834 | Thread* self) const { |
| 835 | StackHandleScope<1> hs(self); |
| 836 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 837 | MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr)); |
| 838 | for (const auto& entry : classes) { |
| 839 | const char* descriptor = dex_file.StringByTypeIdx(entry.GetDexTypeIndex()); |
| 840 | cls.Assign(FindClassAndClearException(class_linker, self, descriptor, class_loader)); |
| 841 | |
| 842 | if (entry.IsResolved()) { |
| 843 | if (cls.Get() == nullptr) { |
| 844 | LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor; |
| 845 | return false; |
| 846 | } else if (entry.GetAccessFlags() != GetAccessFlags(cls.Get())) { |
| 847 | LOG(INFO) << "VerifierDeps: Unexpected access flags on class " |
| 848 | << descriptor |
| 849 | << std::hex |
| 850 | << " (expected=" |
| 851 | << entry.GetAccessFlags() |
| 852 | << ", actual=" |
| 853 | << GetAccessFlags(cls.Get()) << ")" |
| 854 | << std::dec; |
| 855 | return false; |
| 856 | } |
| 857 | } else if (cls.Get() != nullptr) { |
| 858 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of class " << descriptor; |
| 859 | return false; |
| 860 | } |
| 861 | } |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | static std::string GetFieldDescription(const DexFile& dex_file, uint32_t index) { |
| 866 | const DexFile::FieldId& field_id = dex_file.GetFieldId(index); |
| 867 | return std::string(dex_file.GetFieldDeclaringClassDescriptor(field_id)) |
| 868 | + "->" |
| 869 | + dex_file.GetFieldName(field_id) |
| 870 | + ":" |
| 871 | + dex_file.GetFieldTypeDescriptor(field_id); |
| 872 | } |
| 873 | |
| 874 | bool VerifierDeps::VerifyFields(Handle<mirror::ClassLoader> class_loader, |
| 875 | const DexFile& dex_file, |
| 876 | const std::set<FieldResolution>& fields, |
| 877 | Thread* self) const { |
| 878 | // Check recorded fields are resolved the same way, have the same recorded class, |
| 879 | // and have the same recorded flags. |
| 880 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 881 | StackHandleScope<1> hs(self); |
| 882 | Handle<mirror::DexCache> dex_cache( |
| 883 | hs.NewHandle(class_linker->FindDexCache(self, dex_file, /* allow_failure */ false))); |
| 884 | for (const auto& entry : fields) { |
| 885 | ArtField* field = class_linker->ResolveFieldJLS( |
| 886 | dex_file, entry.GetDexFieldIndex(), dex_cache, class_loader); |
| 887 | |
| 888 | if (field == nullptr) { |
| 889 | DCHECK(self->IsExceptionPending()); |
| 890 | self->ClearException(); |
| 891 | } |
| 892 | |
| 893 | if (entry.IsResolved()) { |
| 894 | std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex()); |
| 895 | std::string temp; |
| 896 | if (field == nullptr) { |
| 897 | LOG(INFO) << "VerifierDeps: Could not resolve field " |
| 898 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()); |
| 899 | return false; |
| 900 | } else if (expected_decl_klass != field->GetDeclaringClass()->GetDescriptor(&temp)) { |
| 901 | LOG(INFO) << "VerifierDeps: Unexpected declaring class for field resolution " |
| 902 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()) |
| 903 | << " (expected=" << expected_decl_klass |
| 904 | << ", actual=" << field->GetDeclaringClass()->GetDescriptor(&temp) << ")"; |
| 905 | return false; |
| 906 | } else if (entry.GetAccessFlags() != GetAccessFlags(field)) { |
| 907 | LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved field " |
| 908 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()) |
| 909 | << std::hex << " (expected=" << entry.GetAccessFlags() |
| 910 | << ", actual=" << GetAccessFlags(field) << ")" << std::dec; |
| 911 | return false; |
| 912 | } |
| 913 | } else if (field != nullptr) { |
| 914 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of field " |
| 915 | << GetFieldDescription(dex_file, entry.GetDexFieldIndex()); |
| 916 | return false; |
| 917 | } |
| 918 | } |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | static std::string GetMethodDescription(const DexFile& dex_file, uint32_t index) { |
| 923 | const DexFile::MethodId& method_id = dex_file.GetMethodId(index); |
| 924 | return std::string(dex_file.GetMethodDeclaringClassDescriptor(method_id)) |
| 925 | + "->" |
| 926 | + dex_file.GetMethodName(method_id) |
| 927 | + dex_file.GetMethodSignature(method_id).ToString(); |
| 928 | } |
| 929 | |
| 930 | bool VerifierDeps::VerifyMethods(Handle<mirror::ClassLoader> class_loader, |
| 931 | const DexFile& dex_file, |
| 932 | const std::set<MethodResolution>& methods, |
| 933 | MethodResolutionKind kind, |
| 934 | Thread* self) const { |
| 935 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 936 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
| 937 | |
| 938 | for (const auto& entry : methods) { |
| 939 | const DexFile::MethodId& method_id = dex_file.GetMethodId(entry.GetDexMethodIndex()); |
| 940 | |
| 941 | const char* name = dex_file.GetMethodName(method_id); |
| 942 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 943 | const char* descriptor = dex_file.GetMethodDeclaringClassDescriptor(method_id); |
| 944 | |
| 945 | mirror::Class* cls = FindClassAndClearException(class_linker, self, descriptor, class_loader); |
| 946 | if (cls == nullptr) { |
| 947 | LOG(INFO) << "VerifierDeps: Could not resolve class " << descriptor; |
| 948 | return false; |
| 949 | } |
| 950 | DCHECK(cls->IsResolved()); |
| 951 | ArtMethod* method = nullptr; |
| 952 | if (kind == kDirectMethodResolution) { |
| 953 | method = cls->FindDirectMethod(name, signature, pointer_size); |
| 954 | } else if (kind == kVirtualMethodResolution) { |
| 955 | method = cls->FindVirtualMethod(name, signature, pointer_size); |
| 956 | } else { |
| 957 | DCHECK_EQ(kind, kInterfaceMethodResolution); |
| 958 | method = cls->FindInterfaceMethod(name, signature, pointer_size); |
| 959 | } |
| 960 | |
| 961 | if (entry.IsResolved()) { |
| 962 | std::string temp; |
| 963 | std::string expected_decl_klass = GetStringFromId(dex_file, entry.GetDeclaringClassIndex()); |
| 964 | if (method == nullptr) { |
| 965 | LOG(INFO) << "VerifierDeps: Could not resolve " |
| 966 | << kind |
| 967 | << " method " |
| 968 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()); |
| 969 | return false; |
| 970 | } else if (expected_decl_klass != method->GetDeclaringClass()->GetDescriptor(&temp)) { |
| 971 | LOG(INFO) << "VerifierDeps: Unexpected declaring class for " |
| 972 | << kind |
| 973 | << " method resolution " |
| 974 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()) |
| 975 | << " (expected=" |
| 976 | << expected_decl_klass |
| 977 | << ", actual=" |
| 978 | << method->GetDeclaringClass()->GetDescriptor(&temp) |
| 979 | << ")"; |
| 980 | return false; |
| 981 | } else if (entry.GetAccessFlags() != GetAccessFlags(method)) { |
| 982 | LOG(INFO) << "VerifierDeps: Unexpected access flags for resolved " |
| 983 | << kind |
| 984 | << " method resolution " |
| 985 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()) |
| 986 | << std::hex |
| 987 | << " (expected=" |
| 988 | << entry.GetAccessFlags() |
| 989 | << ", actual=" |
| 990 | << GetAccessFlags(method) << ")" |
| 991 | << std::dec; |
| 992 | return false; |
| 993 | } |
| 994 | } else if (method != nullptr) { |
| 995 | LOG(INFO) << "VerifierDeps: Unexpected successful resolution of " |
| 996 | << kind |
| 997 | << " method " |
| 998 | << GetMethodDescription(dex_file, entry.GetDexMethodIndex()); |
| 999 | return false; |
| 1000 | } |
| 1001 | } |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | bool VerifierDeps::VerifyDexFile(Handle<mirror::ClassLoader> class_loader, |
| 1006 | const DexFile& dex_file, |
| 1007 | const DexFileDeps& deps, |
| 1008 | Thread* self) const { |
| 1009 | bool result = VerifyAssignability( |
| 1010 | class_loader, dex_file, deps.assignable_types_, /* expected_assignability */ true, self); |
| 1011 | result = result && VerifyAssignability( |
| 1012 | class_loader, dex_file, deps.unassignable_types_, /* expected_assignability */ false, self); |
| 1013 | |
| 1014 | result = result && VerifyClasses(class_loader, dex_file, deps.classes_, self); |
| 1015 | result = result && VerifyFields(class_loader, dex_file, deps.fields_, self); |
| 1016 | |
| 1017 | result = result && VerifyMethods( |
| 1018 | class_loader, dex_file, deps.direct_methods_, kDirectMethodResolution, self); |
| 1019 | result = result && VerifyMethods( |
| 1020 | class_loader, dex_file, deps.virtual_methods_, kVirtualMethodResolution, self); |
| 1021 | result = result && VerifyMethods( |
| 1022 | class_loader, dex_file, deps.interface_methods_, kInterfaceMethodResolution, self); |
| 1023 | |
| 1024 | return result; |
| 1025 | } |
| 1026 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 1027 | } // namespace verifier |
| 1028 | } // namespace art |