common_runtime_test.cc revision 7bcfcb80a31f57a84d754e00bca8698829365208
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 <cstdio> 20#include <dirent.h> 21#include <dlfcn.h> 22#include <fcntl.h> 23#include <ScopedLocalRef.h> 24#include <stdlib.h> 25 26#include "../../external/icu/icu4c/source/common/unicode/uvernum.h" 27#include "art_field-inl.h" 28#include "base/macros.h" 29#include "base/logging.h" 30#include "base/stl_util.h" 31#include "base/stringprintf.h" 32#include "base/unix_file/fd_file.h" 33#include "class_linker.h" 34#include "compiler_callbacks.h" 35#include "dex_file-inl.h" 36#include "gc_root-inl.h" 37#include "gc/heap.h" 38#include "gtest/gtest.h" 39#include "handle_scope-inl.h" 40#include "interpreter/unstarted_runtime.h" 41#include "jni_internal.h" 42#include "mirror/class-inl.h" 43#include "mirror/class_loader.h" 44#include "mem_map.h" 45#include "native/dalvik_system_DexFile.h" 46#include "noop_compiler_callbacks.h" 47#include "os.h" 48#include "primitive.h" 49#include "runtime-inl.h" 50#include "scoped_thread_state_change.h" 51#include "thread.h" 52#include "well_known_classes.h" 53 54int main(int argc, char **argv) { 55 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native 56 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses 57 // everything else. In case you want to see all messages, comment out the line. 58 setenv("ANDROID_LOG_TAGS", "*:e", 1); 59 60 art::InitLogging(argv); 61 LOG(::art::INFO) << "Running main() from common_runtime_test.cc..."; 62 testing::InitGoogleTest(&argc, argv); 63 return RUN_ALL_TESTS(); 64} 65 66namespace art { 67 68ScratchFile::ScratchFile() { 69 // ANDROID_DATA needs to be set 70 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << 71 "Are you subclassing RuntimeTest?"; 72 filename_ = getenv("ANDROID_DATA"); 73 filename_ += "/TmpFile-XXXXXX"; 74 int fd = mkstemp(&filename_[0]); 75 CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_; 76 file_.reset(new File(fd, GetFilename(), true)); 77} 78 79ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) { 80 filename_ = other.GetFilename(); 81 filename_ += suffix; 82 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); 83 CHECK_NE(-1, fd); 84 file_.reset(new File(fd, GetFilename(), true)); 85} 86 87ScratchFile::ScratchFile(File* file) { 88 CHECK(file != nullptr); 89 filename_ = file->GetPath(); 90 file_.reset(file); 91} 92 93ScratchFile::~ScratchFile() { 94 Unlink(); 95} 96 97int ScratchFile::GetFd() const { 98 return file_->Fd(); 99} 100 101void ScratchFile::Close() { 102 if (file_.get() != nullptr) { 103 if (file_->FlushCloseOrErase() != 0) { 104 PLOG(WARNING) << "Error closing scratch file."; 105 } 106 } 107} 108 109void ScratchFile::Unlink() { 110 if (!OS::FileExists(filename_.c_str())) { 111 return; 112 } 113 Close(); 114 int unlink_result = unlink(filename_.c_str()); 115 CHECK_EQ(0, unlink_result); 116} 117 118static bool unstarted_initialized_ = false; 119 120CommonRuntimeTestImpl::CommonRuntimeTestImpl() {} 121 122CommonRuntimeTestImpl::~CommonRuntimeTestImpl() { 123 // Ensure the dex files are cleaned up before the runtime. 124 loaded_dex_files_.clear(); 125 runtime_.reset(); 126} 127 128void CommonRuntimeTestImpl::SetUpAndroidRoot() { 129 if (IsHost()) { 130 // $ANDROID_ROOT is set on the device, but not necessarily on the host. 131 // But it needs to be set so that icu4c can find its locale data. 132 const char* android_root_from_env = getenv("ANDROID_ROOT"); 133 if (android_root_from_env == nullptr) { 134 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. 135 const char* android_host_out = getenv("ANDROID_HOST_OUT"); 136 if (android_host_out != nullptr) { 137 setenv("ANDROID_ROOT", android_host_out, 1); 138 } else { 139 // Build it from ANDROID_BUILD_TOP or cwd 140 std::string root; 141 const char* android_build_top = getenv("ANDROID_BUILD_TOP"); 142 if (android_build_top != nullptr) { 143 root += android_build_top; 144 } else { 145 // Not set by build server, so default to current directory 146 char* cwd = getcwd(nullptr, 0); 147 setenv("ANDROID_BUILD_TOP", cwd, 1); 148 root += cwd; 149 free(cwd); 150 } 151#if defined(__linux__) 152 root += "/out/host/linux-x86"; 153#elif defined(__APPLE__) 154 root += "/out/host/darwin-x86"; 155#else 156#error unsupported OS 157#endif 158 setenv("ANDROID_ROOT", root.c_str(), 1); 159 } 160 } 161 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. 162 163 // Not set by build server, so default 164 if (getenv("ANDROID_HOST_OUT") == nullptr) { 165 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); 166 } 167 } 168} 169 170void CommonRuntimeTestImpl::SetUpAndroidData(std::string& android_data) { 171 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache 172 if (IsHost()) { 173 const char* tmpdir = getenv("TMPDIR"); 174 if (tmpdir != nullptr && tmpdir[0] != 0) { 175 android_data = tmpdir; 176 } else { 177 android_data = "/tmp"; 178 } 179 } else { 180 android_data = "/data/dalvik-cache"; 181 } 182 android_data += "/art-data-XXXXXX"; 183 if (mkdtemp(&android_data[0]) == nullptr) { 184 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; 185 } 186 setenv("ANDROID_DATA", android_data.c_str(), 1); 187} 188 189void CommonRuntimeTestImpl::TearDownAndroidData(const std::string& android_data, 190 bool fail_on_error) { 191 if (fail_on_error) { 192 ASSERT_EQ(rmdir(android_data.c_str()), 0); 193 } else { 194 rmdir(android_data.c_str()); 195 } 196} 197 198// Helper - find directory with the following format: 199// ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ 200static std::string GetAndroidToolsDir(const std::string& subdir1, 201 const std::string& subdir2, 202 const std::string& subdir3) { 203 std::string root; 204 const char* android_build_top = getenv("ANDROID_BUILD_TOP"); 205 if (android_build_top != nullptr) { 206 root = android_build_top; 207 } else { 208 // Not set by build server, so default to current directory 209 char* cwd = getcwd(nullptr, 0); 210 setenv("ANDROID_BUILD_TOP", cwd, 1); 211 root = cwd; 212 free(cwd); 213 } 214 215 std::string toolsdir = root + "/" + subdir1; 216 std::string founddir; 217 DIR* dir; 218 if ((dir = opendir(toolsdir.c_str())) != nullptr) { 219 float maxversion = 0; 220 struct dirent* entry; 221 while ((entry = readdir(dir)) != nullptr) { 222 std::string format = subdir2 + "-%f"; 223 float version; 224 if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) { 225 if (version > maxversion) { 226 maxversion = version; 227 founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/"; 228 } 229 } 230 } 231 closedir(dir); 232 } 233 234 if (founddir.empty()) { 235 ADD_FAILURE() << "Cannot find Android tools directory."; 236 } 237 return founddir; 238} 239 240std::string CommonRuntimeTestImpl::GetAndroidHostToolsDir() { 241 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host", 242 "x86_64-linux-glibc2.15", 243 "x86_64-linux"); 244} 245 246std::string CommonRuntimeTestImpl::GetAndroidTargetToolsDir(InstructionSet isa) { 247 switch (isa) { 248 case kArm: 249 case kThumb2: 250 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/arm", 251 "arm-linux-androideabi", 252 "arm-linux-androideabi"); 253 case kArm64: 254 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/aarch64", 255 "aarch64-linux-android", 256 "aarch64-linux-android"); 257 case kX86: 258 case kX86_64: 259 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/x86", 260 "x86_64-linux-android", 261 "x86_64-linux-android"); 262 case kMips: 263 case kMips64: 264 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/mips", 265 "mips64el-linux-android", 266 "mips64el-linux-android"); 267 case kNone: 268 break; 269 } 270 ADD_FAILURE() << "Invalid isa " << isa; 271 return ""; 272} 273 274std::string CommonRuntimeTestImpl::GetCoreArtLocation() { 275 return GetCoreFileLocation("art"); 276} 277 278std::string CommonRuntimeTestImpl::GetCoreOatLocation() { 279 return GetCoreFileLocation("oat"); 280} 281 282std::unique_ptr<const DexFile> CommonRuntimeTestImpl::LoadExpectSingleDexFile( 283 const char* location) { 284 std::vector<std::unique_ptr<const DexFile>> dex_files; 285 std::string error_msg; 286 MemMap::Init(); 287 if (!DexFile::Open(location, location, &error_msg, &dex_files)) { 288 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; 289 UNREACHABLE(); 290 } else { 291 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; 292 return std::move(dex_files[0]); 293 } 294} 295 296void CommonRuntimeTestImpl::SetUp() { 297 SetUpAndroidRoot(); 298 SetUpAndroidData(android_data_); 299 dalvik_cache_.append(android_data_.c_str()); 300 dalvik_cache_.append("/dalvik-cache"); 301 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); 302 ASSERT_EQ(mkdir_result, 0); 303 304 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); 305 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); 306 307 308 RuntimeOptions options; 309 std::string boot_class_path_string = "-Xbootclasspath"; 310 for (const std::string &core_dex_file_name : GetLibCoreDexFileNames()) { 311 boot_class_path_string += ":"; 312 boot_class_path_string += core_dex_file_name; 313 } 314 315 options.push_back(std::make_pair(boot_class_path_string, nullptr)); 316 options.push_back(std::make_pair("-Xcheck:jni", nullptr)); 317 options.push_back(std::make_pair(min_heap_string, nullptr)); 318 options.push_back(std::make_pair(max_heap_string, nullptr)); 319 320 callbacks_.reset(new NoopCompilerCallbacks()); 321 322 SetUpRuntimeOptions(&options); 323 324 // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them. 325 if (callbacks_.get() != nullptr) { 326 options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); 327 } 328 329 PreRuntimeCreate(); 330 if (!Runtime::Create(options, false)) { 331 LOG(FATAL) << "Failed to create runtime"; 332 return; 333 } 334 PostRuntimeCreate(); 335 runtime_.reset(Runtime::Current()); 336 class_linker_ = runtime_->GetClassLinker(); 337 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); 338 339 // Runtime::Create acquired the mutator_lock_ that is normally given away when we 340 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. 341 Thread::Current()->TransitionFromRunnableToSuspended(kNative); 342 343 // Get the boot class path from the runtime so it can be used in tests. 344 boot_class_path_ = class_linker_->GetBootClassPath(); 345 ASSERT_FALSE(boot_class_path_.empty()); 346 java_lang_dex_file_ = boot_class_path_[0]; 347 348 FinalizeSetup(); 349} 350 351void CommonRuntimeTestImpl::FinalizeSetup() { 352 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this 353 // set up. 354 if (!unstarted_initialized_) { 355 interpreter::UnstartedRuntime::Initialize(); 356 unstarted_initialized_ = true; 357 } 358 359 { 360 ScopedObjectAccess soa(Thread::Current()); 361 class_linker_->RunRootClinits(); 362 } 363 364 // We're back in native, take the opportunity to initialize well known classes. 365 WellKnownClasses::Init(Thread::Current()->GetJniEnv()); 366 367 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread 368 // pool is created by the runtime. 369 runtime_->GetHeap()->CreateThreadPool(); 370 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test 371 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject). 372 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U); 373} 374 375void CommonRuntimeTestImpl::ClearDirectory(const char* dirpath) { 376 ASSERT_TRUE(dirpath != nullptr); 377 DIR* dir = opendir(dirpath); 378 ASSERT_TRUE(dir != nullptr); 379 dirent* e; 380 struct stat s; 381 while ((e = readdir(dir)) != nullptr) { 382 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { 383 continue; 384 } 385 std::string filename(dirpath); 386 filename.push_back('/'); 387 filename.append(e->d_name); 388 int stat_result = lstat(filename.c_str(), &s); 389 ASSERT_EQ(0, stat_result) << "unable to stat " << filename; 390 if (S_ISDIR(s.st_mode)) { 391 ClearDirectory(filename.c_str()); 392 int rmdir_result = rmdir(filename.c_str()); 393 ASSERT_EQ(0, rmdir_result) << filename; 394 } else { 395 int unlink_result = unlink(filename.c_str()); 396 ASSERT_EQ(0, unlink_result) << filename; 397 } 398 } 399 closedir(dir); 400} 401 402void CommonRuntimeTestImpl::TearDown() { 403 const char* android_data = getenv("ANDROID_DATA"); 404 ASSERT_TRUE(android_data != nullptr); 405 ClearDirectory(dalvik_cache_.c_str()); 406 int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); 407 ASSERT_EQ(0, rmdir_cache_result); 408 TearDownAndroidData(android_data_, true); 409 dalvik_cache_.clear(); 410 411 // icu4c has a fixed 10-element array "gCommonICUDataArray". 412 // If we run > 10 tests, we fill that array and u_setCommonData fails. 413 // There's a function to clear the array, but it's not public... 414 typedef void (*IcuCleanupFn)(); 415 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); 416 CHECK(sym != nullptr) << dlerror(); 417 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); 418 (*icu_cleanup_fn)(); 419 420 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test 421 422 // Manually closing the JNI libraries. 423 // Runtime does not support repeatedly doing JNI->CreateVM, thus we need to manually clean up the 424 // dynamic linking loader so that gtests would not fail. 425 // Bug: 25785594 426 if (runtime_->IsStarted()) { 427 { 428 // We retrieve the handle by calling dlopen on the library. To close it, we need to call 429 // dlclose twice, the first time to undo our dlopen and the second time to actually unload it. 430 // See man dlopen. 431 void* handle = dlopen("libjavacore.so", RTLD_LAZY); 432 dlclose(handle); 433 CHECK_EQ(0, dlclose(handle)); 434 } 435 { 436 void* handle = dlopen("libopenjdkd.so", RTLD_LAZY); 437 dlclose(handle); 438 CHECK_EQ(0, dlclose(handle)); 439 } 440 } 441} 442 443static std::string GetDexFileName(const std::string& jar_prefix, bool host) { 444 std::string path; 445 if (host) { 446 const char* host_dir = getenv("ANDROID_HOST_OUT"); 447 CHECK(host_dir != nullptr); 448 path = host_dir; 449 } else { 450 path = GetAndroidRoot(); 451 } 452 453 std::string suffix = host 454 ? "-hostdex" // The host version. 455 : "-testdex"; // The unstripped target version. 456 457 return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str()); 458} 459 460std::vector<std::string> CommonRuntimeTestImpl::GetLibCoreDexFileNames() { 461 return std::vector<std::string>({GetDexFileName("core-oj", IsHost()), 462 GetDexFileName("core-libart", IsHost())}); 463} 464 465std::string CommonRuntimeTestImpl::GetTestAndroidRoot() { 466 if (IsHost()) { 467 const char* host_dir = getenv("ANDROID_HOST_OUT"); 468 CHECK(host_dir != nullptr); 469 return host_dir; 470 } 471 return GetAndroidRoot(); 472} 473 474// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. 475#ifdef ART_TARGET 476#ifndef ART_TARGET_NATIVETEST_DIR 477#error "ART_TARGET_NATIVETEST_DIR not set." 478#endif 479// Wrap it as a string literal. 480#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" 481#else 482#define ART_TARGET_NATIVETEST_DIR_STRING "" 483#endif 484 485std::string CommonRuntimeTestImpl::GetTestDexFileName(const char* name) { 486 CHECK(name != nullptr); 487 std::string filename; 488 if (IsHost()) { 489 filename += getenv("ANDROID_HOST_OUT"); 490 filename += "/framework/"; 491 } else { 492 filename += ART_TARGET_NATIVETEST_DIR_STRING; 493 } 494 filename += "art-gtest-"; 495 filename += name; 496 filename += ".jar"; 497 return filename; 498} 499 500std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTestImpl::OpenTestDexFiles( 501 const char* name) { 502 std::string filename = GetTestDexFileName(name); 503 std::string error_msg; 504 std::vector<std::unique_ptr<const DexFile>> dex_files; 505 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); 506 CHECK(success) << "Failed to open '" << filename << "': " << error_msg; 507 for (auto& dex_file : dex_files) { 508 CHECK_EQ(PROT_READ, dex_file->GetPermissions()); 509 CHECK(dex_file->IsReadOnly()); 510 } 511 return dex_files; 512} 513 514std::unique_ptr<const DexFile> CommonRuntimeTestImpl::OpenTestDexFile(const char* name) { 515 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name); 516 EXPECT_EQ(1U, vector.size()); 517 return std::move(vector[0]); 518} 519 520std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) { 521 std::vector<const DexFile*> ret; 522 523 ScopedObjectAccess soa(Thread::Current()); 524 525 StackHandleScope<2> hs(soa.Self()); 526 Handle<mirror::ClassLoader> class_loader = hs.NewHandle( 527 soa.Decode<mirror::ClassLoader*>(jclass_loader)); 528 529 DCHECK_EQ(class_loader->GetClass(), 530 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)); 531 DCHECK_EQ(class_loader->GetParent()->GetClass(), 532 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)); 533 534 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. 535 // We need to get the DexPathList and loop through it. 536 ArtField* cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); 537 ArtField* dex_file_field = 538 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); 539 mirror::Object* dex_path_list = 540 soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> 541 GetObject(class_loader.Get()); 542 if (dex_path_list != nullptr && dex_file_field!= nullptr && cookie_field != nullptr) { 543 // DexPathList has an array dexElements of Elements[] which each contain a dex file. 544 mirror::Object* dex_elements_obj = 545 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> 546 GetObject(dex_path_list); 547 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look 548 // at the mCookie which is a DexFile vector. 549 if (dex_elements_obj != nullptr) { 550 Handle<mirror::ObjectArray<mirror::Object>> dex_elements = 551 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); 552 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { 553 mirror::Object* element = dex_elements->GetWithoutChecks(i); 554 if (element == nullptr) { 555 // Should never happen, fall back to java code to throw a NPE. 556 break; 557 } 558 mirror::Object* dex_file = dex_file_field->GetObject(element); 559 if (dex_file != nullptr) { 560 mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); 561 DCHECK(long_array != nullptr); 562 int32_t long_array_size = long_array->GetLength(); 563 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { 564 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( 565 long_array->GetWithoutChecks(j))); 566 if (cp_dex_file == nullptr) { 567 LOG(WARNING) << "Null DexFile"; 568 continue; 569 } 570 ret.push_back(cp_dex_file); 571 } 572 } 573 } 574 } 575 } 576 577 return ret; 578} 579 580const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) { 581 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader)); 582 DCHECK(!tmp.empty()); 583 const DexFile* ret = tmp[0]; 584 DCHECK(ret != nullptr); 585 return ret; 586} 587 588jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) { 589 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name); 590 std::vector<const DexFile*> class_path; 591 CHECK_NE(0U, dex_files.size()); 592 for (auto& dex_file : dex_files) { 593 class_path.push_back(dex_file.get()); 594 loaded_dex_files_.push_back(std::move(dex_file)); 595 } 596 597 Thread* self = Thread::Current(); 598 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, 599 class_path); 600 self->SetClassLoaderOverride(class_loader); 601 return class_loader; 602} 603 604std::string CommonRuntimeTestImpl::GetCoreFileLocation(const char* suffix) { 605 CHECK(suffix != nullptr); 606 607 std::string location; 608 if (IsHost()) { 609 const char* host_dir = getenv("ANDROID_HOST_OUT"); 610 CHECK(host_dir != nullptr); 611 location = StringPrintf("%s/framework/core.%s", host_dir, suffix); 612 } else { 613 location = StringPrintf("/data/art-test/core.%s", suffix); 614 } 615 616 return location; 617} 618 619CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { 620 vm_->SetCheckJniAbortHook(Hook, &actual_); 621} 622 623CheckJniAbortCatcher::~CheckJniAbortCatcher() { 624 vm_->SetCheckJniAbortHook(nullptr, nullptr); 625 EXPECT_TRUE(actual_.empty()) << actual_; 626} 627 628void CheckJniAbortCatcher::Check(const char* expected_text) { 629 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" 630 << "Expected to find: " << expected_text << "\n" 631 << "In the output : " << actual_; 632 actual_.clear(); 633} 634 635void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { 636 // We use += because when we're hooking the aborts like this, multiple problems can be found. 637 *reinterpret_cast<std::string*>(data) += reason; 638} 639 640} // namespace art 641 642namespace std { 643 644template <typename T> 645std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { 646os << ::art::ToString(rhs); 647return os; 648} 649 650} // namespace std 651