| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "class_verifier.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/stringprintf.h> |
| 21 | |
| 22 | #include "art_method-inl.h" |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 23 | #include "base/enums.h" |
| Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 24 | #include "base/locks.h" |
| 25 | #include "base/logging.h" |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 26 | #include "base/systrace.h" |
| 27 | #include "base/utils.h" |
| 28 | #include "class_linker.h" |
| 29 | #include "compiler_callbacks.h" |
| 30 | #include "dex/class_accessor-inl.h" |
| 31 | #include "dex/class_reference.h" |
| 32 | #include "dex/descriptors_names.h" |
| 33 | #include "dex/dex_file-inl.h" |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 34 | #include "handle.h" |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 35 | #include "handle_scope-inl.h" |
| 36 | #include "method_verifier-inl.h" |
| 37 | #include "mirror/class-inl.h" |
| 38 | #include "mirror/dex_cache.h" |
| 39 | #include "runtime.h" |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 40 | #include "thread.h" |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 41 | #include "verifier_compiler_binding.h" |
| Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 42 | #include "verifier/method_verifier.h" |
| 43 | #include "verifier/reg_type_cache.h" |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | namespace verifier { |
| 47 | |
| 48 | using android::base::StringPrintf; |
| 49 | |
| 50 | // We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make |
| 51 | // sure we only print this once. |
| 52 | static bool gPrintedDxMonitorText = false; |
| 53 | |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 54 | static void UpdateMethodFlags(uint32_t method_index, |
| 55 | Handle<mirror::Class> klass, |
| 56 | Handle<mirror::DexCache> dex_cache, |
| Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 57 | CompilerCallbacks* callbacks, |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 58 | int error_types) |
| 59 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 60 | if (callbacks != nullptr && !CanCompilerHandleVerificationFailure(error_types)) { |
| 61 | MethodReference ref(dex_cache->GetDexFile(), method_index); |
| 62 | callbacks->AddUncompilableMethod(ref); |
| 63 | } |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 64 | if (klass == nullptr) { |
| 65 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 66 | // Flags will be set at runtime. |
| 67 | return; |
| Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 68 | } |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 69 | |
| 70 | // Mark methods with DontCompile/MustCountLocks flags. |
| 71 | ClassLinker* const linker = Runtime::Current()->GetClassLinker(); |
| 72 | ArtMethod* method = |
| 73 | klass->FindClassMethod(dex_cache.Get(), method_index, linker->GetImagePointerSize()); |
| 74 | DCHECK(method != nullptr); |
| 75 | DCHECK(method->GetDeclaringClass() == klass.Get()); |
| 76 | if (!CanCompilerHandleVerificationFailure(error_types)) { |
| 77 | method->SetDontCompile(); |
| Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 78 | } |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 79 | if ((error_types & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
| 80 | method->SetMustCountLocks(); |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 81 | } |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | FailureKind ClassVerifier::VerifyClass(Thread* self, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 85 | VerifierDeps* verifier_deps, |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 86 | const DexFile* dex_file, |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 87 | Handle<mirror::Class> klass, |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 88 | Handle<mirror::DexCache> dex_cache, |
| 89 | Handle<mirror::ClassLoader> class_loader, |
| 90 | const dex::ClassDef& class_def, |
| 91 | CompilerCallbacks* callbacks, |
| 92 | bool allow_soft_failures, |
| 93 | HardFailLogMode log_level, |
| 94 | uint32_t api_level, |
| Alex Light | b1eebde | 2019-10-22 16:30:47 +0000 | [diff] [blame] | 95 | std::string* error) { |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 96 | // A class must not be abstract and final. |
| 97 | if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
| 98 | *error = "Verifier rejected class "; |
| 99 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| 100 | *error += ": class is abstract and final."; |
| 101 | return FailureKind::kHardFailure; |
| 102 | } |
| 103 | |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 104 | // Note that `klass` can be a redefined class, not in the loader's table yet. |
| 105 | // Therefore, we do not use it for class resolution, but only when needing to |
| 106 | // update its methods' flags. |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 107 | ClassAccessor accessor(*dex_file, class_def); |
| 108 | SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(accessor.GetDescriptor()); |
| Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 109 | metrics::AutoTimer timer{GetMetrics()->ClassVerificationTotalTime()}; |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 110 | |
| 111 | int64_t previous_method_idx[2] = { -1, -1 }; |
| 112 | MethodVerifier::FailureData failure_data; |
| 113 | ClassLinker* const linker = Runtime::Current()->GetClassLinker(); |
| 114 | |
| 115 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
| 116 | int64_t* previous_idx = &previous_method_idx[method.IsStaticOrDirect() ? 0u : 1u]; |
| 117 | self->AllowThreadSuspension(); |
| 118 | const uint32_t method_idx = method.GetIndex(); |
| 119 | if (method_idx == *previous_idx) { |
| 120 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 121 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 122 | continue; |
| 123 | } |
| 124 | *previous_idx = method_idx; |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 125 | std::string hard_failure_msg; |
| 126 | MethodVerifier::FailureData result = |
| 127 | MethodVerifier::VerifyMethod(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 128 | linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 129 | Runtime::Current()->GetArenaPool(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 130 | verifier_deps, |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 131 | method_idx, |
| 132 | dex_file, |
| 133 | dex_cache, |
| 134 | class_loader, |
| 135 | class_def, |
| 136 | method.GetCodeItem(), |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 137 | method.GetAccessFlags(), |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 138 | allow_soft_failures, |
| 139 | log_level, |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 140 | api_level, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 141 | Runtime::Current()->IsAotCompiler(), |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 142 | &hard_failure_msg); |
| 143 | if (result.kind == FailureKind::kHardFailure) { |
| 144 | if (failure_data.kind == FailureKind::kHardFailure) { |
| 145 | // If we logged an error before, we need a newline. |
| 146 | *error += "\n"; |
| 147 | } else { |
| 148 | // If we didn't log a hard failure before, print the header of the message. |
| 149 | *error += "Verifier rejected class "; |
| 150 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| 151 | *error += ":"; |
| 152 | } |
| 153 | *error += " "; |
| 154 | *error += hard_failure_msg; |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 155 | } else if (result.kind != FailureKind::kNoFailure) { |
| Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 156 | UpdateMethodFlags(method.GetIndex(), klass, dex_cache, callbacks, result.types); |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 157 | if ((result.types & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
| 158 | // Print a warning about expected slow-down. |
| 159 | // Use a string temporary to print one contiguous warning. |
| 160 | std::string tmp = |
| 161 | StringPrintf("Method %s failed lock verification and will run slower.", |
| 162 | dex_file->PrettyMethod(method.GetIndex()).c_str()); |
| 163 | if (!gPrintedDxMonitorText) { |
| 164 | tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n" |
| 165 | "and incorrect proguard optimizations."; |
| 166 | gPrintedDxMonitorText = true; |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 167 | } |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 168 | LOG(WARNING) << tmp; |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 169 | } |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 170 | } |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 171 | |
| 172 | // Merge the result for the method into the global state for the class. |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 173 | failure_data.Merge(result); |
| 174 | } |
| Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 175 | uint64_t elapsed_time_microseconds = timer.Stop(); |
| Eric Holk | 4bb0900 | 2020-09-30 11:42:34 -0700 | [diff] [blame] | 176 | VLOG(verifier) << "VerifyClass took " << PrettyDuration(UsToNs(elapsed_time_microseconds)) |
| Rock.Yeh | fa88d52 | 2020-06-23 21:29:19 +0800 | [diff] [blame] | 177 | << ", class: " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 178 | |
| Eric Holk | e7ff7ef | 2021-03-17 16:42:24 -0700 | [diff] [blame] | 179 | GetMetrics()->ClassVerificationCount()->AddOne(); |
| Nicolas Geoffray | 9e050ab | 2021-07-14 14:59:25 +0100 | [diff] [blame] | 180 | |
| 181 | if (failure_data.kind == verifier::FailureKind::kHardFailure && callbacks != nullptr) { |
| 182 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
| 183 | callbacks->ClassRejected(ref); |
| 184 | } |
| 185 | |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 186 | return failure_data.kind; |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 189 | void ClassVerifier::Init(ClassLinker* class_linker) { |
| 190 | MethodVerifier::Init(class_linker); |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void ClassVerifier::Shutdown() { |
| 194 | MethodVerifier::Shutdown(); |
| 195 | } |
| 196 | |
| 197 | void ClassVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 198 | MethodVerifier::VisitStaticRoots(visitor); |
| 199 | } |
| 200 | |
| 201 | } // namespace verifier |
| 202 | } // namespace art |