Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "method_verifier.h" |
| 18 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 22 | #include "android-base/strings.h" |
| 23 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 24 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | #include "common_runtime_test.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 27 | #include "scoped_thread_state_change-inl.h" |
Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 28 | #include "utils.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame^] | 29 | #include "verifier_enums.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace verifier { |
| 33 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 34 | class MethodVerifierTest : public CommonRuntimeTest { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 35 | protected: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 36 | void VerifyClass(const std::string& descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 37 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 38 | ASSERT_TRUE(descriptor != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 39 | Thread* self = Thread::Current(); |
| 40 | mirror::Class* klass = class_linker_->FindSystemClass(self, descriptor.c_str()); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 41 | |
| 42 | // Verify the class |
| 43 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame^] | 44 | FailureKind failure = MethodVerifier::VerifyClass( |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 45 | self, klass, nullptr, true, HardFailLogMode::kLogWarning, &error_msg); |
Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 46 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 47 | if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame^] | 48 | ASSERT_TRUE(failure == FailureKind::kSoftFailure || |
| 49 | failure == FailureKind::kNoFailure) << error_msg; |
Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 50 | |
| 51 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame^] | 52 | ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg; |
Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 53 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 56 | void VerifyDexFile(const DexFile& dex) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 57 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 58 | // Verify all the classes defined in this file |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 59 | 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 62 | VerifyClass(descriptor); |
| 63 | } |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | TEST_F(MethodVerifierTest, LibCore) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | ScopedObjectAccess soa(Thread::Current()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 69 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 70 | VerifyDexFile(*java_lang_dex_file_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 73 | } // namespace verifier |
| 74 | } // namespace art |