blob: 74bc0b2afb53d97c723beca06f0985249bf06be6 [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
David Srbecky3e52aa42015-04-12 07:45:18 +010025#include "arch/instruction_set.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "base/mutex.h"
27#include "globals.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070028// TODO: Add inl file and avoid including inl.
29#include "obj_ptr-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030#include "os.h"
Calin Juravlec79470d2017-07-12 17:37:42 -070031#include "scoped_thread_state_change-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080032
33namespace art {
34
Mathieu Chartier3398c782016-09-30 10:27:43 -070035// OBJ pointer helpers to avoid needing .Decode everywhere.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070036#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
37#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
38#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
39#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -070040
Ian Rogerse63db272014-07-15 15:36:11 -070041class ClassLinker;
42class CompilerCallbacks;
43class DexFile;
44class JavaVMExt;
45class Runtime;
46typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
Andreas Gampe26761f72017-07-20 18:00:39 -070047class Thread;
48class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070049
Alex Lighta01b5242017-03-27 10:15:27 -070050uint8_t* DecodeBase64(const char* src, size_t* dst_size);
51
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080052class ScratchFile {
53 public:
Ian Rogerse63db272014-07-15 15:36:11 -070054 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080055
Mathieu Chartier866d8742016-09-21 15:24:18 -070056 explicit ScratchFile(const std::string& filename);
57
Ian Rogerse63db272014-07-15 15:36:11 -070058 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000059
Andreas Gampe0afd1be2016-11-03 17:24:45 -070060 ScratchFile(ScratchFile&& other);
Mathieu Chartier866d8742016-09-21 15:24:18 -070061
62 ScratchFile& operator=(ScratchFile&& other);
63
Ian Rogerse63db272014-07-15 15:36:11 -070064 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070065
Ian Rogerse63db272014-07-15 15:36:11 -070066 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080067
68 const std::string& GetFilename() const {
69 return filename_;
70 }
71
72 File* GetFile() const {
73 return file_.get();
74 }
75
Ian Rogerse63db272014-07-15 15:36:11 -070076 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080077
Andreas Gampee21dc3d2014-12-08 16:59:43 -080078 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070079 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070080
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080081 private:
82 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070083 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080084};
85
Mathieu Chartier91c91162016-01-15 09:48:15 -080086class CommonRuntimeTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080087 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080088 CommonRuntimeTestImpl();
89 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070090 static void SetUpAndroidRoot();
91
92 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
93 // non-derived class, be sure to also call the corresponding tear-down below.
94 static void SetUpAndroidData(std::string& android_data);
95
96 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080097
Narayan Kamathd1ef4362015-11-12 11:49:06 +000098 // Gets the paths of the libcore dex files.
99 static std::vector<std::string> GetLibCoreDexFileNames();
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800100
David Srbecky3e52aa42015-04-12 07:45:18 +0100101 // Returns bin directory which contains host's prebuild tools.
102 static std::string GetAndroidHostToolsDir();
103
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700104 // Returns bin directory which contains target's prebuild tools.
David Srbecky3e52aa42015-04-12 07:45:18 +0100105 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
106
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700107 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
108 std::string GetTestDexFileName(const char* name) const;
109
Andreas Gampe26761f72017-07-20 18:00:39 -0700110 // A helper function to fill the heap.
111 static void FillHeap(Thread* self,
112 ClassLinker* class_linker,
113 VariableSizedHandleScope* handle_scope)
114 REQUIRES_SHARED(Locks::mutator_lock_);
115 // A helper to set up a small heap (4M) to make FillHeap faster.
116 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
117
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800118 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800119 // Allow subclases such as CommonCompilerTest to add extra options.
120 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
121
122 // Called before the runtime is created.
123 virtual void PreRuntimeCreate() {}
124
125 // Called after the runtime is created.
126 virtual void PostRuntimeCreate() {}
127
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800128 static bool IsHost() {
129 return !kIsTargetBuild;
130 }
131
Igor Murashkin37743352014-11-13 14:38:00 -0800132 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
133 static std::string GetCoreArtLocation();
134
135 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
136 static std::string GetCoreOatLocation();
137
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800138 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700139
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700140 void ClearDirectory(const char* dirpath, bool recursive = true);
Alex Lighta59dd802014-07-02 16:28:08 -0700141
Ian Rogerse63db272014-07-15 15:36:11 -0700142 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800143
Mathieu Chartier496577f2016-09-20 15:33:31 -0700144 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
Andreas Gampe833a4852014-05-21 18:46:59 -0700145
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700146 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800147
Calin Juravle7865ac72017-06-28 11:03:12 -0700148 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
149 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700151 // Loads the test dex file identified by the given first_dex_name and second_dex_name
152 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100153 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
154 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800155
Calin Juravle7865ac72017-06-28 11:03:12 -0700156 jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader);
157 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
158 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
159 jclass loader_class,
160 jobject parent_loader);
161
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800162 std::string android_data_;
163 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800164
Ian Rogers700a4022014-05-19 16:49:03 -0700165 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800166
167 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
168 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800169 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800170 const DexFile* java_lang_dex_file_;
171 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800172
Calin Juravlec79470d2017-07-12 17:37:42 -0700173 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
174 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700175 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700176 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
177 Handle<mirror::ClassLoader> class_loader)
178 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700179
180 // Get the first dex file from a PathClassLoader. Will abort if it is null.
181 const DexFile* GetFirstDexFile(jobject jclass_loader);
182
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700183 std::unique_ptr<CompilerCallbacks> callbacks_;
184
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100185 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800186
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100187 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800188
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100189 // Called to finish up runtime creation and filling test fields. By default runs root
190 // initializers, initialize well-known classes, and creates the heap thread pool.
191 virtual void FinalizeSetup();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800192
Calin Juravlec79470d2017-07-12 17:37:42 -0700193 // Creates the class path string for the given dex files (the list of dex file locations
194 // separated by ':').
195 std::string CreateClassPath(
196 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
197 // Same as CreateClassPath but add the dex file checksum after each location. The separator
198 // is '*'.
199 std::string CreateClassPathWithChecksums(
200 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
201
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800202 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800203 static std::string GetCoreFileLocation(const char* suffix);
204
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800205 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800206};
207
Mathieu Chartier91c91162016-01-15 09:48:15 -0800208template <typename TestType>
209class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
210 public:
211 CommonRuntimeTestBase() {}
212 virtual ~CommonRuntimeTestBase() {}
213
214 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100215 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800216 CommonRuntimeTestImpl::SetUp();
217 }
218
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100219 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800220 CommonRuntimeTestImpl::TearDown();
221 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800222};
223
224using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
225
226template <typename Param>
227using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
228
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800229// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
230// rather than aborting, so be careful!
231class CheckJniAbortCatcher {
232 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700233 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800234
Ian Rogerse63db272014-07-15 15:36:11 -0700235 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800236
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700237 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700238 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800239
240 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700241 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800242
Ian Rogers68d8b422014-07-17 11:09:10 -0700243 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800244 std::string actual_;
245
246 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
247};
248
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700249#define TEST_DISABLED_FOR_TARGET() \
250 if (kIsTargetBuild) { \
251 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
252 return; \
253 }
254
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100255#define TEST_DISABLED_FOR_MIPS() \
Douglas Leungd90957f2015-04-30 19:22:49 -0700256 if (kRuntimeISA == kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100257 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
258 return; \
259 }
260
Roland Levillain19772bf2017-02-16 11:28:10 +0000261#define TEST_DISABLED_FOR_X86() \
262 if (kRuntimeISA == kX86) { \
263 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000264 return; \
265 }
266
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700267#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
268 if (mirror::kUseStringCompression) { \
269 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
270 return; \
271 }
272
Roland Levillain6d729a72017-06-30 18:34:01 +0100273#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
274 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \
275 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
276 return; \
277 }
278
Roland Levillain04147ef2016-09-06 11:09:41 +0100279#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
280 if (!kHostStaticBuildEnabled) { \
281 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
282 return; \
283 }
284
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700285#define TEST_DISABLED_FOR_MEMORY_TOOL() \
286 if (RUNNING_ON_MEMORY_TOOL > 0) { \
287 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
288 return; \
289 }
290
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700291#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
292 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
293 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
294 return; \
295 }
296
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800297} // namespace art
298
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800299#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_