Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | #include <jni.h> |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 25 | #include "arch/instruction_set.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 26 | #include "base/mutex.h" |
| 27 | #include "globals.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 28 | // TODO: Add inl file and avoid including inl. |
| 29 | #include "obj_ptr-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 30 | #include "os.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 34 | // OBJ pointer helpers to avoid needing .Decode everywhere. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 35 | #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 Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 39 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 40 | class ClassLinker; |
| 41 | class CompilerCallbacks; |
| 42 | class DexFile; |
| 43 | class JavaVMExt; |
| 44 | class Runtime; |
| 45 | typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; |
| 46 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 47 | class ScratchFile { |
| 48 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 49 | ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 50 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 51 | explicit ScratchFile(const std::string& filename); |
| 52 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 53 | ScratchFile(const ScratchFile& other, const char* suffix); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 54 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 55 | explicit ScratchFile(ScratchFile&& other); |
| 56 | |
| 57 | ScratchFile& operator=(ScratchFile&& other); |
| 58 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 59 | explicit ScratchFile(File* file); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 60 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 61 | ~ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 62 | |
| 63 | const std::string& GetFilename() const { |
| 64 | return filename_; |
| 65 | } |
| 66 | |
| 67 | File* GetFile() const { |
| 68 | return file_.get(); |
| 69 | } |
| 70 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 71 | int GetFd() const; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 72 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 73 | void Close(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 74 | void Unlink(); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 75 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 76 | private: |
| 77 | std::string filename_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 78 | std::unique_ptr<File> file_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 81 | class CommonRuntimeTestImpl { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 82 | public: |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 83 | CommonRuntimeTestImpl(); |
| 84 | virtual ~CommonRuntimeTestImpl(); |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 85 | static void SetUpAndroidRoot(); |
| 86 | |
| 87 | // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a |
| 88 | // non-derived class, be sure to also call the corresponding tear-down below. |
| 89 | static void SetUpAndroidData(std::string& android_data); |
| 90 | |
| 91 | static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 92 | |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 93 | // Gets the paths of the libcore dex files. |
| 94 | static std::vector<std::string> GetLibCoreDexFileNames(); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 95 | |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 96 | // Returns bin directory which contains host's prebuild tools. |
| 97 | static std::string GetAndroidHostToolsDir(); |
| 98 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 99 | // Returns bin directory wahich contains target's prebuild tools. |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 100 | static std::string GetAndroidTargetToolsDir(InstructionSet isa); |
| 101 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 102 | protected: |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 103 | // Allow subclases such as CommonCompilerTest to add extra options. |
| 104 | virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} |
| 105 | |
| 106 | // Called before the runtime is created. |
| 107 | virtual void PreRuntimeCreate() {} |
| 108 | |
| 109 | // Called after the runtime is created. |
| 110 | virtual void PostRuntimeCreate() {} |
| 111 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 112 | static bool IsHost() { |
| 113 | return !kIsTargetBuild; |
| 114 | } |
| 115 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 116 | // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art |
| 117 | static std::string GetCoreArtLocation(); |
| 118 | |
| 119 | // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat |
| 120 | static std::string GetCoreOatLocation(); |
| 121 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 122 | std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 123 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 124 | void ClearDirectory(const char* dirpath); |
| 125 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 126 | std::string GetTestAndroidRoot(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 127 | |
Andreas Gampe | e1459ae | 2016-06-29 09:36:30 -0700 | [diff] [blame] | 128 | std::string GetTestDexFileName(const char* name) const; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 129 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 130 | std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 131 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 132 | std::unique_ptr<const DexFile> OpenTestDexFile(const char* name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 134 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 135 | jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 136 | |
| 137 | std::string android_data_; |
| 138 | std::string dalvik_cache_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 139 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 140 | std::unique_ptr<Runtime> runtime_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 141 | |
| 142 | // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all |
| 143 | // owned by the runtime. |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 144 | ClassLinker* class_linker_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 145 | const DexFile* java_lang_dex_file_; |
| 146 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 147 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 148 | // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex |
| 149 | // arrays. |
| 150 | std::vector<const DexFile*> GetDexFiles(jobject jclass_loader); |
| 151 | |
| 152 | // Get the first dex file from a PathClassLoader. Will abort if it is null. |
| 153 | const DexFile* GetFirstDexFile(jobject jclass_loader); |
| 154 | |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 155 | std::unique_ptr<CompilerCallbacks> callbacks_; |
| 156 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 157 | virtual void SetUp(); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 158 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 159 | virtual void TearDown(); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 160 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 161 | // Called to finish up runtime creation and filling test fields. By default runs root |
| 162 | // initializers, initialize well-known classes, and creates the heap thread pool. |
| 163 | virtual void FinalizeSetup(); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 164 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 165 | private: |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 166 | static std::string GetCoreFileLocation(const char* suffix); |
| 167 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 168 | std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 171 | template <typename TestType> |
| 172 | class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl { |
| 173 | public: |
| 174 | CommonRuntimeTestBase() {} |
| 175 | virtual ~CommonRuntimeTestBase() {} |
| 176 | |
| 177 | protected: |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 178 | virtual void SetUp() OVERRIDE { |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 179 | CommonRuntimeTestImpl::SetUp(); |
| 180 | } |
| 181 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 182 | virtual void TearDown() OVERRIDE { |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 183 | CommonRuntimeTestImpl::TearDown(); |
| 184 | } |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>; |
| 188 | |
| 189 | template <typename Param> |
| 190 | using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>; |
| 191 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 192 | // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on |
| 193 | // rather than aborting, so be careful! |
| 194 | class CheckJniAbortCatcher { |
| 195 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 196 | CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 197 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 198 | ~CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 199 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 200 | void Check(const std::string& expected_text); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 201 | void Check(const char* expected_text); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 202 | |
| 203 | private: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 204 | static void Hook(void* data, const std::string& reason); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 205 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 206 | JavaVMExt* const vm_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 207 | std::string actual_; |
| 208 | |
| 209 | DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); |
| 210 | }; |
| 211 | |
Jeff Hao | 0f7eaeb | 2016-08-31 17:56:13 -0700 | [diff] [blame] | 212 | #define TEST_DISABLED_FOR_TARGET() \ |
| 213 | if (kIsTargetBuild) { \ |
| 214 | printf("WARNING: TEST DISABLED FOR TARGET\n"); \ |
| 215 | return; \ |
| 216 | } |
| 217 | |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 218 | #define TEST_DISABLED_FOR_MIPS() \ |
Douglas Leung | d90957f | 2015-04-30 19:22:49 -0700 | [diff] [blame] | 219 | if (kRuntimeISA == kMips) { \ |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 220 | printf("WARNING: TEST DISABLED FOR MIPS\n"); \ |
| 221 | return; \ |
| 222 | } |
| 223 | |
Roland Levillain | 63b6eb4 | 2016-07-28 16:37:28 +0100 | [diff] [blame] | 224 | #define TEST_DISABLED_FOR_READ_BARRIER_ON_X86() \ |
| 225 | if (kUseReadBarrier && kRuntimeISA == kX86) { \ |
| 226 | printf("WARNING: TEST DISABLED FOR READ BARRIER ON X86\n"); \ |
| 227 | return; \ |
| 228 | } |
| 229 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 230 | #define TEST_DISABLED_FOR_STRING_COMPRESSION() \ |
| 231 | if (mirror::kUseStringCompression) { \ |
| 232 | printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \ |
| 233 | return; \ |
| 234 | } |
| 235 | |
Roland Levillain | 04147ef | 2016-09-06 11:09:41 +0100 | [diff] [blame] | 236 | #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \ |
| 237 | if (!kHostStaticBuildEnabled) { \ |
| 238 | printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \ |
| 239 | return; \ |
| 240 | } |
| 241 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 242 | } // namespace art |
| 243 | |
| 244 | namespace std { |
| 245 | |
| 246 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 247 | template <typename T> |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 248 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 249 | |
| 250 | } // namespace std |
| 251 | |
| 252 | #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ |