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 // TODO: Add inl file and avoid including inl. 29 #include "obj_ptr-inl.h" 30 #include "os.h" 31 #include "scoped_thread_state_change-inl.h" 32 33 namespace art { 34 35 // OBJ pointer helpers to avoid needing .Decode everywhere. 36 #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()); 40 41 class ClassLinker; 42 class CompilerCallbacks; 43 class DexFile; 44 class JavaVMExt; 45 class Runtime; 46 typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; 47 48 uint8_t* DecodeBase64(const char* src, size_t* dst_size); 49 50 class ScratchFile { 51 public: 52 ScratchFile(); 53 54 explicit ScratchFile(const std::string& filename); 55 56 ScratchFile(const ScratchFile& other, const char* suffix); 57 58 ScratchFile(ScratchFile&& other); 59 60 ScratchFile& operator=(ScratchFile&& other); 61 62 explicit ScratchFile(File* file); 63 64 ~ScratchFile(); 65 GetFilename()66 const std::string& GetFilename() const { 67 return filename_; 68 } 69 GetFile()70 File* GetFile() const { 71 return file_.get(); 72 } 73 74 int GetFd() const; 75 76 void Close(); 77 void Unlink(); 78 79 private: 80 std::string filename_; 81 std::unique_ptr<File> file_; 82 }; 83 84 class CommonRuntimeTestImpl { 85 public: 86 CommonRuntimeTestImpl(); 87 virtual ~CommonRuntimeTestImpl(); 88 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); 95 96 // Gets the paths of the libcore dex files. 97 static std::vector<std::string> GetLibCoreDexFileNames(); 98 99 // Returns bin directory which contains host's prebuild tools. 100 static std::string GetAndroidHostToolsDir(); 101 102 // Returns bin directory which contains target's prebuild tools. 103 static std::string GetAndroidTargetToolsDir(InstructionSet isa); 104 105 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods). 106 std::string GetTestDexFileName(const char* name) const; 107 108 protected: 109 // Allow subclases such as CommonCompilerTest to add extra options. SetUpRuntimeOptions(RuntimeOptions * options ATTRIBUTE_UNUSED)110 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} 111 112 // Called before the runtime is created. PreRuntimeCreate()113 virtual void PreRuntimeCreate() {} 114 115 // Called after the runtime is created. PostRuntimeCreate()116 virtual void PostRuntimeCreate() {} 117 IsHost()118 static bool IsHost() { 119 return !kIsTargetBuild; 120 } 121 122 // 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 128 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); 129 130 void ClearDirectory(const char* dirpath, bool recursive = true); 131 132 std::string GetTestAndroidRoot(); 133 134 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name); 135 136 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name); 137 138 // Loads the test dex file identified by the given dex_name into a PathClassLoader. 139 // Returns the created class loader. 140 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_); 141 // 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. 143 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name) 144 REQUIRES_SHARED(Locks::mutator_lock_); 145 146 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 152 std::string android_data_; 153 std::string dalvik_cache_; 154 155 std::unique_ptr<Runtime> runtime_; 156 157 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all 158 // owned by the runtime. 159 ClassLinker* class_linker_; 160 const DexFile* java_lang_dex_file_; 161 std::vector<const DexFile*> boot_class_path_; 162 163 // 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. 165 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader); 166 std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa, 167 Handle<mirror::ClassLoader> class_loader) 168 REQUIRES_SHARED(Locks::mutator_lock_); 169 170 // Get the first dex file from a PathClassLoader. Will abort if it is null. 171 const DexFile* GetFirstDexFile(jobject jclass_loader); 172 173 std::unique_ptr<CompilerCallbacks> callbacks_; 174 175 virtual void SetUp(); 176 177 virtual void TearDown(); 178 179 // 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(); 182 183 // 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 192 private: 193 static std::string GetCoreFileLocation(const char* suffix); 194 195 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; 196 }; 197 198 template <typename TestType> 199 class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl { 200 public: CommonRuntimeTestBase()201 CommonRuntimeTestBase() {} ~CommonRuntimeTestBase()202 virtual ~CommonRuntimeTestBase() {} 203 204 protected: SetUp()205 virtual void SetUp() OVERRIDE { 206 CommonRuntimeTestImpl::SetUp(); 207 } 208 TearDown()209 virtual void TearDown() OVERRIDE { 210 CommonRuntimeTestImpl::TearDown(); 211 } 212 }; 213 214 using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>; 215 216 template <typename Param> 217 using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>; 218 219 // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on 220 // rather than aborting, so be careful! 221 class CheckJniAbortCatcher { 222 public: 223 CheckJniAbortCatcher(); 224 225 ~CheckJniAbortCatcher(); 226 227 void Check(const std::string& expected_text); 228 void Check(const char* expected_text); 229 230 private: 231 static void Hook(void* data, const std::string& reason); 232 233 JavaVMExt* const vm_; 234 std::string actual_; 235 236 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); 237 }; 238 239 #define TEST_DISABLED_FOR_TARGET() \ 240 if (kIsTargetBuild) { \ 241 printf("WARNING: TEST DISABLED FOR TARGET\n"); \ 242 return; \ 243 } 244 245 #define TEST_DISABLED_FOR_MIPS() \ 246 if (kRuntimeISA == kMips) { \ 247 printf("WARNING: TEST DISABLED FOR MIPS\n"); \ 248 return; \ 249 } 250 251 #define TEST_DISABLED_FOR_X86() \ 252 if (kRuntimeISA == kX86) { \ 253 printf("WARNING: TEST DISABLED FOR X86\n"); \ 254 return; \ 255 } 256 257 #define TEST_DISABLED_FOR_STRING_COMPRESSION() \ 258 if (mirror::kUseStringCompression) { \ 259 printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \ 260 return; \ 261 } 262 263 #define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \ 264 if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \ 265 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \ 266 return; \ 267 } 268 269 #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \ 270 if (!kHostStaticBuildEnabled) { \ 271 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \ 272 return; \ 273 } 274 275 #define TEST_DISABLED_FOR_MEMORY_TOOL() \ 276 if (RUNNING_ON_MEMORY_TOOL > 0) { \ 277 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \ 278 return; \ 279 } 280 281 #define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \ 282 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \ 283 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \ 284 return; \ 285 } 286 287 } // namespace art 288 289 #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ 290