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" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | #include "os.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 32 | class ClassLinker; |
| 33 | class CompilerCallbacks; |
| 34 | class DexFile; |
| 35 | class JavaVMExt; |
| 36 | class Runtime; |
| 37 | typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; |
| 38 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 39 | class ScratchFile { |
| 40 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 42 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 43 | ScratchFile(const ScratchFile& other, const char* suffix); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 44 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 45 | explicit ScratchFile(File* file); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 46 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 47 | ~ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 48 | |
| 49 | const std::string& GetFilename() const { |
| 50 | return filename_; |
| 51 | } |
| 52 | |
| 53 | File* GetFile() const { |
| 54 | return file_.get(); |
| 55 | } |
| 56 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 57 | int GetFd() const; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 58 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 59 | void Close(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 60 | void Unlink(); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 61 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 62 | private: |
| 63 | std::string filename_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 64 | std::unique_ptr<File> file_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 67 | class CommonRuntimeTest : public testing::Test { |
| 68 | public: |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 69 | static void SetUpAndroidRoot(); |
| 70 | |
| 71 | // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a |
| 72 | // non-derived class, be sure to also call the corresponding tear-down below. |
| 73 | static void SetUpAndroidData(std::string& android_data); |
| 74 | |
| 75 | static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 76 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 77 | CommonRuntimeTest(); |
| 78 | ~CommonRuntimeTest(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 79 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 80 | // Gets the path of the libcore dex file. |
| 81 | static std::string GetLibCoreDexFileName(); |
| 82 | |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 83 | // Returns bin directory which contains host's prebuild tools. |
| 84 | static std::string GetAndroidHostToolsDir(); |
| 85 | |
| 86 | // Returns bin directory which contains target's prebuild tools. |
| 87 | static std::string GetAndroidTargetToolsDir(InstructionSet isa); |
| 88 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 89 | protected: |
| 90 | static bool IsHost() { |
| 91 | return !kIsTargetBuild; |
| 92 | } |
| 93 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 94 | // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art |
| 95 | static std::string GetCoreArtLocation(); |
| 96 | |
| 97 | // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat |
| 98 | static std::string GetCoreOatLocation(); |
| 99 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 100 | std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 101 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 102 | virtual void SetUp(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 103 | |
| 104 | // Allow subclases such as CommonCompilerTest to add extra options. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 105 | virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 106 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 107 | void ClearDirectory(const char* dirpath); |
| 108 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 109 | virtual void TearDown(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 110 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 111 | // Called before the runtime is created. |
| 112 | virtual void PreRuntimeCreate() {} |
| 113 | |
| 114 | // Called after the runtime is created. |
| 115 | virtual void PostRuntimeCreate() {} |
| 116 | |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 117 | // Gets the path of the specified dex file for host or target. |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 118 | static std::string GetDexFileName(const std::string& jar_prefix); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 119 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 120 | std::string GetTestAndroidRoot(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 121 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 122 | std::string GetTestDexFileName(const char* name); |
| 123 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 124 | std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 125 | SHARED_REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 126 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 127 | std::unique_ptr<const DexFile> OpenTestDexFile(const char* name) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 128 | SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 129 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 130 | jobject LoadDex(const char* dex_name) SHARED_REQUIRES(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 131 | |
| 132 | std::string android_data_; |
| 133 | std::string dalvik_cache_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 134 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 135 | std::unique_ptr<Runtime> runtime_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 136 | |
| 137 | // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all |
| 138 | // owned by the runtime. |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 139 | ClassLinker* class_linker_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 140 | const DexFile* java_lang_dex_file_; |
| 141 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 142 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 143 | // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex |
| 144 | // arrays. |
| 145 | std::vector<const DexFile*> GetDexFiles(jobject jclass_loader); |
| 146 | |
| 147 | // Get the first dex file from a PathClassLoader. Will abort if it is null. |
| 148 | const DexFile* GetFirstDexFile(jobject jclass_loader); |
| 149 | |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 150 | std::unique_ptr<CompilerCallbacks> callbacks_; |
| 151 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 152 | private: |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 153 | static std::string GetCoreFileLocation(const char* suffix); |
| 154 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 155 | std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on |
| 159 | // rather than aborting, so be careful! |
| 160 | class CheckJniAbortCatcher { |
| 161 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 162 | CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 163 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 164 | ~CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 165 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 166 | void Check(const char* expected_text); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 167 | |
| 168 | private: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 169 | static void Hook(void* data, const std::string& reason); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 170 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 171 | JavaVMExt* const vm_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 172 | std::string actual_; |
| 173 | |
| 174 | DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); |
| 175 | }; |
| 176 | |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 177 | #define TEST_DISABLED_FOR_MIPS() \ |
Douglas Leung | d90957f | 2015-04-30 19:22:49 -0700 | [diff] [blame] | 178 | if (kRuntimeISA == kMips) { \ |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 179 | printf("WARNING: TEST DISABLED FOR MIPS\n"); \ |
| 180 | return; \ |
| 181 | } |
| 182 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 183 | } // namespace art |
| 184 | |
| 185 | namespace std { |
| 186 | |
| 187 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 188 | template <typename T> |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 189 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 190 | |
| 191 | } // namespace std |
| 192 | |
| 193 | #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ |