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 | |
| 25 | #include "base/mutex.h" |
| 26 | #include "globals.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 27 | #include "os.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 31 | class ClassLinker; |
| 32 | class CompilerCallbacks; |
| 33 | class DexFile; |
| 34 | class JavaVMExt; |
| 35 | class Runtime; |
| 36 | typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; |
| 37 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 38 | class ScratchFile { |
| 39 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 40 | ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 41 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 42 | ScratchFile(const ScratchFile& other, const char* suffix); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 43 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 44 | explicit ScratchFile(File* file); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 45 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 46 | ~ScratchFile(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 47 | |
| 48 | const std::string& GetFilename() const { |
| 49 | return filename_; |
| 50 | } |
| 51 | |
| 52 | File* GetFile() const { |
| 53 | return file_.get(); |
| 54 | } |
| 55 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 56 | int GetFd() const; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 57 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 58 | void Unlink(); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 59 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 60 | private: |
| 61 | std::string filename_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 62 | std::unique_ptr<File> file_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 65 | class CommonRuntimeTest : public testing::Test { |
| 66 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 67 | static void SetEnvironmentVariables(std::string& android_data); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 68 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 69 | CommonRuntimeTest(); |
| 70 | ~CommonRuntimeTest(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 71 | |
| 72 | protected: |
| 73 | static bool IsHost() { |
| 74 | return !kIsTargetBuild; |
| 75 | } |
| 76 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 77 | const DexFile* LoadExpectSingleDexFile(const char* location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 78 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 79 | virtual void SetUp(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 80 | |
| 81 | // Allow subclases such as CommonCompilerTest to add extra options. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 82 | virtual void SetUpRuntimeOptions(RuntimeOptions* options) {} |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 83 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 84 | void ClearDirectory(const char* dirpath); |
| 85 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 86 | virtual void TearDown(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 87 | |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame^] | 88 | // Gets the path of the libcore dex file. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 89 | std::string GetLibCoreDexFileName(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 90 | |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame^] | 91 | // Gets the path of the specified dex file for host or target. |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 92 | std::string GetDexFileName(const std::string& jar_prefix); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 93 | |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame^] | 94 | // Gets the path of the libcore oat file. |
| 95 | std::string GetLibCoreOatFileName(); |
| 96 | |
| 97 | // Gets the path of the specified oat file for host or target. |
| 98 | std::string GetOatFileName(const std::string& oat_prefix); |
| 99 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 100 | std::string GetTestAndroidRoot(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 101 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 102 | std::vector<const DexFile*> OpenTestDexFiles(const char* name) |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 103 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 104 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 105 | const DexFile* OpenTestDexFile(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 106 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 107 | jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 108 | |
| 109 | std::string android_data_; |
| 110 | std::string dalvik_cache_; |
| 111 | const DexFile* java_lang_dex_file_; // owned by runtime_ |
| 112 | std::vector<const DexFile*> boot_class_path_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 113 | std::unique_ptr<Runtime> runtime_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 114 | // Owned by the runtime |
| 115 | ClassLinker* class_linker_; |
| 116 | |
| 117 | private: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 118 | std::unique_ptr<CompilerCallbacks> callbacks_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 119 | std::vector<const DexFile*> opened_dex_files_; |
| 120 | }; |
| 121 | |
| 122 | // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on |
| 123 | // rather than aborting, so be careful! |
| 124 | class CheckJniAbortCatcher { |
| 125 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 126 | CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 127 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 128 | ~CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 129 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 130 | void Check(const char* expected_text); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 131 | |
| 132 | private: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 133 | static void Hook(void* data, const std::string& reason); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 134 | |
| 135 | JavaVMExt* vm_; |
| 136 | std::string actual_; |
| 137 | |
| 138 | DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); |
| 139 | }; |
| 140 | |
| 141 | // TODO: These tests were disabled for portable when we went to having |
| 142 | // MCLinker link LLVM ELF output because we no longer just have code |
| 143 | // blobs in memory. We'll need to dlopen to load and relocate |
| 144 | // temporary output to resurrect these tests. |
| 145 | #define TEST_DISABLED_FOR_PORTABLE() \ |
| 146 | if (kUsePortableCompiler) { \ |
| 147 | printf("WARNING: TEST DISABLED FOR PORTABLE\n"); \ |
| 148 | return; \ |
| 149 | } |
| 150 | |
Hiroshi Yamauchi | 05b15d6 | 2014-03-19 12:57:56 -0700 | [diff] [blame] | 151 | // TODO: When heap reference poisoning works with the compiler, get rid of this. |
| 152 | #define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \ |
| 153 | if (kPoisonHeapReferences) { \ |
| 154 | printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \ |
| 155 | return; \ |
| 156 | } |
| 157 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 158 | } // namespace art |
| 159 | |
| 160 | namespace std { |
| 161 | |
| 162 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 163 | template <typename T> |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 164 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 165 | |
| 166 | } // namespace std |
| 167 | |
| 168 | #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ |