blob: d9874677e8764bca519df295d02c8792a09828a6 [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
Andreas Gampe9186ced2016-12-12 14:28:21 -080022#include "android-base/strings.h"
23
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_runtime_test.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070026#include "dex_file-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Narayan Kamath56ee4892016-10-28 10:57:41 +010028#include "utils.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070029#include "verifier_enums.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070030
31namespace art {
32namespace verifier {
33
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080034class MethodVerifierTest : public CommonRuntimeTest {
Ian Rogers776ac1f2012-04-13 23:36:36 -070035 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 void VerifyClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 ASSERT_TRUE(descriptor != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -070039 Thread* self = Thread::Current();
40 mirror::Class* klass = class_linker_->FindSystemClass(self, descriptor.c_str());
Ian Rogers776ac1f2012-04-13 23:36:36 -070041
42 // Verify the class
43 std::string error_msg;
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070044 FailureKind failure = MethodVerifier::VerifyClass(
Andreas Gampe5fd66d02016-09-12 20:22:19 -070045 self, klass, nullptr, true, HardFailLogMode::kLogWarning, &error_msg);
Narayan Kamath56ee4892016-10-28 10:57:41 +010046
Andreas Gampe9186ced2016-12-12 14:28:21 -080047 if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070048 ASSERT_TRUE(failure == FailureKind::kSoftFailure ||
49 failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010050
51 } else {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070052 ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010053 }
Ian Rogers776ac1f2012-04-13 23:36:36 -070054 }
55
Richard Uhlerfbef44d2014-12-23 09:48:51 -080056 void VerifyDexFile(const DexFile& dex)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070057 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -080059 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);
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 VerifyClass(descriptor);
63 }
64 }
65};
66
67TEST_F(MethodVerifierTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -080069 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
70 VerifyDexFile(*java_lang_dex_file_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070071}
72
Ian Rogers776ac1f2012-04-13 23:36:36 -070073} // namespace verifier
74} // namespace art