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 | |
| 19 | #include "compiler_callbacks.h" |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 20 | #include "leb128.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 21 | #include "mirror/class-inl.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 22 | #include "obj_ptr-inl.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 23 | #include "runtime.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace verifier { |
| 27 | |
| 28 | VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files) { |
| 29 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 30 | for (const DexFile* dex_file : dex_files) { |
| 31 | DCHECK(GetDexFileDeps(*dex_file) == nullptr); |
| 32 | std::unique_ptr<DexFileDeps> deps(new DexFileDeps()); |
| 33 | dex_deps_.emplace(dex_file, std::move(deps)); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) { |
| 38 | auto it = dex_deps_.find(&dex_file); |
| 39 | return (it == dex_deps_.end()) ? nullptr : it->second.get(); |
| 40 | } |
| 41 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 42 | const VerifierDeps::DexFileDeps* VerifierDeps::GetDexFileDeps(const DexFile& dex_file) const { |
| 43 | auto it = dex_deps_.find(&dex_file); |
| 44 | return (it == dex_deps_.end()) ? nullptr : it->second.get(); |
| 45 | } |
| 46 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 47 | template <typename T> |
| 48 | uint16_t VerifierDeps::GetAccessFlags(T* element) { |
| 49 | static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant"); |
| 50 | if (element == nullptr) { |
| 51 | return VerifierDeps::kUnresolvedMarker; |
| 52 | } else { |
| 53 | uint16_t access_flags = Low16Bits(element->GetAccessFlags()); |
| 54 | CHECK_NE(access_flags, VerifierDeps::kUnresolvedMarker); |
| 55 | return access_flags; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | template <typename T> |
| 60 | uint32_t VerifierDeps::GetDeclaringClassStringId(const DexFile& dex_file, T* element) { |
| 61 | static_assert(kAccJavaFlagsMask == 0xFFFF, "Unexpected value of a constant"); |
| 62 | if (element == nullptr) { |
| 63 | return VerifierDeps::kUnresolvedMarker; |
| 64 | } else { |
| 65 | std::string temp; |
| 66 | uint32_t string_id = GetIdFromString( |
| 67 | dex_file, element->GetDeclaringClass()->GetDescriptor(&temp)); |
| 68 | return string_id; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | uint32_t VerifierDeps::GetIdFromString(const DexFile& dex_file, const std::string& str) { |
| 73 | const DexFile::StringId* string_id = dex_file.FindStringId(str.c_str()); |
| 74 | if (string_id != nullptr) { |
| 75 | // String is in the DEX file. Return its ID. |
| 76 | return dex_file.GetIndexForStringId(*string_id); |
| 77 | } |
| 78 | |
| 79 | // String is not in the DEX file. Assign a new ID to it which is higher than |
| 80 | // the number of strings in the DEX file. |
| 81 | |
| 82 | DexFileDeps* deps = GetDexFileDeps(dex_file); |
| 83 | DCHECK(deps != nullptr); |
| 84 | |
| 85 | uint32_t num_ids_in_dex = dex_file.NumStringIds(); |
| 86 | uint32_t num_extra_ids = deps->strings_.size(); |
| 87 | |
| 88 | for (size_t i = 0; i < num_extra_ids; ++i) { |
| 89 | if (deps->strings_[i] == str) { |
| 90 | return num_ids_in_dex + i; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | deps->strings_.push_back(str); |
| 95 | |
| 96 | uint32_t new_id = num_ids_in_dex + num_extra_ids; |
| 97 | CHECK_GE(new_id, num_ids_in_dex); // check for overflows |
| 98 | DCHECK_EQ(str, GetStringFromId(dex_file, new_id)); |
| 99 | |
| 100 | return new_id; |
| 101 | } |
| 102 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 103 | std::string VerifierDeps::GetStringFromId(const DexFile& dex_file, uint32_t string_id) const { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 104 | uint32_t num_ids_in_dex = dex_file.NumStringIds(); |
| 105 | if (string_id < num_ids_in_dex) { |
| 106 | return std::string(dex_file.StringDataByIdx(string_id)); |
| 107 | } else { |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 108 | const DexFileDeps* deps = GetDexFileDeps(dex_file); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 109 | DCHECK(deps != nullptr); |
| 110 | string_id -= num_ids_in_dex; |
| 111 | CHECK_LT(string_id, deps->strings_.size()); |
| 112 | return deps->strings_[string_id]; |
| 113 | } |
| 114 | } |
| 115 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 116 | bool VerifierDeps::IsInClassPath(ObjPtr<mirror::Class> klass) const { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 117 | DCHECK(klass != nullptr); |
| 118 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 119 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 120 | if (dex_cache == nullptr) { |
| 121 | // This is a synthesized class, in this case always an array. They are not |
| 122 | // defined in the compiled DEX files and therefore are part of the classpath. |
| 123 | // We could avoid recording dependencies on arrays with component types in |
| 124 | // the compiled DEX files but we choose to record them anyway so as to |
| 125 | // record the access flags VM sets for array classes. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 126 | DCHECK(klass->IsArrayClass()) << klass->PrettyDescriptor(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 131 | DCHECK(dex_file != nullptr); |
| 132 | |
| 133 | // Test if the `dex_deps_` contains an entry for `dex_file`. If not, the dex |
| 134 | // file was not registered as being compiled and we assume `klass` is in the |
| 135 | // classpath. |
| 136 | return (GetDexFileDeps(*dex_file) == nullptr); |
| 137 | } |
| 138 | |
| 139 | void VerifierDeps::AddClassResolution(const DexFile& dex_file, |
| 140 | uint16_t type_idx, |
| 141 | mirror::Class* klass) { |
| 142 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 143 | if (dex_deps == nullptr) { |
| 144 | // This invocation is from verification of a dex file which is not being compiled. |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (klass != nullptr && !IsInClassPath(klass)) { |
| 149 | // Class resolved into one of the DEX files which are being compiled. |
| 150 | // This is not a classpath dependency. |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 155 | dex_deps->classes_.emplace(ClassResolution(type_idx, GetAccessFlags(klass))); |
| 156 | } |
| 157 | |
| 158 | void VerifierDeps::AddFieldResolution(const DexFile& dex_file, |
| 159 | uint32_t field_idx, |
| 160 | ArtField* field) { |
| 161 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 162 | if (dex_deps == nullptr) { |
| 163 | // This invocation is from verification of a dex file which is not being compiled. |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (field != nullptr && !IsInClassPath(field->GetDeclaringClass())) { |
| 168 | // Field resolved into one of the DEX files which are being compiled. |
| 169 | // This is not a classpath dependency. |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 174 | dex_deps->fields_.emplace(FieldResolution( |
| 175 | field_idx, GetAccessFlags(field), GetDeclaringClassStringId(dex_file, field))); |
| 176 | } |
| 177 | |
| 178 | void VerifierDeps::AddMethodResolution(const DexFile& dex_file, |
| 179 | uint32_t method_idx, |
| 180 | MethodResolutionKind resolution_kind, |
| 181 | ArtMethod* method) { |
| 182 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 183 | if (dex_deps == nullptr) { |
| 184 | // This invocation is from verification of a dex file which is not being compiled. |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (method != nullptr && !IsInClassPath(method->GetDeclaringClass())) { |
| 189 | // Method resolved into one of the DEX files which are being compiled. |
| 190 | // This is not a classpath dependency. |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 195 | MethodResolution method_tuple(method_idx, |
| 196 | GetAccessFlags(method), |
| 197 | GetDeclaringClassStringId(dex_file, method)); |
| 198 | if (resolution_kind == kDirectMethodResolution) { |
| 199 | dex_deps->direct_methods_.emplace(method_tuple); |
| 200 | } else if (resolution_kind == kVirtualMethodResolution) { |
| 201 | dex_deps->virtual_methods_.emplace(method_tuple); |
| 202 | } else { |
| 203 | DCHECK_EQ(resolution_kind, kInterfaceMethodResolution); |
| 204 | dex_deps->interface_methods_.emplace(method_tuple); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void VerifierDeps::AddAssignability(const DexFile& dex_file, |
| 209 | mirror::Class* destination, |
| 210 | mirror::Class* source, |
| 211 | bool is_strict, |
| 212 | bool is_assignable) { |
| 213 | // Test that the method is only called on reference types. |
| 214 | // Note that concurrent verification of `destination` and `source` may have |
| 215 | // set their status to erroneous. However, the tests performed below rely |
| 216 | // merely on no issues with linking (valid access flags, superclass and |
| 217 | // implemented interfaces). If the class at any point reached the IsResolved |
| 218 | // status, the requirement holds. This is guaranteed by RegTypeCache::ResolveClass. |
| 219 | DCHECK(destination != nullptr && !destination->IsPrimitive()); |
| 220 | DCHECK(source != nullptr && !source->IsPrimitive()); |
| 221 | |
| 222 | if (destination == source || |
| 223 | destination->IsObjectClass() || |
| 224 | (!is_strict && destination->IsInterface())) { |
| 225 | // Cases when `destination` is trivially assignable from `source`. |
| 226 | DCHECK(is_assignable); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | DCHECK_EQ(is_assignable, destination->IsAssignableFrom(source)); |
| 231 | |
| 232 | if (destination->IsArrayClass() && source->IsArrayClass()) { |
| 233 | // Both types are arrays. Break down to component types and add recursively. |
| 234 | // This helps filter out destinations from compiled DEX files (see below) |
| 235 | // and deduplicate entries with the same canonical component type. |
| 236 | mirror::Class* destination_component = destination->GetComponentType(); |
| 237 | mirror::Class* source_component = source->GetComponentType(); |
| 238 | |
| 239 | // Only perform the optimization if both types are resolved which guarantees |
| 240 | // that they linked successfully, as required at the top of this method. |
| 241 | if (destination_component->IsResolved() && source_component->IsResolved()) { |
| 242 | AddAssignability(dex_file, |
| 243 | destination_component, |
| 244 | source_component, |
| 245 | /* is_strict */ true, |
| 246 | is_assignable); |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | DexFileDeps* dex_deps = GetDexFileDeps(dex_file); |
| 252 | if (dex_deps == nullptr) { |
| 253 | // This invocation is from verification of a DEX file which is not being compiled. |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (!IsInClassPath(destination) && !IsInClassPath(source)) { |
| 258 | // Both `destination` and `source` are defined in the compiled DEX files. |
| 259 | // No need to record a dependency. |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 264 | |
| 265 | // Get string IDs for both descriptors and store in the appropriate set. |
| 266 | |
| 267 | std::string temp1, temp2; |
| 268 | std::string destination_desc(destination->GetDescriptor(&temp1)); |
| 269 | std::string source_desc(source->GetDescriptor(&temp2)); |
| 270 | uint32_t destination_id = GetIdFromString(dex_file, destination_desc); |
| 271 | uint32_t source_id = GetIdFromString(dex_file, source_desc); |
| 272 | |
| 273 | if (is_assignable) { |
| 274 | dex_deps->assignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 275 | } else { |
| 276 | dex_deps->unassignable_types_.emplace(TypeAssignability(destination_id, source_id)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static inline VerifierDeps* GetVerifierDepsSingleton() { |
| 281 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
| 282 | if (callbacks == nullptr) { |
| 283 | return nullptr; |
| 284 | } |
| 285 | return callbacks->GetVerifierDeps(); |
| 286 | } |
| 287 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 288 | void VerifierDeps::MaybeRecordVerificationStatus(const DexFile& dex_file, |
| 289 | uint16_t type_idx, |
| 290 | MethodVerifier::FailureKind failure_kind) { |
| 291 | if (failure_kind == MethodVerifier::kNoFailure) { |
| 292 | // We only record classes that did not fully verify at compile time. |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | VerifierDeps* singleton = GetVerifierDepsSingleton(); |
| 297 | if (singleton != nullptr) { |
| 298 | DexFileDeps* dex_deps = singleton->GetDexFileDeps(dex_file); |
| 299 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 300 | dex_deps->unverified_classes_.push_back(type_idx); |
| 301 | } |
| 302 | } |
| 303 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 304 | void VerifierDeps::MaybeRecordClassResolution(const DexFile& dex_file, |
| 305 | uint16_t type_idx, |
| 306 | mirror::Class* klass) { |
| 307 | VerifierDeps* singleton = GetVerifierDepsSingleton(); |
| 308 | if (singleton != nullptr) { |
| 309 | singleton->AddClassResolution(dex_file, type_idx, klass); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void VerifierDeps::MaybeRecordFieldResolution(const DexFile& dex_file, |
| 314 | uint32_t field_idx, |
| 315 | ArtField* field) { |
| 316 | VerifierDeps* singleton = GetVerifierDepsSingleton(); |
| 317 | if (singleton != nullptr) { |
| 318 | singleton->AddFieldResolution(dex_file, field_idx, field); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void VerifierDeps::MaybeRecordMethodResolution(const DexFile& dex_file, |
| 323 | uint32_t method_idx, |
| 324 | MethodResolutionKind resolution_kind, |
| 325 | ArtMethod* method) { |
| 326 | VerifierDeps* singleton = GetVerifierDepsSingleton(); |
| 327 | if (singleton != nullptr) { |
| 328 | singleton->AddMethodResolution(dex_file, method_idx, resolution_kind, method); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void VerifierDeps::MaybeRecordAssignability(const DexFile& dex_file, |
| 333 | mirror::Class* destination, |
| 334 | mirror::Class* source, |
| 335 | bool is_strict, |
| 336 | bool is_assignable) { |
| 337 | VerifierDeps* singleton = GetVerifierDepsSingleton(); |
| 338 | if (singleton != nullptr) { |
| 339 | singleton->AddAssignability(dex_file, destination, source, is_strict, is_assignable); |
| 340 | } |
| 341 | } |
| 342 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 343 | static inline uint32_t DecodeUint32WithOverflowCheck(const uint8_t** in, const uint8_t* end) { |
| 344 | CHECK_LT(*in, end); |
| 345 | return DecodeUnsignedLeb128(in); |
| 346 | } |
| 347 | |
| 348 | template<typename T1, typename T2> |
| 349 | static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2>& t) { |
| 350 | EncodeUnsignedLeb128(out, std::get<0>(t)); |
| 351 | EncodeUnsignedLeb128(out, std::get<1>(t)); |
| 352 | } |
| 353 | |
| 354 | template<typename T1, typename T2> |
| 355 | static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2>* t) { |
| 356 | T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 357 | T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end)); |
| 358 | *t = std::make_tuple(v1, v2); |
| 359 | } |
| 360 | |
| 361 | template<typename T1, typename T2, typename T3> |
| 362 | static inline void EncodeTuple(std::vector<uint8_t>* out, const std::tuple<T1, T2, T3>& t) { |
| 363 | EncodeUnsignedLeb128(out, std::get<0>(t)); |
| 364 | EncodeUnsignedLeb128(out, std::get<1>(t)); |
| 365 | EncodeUnsignedLeb128(out, std::get<2>(t)); |
| 366 | } |
| 367 | |
| 368 | template<typename T1, typename T2, typename T3> |
| 369 | static inline void DecodeTuple(const uint8_t** in, const uint8_t* end, std::tuple<T1, T2, T3>* t) { |
| 370 | T1 v1 = static_cast<T1>(DecodeUint32WithOverflowCheck(in, end)); |
| 371 | T2 v2 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end)); |
| 372 | T3 v3 = static_cast<T2>(DecodeUint32WithOverflowCheck(in, end)); |
| 373 | *t = std::make_tuple(v1, v2, v3); |
| 374 | } |
| 375 | |
| 376 | template<typename T> |
| 377 | static inline void EncodeSet(std::vector<uint8_t>* out, const std::set<T>& set) { |
| 378 | EncodeUnsignedLeb128(out, set.size()); |
| 379 | for (const T& entry : set) { |
| 380 | EncodeTuple(out, entry); |
| 381 | } |
| 382 | } |
| 383 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 384 | static inline void EncodeUint16Vector(std::vector<uint8_t>* out, |
| 385 | const std::vector<uint16_t>& vector) { |
| 386 | EncodeUnsignedLeb128(out, vector.size()); |
| 387 | for (uint16_t entry : vector) { |
| 388 | EncodeUnsignedLeb128(out, entry); |
| 389 | } |
| 390 | } |
| 391 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 392 | template<typename T> |
| 393 | static inline void DecodeSet(const uint8_t** in, const uint8_t* end, std::set<T>* set) { |
| 394 | DCHECK(set->empty()); |
| 395 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 396 | for (size_t i = 0; i < num_entries; ++i) { |
| 397 | T tuple; |
| 398 | DecodeTuple(in, end, &tuple); |
| 399 | set->emplace(tuple); |
| 400 | } |
| 401 | } |
| 402 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 403 | static inline void DecodeUint16Vector(const uint8_t** in, |
| 404 | const uint8_t* end, |
| 405 | std::vector<uint16_t>* vector) { |
| 406 | DCHECK(vector->empty()); |
| 407 | size_t num_entries = DecodeUint32WithOverflowCheck(in, end); |
| 408 | vector->reserve(num_entries); |
| 409 | for (size_t i = 0; i < num_entries; ++i) { |
| 410 | vector->push_back(dchecked_integral_cast<uint16_t>(DecodeUint32WithOverflowCheck(in, end))); |
| 411 | } |
| 412 | } |
| 413 | |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 414 | static inline void EncodeStringVector(std::vector<uint8_t>* out, |
| 415 | const std::vector<std::string>& strings) { |
| 416 | EncodeUnsignedLeb128(out, strings.size()); |
| 417 | for (const std::string& str : strings) { |
| 418 | const uint8_t* data = reinterpret_cast<const uint8_t*>(str.c_str()); |
| 419 | size_t length = str.length() + 1; |
| 420 | out->insert(out->end(), data, data + length); |
| 421 | DCHECK_EQ(0u, out->back()); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static inline void DecodeStringVector(const uint8_t** in, |
| 426 | const uint8_t* end, |
| 427 | std::vector<std::string>* strings) { |
| 428 | DCHECK(strings->empty()); |
| 429 | size_t num_strings = DecodeUint32WithOverflowCheck(in, end); |
| 430 | strings->reserve(num_strings); |
| 431 | for (size_t i = 0; i < num_strings; ++i) { |
| 432 | CHECK_LT(*in, end); |
| 433 | const char* string_start = reinterpret_cast<const char*>(*in); |
| 434 | strings->emplace_back(std::string(string_start)); |
| 435 | *in += strings->back().length() + 1; |
| 436 | } |
| 437 | } |
| 438 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 439 | void VerifierDeps::Encode(const std::vector<const DexFile*>& dex_files, |
| 440 | std::vector<uint8_t>* buffer) const { |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 441 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 442 | for (const DexFile* dex_file : dex_files) { |
| 443 | const DexFileDeps& deps = *GetDexFileDeps(*dex_file); |
| 444 | EncodeStringVector(buffer, deps.strings_); |
| 445 | EncodeSet(buffer, deps.assignable_types_); |
| 446 | EncodeSet(buffer, deps.unassignable_types_); |
| 447 | EncodeSet(buffer, deps.classes_); |
| 448 | EncodeSet(buffer, deps.fields_); |
| 449 | EncodeSet(buffer, deps.direct_methods_); |
| 450 | EncodeSet(buffer, deps.virtual_methods_); |
| 451 | EncodeSet(buffer, deps.interface_methods_); |
| 452 | EncodeUint16Vector(buffer, deps.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | VerifierDeps::VerifierDeps(const std::vector<const DexFile*>& dex_files, ArrayRef<uint8_t> data) |
| 457 | : VerifierDeps(dex_files) { |
| 458 | const uint8_t* data_start = data.data(); |
| 459 | const uint8_t* data_end = data_start + data.size(); |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 460 | for (const DexFile* dex_file : dex_files) { |
| 461 | DexFileDeps* deps = GetDexFileDeps(*dex_file); |
| 462 | DecodeStringVector(&data_start, data_end, &deps->strings_); |
| 463 | DecodeSet(&data_start, data_end, &deps->assignable_types_); |
| 464 | DecodeSet(&data_start, data_end, &deps->unassignable_types_); |
| 465 | DecodeSet(&data_start, data_end, &deps->classes_); |
| 466 | DecodeSet(&data_start, data_end, &deps->fields_); |
| 467 | DecodeSet(&data_start, data_end, &deps->direct_methods_); |
| 468 | DecodeSet(&data_start, data_end, &deps->virtual_methods_); |
| 469 | DecodeSet(&data_start, data_end, &deps->interface_methods_); |
| 470 | DecodeUint16Vector(&data_start, data_end, &deps->unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 471 | } |
| 472 | CHECK_LE(data_start, data_end); |
| 473 | } |
| 474 | |
| 475 | bool VerifierDeps::Equals(const VerifierDeps& rhs) const { |
| 476 | MutexLock mu(Thread::Current(), *Locks::verifier_deps_lock_); |
| 477 | |
| 478 | if (dex_deps_.size() != rhs.dex_deps_.size()) { |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | auto lhs_it = dex_deps_.begin(); |
| 483 | auto rhs_it = rhs.dex_deps_.begin(); |
| 484 | |
| 485 | for (; (lhs_it != dex_deps_.end()) && (rhs_it != rhs.dex_deps_.end()); lhs_it++, rhs_it++) { |
| 486 | const DexFile* lhs_dex_file = lhs_it->first; |
| 487 | const DexFile* rhs_dex_file = rhs_it->first; |
| 488 | if (lhs_dex_file != rhs_dex_file) { |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | DexFileDeps* lhs_deps = lhs_it->second.get(); |
| 493 | DexFileDeps* rhs_deps = rhs_it->second.get(); |
| 494 | if (!lhs_deps->Equals(*rhs_deps)) { |
| 495 | return false; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | DCHECK((lhs_it == dex_deps_.end()) && (rhs_it == rhs.dex_deps_.end())); |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | bool VerifierDeps::DexFileDeps::Equals(const VerifierDeps::DexFileDeps& rhs) const { |
| 504 | return (strings_ == rhs.strings_) && |
| 505 | (assignable_types_ == rhs.assignable_types_) && |
| 506 | (unassignable_types_ == rhs.unassignable_types_) && |
| 507 | (classes_ == rhs.classes_) && |
| 508 | (fields_ == rhs.fields_) && |
| 509 | (direct_methods_ == rhs.direct_methods_) && |
| 510 | (virtual_methods_ == rhs.virtual_methods_) && |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 511 | (interface_methods_ == rhs.interface_methods_) && |
| 512 | (unverified_classes_ == rhs.unverified_classes_); |
David Brazdil | 6f82fbd | 2016-09-14 11:55:26 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame^] | 515 | void VerifierDeps::Dump(VariableIndentationOutputStream* vios) const { |
| 516 | for (const auto& dep : dex_deps_) { |
| 517 | const DexFile& dex_file = *dep.first; |
| 518 | vios->Stream() |
| 519 | << "Dependencies of " |
| 520 | << dex_file.GetLocation() |
| 521 | << ":\n"; |
| 522 | |
| 523 | ScopedIndentation indent(vios); |
| 524 | |
| 525 | for (const std::string& str : dep.second->strings_) { |
| 526 | vios->Stream() << "Extra string: " << str << "\n"; |
| 527 | } |
| 528 | |
| 529 | for (const TypeAssignability& entry : dep.second->assignable_types_) { |
| 530 | vios->Stream() |
| 531 | << GetStringFromId(dex_file, entry.GetSource()) |
| 532 | << " must be assignable to " |
| 533 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 534 | << "\n"; |
| 535 | } |
| 536 | |
| 537 | for (const TypeAssignability& entry : dep.second->unassignable_types_) { |
| 538 | vios->Stream() |
| 539 | << GetStringFromId(dex_file, entry.GetSource()) |
| 540 | << " must not be assignable to " |
| 541 | << GetStringFromId(dex_file, entry.GetDestination()) |
| 542 | << "\n"; |
| 543 | } |
| 544 | |
| 545 | for (const ClassResolution& entry : dep.second->classes_) { |
| 546 | vios->Stream() |
| 547 | << dex_file.StringByTypeIdx(entry.GetDexTypeIndex()) |
| 548 | << (entry.IsResolved() ? " must be resolved " : "must not be resolved ") |
| 549 | << " with access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 550 | << "\n"; |
| 551 | } |
| 552 | |
| 553 | for (const FieldResolution& entry : dep.second->fields_) { |
| 554 | const DexFile::FieldId& field_id = dex_file.GetFieldId(entry.GetDexFieldIndex()); |
| 555 | vios->Stream() |
| 556 | << dex_file.GetFieldDeclaringClassDescriptor(field_id) << "->" |
| 557 | << dex_file.GetFieldName(field_id) << ":" |
| 558 | << dex_file.GetFieldTypeDescriptor(field_id) |
| 559 | << " is expected to be "; |
| 560 | if (!entry.IsResolved()) { |
| 561 | vios->Stream() << "unresolved\n"; |
| 562 | } else { |
| 563 | vios->Stream() |
| 564 | << "in class " |
| 565 | << GetStringFromId(dex_file, entry.GetDeclaringClassIndex()) |
| 566 | << ", and have the access flags " << std::hex << entry.GetAccessFlags() << std::dec |
| 567 | << "\n"; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | for (const auto& entry : |
| 572 | { std::make_pair(kDirectMethodResolution, dep.second->direct_methods_), |
| 573 | std::make_pair(kVirtualMethodResolution, dep.second->virtual_methods_), |
| 574 | std::make_pair(kInterfaceMethodResolution, dep.second->interface_methods_) }) { |
| 575 | for (const MethodResolution& method : entry.second) { |
| 576 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); |
| 577 | vios->Stream() |
| 578 | << dex_file.GetMethodDeclaringClassDescriptor(method_id) << "->" |
| 579 | << dex_file.GetMethodName(method_id) |
| 580 | << dex_file.GetMethodSignature(method_id).ToString() |
| 581 | << " is expected to be "; |
| 582 | if (!method.IsResolved()) { |
| 583 | vios->Stream() << "unresolved\n"; |
| 584 | } else { |
| 585 | vios->Stream() |
| 586 | << "in class " |
| 587 | << GetStringFromId(dex_file, method.GetDeclaringClassIndex()) |
| 588 | << ", have the access flags " << std::hex << method.GetAccessFlags() << std::dec |
| 589 | << ", and be of kind " << entry.first |
| 590 | << "\n"; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | for (uint16_t type_index : dep.second->unverified_classes_) { |
| 596 | vios->Stream() |
| 597 | << dex_file.StringByTypeIdx(type_index) |
| 598 | << " is expected to be verified at runtime\n"; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 603 | } // namespace verifier |
| 604 | } // namespace art |