blob: c494513b620214009eecfd8c4772174376bbff40 [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;
47
Alex Lighta01b5242017-03-27 10:15:27 -070048uint8_t* DecodeBase64(const char* src, size_t* dst_size);
49
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080050class ScratchFile {
51 public:
Ian Rogerse63db272014-07-15 15:36:11 -070052 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080053
Mathieu Chartier866d8742016-09-21 15:24:18 -070054 explicit ScratchFile(const std::string& filename);
55
Ian Rogerse63db272014-07-15 15:36:11 -070056 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000057
Andreas Gampe0afd1be2016-11-03 17:24:45 -070058 ScratchFile(ScratchFile&& other);
Mathieu Chartier866d8742016-09-21 15:24:18 -070059
60 ScratchFile& operator=(ScratchFile&& other);
61
Ian Rogerse63db272014-07-15 15:36:11 -070062 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070063
Ian Rogerse63db272014-07-15 15:36:11 -070064 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080065
66 const std::string& GetFilename() const {
67 return filename_;
68 }
69
70 File* GetFile() const {
71 return file_.get();
72 }
73
Ian Rogerse63db272014-07-15 15:36:11 -070074 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080075
Andreas Gampee21dc3d2014-12-08 16:59:43 -080076 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070077 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070078
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080079 private:
80 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070081 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080082};
83
Mathieu Chartier91c91162016-01-15 09:48:15 -080084class CommonRuntimeTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080085 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080086 CommonRuntimeTestImpl();
87 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070088 static void SetUpAndroidRoot();
89
90 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
91 // non-derived class, be sure to also call the corresponding tear-down below.
92 static void SetUpAndroidData(std::string& android_data);
93
94 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080095
Narayan Kamathd1ef4362015-11-12 11:49:06 +000096 // Gets the paths of the libcore dex files.
97 static std::vector<std::string> GetLibCoreDexFileNames();
Igor Murashkinaaebaa02015-01-26 10:55:53 -080098
David Srbecky3e52aa42015-04-12 07:45:18 +010099 // Returns bin directory which contains host's prebuild tools.
100 static std::string GetAndroidHostToolsDir();
101
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700102 // Returns bin directory which contains target's prebuild tools.
David Srbecky3e52aa42015-04-12 07:45:18 +0100103 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
104
Mathieu Chartierd00e02b2017-05-24 12:04:13 -0700105 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
106 std::string GetTestDexFileName(const char* name) const;
107
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800108 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800109 // Allow subclases such as CommonCompilerTest to add extra options.
110 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
111
112 // Called before the runtime is created.
113 virtual void PreRuntimeCreate() {}
114
115 // Called after the runtime is created.
116 virtual void PostRuntimeCreate() {}
117
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800118 static bool IsHost() {
119 return !kIsTargetBuild;
120 }
121
Igor Murashkin37743352014-11-13 14:38:00 -0800122 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
123 static std::string GetCoreArtLocation();
124
125 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
126 static std::string GetCoreOatLocation();
127
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800128 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700129
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700130 void ClearDirectory(const char* dirpath, bool recursive = true);
Alex Lighta59dd802014-07-02 16:28:08 -0700131
Ian Rogerse63db272014-07-15 15:36:11 -0700132 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800133
Mathieu Chartier496577f2016-09-20 15:33:31 -0700134 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
Andreas Gampe833a4852014-05-21 18:46:59 -0700135
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700136 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800137
Calin Juravle7865ac72017-06-28 11:03:12 -0700138 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
139 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700140 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700141 // Loads the test dex file identified by the given first_dex_name and second_dex_name
142 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100143 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
144 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800145
Calin Juravle7865ac72017-06-28 11:03:12 -0700146 jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader);
147 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
148 jobject LoadDexInWellKnownClassLoader(const std::string& dex_name,
149 jclass loader_class,
150 jobject parent_loader);
151
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800152 std::string android_data_;
153 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800154
Ian Rogers700a4022014-05-19 16:49:03 -0700155 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800156
157 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
158 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800159 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800160 const DexFile* java_lang_dex_file_;
161 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800162
Calin Juravlec79470d2017-07-12 17:37:42 -0700163 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
164 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700165 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Calin Juravlec79470d2017-07-12 17:37:42 -0700166 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa,
167 Handle<mirror::ClassLoader> class_loader)
168 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700169
170 // Get the first dex file from a PathClassLoader. Will abort if it is null.
171 const DexFile* GetFirstDexFile(jobject jclass_loader);
172
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700173 std::unique_ptr<CompilerCallbacks> callbacks_;
174
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100175 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800176
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100177 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800178
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100179 // Called to finish up runtime creation and filling test fields. By default runs root
180 // initializers, initialize well-known classes, and creates the heap thread pool.
181 virtual void FinalizeSetup();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800182
Calin Juravlec79470d2017-07-12 17:37:42 -0700183 // Creates the class path string for the given dex files (the list of dex file locations
184 // separated by ':').
185 std::string CreateClassPath(
186 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
187 // Same as CreateClassPath but add the dex file checksum after each location. The separator
188 // is '*'.
189 std::string CreateClassPathWithChecksums(
190 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
191
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800192 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800193 static std::string GetCoreFileLocation(const char* suffix);
194
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800195 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800196};
197
Mathieu Chartier91c91162016-01-15 09:48:15 -0800198template <typename TestType>
199class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
200 public:
201 CommonRuntimeTestBase() {}
202 virtual ~CommonRuntimeTestBase() {}
203
204 protected:
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100205 virtual void SetUp() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800206 CommonRuntimeTestImpl::SetUp();
207 }
208
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100209 virtual void TearDown() OVERRIDE {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800210 CommonRuntimeTestImpl::TearDown();
211 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800212};
213
214using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
215
216template <typename Param>
217using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
218
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800219// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
220// rather than aborting, so be careful!
221class CheckJniAbortCatcher {
222 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700223 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800224
Ian Rogerse63db272014-07-15 15:36:11 -0700225 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800226
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700227 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700228 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800229
230 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700231 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800232
Ian Rogers68d8b422014-07-17 11:09:10 -0700233 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800234 std::string actual_;
235
236 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
237};
238
Jeff Hao0f7eaeb2016-08-31 17:56:13 -0700239#define TEST_DISABLED_FOR_TARGET() \
240 if (kIsTargetBuild) { \
241 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
242 return; \
243 }
244
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100245#define TEST_DISABLED_FOR_MIPS() \
Douglas Leungd90957f2015-04-30 19:22:49 -0700246 if (kRuntimeISA == kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100247 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
248 return; \
249 }
250
Roland Levillain19772bf2017-02-16 11:28:10 +0000251#define TEST_DISABLED_FOR_X86() \
252 if (kRuntimeISA == kX86) { \
253 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000254 return; \
255 }
256
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700257#define TEST_DISABLED_FOR_STRING_COMPRESSION() \
258 if (mirror::kUseStringCompression) { \
259 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \
260 return; \
261 }
262
Roland Levillain04147ef2016-09-06 11:09:41 +0100263#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
264 if (!kHostStaticBuildEnabled) { \
265 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
266 return; \
267 }
268
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700269#define TEST_DISABLED_FOR_MEMORY_TOOL() \
270 if (RUNNING_ON_MEMORY_TOOL > 0) { \
271 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
272 return; \
273 }
274
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700275#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
276 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
277 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
278 return; \
279 }
280
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800281} // namespace art
282
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800283#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_