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