blob: 892abb4dbba07f35ea29a7e9c4f224bb68099dd9 [file] [log] [blame]
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -08001/*
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
17#ifndef ART_RUNTIME_COMMON_RUNTIME_TEST_H_
18#define ART_RUNTIME_COMMON_RUNTIME_TEST_H_
19
Ian Rogerse63db272014-07-15 15:36:11 -070020#include <gtest/gtest.h>
21#include <jni.h>
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022
Ian Rogerse63db272014-07-15 15:36:11 -070023#include <string>
24
Andreas Gampe170331f2017-12-07 18:41:03 -080025#include <android-base/logging.h>
26
David Srbecky3e52aa42015-04-12 07:45:18 +010027#include "arch/instruction_set.h"
David Sehrd5f8de82018-04-27 14:12:03 -070028#include "base/common_art_test.h"
David Sehr1979c642018-04-26 14:41:18 -070029#include "base/globals.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/os.h"
Mathieu Chartier2daa1342018-02-20 16:19:28 -080032#include "base/unix_file/fd_file.h"
33#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/compact_dex_level.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070035// TODO: Add inl file and avoid including inl.
36#include "obj_ptr-inl.h"
Calin Juravlec79470d2017-07-12 17:37:42 -070037#include "scoped_thread_state_change-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080038
39namespace art {
40
Andreas Gampe170331f2017-12-07 18:41:03 -080041using LogSeverity = android::base::LogSeverity;
42using ScopedLogSeverity = android::base::ScopedLogSeverity;
43
Mathieu Chartier3398c782016-09-30 10:27:43 -070044// OBJ pointer helpers to avoid needing .Decode everywhere.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070045#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
46#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
47#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
48#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -070049
Ian Rogerse63db272014-07-15 15:36:11 -070050class ClassLinker;
51class CompilerCallbacks;
52class DexFile;
53class JavaVMExt;
54class Runtime;
55typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
Andreas Gampe26761f72017-07-20 18:00:39 -070056class Thread;
57class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070058
David Sehrd5f8de82018-04-27 14:12:03 -070059class CommonRuntimeTestImpl : public CommonArtTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080060 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080061 CommonRuntimeTestImpl();
62 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070063
David Srbecky3e52aa42015-04-12 07:45:18 +010064 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
65
Andreas Gampe26761f72017-07-20 18:00:39 -070066 // A helper function to fill the heap.
67 static void FillHeap(Thread* self,
68 ClassLinker* class_linker,
69 VariableSizedHandleScope* handle_scope)
70 REQUIRES_SHARED(Locks::mutator_lock_);
71 // A helper to set up a small heap (4M) to make FillHeap faster.
72 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
73
Mathieu Chartier2daa1342018-02-20 16:19:28 -080074 template <typename Mutator>
75 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
76 std::vector<std::unique_ptr<const DexFile>> dex_files;
77 std::string error_msg;
78 const ArtDexFileLoader dex_file_loader;
79 CHECK(dex_file_loader.Open(input_jar.c_str(),
80 input_jar.c_str(),
81 /*verify*/ true,
82 /*verify_checksum*/ true,
83 &error_msg,
84 &dex_files)) << error_msg;
85 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
86 const std::unique_ptr<const DexFile>& dex = dex_files[0];
87 CHECK(dex->EnableWrite()) << "Failed to enable write";
88 DexFile* dex_file = const_cast<DexFile*>(dex.get());
89 mutator(dex_file);
90 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
91 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
92 return false;
93 }
94 if (output_dex->Flush() != 0) {
95 PLOG(FATAL) << "Could not flush the output file.";
96 }
97 return true;
98 }
99
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800100 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800101 // Allow subclases such as CommonCompilerTest to add extra options.
102 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
103
104 // Called before the runtime is created.
105 virtual void PreRuntimeCreate() {}
106
107 // Called after the runtime is created.
108 virtual void PostRuntimeCreate() {}
109
Calin Juravle7865ac72017-06-28 11:03:12 -0700110 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
111 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700112 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700113 // Loads the test dex file identified by the given first_dex_name and second_dex_name
114 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100115 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
116 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800117
Calin Juravle7865ac72017-06-28 11:03:12 -0700118 jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader);
119 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
120 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
121 jclass loader_class,
122 jobject parent_loader);
123
Ian Rogers700a4022014-05-19 16:49:03 -0700124 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800125
126 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
127 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800128 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800129 const DexFile* java_lang_dex_file_;
130 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800131
Calin Juravlec79470d2017-07-12 17:37:42 -0700132 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
133 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700134 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700135 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
136 Handle<mirror::ClassLoader> class_loader)
137 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700138
139 // Get the first dex file from a PathClassLoader. Will abort if it is null.
140 const DexFile* GetFirstDexFile(jobject jclass_loader);
141
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700142 std::unique_ptr<CompilerCallbacks> callbacks_;
143
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100144 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800145
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100146 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800147
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100148 // Called to finish up runtime creation and filling test fields. By default runs root
149 // initializers, initialize well-known classes, and creates the heap thread pool.
150 virtual void FinalizeSetup();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800151};
152
Mathieu Chartier91c91162016-01-15 09:48:15 -0800153template <typename TestType>
154class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
155 public:
156 CommonRuntimeTestBase() {}
157 virtual ~CommonRuntimeTestBase() {}
158
159 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100160 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800161 CommonRuntimeTestImpl::SetUp();
162 }
163
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100164 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800165 CommonRuntimeTestImpl::TearDown();
166 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800167};
168
169using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
170
171template <typename Param>
172using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
173
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800174// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
175// rather than aborting, so be careful!
176class CheckJniAbortCatcher {
177 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700178 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800179
Ian Rogerse63db272014-07-15 15:36:11 -0700180 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800181
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700182 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700183 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800184
185 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700186 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187
Ian Rogers68d8b422014-07-17 11:09:10 -0700188 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800189 std::string actual_;
190
191 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
192};
193
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100194#define TEST_DISABLED_FOR_MIPS() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000195 if (kRuntimeISA == InstructionSet::kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100196 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
197 return; \
198 }
199
Roland Levillain19772bf2017-02-16 11:28:10 +0000200#define TEST_DISABLED_FOR_X86() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000201 if (kRuntimeISA == InstructionSet::kX86) { \
Roland Levillain19772bf2017-02-16 11:28:10 +0000202 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000203 return; \
204 }
205
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700206#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
207 if (mirror::kUseStringCompression) { \
208 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
209 return; \
210 }
211
Roland Levillain6d729a72017-06-30 18:34:01 +0100212#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
213 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
214 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
215 return; \
216 }
217
Alex Klyubin3856af02017-10-23 13:53:13 -0700218#define TEST_DISABLED_FOR_HEAP_POISONING() \
219 if (kPoisonHeapReferences) { \
220 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
221 return; \
222 }
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800223} // namespace art
224
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800225#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_