blob: aa794704c574997bc5b76cde8b2c525a9930eaac [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
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 Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "method_verifier.h"
18
Ian Rogers776ac1f2012-04-13 23:36:36 -070019#include <stdio.h>
Eric Holk096bef82020-10-19 12:04:39 -070020
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Ian Rogers776ac1f2012-04-13 23:36:36 -070022
Andreas Gampe9186ced2016-12-12 14:28:21 -080023#include "android-base/strings.h"
Eric Holk480d9812021-01-27 23:41:45 +000024#include "base/metrics/metrics_test.h"
David Sehrc431b9d2018-03-02 12:01:51 -080025#include "base/utils.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "class_linker-inl.h"
Andreas Gampea43ba3d2019-03-13 15:49:20 -070027#include "class_verifier.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_thread_state_change-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070031#include "verifier_enums.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070032
33namespace art {
34namespace verifier {
35
Eric Holk096bef82020-10-19 12:04:39 -070036using metrics::test::CounterValue;
37
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080038class MethodVerifierTest : public CommonRuntimeTest {
Ian Rogers776ac1f2012-04-13 23:36:36 -070039 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 void VerifyClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe5125562019-02-06 17:38:26 +000042 ASSERT_FALSE(descriptor.empty());
Ian Rogers7b078e82014-09-10 14:44:24 -070043 Thread* self = Thread::Current();
Nicolas Geoffray7744b692021-07-06 16:19:32 +010044 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 Rogers776ac1f2012-04-13 23:36:36 -070049
50 // Verify the class
51 std::string error_msg;
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +000052 FailureKind failure = ClassVerifier::VerifyClass(self,
53 /* verifier_deps= */ nullptr,
Nicolas Geoffray7744b692021-07-06 16:19:32 +010054 dex_cache->GetDexFile(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +000055 klass,
Nicolas Geoffray7744b692021-07-06 16:19:32 +010056 dex_cache,
57 loader,
58 *klass->GetClassDef(),
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +000059 nullptr,
60 true,
61 HardFailLogMode::kLogWarning,
62 /* api_level= */ 0u,
63 &error_msg);
Narayan Kamath56ee4892016-10-28 10:57:41 +010064
Andreas Gampe9186ced2016-12-12 14:28:21 -080065 if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070066 ASSERT_TRUE(failure == FailureKind::kSoftFailure ||
67 failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010068
69 } else {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070070 ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010071 }
Ian Rogers776ac1f2012-04-13 23:36:36 -070072 }
73
Richard Uhlerfbef44d2014-12-23 09:48:51 -080074 void VerifyDexFile(const DexFile& dex)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070075 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070076 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -080077 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080078 const dex::ClassDef& class_def = dex.GetClassDef(i);
Richard Uhlerfbef44d2014-12-23 09:48:51 -080079 const char* descriptor = dex.GetClassDescriptor(class_def);
Ian Rogers776ac1f2012-04-13 23:36:36 -070080 VerifyClass(descriptor);
81 }
82 }
83};
84
85TEST_F(MethodVerifierTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -080087 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
88 VerifyDexFile(*java_lang_dex_file_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070089}
90
Eric Holka79872b2020-10-01 13:09:53 -070091// Make sure verification time metrics are collected.
92TEST_F(MethodVerifierTest, VerificationTimeMetrics) {
93 ScopedObjectAccess soa(Thread::Current());
94 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
Eric Holk096bef82020-10-19 12:04:39 -070095 auto* class_verification_total_time = GetMetrics()->ClassVerificationTotalTime();
Eric Holke7ff7ef2021-03-17 16:42:24 -070096 auto* class_verification_count = GetMetrics()->ClassVerificationCount();
Eric Holk096bef82020-10-19 12:04:39 -070097 const uint64_t original_time = CounterValue(*class_verification_total_time);
Eric Holke7ff7ef2021-03-17 16:42:24 -070098 const uint64_t original_count = CounterValue(*class_verification_count);
Eric Holka79872b2020-10-01 13:09:53 -070099 VerifyDexFile(*java_lang_dex_file_);
Eric Holk096bef82020-10-19 12:04:39 -0700100 ASSERT_GT(CounterValue(*class_verification_total_time), original_time);
Eric Holke7ff7ef2021-03-17 16:42:24 -0700101 ASSERT_GT(CounterValue(*class_verification_count), original_count);
Eric Holka79872b2020-10-01 13:09:53 -0700102}
103
Ian Rogers776ac1f2012-04-13 23:36:36 -0700104} // namespace verifier
105} // namespace art