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 20 #include <gtest/gtest.h> 21 #include <jni.h> 22 23 #include <string> 24 25 #include "arch/instruction_set.h" 26 #include "base/mutex.h" 27 #include "globals.h" 28 #include "os.h" 29 30 namespace art { 31 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 39 class ScratchFile { 40 public: 41 ScratchFile(); 42 43 ScratchFile(const ScratchFile& other, const char* suffix); 44 45 explicit ScratchFile(File* file); 46 47 ~ScratchFile(); 48 GetFilename()49 const std::string& GetFilename() const { 50 return filename_; 51 } 52 GetFile()53 File* GetFile() const { 54 return file_.get(); 55 } 56 57 int GetFd() const; 58 59 void Close(); 60 void Unlink(); 61 62 private: 63 std::string filename_; 64 std::unique_ptr<File> file_; 65 }; 66 67 class CommonRuntimeTest : public testing::Test { 68 public: 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); 76 77 CommonRuntimeTest(); 78 ~CommonRuntimeTest(); 79 80 // Gets the path of the libcore dex file. 81 static std::string GetLibCoreDexFileName(); 82 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 89 protected: IsHost()90 static bool IsHost() { 91 return !kIsTargetBuild; 92 } 93 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 100 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); 101 102 virtual void SetUp(); 103 104 // Allow subclases such as CommonCompilerTest to add extra options. SetUpRuntimeOptions(RuntimeOptions * options ATTRIBUTE_UNUSED)105 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} 106 107 void ClearDirectory(const char* dirpath); 108 109 virtual void TearDown(); 110 111 // Called before the runtime is created. PreRuntimeCreate()112 virtual void PreRuntimeCreate() {} 113 114 // Called after the runtime is created. PostRuntimeCreate()115 virtual void PostRuntimeCreate() {} 116 117 // Gets the path of the specified dex file for host or target. 118 static std::string GetDexFileName(const std::string& jar_prefix); 119 120 std::string GetTestAndroidRoot(); 121 122 std::string GetTestDexFileName(const char* name); 123 124 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name) 125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 126 127 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name) 128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 129 130 jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 131 132 std::string android_data_; 133 std::string dalvik_cache_; 134 135 std::unique_ptr<Runtime> runtime_; 136 137 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all 138 // owned by the runtime. 139 ClassLinker* class_linker_; 140 const DexFile* java_lang_dex_file_; 141 std::vector<const DexFile*> boot_class_path_; 142 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 150 std::unique_ptr<CompilerCallbacks> callbacks_; 151 152 private: 153 static std::string GetCoreFileLocation(const char* suffix); 154 155 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; 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: 162 CheckJniAbortCatcher(); 163 164 ~CheckJniAbortCatcher(); 165 166 void Check(const char* expected_text); 167 168 private: 169 static void Hook(void* data, const std::string& reason); 170 171 JavaVMExt* const vm_; 172 std::string actual_; 173 174 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); 175 }; 176 177 // TODO: When heap reference poisoning works with the compiler, get rid of this. 178 #define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \ 179 if (kPoisonHeapReferences) { \ 180 printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \ 181 return; \ 182 } 183 184 #define TEST_DISABLED_FOR_MIPS() \ 185 if (kRuntimeISA == kMips) { \ 186 printf("WARNING: TEST DISABLED FOR MIPS\n"); \ 187 return; \ 188 } 189 190 } // namespace art 191 192 namespace std { 193 194 // TODO: isn't gtest supposed to be able to print STL types for itself? 195 template <typename T> 196 std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); 197 198 } // namespace std 199 200 #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ 201