blob: 24dbd058ec49e9775f7a4dc49826fa587e621f92 [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"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080031
32namespace art {
33
Mathieu Chartier3398c782016-09-30 10:27:43 -070034// OBJ pointer helpers to avoid needing .Decode everywhere.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070035#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
36#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
37#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
38#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
Mathieu Chartier3398c782016-09-30 10:27:43 -070039
Ian Rogerse63db272014-07-15 15:36:11 -070040class ClassLinker;
41class CompilerCallbacks;
42class DexFile;
43class JavaVMExt;
44class Runtime;
45typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
46
Alex Lighta01b5242017-03-27 10:15:27 -070047uint8_t* DecodeBase64(const char* src, size_t* dst_size);
48
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080049class ScratchFile {
50 public:
Ian Rogerse63db272014-07-15 15:36:11 -070051 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080052
Mathieu Chartier866d8742016-09-21 15:24:18 -070053 explicit ScratchFile(const std::string& filename);
54
Ian Rogerse63db272014-07-15 15:36:11 -070055 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000056
Andreas Gampe0afd1be2016-11-03 17:24:45 -070057 ScratchFile(ScratchFile&& other);
Mathieu Chartier866d8742016-09-21 15:24:18 -070058
59 ScratchFile& operator=(ScratchFile&& other);
60
Ian Rogerse63db272014-07-15 15:36:11 -070061 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070062
Ian Rogerse63db272014-07-15 15:36:11 -070063 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080064
65 const std::string& GetFilename() const {
66 return filename_;
67 }
68
69 File* GetFile() const {
70 return file_.get();
71 }
72
Ian Rogerse63db272014-07-15 15:36:11 -070073 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080074
Andreas Gampee21dc3d2014-12-08 16:59:43 -080075 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070076 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070077
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080078 private:
79 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070080 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080081};
82
Mathieu Chartier91c91162016-01-15 09:48:15 -080083class CommonRuntimeTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080084 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080085 CommonRuntimeTestImpl();
86 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070087 static void SetUpAndroidRoot();
88
89 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
90 // non-derived class, be sure to also call the corresponding tear-down below.
91 static void SetUpAndroidData(std::string& android_data);
92
93 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080094
Narayan Kamathd1ef4362015-11-12 11:49:06 +000095 // Gets the paths of the libcore dex files.
96 static std::vector<std::string> GetLibCoreDexFileNames();
Igor Murashkinaaebaa02015-01-26 10:55:53 -080097
David Srbecky3e52aa42015-04-12 07:45:18 +010098 // Returns bin directory which contains host's prebuild tools.
99 static std::string GetAndroidHostToolsDir();
100
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700101 // Returns bin directory which contains target's prebuild tools.
David Srbecky3e52aa42015-04-12 07:45:18 +0100102 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
103
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700104 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
105 std::string GetTestDexFileName(const char* name) const;
106
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800107 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800108 // Allow subclases such as CommonCompilerTest to add extra options.
109 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
110
111 // Called before the runtime is created.
112 virtual void PreRuntimeCreate() {}
113
114 // Called after the runtime is created.
115 virtual void PostRuntimeCreate() {}
116
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800117 static bool IsHost() {
118 return !kIsTargetBuild;
119 }
120
Igor Murashkin37743352014-11-13 14:38:00 -0800121 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
122 static std::string GetCoreArtLocation();
123
124 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
125 static std::string GetCoreOatLocation();
126
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800127 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700128
Alex Lighta59dd802014-07-02 16:28:08 -0700129 void ClearDirectory(const char* dirpath);
130
Ian Rogerse63db272014-07-15 15:36:11 -0700131 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800132
Mathieu Chartier496577f2016-09-20 15:33:31 -0700133 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
Andreas Gampe833a4852014-05-21 18:46:59 -0700134
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800135 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700136 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800137
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100139 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
140 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800141
142 std::string android_data_;
143 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800144
Ian Rogers700a4022014-05-19 16:49:03 -0700145 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800146
147 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
148 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800149 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800150 const DexFile* java_lang_dex_file_;
151 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800152
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700153 // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex
154 // arrays.
155 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
156
157 // Get the first dex file from a PathClassLoader. Will abort if it is null.
158 const DexFile* GetFirstDexFile(jobject jclass_loader);
159
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700160 std::unique_ptr<CompilerCallbacks> callbacks_;
161
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100162 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800163
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100164 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800165
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100166 // Called to finish up runtime creation and filling test fields. By default runs root
167 // initializers, initialize well-known classes, and creates the heap thread pool.
168 virtual void FinalizeSetup();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800169
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800170 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800171 static std::string GetCoreFileLocation(const char* suffix);
172
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800173 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800174};
175
Mathieu Chartier91c91162016-01-15 09:48:15 -0800176template <typename TestType>
177class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
178 public:
179 CommonRuntimeTestBase() {}
180 virtual ~CommonRuntimeTestBase() {}
181
182 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100183 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800184 CommonRuntimeTestImpl::SetUp();
185 }
186
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100187 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800188 CommonRuntimeTestImpl::TearDown();
189 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800190};
191
192using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
193
194template <typename Param>
195using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
196
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800197// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
198// rather than aborting, so be careful!
199class CheckJniAbortCatcher {
200 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700201 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800202
Ian Rogerse63db272014-07-15 15:36:11 -0700203 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800204
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700205 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700206 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800207
208 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700209 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800210
Ian Rogers68d8b422014-07-17 11:09:10 -0700211 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800212 std::string actual_;
213
214 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
215};
216
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700217#define TEST_DISABLED_FOR_TARGET() \
218 if (kIsTargetBuild) { \
219 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
220 return; \
221 }
222
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100223#define TEST_DISABLED_FOR_MIPS() \
Douglas Leungd90957f2015-04-30 19:22:49 -0700224 if (kRuntimeISA == kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100225 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
226 return; \
227 }
228
Roland Levillain19772bf2017-02-16 11:28:10 +0000229#define TEST_DISABLED_FOR_X86() \
230 if (kRuntimeISA == kX86) { \
231 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000232 return; \
233 }
234
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700235#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
236 if (mirror::kUseStringCompression) { \
237 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
238 return; \
239 }
240
Roland Levillain04147ef2016-09-06 11:09:41 +0100241#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
242 if (!kHostStaticBuildEnabled) { \
243 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
244 return; \
245 }
246
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700247#define TEST_DISABLED_FOR_MEMORY_TOOL() \
248 if (RUNNING_ON_MEMORY_TOOL > 0) { \
249 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
250 return; \
251 }
252
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700253#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
254 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
255 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
256 return; \
257 }
258
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800259} // namespace art
260
261namespace std {
262
263// TODO: isn't gtest supposed to be able to print STL types for itself?
264template <typename T>
Ian Rogerse63db272014-07-15 15:36:11 -0700265std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800266
267} // namespace std
268
269#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_