common_runtime_test.cc revision 4460a84be92b5a94ecfb5c650aef4945ab849c93
1/* 2 * Copyright (C) 2012 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#include "common_runtime_test.h" 18 19#include <dirent.h> 20#include <dlfcn.h> 21#include <fcntl.h> 22#include <ScopedLocalRef.h> 23#include <stdlib.h> 24 25#include "../../external/icu/icu4c/source/common/unicode/uvernum.h" 26#include "base/macros.h" 27#include "base/logging.h" 28#include "base/stl_util.h" 29#include "base/stringprintf.h" 30#include "base/unix_file/fd_file.h" 31#include "class_linker.h" 32#include "compiler_callbacks.h" 33#include "dex_file.h" 34#include "gc_root-inl.h" 35#include "gc/heap.h" 36#include "gtest/gtest.h" 37#include "interpreter/unstarted_runtime.h" 38#include "jni_internal.h" 39#include "mirror/class_loader.h" 40#include "mem_map.h" 41#include "noop_compiler_callbacks.h" 42#include "os.h" 43#include "runtime-inl.h" 44#include "scoped_thread_state_change.h" 45#include "thread.h" 46#include "well_known_classes.h" 47 48int main(int argc, char **argv) { 49 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native 50 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses 51 // everything else. In case you want to see all messages, comment out the line. 52 setenv("ANDROID_LOG_TAGS", "*:e", 1); 53 54 art::InitLogging(argv); 55 LOG(::art::INFO) << "Running main() from common_runtime_test.cc..."; 56 testing::InitGoogleTest(&argc, argv); 57 return RUN_ALL_TESTS(); 58} 59 60namespace art { 61 62ScratchFile::ScratchFile() { 63 // ANDROID_DATA needs to be set 64 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << 65 "Are you subclassing RuntimeTest?"; 66 filename_ = getenv("ANDROID_DATA"); 67 filename_ += "/TmpFile-XXXXXX"; 68 int fd = mkstemp(&filename_[0]); 69 CHECK_NE(-1, fd); 70 file_.reset(new File(fd, GetFilename(), true)); 71} 72 73ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) { 74 filename_ = other.GetFilename(); 75 filename_ += suffix; 76 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); 77 CHECK_NE(-1, fd); 78 file_.reset(new File(fd, GetFilename(), true)); 79} 80 81ScratchFile::ScratchFile(File* file) { 82 CHECK(file != NULL); 83 filename_ = file->GetPath(); 84 file_.reset(file); 85} 86 87ScratchFile::~ScratchFile() { 88 Unlink(); 89} 90 91int ScratchFile::GetFd() const { 92 return file_->Fd(); 93} 94 95void ScratchFile::Close() { 96 if (file_.get() != nullptr) { 97 if (file_->FlushCloseOrErase() != 0) { 98 PLOG(WARNING) << "Error closing scratch file."; 99 } 100 } 101} 102 103void ScratchFile::Unlink() { 104 if (!OS::FileExists(filename_.c_str())) { 105 return; 106 } 107 Close(); 108 int unlink_result = unlink(filename_.c_str()); 109 CHECK_EQ(0, unlink_result); 110} 111 112static bool unstarted_initialized_ = false; 113 114CommonRuntimeTest::CommonRuntimeTest() {} 115CommonRuntimeTest::~CommonRuntimeTest() { 116 // Ensure the dex files are cleaned up before the runtime. 117 loaded_dex_files_.clear(); 118 runtime_.reset(); 119} 120 121void CommonRuntimeTest::SetUpAndroidRoot() { 122 if (IsHost()) { 123 // $ANDROID_ROOT is set on the device, but not necessarily on the host. 124 // But it needs to be set so that icu4c can find its locale data. 125 const char* android_root_from_env = getenv("ANDROID_ROOT"); 126 if (android_root_from_env == nullptr) { 127 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. 128 const char* android_host_out = getenv("ANDROID_HOST_OUT"); 129 if (android_host_out != nullptr) { 130 setenv("ANDROID_ROOT", android_host_out, 1); 131 } else { 132 // Build it from ANDROID_BUILD_TOP or cwd 133 std::string root; 134 const char* android_build_top = getenv("ANDROID_BUILD_TOP"); 135 if (android_build_top != nullptr) { 136 root += android_build_top; 137 } else { 138 // Not set by build server, so default to current directory 139 char* cwd = getcwd(nullptr, 0); 140 setenv("ANDROID_BUILD_TOP", cwd, 1); 141 root += cwd; 142 free(cwd); 143 } 144#if defined(__linux__) 145 root += "/out/host/linux-x86"; 146#elif defined(__APPLE__) 147 root += "/out/host/darwin-x86"; 148#else 149#error unsupported OS 150#endif 151 setenv("ANDROID_ROOT", root.c_str(), 1); 152 } 153 } 154 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. 155 156 // Not set by build server, so default 157 if (getenv("ANDROID_HOST_OUT") == nullptr) { 158 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); 159 } 160 } 161} 162 163void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) { 164 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache 165 if (IsHost()) { 166 const char* tmpdir = getenv("TMPDIR"); 167 if (tmpdir != nullptr && tmpdir[0] != 0) { 168 android_data = tmpdir; 169 } else { 170 android_data = "/tmp"; 171 } 172 } else { 173 android_data = "/data/dalvik-cache"; 174 } 175 android_data += "/art-data-XXXXXX"; 176 if (mkdtemp(&android_data[0]) == nullptr) { 177 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; 178 } 179 setenv("ANDROID_DATA", android_data.c_str(), 1); 180} 181 182void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) { 183 if (fail_on_error) { 184 ASSERT_EQ(rmdir(android_data.c_str()), 0); 185 } else { 186 rmdir(android_data.c_str()); 187 } 188} 189 190std::string CommonRuntimeTest::GetCoreArtLocation() { 191 return GetCoreFileLocation("art"); 192} 193 194std::string CommonRuntimeTest::GetCoreOatLocation() { 195 return GetCoreFileLocation("oat"); 196} 197 198std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { 199 std::vector<std::unique_ptr<const DexFile>> dex_files; 200 std::string error_msg; 201 MemMap::Init(); 202 if (!DexFile::Open(location, location, &error_msg, &dex_files)) { 203 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; 204 UNREACHABLE(); 205 } else { 206 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; 207 return std::move(dex_files[0]); 208 } 209} 210 211void CommonRuntimeTest::SetUp() { 212 SetUpAndroidRoot(); 213 SetUpAndroidData(android_data_); 214 dalvik_cache_.append(android_data_.c_str()); 215 dalvik_cache_.append("/dalvik-cache"); 216 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); 217 ASSERT_EQ(mkdir_result, 0); 218 219 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); 220 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); 221 222 callbacks_.reset(new NoopCompilerCallbacks()); 223 224 RuntimeOptions options; 225 std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName(); 226 options.push_back(std::make_pair(boot_class_path_string, nullptr)); 227 options.push_back(std::make_pair("-Xcheck:jni", nullptr)); 228 options.push_back(std::make_pair(min_heap_string, nullptr)); 229 options.push_back(std::make_pair(max_heap_string, nullptr)); 230 options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); 231 SetUpRuntimeOptions(&options); 232 233 PreRuntimeCreate(); 234 if (!Runtime::Create(options, false)) { 235 LOG(FATAL) << "Failed to create runtime"; 236 return; 237 } 238 PostRuntimeCreate(); 239 runtime_.reset(Runtime::Current()); 240 class_linker_ = runtime_->GetClassLinker(); 241 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); 242 243 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this 244 // set up. 245 if (!unstarted_initialized_) { 246 interpreter::UnstartedRuntimeInitialize(); 247 unstarted_initialized_ = true; 248 } 249 250 class_linker_->RunRootClinits(); 251 boot_class_path_ = class_linker_->GetBootClassPath(); 252 java_lang_dex_file_ = boot_class_path_[0]; 253 254 255 // Runtime::Create acquired the mutator_lock_ that is normally given away when we 256 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. 257 Thread::Current()->TransitionFromRunnableToSuspended(kNative); 258 259 // We're back in native, take the opportunity to initialize well known classes. 260 WellKnownClasses::Init(Thread::Current()->GetJniEnv()); 261 262 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread 263 // pool is created by the runtime. 264 runtime_->GetHeap()->CreateThreadPool(); 265 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test 266 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject). 267 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U); 268 269 // Get the boot class path from the runtime so it can be used in tests. 270 boot_class_path_ = class_linker_->GetBootClassPath(); 271 ASSERT_FALSE(boot_class_path_.empty()); 272 java_lang_dex_file_ = boot_class_path_[0]; 273} 274 275void CommonRuntimeTest::ClearDirectory(const char* dirpath) { 276 ASSERT_TRUE(dirpath != nullptr); 277 DIR* dir = opendir(dirpath); 278 ASSERT_TRUE(dir != nullptr); 279 dirent* e; 280 struct stat s; 281 while ((e = readdir(dir)) != nullptr) { 282 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { 283 continue; 284 } 285 std::string filename(dirpath); 286 filename.push_back('/'); 287 filename.append(e->d_name); 288 int stat_result = lstat(filename.c_str(), &s); 289 ASSERT_EQ(0, stat_result) << "unable to stat " << filename; 290 if (S_ISDIR(s.st_mode)) { 291 ClearDirectory(filename.c_str()); 292 int rmdir_result = rmdir(filename.c_str()); 293 ASSERT_EQ(0, rmdir_result) << filename; 294 } else { 295 int unlink_result = unlink(filename.c_str()); 296 ASSERT_EQ(0, unlink_result) << filename; 297 } 298 } 299 closedir(dir); 300} 301 302void CommonRuntimeTest::TearDown() { 303 const char* android_data = getenv("ANDROID_DATA"); 304 ASSERT_TRUE(android_data != nullptr); 305 ClearDirectory(dalvik_cache_.c_str()); 306 int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); 307 ASSERT_EQ(0, rmdir_cache_result); 308 TearDownAndroidData(android_data_, true); 309 310 // icu4c has a fixed 10-element array "gCommonICUDataArray". 311 // If we run > 10 tests, we fill that array and u_setCommonData fails. 312 // There's a function to clear the array, but it's not public... 313 typedef void (*IcuCleanupFn)(); 314 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); 315 CHECK(sym != nullptr) << dlerror(); 316 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); 317 (*icu_cleanup_fn)(); 318 319 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test 320} 321 322std::string CommonRuntimeTest::GetLibCoreDexFileName() { 323 return GetDexFileName("core-libart"); 324} 325 326std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) { 327 if (IsHost()) { 328 const char* host_dir = getenv("ANDROID_HOST_OUT"); 329 CHECK(host_dir != nullptr); 330 return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str()); 331 } 332 return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str()); 333} 334 335std::string CommonRuntimeTest::GetTestAndroidRoot() { 336 if (IsHost()) { 337 const char* host_dir = getenv("ANDROID_HOST_OUT"); 338 CHECK(host_dir != nullptr); 339 return host_dir; 340 } 341 return GetAndroidRoot(); 342} 343 344// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. 345#ifdef ART_TARGET 346#ifndef ART_TARGET_NATIVETEST_DIR 347#error "ART_TARGET_NATIVETEST_DIR not set." 348#endif 349// Wrap it as a string literal. 350#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" 351#else 352#define ART_TARGET_NATIVETEST_DIR_STRING "" 353#endif 354 355std::string CommonRuntimeTest::GetTestDexFileName(const char* name) { 356 CHECK(name != nullptr); 357 std::string filename; 358 if (IsHost()) { 359 filename += getenv("ANDROID_HOST_OUT"); 360 filename += "/framework/"; 361 } else { 362 filename += ART_TARGET_NATIVETEST_DIR_STRING; 363 } 364 filename += "art-gtest-"; 365 filename += name; 366 filename += ".jar"; 367 return filename; 368} 369 370std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) { 371 std::string filename = GetTestDexFileName(name); 372 std::string error_msg; 373 std::vector<std::unique_ptr<const DexFile>> dex_files; 374 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); 375 CHECK(success) << "Failed to open '" << filename << "': " << error_msg; 376 for (auto& dex_file : dex_files) { 377 CHECK_EQ(PROT_READ, dex_file->GetPermissions()); 378 CHECK(dex_file->IsReadOnly()); 379 } 380 return dex_files; 381} 382 383std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) { 384 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name); 385 EXPECT_EQ(1U, vector.size()); 386 return std::move(vector[0]); 387} 388 389jobject CommonRuntimeTest::LoadDex(const char* dex_name) { 390 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name); 391 std::vector<const DexFile*> class_path; 392 CHECK_NE(0U, dex_files.size()); 393 for (auto& dex_file : dex_files) { 394 class_path.push_back(dex_file.get()); 395 class_linker_->RegisterDexFile(*dex_file); 396 loaded_dex_files_.push_back(std::move(dex_file)); 397 } 398 Thread* self = Thread::Current(); 399 JNIEnvExt* env = self->GetJniEnv(); 400 ScopedLocalRef<jobject> class_loader_local(env, 401 env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); 402 jobject class_loader = env->NewGlobalRef(class_loader_local.get()); 403 self->SetClassLoaderOverride(class_loader_local.get()); 404 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path); 405 return class_loader; 406} 407 408std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) { 409 CHECK(suffix != nullptr); 410 411 std::string location; 412 if (IsHost()) { 413 const char* host_dir = getenv("ANDROID_HOST_OUT"); 414 CHECK(host_dir != NULL); 415 location = StringPrintf("%s/framework/core.%s", host_dir, suffix); 416 } else { 417 location = StringPrintf("/data/art-test/core.%s", suffix); 418 } 419 420 return location; 421} 422 423CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { 424 vm_->SetCheckJniAbortHook(Hook, &actual_); 425} 426 427CheckJniAbortCatcher::~CheckJniAbortCatcher() { 428 vm_->SetCheckJniAbortHook(nullptr, nullptr); 429 EXPECT_TRUE(actual_.empty()) << actual_; 430} 431 432void CheckJniAbortCatcher::Check(const char* expected_text) { 433 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" 434 << "Expected to find: " << expected_text << "\n" 435 << "In the output : " << actual_; 436 actual_.clear(); 437} 438 439void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { 440 // We use += because when we're hooking the aborts like this, multiple problems can be found. 441 *reinterpret_cast<std::string*>(data) += reason; 442} 443 444} // namespace art 445 446namespace std { 447 448template <typename T> 449std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { 450os << ::art::ToString(rhs); 451return os; 452} 453 454} // namespace std 455