blob: e56cde9d41953b5c47399c55a8faece815f1ba1c [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
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "class_linker-inl.h"
Andreas Gampea43ba3d2019-03-13 15:49:20 -070026#include "class_verifier.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027#include "common_runtime_test.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070029#include "scoped_thread_state_change-inl.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070030#include "verifier_enums.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070031
32namespace art {
33namespace verifier {
34
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080035class MethodVerifierTest : public CommonRuntimeTest {
Ian Rogers776ac1f2012-04-13 23:36:36 -070036 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037 void VerifyClass(const std::string& descriptor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070038 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe5125562019-02-06 17:38:26 +000039 ASSERT_FALSE(descriptor.empty());
Ian Rogers7b078e82014-09-10 14:44:24 -070040 Thread* self = Thread::Current();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +010041 ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(self, descriptor.c_str());
Ian Rogers776ac1f2012-04-13 23:36:36 -070042
43 // Verify the class
44 std::string error_msg;
Andreas Gampea43ba3d2019-03-13 15:49:20 -070045 FailureKind failure = ClassVerifier::VerifyClass(
Andreas Gampe98ea9d92018-10-19 14:06:15 -070046 self, klass, nullptr, true, HardFailLogMode::kLogWarning, /* api_level= */ 0u, &error_msg);
Narayan Kamath56ee4892016-10-28 10:57:41 +010047
Andreas Gampe9186ced2016-12-12 14:28:21 -080048 if (android::base::StartsWith(descriptor, "Ljava/lang/invoke")) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070049 ASSERT_TRUE(failure == FailureKind::kSoftFailure ||
50 failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010051
52 } else {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070053 ASSERT_TRUE(failure == FailureKind::kNoFailure) << error_msg;
Narayan Kamath56ee4892016-10-28 10:57:41 +010054 }
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 }
56
Richard Uhlerfbef44d2014-12-23 09:48:51 -080057 void VerifyDexFile(const DexFile& dex)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070059 // Verify all the classes defined in this file
Richard Uhlerfbef44d2014-12-23 09:48:51 -080060 for (size_t i = 0; i < dex.NumClassDefs(); i++) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080061 const dex::ClassDef& class_def = dex.GetClassDef(i);
Richard Uhlerfbef44d2014-12-23 09:48:51 -080062 const char* descriptor = dex.GetClassDescriptor(class_def);
Ian Rogers776ac1f2012-04-13 23:36:36 -070063 VerifyClass(descriptor);
64 }
65 }
66};
67
68TEST_F(MethodVerifierTest, LibCore) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -080070 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
71 VerifyDexFile(*java_lang_dex_file_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070072}
73
Ian Rogers776ac1f2012-04-13 23:36:36 -070074} // namespace verifier
75} // namespace art