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 != nullptr);
35 Thread* self = Thread::Current();
36 mirror::Class* klass = class_linker_->FindSystemClass(self, descriptor.c_str());
37
38 // Verify the class
39 std::string error_msg;
40 ASSERT_TRUE(MethodVerifier::VerifyClass(self, klass, true, &error_msg) == MethodVerifier::kNoFailure)
41 << error_msg;
42 }
43
VerifyDexFile(const DexFile & dex)44 void VerifyDexFile(const DexFile& dex)
45 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
46 // Verify all the classes defined in this file
47 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
48 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
49 const char* descriptor = dex.GetClassDescriptor(class_def);
50 VerifyClass(descriptor);
51 }
52 }
53 };
54
TEST_F(MethodVerifierTest,LibCore)55 TEST_F(MethodVerifierTest, LibCore) {
56 ScopedObjectAccess soa(Thread::Current());
57 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
58 VerifyDexFile(*java_lang_dex_file_);
59 }
60
61 } // namespace verifier
62 } // namespace art
63