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_REQUIRES(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 MethodVerifier::FailureKind failure = MethodVerifier::VerifyClass(self,
41 klass,
42 nullptr,
43 true,
44 LogSeverity::WARNING,
45 &error_msg);
46 ASSERT_TRUE(failure == MethodVerifier::kNoFailure) << error_msg;
47 }
48
VerifyDexFile(const DexFile & dex)49 void VerifyDexFile(const DexFile& dex)
50 SHARED_REQUIRES(Locks::mutator_lock_) {
51 // Verify all the classes defined in this file
52 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
53 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
54 const char* descriptor = dex.GetClassDescriptor(class_def);
55 VerifyClass(descriptor);
56 }
57 }
58 };
59
TEST_F(MethodVerifierTest,LibCore)60 TEST_F(MethodVerifierTest, LibCore) {
61 ScopedObjectAccess soa(Thread::Current());
62 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
63 VerifyDexFile(*java_lang_dex_file_);
64 }
65
66 } // namespace verifier
67 } // namespace art
68