| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -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 | |
| Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "method_verifier.h" |
| 18 | |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 20 | |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 22 | |
| Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 23 | #include "android-base/strings.h" |
| Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 24 | #include "base/metrics/metrics_test.h" |
| David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 25 | #include "base/utils.h" |
| Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 26 | #include "class_linker-inl.h" |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 27 | #include "class_verifier.h" |
| Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | #include "common_runtime_test.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 29 | #include "dex/dex_file-inl.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 30 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 31 | #include "verifier_enums.h" |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | namespace verifier { |
| 35 | |
| Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 36 | using metrics::test::CounterValue; |
| 37 | |
| Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 38 | class MethodVerifierTest : public CommonRuntimeTest { |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 39 | protected: |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 40 | void VerifyClass(const std::string& descriptor) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 41 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Vladimir Marko | e512556 | 2019-02-06 17:38:26 +0000 | [diff] [blame] | 42 | ASSERT_FALSE(descriptor.empty()); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 43 | Thread* self = Thread::Current(); |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 44 | StackHandleScope<3> hs(self); |
| 45 | Handle<mirror::Class> klass( |
| 46 | hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str()))); |
| 47 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 48 | Handle<mirror::ClassLoader> loader(hs.NewHandle(klass->GetClassLoader())); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 49 | |
| 50 | // Verify the class |
| 51 | std::string error_msg; |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 52 | FailureKind failure = ClassVerifier::VerifyClass(self, |
| 53 | /* verifier_deps= */ nullptr, |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 54 | dex_cache->GetDexFile(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 55 | klass, |
| Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 56 | dex_cache, |
| 57 | loader, |
| 58 | *klass->GetClassDef(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 59 | nullptr, |
| 60 | true, |
| 61 | HardFailLogMode::kLogWarning, |
| 62 | /* api_level= */ 0u, |
| 63 | &error_msg); |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 64 | |
| Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 65 | if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) { |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(failure == FailureKind::kSoftFailure || |
| 67 | failure == FailureKind::kNoFailure) << error_msg; |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 68 | |
| 69 | } else { |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 70 | ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg; |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 71 | } |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 74 | void VerifyDexFile(const DexFile& dex) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 75 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 76 | // Verify all the classes defined in this file |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 77 | for (size_t i = 0; i < dex.NumClassDefs(); i++) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 78 | const dex::ClassDef& class_def = dex.GetClassDef(i); |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 79 | const char* descriptor = dex.GetClassDescriptor(class_def); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 80 | VerifyClass(descriptor); |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | TEST_F(MethodVerifierTest, LibCore) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 86 | ScopedObjectAccess soa(Thread::Current()); |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 87 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 88 | VerifyDexFile(*java_lang_dex_file_); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 91 | // Make sure verification time metrics are collected. |
| 92 | TEST_F(MethodVerifierTest, VerificationTimeMetrics) { |
| 93 | ScopedObjectAccess soa(Thread::Current()); |
| 94 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 95 | auto* class_verification_total_time = GetMetrics()->ClassVerificationTotalTime(); |
| Eric Holk | e7ff7ef | 2021-03-17 16:42:24 -0700 | [diff] [blame] | 96 | auto* class_verification_count = GetMetrics()->ClassVerificationCount(); |
| Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 97 | const uint64_t original_time = CounterValue(*class_verification_total_time); |
| Eric Holk | e7ff7ef | 2021-03-17 16:42:24 -0700 | [diff] [blame] | 98 | const uint64_t original_count = CounterValue(*class_verification_count); |
| Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 99 | VerifyDexFile(*java_lang_dex_file_); |
| Eric Holk | 096bef8 | 2020-10-19 12:04:39 -0700 | [diff] [blame] | 100 | ASSERT_GT(CounterValue(*class_verification_total_time), original_time); |
| Eric Holk | e7ff7ef | 2021-03-17 16:42:24 -0700 | [diff] [blame] | 101 | ASSERT_GT(CounterValue(*class_verification_count), original_count); |
| Eric Holk | a79872b | 2020-10-01 13:09:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 104 | } // namespace verifier |
| 105 | } // namespace art |