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 "method_verifier.h"
18
19 #include <stdio.h>
20
21 #include <memory>
22
23 #include "android-base/strings.h"
24 #include "base/metrics/metrics_test.h"
25 #include "base/utils.h"
26 #include "class_linker-inl.h"
27 #include "class_verifier.h"
28 #include "common_runtime_test.h"
29 #include "dex/dex_file-inl.h"
30 #include "scoped_thread_state_change-inl.h"
31 #include "verifier_enums.h"
32
33 namespace art HIDDEN {
34 namespace verifier {
35
36 using metrics::test::CounterValue;
37
38 class MethodVerifierTest : public CommonRuntimeTest {
39 protected:
MethodVerifierTest()40 MethodVerifierTest() {
41 use_boot_image_ = true; // Make the Runtime creation cheaper.
42 }
43
VerifyClass(const std::string & descriptor)44 void VerifyClass(const std::string& descriptor)
45 REQUIRES_SHARED(Locks::mutator_lock_) {
46 ASSERT_FALSE(descriptor.empty());
47 Thread* self = Thread::Current();
48 StackHandleScope<3> hs(self);
49 Handle<mirror::Class> klass(
50 hs.NewHandle(class_linker_->FindSystemClass(self, descriptor.c_str())));
51 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
52 Handle<mirror::ClassLoader> loader(hs.NewHandle(klass->GetClassLoader()));
53
54 // Verify the class
55 std::string error_msg;
56 FailureKind failure = ClassVerifier::VerifyClass(self,
57 /* verifier_deps= */ nullptr,
58 dex_cache->GetDexFile(),
59 klass,
60 dex_cache,
61 loader,
62 *klass->GetClassDef(),
63 nullptr,
64 HardFailLogMode::kLogWarning,
65 /* api_level= */ 0u,
66 &error_msg);
67
68 if (descriptor.starts_with("Ljava/lang/invoke")) {
69 ASSERT_TRUE(failure == FailureKind::kSoftFailure ||
70 failure == FailureKind::kNoFailure) << error_msg;
71
72 } else {
73 ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg;
74 }
75 }
76
VerifyDexFile(const DexFile & dex)77 void VerifyDexFile(const DexFile& dex)
78 REQUIRES_SHARED(Locks::mutator_lock_) {
79 // Verify all the classes defined in this file
80 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
81 const dex::ClassDef& class_def = dex.GetClassDef(i);
82 const char* descriptor = dex.GetClassDescriptor(class_def);
83 VerifyClass(descriptor);
84 }
85 }
86 };
87
TEST_F(MethodVerifierTest,LibCore)88 TEST_F(MethodVerifierTest, LibCore) {
89 ScopedObjectAccess soa(Thread::Current());
90 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
91 VerifyDexFile(*java_lang_dex_file_);
92 }
93
94 // Make sure verification time metrics are collected.
TEST_F(MethodVerifierTest,VerificationTimeMetrics)95 TEST_F(MethodVerifierTest, VerificationTimeMetrics) {
96 ScopedObjectAccess soa(Thread::Current());
97 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
98 auto* class_verification_total_time = GetMetrics()->ClassVerificationTotalTime();
99 auto* class_verification_count = GetMetrics()->ClassVerificationCount();
100 const uint64_t original_time = CounterValue(*class_verification_total_time);
101 const uint64_t original_count = CounterValue(*class_verification_count);
102 VerifyDexFile(*java_lang_dex_file_);
103 ASSERT_GT(CounterValue(*class_verification_total_time), original_time);
104 ASSERT_GT(CounterValue(*class_verification_count), original_count);
105 }
106
107 } // namespace verifier
108 } // namespace art
109