| 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 | |
| David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/utils.h" |
| Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 25 | #include "class_linker-inl.h" |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 26 | #include "class_verifier.h" |
| Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 27 | #include "common_runtime_test.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 28 | #include "dex/dex_file-inl.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 29 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 30 | #include "verifier_enums.h" |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | namespace verifier { |
| 34 | |
| Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 35 | class MethodVerifierTest : public CommonRuntimeTest { |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 36 | protected: |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | void VerifyClass(const std::string& descriptor) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 38 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Vladimir Marko | e512556 | 2019-02-06 17:38:26 +0000 | [diff] [blame] | 39 | ASSERT_FALSE(descriptor.empty()); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 40 | Thread* self = Thread::Current(); |
| Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 41 | ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(self, descriptor.c_str()); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 42 | |
| 43 | // Verify the class |
| 44 | std::string error_msg; |
| Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 45 | FailureKind failure = ClassVerifier::VerifyClass( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 46 | self, klass, nullptr, true, HardFailLogMode::kLogWarning, /* api_level= */ 0u, &error_msg); |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 47 | |
| Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 48 | if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) { |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(failure == FailureKind::kSoftFailure || |
| 50 | failure == FailureKind::kNoFailure) << error_msg; |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 51 | |
| 52 | } else { |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg; |
| Narayan Kamath | 56ee489 | 2016-10-28 10:57:41 +0100 | [diff] [blame] | 54 | } |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 57 | void VerifyDexFile(const DexFile& dex) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 58 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 59 | // Verify all the classes defined in this file |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 60 | for (size_t i = 0; i < dex.NumClassDefs(); i++) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 61 | const dex::ClassDef& class_def = dex.GetClassDef(i); |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 62 | const char* descriptor = dex.GetClassDescriptor(class_def); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 63 | VerifyClass(descriptor); |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | TEST_F(MethodVerifierTest, LibCore) { |
| Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | ScopedObjectAccess soa(Thread::Current()); |
| Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 70 | ASSERT_TRUE(java_lang_dex_file_ != nullptr); |
| 71 | VerifyDexFile(*java_lang_dex_file_); |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 74 | } // namespace verifier |
| 75 | } // namespace art |