blob: e1ab3dfd7ff03d633e11481534dc48b638c3b798 [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"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080029#include "base/locks.h"
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/os.h"
Mathieu Chartier2daa1342018-02-20 16:19:28 -080031#include "base/unix_file/fd_file.h"
32#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/compact_dex_level.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070034// TODO: Add inl file and avoid including inl.
35#include "obj_ptr-inl.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080036#include "runtime_globals.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
David Sehra8d23cb2019-04-08 11:29:11 -070044template<class MirrorType>
45static inline ObjPtr<MirrorType> MakeObjPtr(MirrorType* ptr) {
46 return ptr;
47}
48
49template<class MirrorType>
50static inline ObjPtr<MirrorType> MakeObjPtr(ObjPtr<MirrorType> ptr) {
51 return ptr;
52}
53
54// OBJ pointer helpers to avoid needing .Decode everywhere.
55#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
56#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
57#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
58#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
59
Ian Rogerse63db272014-07-15 15:36:11 -070060class ClassLinker;
61class CompilerCallbacks;
62class DexFile;
63class JavaVMExt;
64class Runtime;
65typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
Andreas Gampe26761f72017-07-20 18:00:39 -070066class Thread;
67class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070068
David Sehrd5f8de82018-04-27 14:12:03 -070069class CommonRuntimeTestImpl : public CommonArtTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080070 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080071 CommonRuntimeTestImpl();
72 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070073
David Srbecky3e52aa42015-04-12 07:45:18 +010074 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
75
Andreas Gampe26761f72017-07-20 18:00:39 -070076 // A helper function to fill the heap.
77 static void FillHeap(Thread* self,
78 ClassLinker* class_linker,
79 VariableSizedHandleScope* handle_scope)
80 REQUIRES_SHARED(Locks::mutator_lock_);
81 // A helper to set up a small heap (4M) to make FillHeap faster.
82 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
83
Mathieu Chartier2daa1342018-02-20 16:19:28 -080084 template <typename Mutator>
85 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
86 std::vector<std::unique_ptr<const DexFile>> dex_files;
87 std::string error_msg;
88 const ArtDexFileLoader dex_file_loader;
89 CHECK(dex_file_loader.Open(input_jar.c_str(),
90 input_jar.c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070091 /*verify=*/ true,
92 /*verify_checksum=*/ true,
Mathieu Chartier2daa1342018-02-20 16:19:28 -080093 &error_msg,
94 &dex_files)) << error_msg;
95 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
96 const std::unique_ptr<const DexFile>& dex = dex_files[0];
97 CHECK(dex->EnableWrite()) << "Failed to enable write";
98 DexFile* dex_file = const_cast<DexFile*>(dex.get());
99 mutator(dex_file);
100 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
101 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
102 return false;
103 }
104 if (output_dex->Flush() != 0) {
105 PLOG(FATAL) << "Could not flush the output file.";
106 }
107 return true;
108 }
109
Vladimir Marko2b076df2019-02-20 11:27:52 +0000110 void MakeInterpreted(ObjPtr<mirror::Class> klass)
111 REQUIRES_SHARED(Locks::mutator_lock_);
112
Vladimir Markob9c29f62019-03-20 14:22:51 +0000113 bool StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
114 /*out*/std::string* error_msg);
Vladimir Marko7a85e702018-12-03 18:47:23 +0000115
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800116 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800117 // Allow subclases such as CommonCompilerTest to add extra options.
118 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
119
120 // Called before the runtime is created.
121 virtual void PreRuntimeCreate() {}
122
123 // Called after the runtime is created.
124 virtual void PostRuntimeCreate() {}
125
Calin Juravle7865ac72017-06-28 11:03:12 -0700126 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
127 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700129 // Loads the test dex file identified by the given first_dex_name and second_dex_name
130 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100131 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
132 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800133
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000134 jobject LoadDexInPathClassLoader(const std::string& dex_name,
135 jobject parent_loader,
136 jobject shared_libraries = nullptr);
Calin Juravle7865ac72017-06-28 11:03:12 -0700137 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
David Brazdil1a9ac532019-03-05 11:57:13 +0000138 jobject LoadDexInInMemoryDexClassLoader(const std::string& dex_name, jobject parent_loader);
Calin Juravle7865ac72017-06-28 11:03:12 -0700139 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
140 jclass loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000141 jobject parent_loader,
142 jobject shared_libraries = nullptr);
Calin Juravle7865ac72017-06-28 11:03:12 -0700143
Ian Rogers700a4022014-05-19 16:49:03 -0700144 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800145
146 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
147 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800148 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800149 const DexFile* java_lang_dex_file_;
150 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800151
Calin Juravlec79470d2017-07-12 17:37:42 -0700152 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
153 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700154 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700155 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
156 Handle<mirror::ClassLoader> class_loader)
157 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700158
159 // Get the first dex file from a PathClassLoader. Will abort if it is null.
160 const DexFile* GetFirstDexFile(jobject jclass_loader);
161
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700162 std::unique_ptr<CompilerCallbacks> callbacks_;
163
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100164 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800165
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100166 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800167
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100168 // Called to finish up runtime creation and filling test fields. By default runs root
169 // initializers, initialize well-known classes, and creates the heap thread pool.
170 virtual void FinalizeSetup();
Vladimir Marko672c0802019-07-26 13:03:13 +0100171
172 // Returns the directory where the pre-compiled core.art can be found.
173 static std::string GetImageDirectory();
174 static std::string GetImageLocation();
175 static std::string GetSystemImageFile();
176
177 static void EnterTransactionMode();
178 static void ExitTransactionMode();
179 static void RollbackAndExitTransactionMode() REQUIRES_SHARED(Locks::mutator_lock_);
180 static bool IsTransactionAborted();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800181};
182
Mathieu Chartier91c91162016-01-15 09:48:15 -0800183template <typename TestType>
184class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
185 public:
186 CommonRuntimeTestBase() {}
187 virtual ~CommonRuntimeTestBase() {}
188
189 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100190 void SetUp() override {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800191 CommonRuntimeTestImpl::SetUp();
192 }
193
Roland Levillainf73caca2018-08-24 17:19:07 +0100194 void TearDown() override {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800195 CommonRuntimeTestImpl::TearDown();
196 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800197};
198
199using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
200
201template <typename Param>
202using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
203
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800204// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
205// rather than aborting, so be careful!
206class CheckJniAbortCatcher {
207 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700208 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800209
Ian Rogerse63db272014-07-15 15:36:11 -0700210 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800211
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700212 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700213 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800214
215 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700216 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800217
Ian Rogers68d8b422014-07-17 11:09:10 -0700218 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800219 std::string actual_;
220
221 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
222};
223
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700224#define TEST_DISABLED_FOR_ARM() \
225 if (kRuntimeISA == InstructionSet::kArm || kRuntimeISA == InstructionSet::kThumb2) { \
226 printf("WARNING: TEST DISABLED FOR ARM\n"); \
227 return; \
228 }
229
230#define TEST_DISABLED_FOR_ARM64() \
231 if (kRuntimeISA == InstructionSet::kArm64) { \
232 printf("WARNING: TEST DISABLED FOR ARM64\n"); \
233 return; \
234 }
235
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100236#define TEST_DISABLED_FOR_MIPS() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000237 if (kRuntimeISA == InstructionSet::kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100238 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
239 return; \
240 }
241
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700242#define TEST_DISABLED_FOR_MIPS64() \
243 if (kRuntimeISA == InstructionSet::kMips64) { \
244 printf("WARNING: TEST DISABLED FOR MIPS64\n"); \
245 return; \
246 }
247
Roland Levillain19772bf2017-02-16 11:28:10 +0000248#define TEST_DISABLED_FOR_X86() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000249 if (kRuntimeISA == InstructionSet::kX86) { \
Roland Levillain19772bf2017-02-16 11:28:10 +0000250 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000251 return; \
252 }
253
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700254#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
255 if (mirror::kUseStringCompression) { \
256 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
257 return; \
258 }
259
Roland Levillain6d729a72017-06-30 18:34:01 +0100260#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
261 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
262 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
263 return; \
264 }
265
Alex Klyubin3856af02017-10-23 13:53:13 -0700266#define TEST_DISABLED_FOR_HEAP_POISONING() \
267 if (kPoisonHeapReferences) { \
268 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
269 return; \
270 }
Roland Levillainf5dd1142018-07-03 13:29:18 +0100271
272#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS() \
273 if (kRunningOnMemoryTool && kPoisonHeapReferences && !kEmitCompilerReadBarrier) { \
274 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING WITHOUT READ BARRIERS\n"); \
275 return; \
276 }
277
Nicolas Geoffray8d6651d2019-07-08 10:03:16 +0100278#define TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT() \
279 if (CacheOperationsMaySegFault()) { \
280 printf("WARNING: TEST DISABLED ON KERNEL THAT SEGFAULT ON CACHE OPERATIONS\n"); \
281 return; \
282 }
283
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800284} // namespace art
285
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800286#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_