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 #include <memory>
21 
22 #include "android-base/strings.h"
23 
24 #include "base/utils.h"
25 #include "class_linker-inl.h"
26 #include "class_verifier.h"
27 #include "common_runtime_test.h"
28 #include "dex/dex_file-inl.h"
29 #include "scoped_thread_state_change-inl.h"
30 #include "verifier_enums.h"
31 
32 namespace art {
33 namespace verifier {
34 
35 class MethodVerifierTest : public CommonRuntimeTest {
36  protected:
VerifyClass(const std::string & descriptor)37   void VerifyClass(const std::string& descriptor)
38       REQUIRES_SHARED(Locks::mutator_lock_) {
39     ASSERT_FALSE(descriptor.empty());
40     Thread* self = Thread::Current();
41     ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(self, descriptor.c_str());
42 
43     // Verify the class
44     std::string error_msg;
45     FailureKind failure = ClassVerifier::VerifyClass(
46         self, klass, nullptr, true, HardFailLogMode::kLogWarning, /* api_level= */ 0u, &error_msg);
47 
48     if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) {
49       ASSERT_TRUE(failure == FailureKind::kSoftFailure ||
50                   failure == FailureKind::kNoFailure) << error_msg;
51 
52     } else {
53       ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg;
54     }
55   }
56 
VerifyDexFile(const DexFile & dex)57   void VerifyDexFile(const DexFile& dex)
58       REQUIRES_SHARED(Locks::mutator_lock_) {
59     // Verify all the classes defined in this file
60     for (size_t i = 0; i < dex.NumClassDefs(); i++) {
61       const dex::ClassDef& class_def = dex.GetClassDef(i);
62       const char* descriptor = dex.GetClassDescriptor(class_def);
63       VerifyClass(descriptor);
64     }
65   }
66 };
67 
TEST_F(MethodVerifierTest,LibCore)68 TEST_F(MethodVerifierTest, LibCore) {
69   ScopedObjectAccess soa(Thread::Current());
70   ASSERT_TRUE(java_lang_dex_file_ != nullptr);
71   VerifyDexFile(*java_lang_dex_file_);
72 }
73 
74 }  // namespace verifier
75 }  // namespace art
76