blob: 0f931e3dff0ad4840f3ab2daafba61a0b3d3cf08 [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"
29#include "globals.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070030// TODO: Add inl file and avoid including inl.
31#include "obj_ptr-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032#include "os.h"
Calin Juravlec79470d2017-07-12 17:37:42 -070033#include "scoped_thread_state_change-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080034
35namespace art {
36
Andreas Gampe170331f2017-12-07 18:41:03 -080037using LogSeverity = android::base::LogSeverity;
38using ScopedLogSeverity = android::base::ScopedLogSeverity;
39
Mathieu Chartier3398c782016-09-30 10:27:43 -070040// OBJ pointer helpers to avoid needing .Decode everywhere.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070041#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
42#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
43#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
44#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -070045
Ian Rogerse63db272014-07-15 15:36:11 -070046class ClassLinker;
47class CompilerCallbacks;
48class DexFile;
49class JavaVMExt;
50class Runtime;
51typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
Andreas Gampe26761f72017-07-20 18:00:39 -070052class Thread;
53class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070054
Alex Lighta01b5242017-03-27 10:15:27 -070055uint8_t* DecodeBase64(const char* src, size_t* dst_size);
56
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080057class ScratchFile {
58 public:
Ian Rogerse63db272014-07-15 15:36:11 -070059 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080060
Mathieu Chartier866d8742016-09-21 15:24:18 -070061 explicit ScratchFile(const std::string& filename);
62
Ian Rogerse63db272014-07-15 15:36:11 -070063 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000064
Andreas Gampe0afd1be2016-11-03 17:24:45 -070065 ScratchFile(ScratchFile&& other);
Mathieu Chartier866d8742016-09-21 15:24:18 -070066
67 ScratchFile& operator=(ScratchFile&& other);
68
Ian Rogerse63db272014-07-15 15:36:11 -070069 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070070
Ian Rogerse63db272014-07-15 15:36:11 -070071 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080072
73 const std::string& GetFilename() const {
74 return filename_;
75 }
76
77 File* GetFile() const {
78 return file_.get();
79 }
80
Ian Rogerse63db272014-07-15 15:36:11 -070081 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080082
Andreas Gampee21dc3d2014-12-08 16:59:43 -080083 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070084 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070085
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080086 private:
87 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070088 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080089};
90
Mathieu Chartier91c91162016-01-15 09:48:15 -080091class CommonRuntimeTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080092 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080093 CommonRuntimeTestImpl();
94 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070095 static void SetUpAndroidRoot();
96
97 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
98 // non-derived class, be sure to also call the corresponding tear-down below.
99 static void SetUpAndroidData(std::string& android_data);
100
101 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800102
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000103 // Gets the paths of the libcore dex files.
104 static std::vector<std::string> GetLibCoreDexFileNames();
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800105
David Srbecky3e52aa42015-04-12 07:45:18 +0100106 // Returns bin directory which contains host's prebuild tools.
107 static std::string GetAndroidHostToolsDir();
108
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700109 // Returns bin directory which contains target's prebuild tools.
David Srbecky3e52aa42015-04-12 07:45:18 +0100110 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
111
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700112 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
113 std::string GetTestDexFileName(const char* name) const;
114
Andreas Gampe26761f72017-07-20 18:00:39 -0700115 // A helper function to fill the heap.
116 static void FillHeap(Thread* self,
117 ClassLinker* class_linker,
118 VariableSizedHandleScope* handle_scope)
119 REQUIRES_SHARED(Locks::mutator_lock_);
120 // A helper to set up a small heap (4M) to make FillHeap faster.
121 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
122
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800123 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800124 // Allow subclases such as CommonCompilerTest to add extra options.
125 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
126
127 // Called before the runtime is created.
128 virtual void PreRuntimeCreate() {}
129
130 // Called after the runtime is created.
131 virtual void PostRuntimeCreate() {}
132
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800133 static bool IsHost() {
134 return !kIsTargetBuild;
135 }
136
Igor Murashkin37743352014-11-13 14:38:00 -0800137 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
138 static std::string GetCoreArtLocation();
139
140 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
141 static std::string GetCoreOatLocation();
142
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800143 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700144
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700145 void ClearDirectory(const char* dirpath, bool recursive = true);
Alex Lighta59dd802014-07-02 16:28:08 -0700146
Ian Rogerse63db272014-07-15 15:36:11 -0700147 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800148
Mathieu Chartier496577f2016-09-20 15:33:31 -0700149 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
Andreas Gampe833a4852014-05-21 18:46:59 -0700150
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700151 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800152
Calin Juravle7865ac72017-06-28 11:03:12 -0700153 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
154 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700156 // Loads the test dex file identified by the given first_dex_name and second_dex_name
157 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100158 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
159 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800160
Calin Juravle7865ac72017-06-28 11:03:12 -0700161 jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader);
162 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
163 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
164 jclass loader_class,
165 jobject parent_loader);
166
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800167 std::string android_data_;
168 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800169
Ian Rogers700a4022014-05-19 16:49:03 -0700170 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800171
172 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
173 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800174 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800175 const DexFile* java_lang_dex_file_;
176 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800177
Calin Juravlec79470d2017-07-12 17:37:42 -0700178 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
179 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700180 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700181 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
182 Handle<mirror::ClassLoader> class_loader)
183 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700184
185 // Get the first dex file from a PathClassLoader. Will abort if it is null.
186 const DexFile* GetFirstDexFile(jobject jclass_loader);
187
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700188 std::unique_ptr<CompilerCallbacks> callbacks_;
189
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100190 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800191
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100192 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800193
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100194 // Called to finish up runtime creation and filling test fields. By default runs root
195 // initializers, initialize well-known classes, and creates the heap thread pool.
196 virtual void FinalizeSetup();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800197
Calin Juravlec79470d2017-07-12 17:37:42 -0700198 // Creates the class path string for the given dex files (the list of dex file locations
199 // separated by ':').
200 std::string CreateClassPath(
201 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
202 // Same as CreateClassPath but add the dex file checksum after each location. The separator
203 // is '*'.
204 std::string CreateClassPathWithChecksums(
205 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
206
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800207 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800208 static std::string GetCoreFileLocation(const char* suffix);
209
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800210 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800211};
212
Mathieu Chartier91c91162016-01-15 09:48:15 -0800213template <typename TestType>
214class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
215 public:
216 CommonRuntimeTestBase() {}
217 virtual ~CommonRuntimeTestBase() {}
218
219 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100220 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800221 CommonRuntimeTestImpl::SetUp();
222 }
223
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100224 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800225 CommonRuntimeTestImpl::TearDown();
226 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800227};
228
229using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
230
231template <typename Param>
232using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
233
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800234// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
235// rather than aborting, so be careful!
236class CheckJniAbortCatcher {
237 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700238 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800239
Ian Rogerse63db272014-07-15 15:36:11 -0700240 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800241
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700242 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700243 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800244
245 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700246 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800247
Ian Rogers68d8b422014-07-17 11:09:10 -0700248 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800249 std::string actual_;
250
251 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
252};
253
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700254#define TEST_DISABLED_FOR_TARGET() \
255 if (kIsTargetBuild) { \
256 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
257 return; \
258 }
259
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100260#define TEST_DISABLED_FOR_MIPS() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000261 if (kRuntimeISA == InstructionSet::kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100262 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
263 return; \
264 }
265
Roland Levillain19772bf2017-02-16 11:28:10 +0000266#define TEST_DISABLED_FOR_X86() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000267 if (kRuntimeISA == InstructionSet::kX86) { \
Roland Levillain19772bf2017-02-16 11:28:10 +0000268 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000269 return; \
270 }
271
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700272#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
273 if (mirror::kUseStringCompression) { \
274 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
275 return; \
276 }
277
Roland Levillain6d729a72017-06-30 18:34:01 +0100278#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
279 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
280 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
281 return; \
282 }
283
Roland Levillain04147ef2016-09-06 11:09:41 +0100284#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
285 if (!kHostStaticBuildEnabled) { \
286 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
287 return; \
288 }
289
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700290#define TEST_DISABLED_FOR_MEMORY_TOOL() \
291 if (RUNNING_ON_MEMORY_TOOL > 0) { \
292 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
293 return; \
294 }
295
Roland Levillain68db2252017-08-14 12:48:47 +0100296#define TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND() \
297 if (RUNNING_ON_MEMORY_TOOL > 0 && kMemoryToolIsValgrind) { \
298 printf("WARNING: TEST DISABLED FOR MEMORY TOOL VALGRIND\n"); \
299 return; \
300 }
301
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700302#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
303 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
304 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
305 return; \
306 }
307
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800308} // namespace art
309
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800310#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_