blob: 7fc70e294fdcb59d636387805dccb9c2010784dc [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"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "base/mutex.h"
David Sehr8f4b0562018-03-02 12:01:51 -080029#include "base/os.h"
Mathieu Chartier2daa1342018-02-20 16:19:28 -080030#include "base/unix_file/fd_file.h"
31#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/compact_dex_level.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "globals.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070034// TODO: Add inl file and avoid including inl.
35#include "obj_ptr-inl.h"
Calin Juravlec79470d2017-07-12 17:37:42 -070036#include "scoped_thread_state_change-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080037
38namespace art {
39
Andreas Gampe170331f2017-12-07 18:41:03 -080040using LogSeverity = android::base::LogSeverity;
41using ScopedLogSeverity = android::base::ScopedLogSeverity;
42
Mathieu Chartier3398c782016-09-30 10:27:43 -070043// OBJ pointer helpers to avoid needing .Decode everywhere.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070044#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
45#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
46#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
47#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -070048
Ian Rogerse63db272014-07-15 15:36:11 -070049class ClassLinker;
50class CompilerCallbacks;
51class DexFile;
52class JavaVMExt;
53class Runtime;
54typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
Andreas Gampe26761f72017-07-20 18:00:39 -070055class Thread;
56class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070057
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080058class ScratchFile {
59 public:
Ian Rogerse63db272014-07-15 15:36:11 -070060 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080061
Mathieu Chartier866d8742016-09-21 15:24:18 -070062 explicit ScratchFile(const std::string& filename);
63
Ian Rogerse63db272014-07-15 15:36:11 -070064 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000065
Andreas Gampe0afd1be2016-11-03 17:24:45 -070066 ScratchFile(ScratchFile&& other);
Mathieu Chartier866d8742016-09-21 15:24:18 -070067
68 ScratchFile& operator=(ScratchFile&& other);
69
Ian Rogerse63db272014-07-15 15:36:11 -070070 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070071
Ian Rogerse63db272014-07-15 15:36:11 -070072 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080073
74 const std::string& GetFilename() const {
75 return filename_;
76 }
77
78 File* GetFile() const {
79 return file_.get();
80 }
81
Ian Rogerse63db272014-07-15 15:36:11 -070082 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080083
Andreas Gampee21dc3d2014-12-08 16:59:43 -080084 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070085 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070086
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080087 private:
88 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070089 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080090};
91
Mathieu Chartier91c91162016-01-15 09:48:15 -080092class CommonRuntimeTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080093 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080094 CommonRuntimeTestImpl();
95 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070096 static void SetUpAndroidRoot();
97
98 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
99 // non-derived class, be sure to also call the corresponding tear-down below.
100 static void SetUpAndroidData(std::string& android_data);
101
102 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800103
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000104 // Gets the paths of the libcore dex files.
105 static std::vector<std::string> GetLibCoreDexFileNames();
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800106
David Srbecky3e52aa42015-04-12 07:45:18 +0100107 // Returns bin directory which contains host's prebuild tools.
108 static std::string GetAndroidHostToolsDir();
109
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700110 // Returns bin directory which contains target's prebuild tools.
David Srbecky3e52aa42015-04-12 07:45:18 +0100111 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
112
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700113 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
114 std::string GetTestDexFileName(const char* name) const;
115
Andreas Gampe26761f72017-07-20 18:00:39 -0700116 // A helper function to fill the heap.
117 static void FillHeap(Thread* self,
118 ClassLinker* class_linker,
119 VariableSizedHandleScope* handle_scope)
120 REQUIRES_SHARED(Locks::mutator_lock_);
121 // A helper to set up a small heap (4M) to make FillHeap faster.
122 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
123
Mathieu Chartier2daa1342018-02-20 16:19:28 -0800124 template <typename Mutator>
125 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
126 std::vector<std::unique_ptr<const DexFile>> dex_files;
127 std::string error_msg;
128 const ArtDexFileLoader dex_file_loader;
129 CHECK(dex_file_loader.Open(input_jar.c_str(),
130 input_jar.c_str(),
131 /*verify*/ true,
132 /*verify_checksum*/ true,
133 &error_msg,
134 &dex_files)) << error_msg;
135 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
136 const std::unique_ptr<const DexFile>& dex = dex_files[0];
137 CHECK(dex->EnableWrite()) << "Failed to enable write";
138 DexFile* dex_file = const_cast<DexFile*>(dex.get());
139 mutator(dex_file);
140 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
141 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
142 return false;
143 }
144 if (output_dex->Flush() != 0) {
145 PLOG(FATAL) << "Could not flush the output file.";
146 }
147 return true;
148 }
149
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800150 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800151 // Allow subclases such as CommonCompilerTest to add extra options.
152 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
153
154 // Called before the runtime is created.
155 virtual void PreRuntimeCreate() {}
156
157 // Called after the runtime is created.
158 virtual void PostRuntimeCreate() {}
159
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800160 static bool IsHost() {
161 return !kIsTargetBuild;
162 }
163
Igor Murashkin37743352014-11-13 14:38:00 -0800164 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
165 static std::string GetCoreArtLocation();
166
167 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
168 static std::string GetCoreOatLocation();
169
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800170 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700171
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700172 void ClearDirectory(const char* dirpath, bool recursive = true);
Alex Lighta59dd802014-07-02 16:28:08 -0700173
Ian Rogerse63db272014-07-15 15:36:11 -0700174 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800175
Mathieu Chartier496577f2016-09-20 15:33:31 -0700176 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
Andreas Gampe833a4852014-05-21 18:46:59 -0700177
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700178 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800179
Calin Juravle7865ac72017-06-28 11:03:12 -0700180 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
181 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700182 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700183 // Loads the test dex file identified by the given first_dex_name and second_dex_name
184 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100185 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
186 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187
Calin Juravle7865ac72017-06-28 11:03:12 -0700188 jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader);
189 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
190 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
191 jclass loader_class,
192 jobject parent_loader);
193
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800194 std::string android_data_;
195 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800196
Ian Rogers700a4022014-05-19 16:49:03 -0700197 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800198
199 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
200 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800201 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800202 const DexFile* java_lang_dex_file_;
203 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800204
Calin Juravlec79470d2017-07-12 17:37:42 -0700205 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
206 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700207 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700208 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
209 Handle<mirror::ClassLoader> class_loader)
210 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700211
212 // Get the first dex file from a PathClassLoader. Will abort if it is null.
213 const DexFile* GetFirstDexFile(jobject jclass_loader);
214
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700215 std::unique_ptr<CompilerCallbacks> callbacks_;
216
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100217 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800218
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100219 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800220
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100221 // Called to finish up runtime creation and filling test fields. By default runs root
222 // initializers, initialize well-known classes, and creates the heap thread pool.
223 virtual void FinalizeSetup();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800224
Calin Juravlec79470d2017-07-12 17:37:42 -0700225 // Creates the class path string for the given dex files (the list of dex file locations
226 // separated by ':').
227 std::string CreateClassPath(
228 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
229 // Same as CreateClassPath but add the dex file checksum after each location. The separator
230 // is '*'.
231 std::string CreateClassPathWithChecksums(
232 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
233
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800234 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800235 static std::string GetCoreFileLocation(const char* suffix);
236
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800237 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800238};
239
Mathieu Chartier91c91162016-01-15 09:48:15 -0800240template <typename TestType>
241class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
242 public:
243 CommonRuntimeTestBase() {}
244 virtual ~CommonRuntimeTestBase() {}
245
246 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100247 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800248 CommonRuntimeTestImpl::SetUp();
249 }
250
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100251 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800252 CommonRuntimeTestImpl::TearDown();
253 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800254};
255
256using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
257
258template <typename Param>
259using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
260
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800261// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
262// rather than aborting, so be careful!
263class CheckJniAbortCatcher {
264 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700265 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800266
Ian Rogerse63db272014-07-15 15:36:11 -0700267 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800268
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700269 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700270 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800271
272 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700273 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800274
Ian Rogers68d8b422014-07-17 11:09:10 -0700275 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800276 std::string actual_;
277
278 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
279};
280
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700281#define TEST_DISABLED_FOR_TARGET() \
282 if (kIsTargetBuild) { \
283 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
284 return; \
285 }
286
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100287#define TEST_DISABLED_FOR_MIPS() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000288 if (kRuntimeISA == InstructionSet::kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100289 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
290 return; \
291 }
292
Roland Levillain19772bf2017-02-16 11:28:10 +0000293#define TEST_DISABLED_FOR_X86() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000294 if (kRuntimeISA == InstructionSet::kX86) { \
Roland Levillain19772bf2017-02-16 11:28:10 +0000295 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000296 return; \
297 }
298
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700299#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
300 if (mirror::kUseStringCompression) { \
301 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
302 return; \
303 }
304
Roland Levillain6d729a72017-06-30 18:34:01 +0100305#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
306 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
307 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
308 return; \
309 }
310
Roland Levillain04147ef2016-09-06 11:09:41 +0100311#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
312 if (!kHostStaticBuildEnabled) { \
313 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
314 return; \
315 }
316
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700317#define TEST_DISABLED_FOR_MEMORY_TOOL() \
318 if (RUNNING_ON_MEMORY_TOOL > 0) { \
319 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
320 return; \
321 }
322
Roland Levillain68db2252017-08-14 12:48:47 +0100323#define TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND() \
324 if (RUNNING_ON_MEMORY_TOOL > 0 && kMemoryToolIsValgrind) { \
325 printf("WARNING: TEST DISABLED FOR MEMORY TOOL VALGRIND\n"); \
326 return; \
327 }
328
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700329#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
330 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
331 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
332 return; \
333 }
334
Mathieu Chartier2c4b0842017-12-13 11:49:51 -0800335#define TEST_DISABLED_FOR_COMPACT_DEX() \
336 if (kDefaultCompactDexLevel != CompactDexLevel::kCompactDexLevelNone) { \
337 printf("WARNING: TEST DISABLED FOR COMPACT DEX\n"); \
338 return; \
339 }
Alex Klyubin3856af02017-10-23 13:53:13 -0700340
341#define TEST_DISABLED_FOR_HEAP_POISONING() \
342 if (kPoisonHeapReferences) { \
343 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
344 return; \
345 }
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800346} // namespace art
347
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800348#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_