blob: 646987a1e56ffbbc87ca986744c105b34d05eeea [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "method_verifier.h"
18
Ian Rogers776ac1f2012-04-13 23:36:36 -070019#include <stdio.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Ian Rogers776ac1f2012-04-13 23:36:36 -070021
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023#include "common_runtime_test.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070024#include "dex_file.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "scoped_thread_state_change.h"
Andreas Gampe5fd66d02016-09-12 20:22:19 -070026#include "verifier_log_mode.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027
28namespace art {
29namespace verifier {
30
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031class MethodVerifierTest : public CommonRuntimeTest {
Ian Rogers776ac1f2012-04-13 23:36:36 -070032 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 void VerifyClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070034 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070035 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -070036 Thread* self = Thread::Current();
37 mirror::Class* klass = class_linker_->FindSystemClass(self, descriptor.c_str());
Ian Rogers776ac1f2012-04-13 23:36:36 -070038
39 // Verify the class
40 std::string error_msg;
Andreas Gampe5fd66d02016-09-12 20:22:19 -070041 MethodVerifier::FailureKind failure = MethodVerifier::VerifyClass(
42 self, klass, nullptr, true, HardFailLogMode::kLogWarning, &error_msg);
Andreas Gampe7fe30232016-03-25 16:58:00 -070043 ASSERT_TRUE(failure == MethodVerifier::kNoFailure) << error_msg;
Ian Rogers776ac1f2012-04-13 23:36:36 -070044 }
45
Richard Uhlerfbef44d2014-12-23 09:48:51 -080046 void VerifyDexFile(const DexFile& dex)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070048 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -080049 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
50 const DexFile::ClassDef& class_def = dex.GetClassDef(i);
51 const char* descriptor = dex.GetClassDescriptor(class_def);
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 VerifyClass(descriptor);
53 }
54 }
55};
56
57TEST_F(MethodVerifierTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -080059 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
60 VerifyDexFile(*java_lang_dex_file_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070061}
62
Ian Rogers776ac1f2012-04-13 23:36:36 -070063} // namespace verifier
64} // namespace art