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