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 "class_linker-inl.h"
23 #include "common_runtime_test.h"
24 #include "dex_file.h"
25 #include "scoped_thread_state_change.h"
26 
27 namespace art {
28 namespace verifier {
29 
30 class MethodVerifierTest : public CommonRuntimeTest {
31  protected:
VerifyClass(const std::string & descriptor)32   void VerifyClass(const std::string& descriptor)
33       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
34     ASSERT_TRUE(descriptor != NULL);
35     mirror::Class* klass = class_linker_->FindSystemClass(Thread::Current(), descriptor.c_str());
36 
37     // Verify the class
38     std::string error_msg;
39     ASSERT_TRUE(MethodVerifier::VerifyClass(klass, true, &error_msg) == MethodVerifier::kNoFailure)
40         << error_msg;
41   }
42 
VerifyDexFile(const DexFile * dex)43   void VerifyDexFile(const DexFile* dex)
44       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
45     ASSERT_TRUE(dex != NULL);
46 
47     // Verify all the classes defined in this file
48     for (size_t i = 0; i < dex->NumClassDefs(); i++) {
49       const DexFile::ClassDef& class_def = dex->GetClassDef(i);
50       const char* descriptor = dex->GetClassDescriptor(class_def);
51       VerifyClass(descriptor);
52     }
53   }
54 };
55 
TEST_F(MethodVerifierTest,LibCore)56 TEST_F(MethodVerifierTest, LibCore) {
57   ScopedObjectAccess soa(Thread::Current());
58   VerifyDexFile(java_lang_dex_file_);
59 }
60 
61 }  // namespace verifier
62 }  // namespace art
63