blob: 84865973c6080525959721141c6c23d9280b4bc2 [file] [log] [blame]
Elliott Hugheseb02a122012-06-12 11:35:40 -07001/*
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
Ian Rogerse63db272014-07-15 15:36:11 -070017#include "common_runtime_test.h"
18
19#include <dirent.h>
20#include <dlfcn.h>
21#include <fcntl.h>
22#include <ScopedLocalRef.h>
Andreas Gampe369810a2015-01-14 19:53:31 -080023#include <stdlib.h>
Ian Rogerse63db272014-07-15 15:36:11 -070024
25#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -070026#include "base/macros.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#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"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "gc/heap.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070036#include "gtest/gtest.h"
Andreas Gampe9b5cba42015-03-11 09:53:50 -070037#include "interpreter/unstarted_runtime.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "jni_internal.h"
39#include "mirror/class_loader.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080040#include "mem_map.h"
Ian Rogerse63db272014-07-15 15:36:11 -070041#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"
Elliott Hugheseb02a122012-06-12 11:35:40 -070047
48int main(int argc, char **argv) {
Andreas Gampe369810a2015-01-14 19:53:31 -080049 // 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.
Richard Uhler892fc962015-03-10 16:57:05 +000052 setenv("ANDROID_LOG_TAGS", "*:e", 1);
Andreas Gampe369810a2015-01-14 19:53:31 -080053
Elliott Hugheseb02a122012-06-12 11:35:40 -070054 art::InitLogging(argv);
Ian Rogersc7dd2952014-10-21 23:31:19 -070055 LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
Elliott Hugheseb02a122012-06-12 11:35:40 -070056 testing::InitGoogleTest(&argc, argv);
57 return RUN_ALL_TESTS();
58}
Ian Rogerse63db272014-07-15 15:36:11 -070059
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);
Andreas Gampe4303ba92014-11-06 01:00:46 -080070 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070071}
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);
Andreas Gampe4303ba92014-11-06 01:00:46 -080078 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070079}
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
Andreas Gampee21dc3d2014-12-08 16:59:43 -080095void ScratchFile::Close() {
Andreas Gampe4303ba92014-11-06 01:00:46 -080096 if (file_.get() != nullptr) {
97 if (file_->FlushCloseOrErase() != 0) {
98 PLOG(WARNING) << "Error closing scratch file.";
99 }
100 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800101}
102
103void ScratchFile::Unlink() {
104 if (!OS::FileExists(filename_.c_str())) {
105 return;
106 }
107 Close();
Ian Rogerse63db272014-07-15 15:36:11 -0700108 int unlink_result = unlink(filename_.c_str());
109 CHECK_EQ(0, unlink_result);
110}
111
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700112static bool unstarted_initialized_ = false;
113
Ian Rogerse63db272014-07-15 15:36:11 -0700114CommonRuntimeTest::CommonRuntimeTest() {}
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800115CommonRuntimeTest::~CommonRuntimeTest() {
116 // Ensure the dex files are cleaned up before the runtime.
117 loaded_dex_files_.clear();
118 runtime_.reset();
119}
Ian Rogerse63db272014-07-15 15:36:11 -0700120
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700121void CommonRuntimeTest::SetUpAndroidRoot() {
Ian Rogerse63db272014-07-15 15:36:11 -0700122 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 }
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700161}
Ian Rogerse63db272014-07-15 15:36:11 -0700162
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700163void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
Ian Rogerse63db272014-07-15 15:36:11 -0700164 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
Andreas Gampe5a79fde2014-08-06 13:12:26 -0700165 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";
Ian Rogerse63db272014-07-15 15:36:11 -0700176 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
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700182void 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
Igor Murashkin37743352014-11-13 14:38:00 -0800190std::string CommonRuntimeTest::GetCoreArtLocation() {
191 return GetCoreFileLocation("art");
192}
193
194std::string CommonRuntimeTest::GetCoreOatLocation() {
195 return GetCoreFileLocation("oat");
196}
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700197
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800198std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
199 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700200 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800201 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700202 if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
203 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800204 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700205 } else {
206 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800207 return std::move(dex_files[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700208 }
209}
210
211void CommonRuntimeTest::SetUp() {
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700212 SetUpAndroidRoot();
213 SetUpAndroidData(android_data_);
Ian Rogerse63db272014-07-15 15:36:11 -0700214 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
Ian Rogerse63db272014-07-15 15:36:11 -0700219 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;
Richard Uhlerc2752592015-01-02 13:28:22 -0800225 std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
226 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700227 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800228 options.push_back(std::make_pair(min_heap_string, nullptr));
229 options.push_back(std::make_pair(max_heap_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700230 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
231 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800232
Richard Uhler66d874d2015-01-15 09:37:19 -0800233 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700234 if (!Runtime::Create(options, false)) {
235 LOG(FATAL) << "Failed to create runtime";
236 return;
237 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800238 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700239 runtime_.reset(Runtime::Current());
240 class_linker_ = runtime_->GetClassLinker();
241 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700242
243 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
244 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700245 if (!unstarted_initialized_) {
246 interpreter::UnstartedRuntimeInitialize();
247 unstarted_initialized_ = true;
248 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700249
Ian Rogerse63db272014-07-15 15:36:11 -0700250 class_linker_->RunRootClinits();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800251 boot_class_path_ = class_linker_->GetBootClassPath();
252 java_lang_dex_file_ = boot_class_path_[0];
253
Ian Rogerse63db272014-07-15 15:36:11 -0700254
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
Richard Uhlerc2752592015-01-02 13:28:22 -0800266
267 // Get the boot class path from the runtime so it can be used in tests.
268 boot_class_path_ = class_linker_->GetBootClassPath();
269 ASSERT_FALSE(boot_class_path_.empty());
270 java_lang_dex_file_ = boot_class_path_[0];
Ian Rogerse63db272014-07-15 15:36:11 -0700271}
272
Alex Lighta59dd802014-07-02 16:28:08 -0700273void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
274 ASSERT_TRUE(dirpath != nullptr);
275 DIR* dir = opendir(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700276 ASSERT_TRUE(dir != nullptr);
277 dirent* e;
Alex Lighta59dd802014-07-02 16:28:08 -0700278 struct stat s;
Ian Rogerse63db272014-07-15 15:36:11 -0700279 while ((e = readdir(dir)) != nullptr) {
280 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
281 continue;
282 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700283 std::string filename(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700284 filename.push_back('/');
285 filename.append(e->d_name);
Alex Lighta59dd802014-07-02 16:28:08 -0700286 int stat_result = lstat(filename.c_str(), &s);
287 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
288 if (S_ISDIR(s.st_mode)) {
289 ClearDirectory(filename.c_str());
290 int rmdir_result = rmdir(filename.c_str());
291 ASSERT_EQ(0, rmdir_result) << filename;
292 } else {
293 int unlink_result = unlink(filename.c_str());
294 ASSERT_EQ(0, unlink_result) << filename;
295 }
Ian Rogerse63db272014-07-15 15:36:11 -0700296 }
297 closedir(dir);
Alex Lighta59dd802014-07-02 16:28:08 -0700298}
299
300void CommonRuntimeTest::TearDown() {
301 const char* android_data = getenv("ANDROID_DATA");
302 ASSERT_TRUE(android_data != nullptr);
303 ClearDirectory(dalvik_cache_.c_str());
Ian Rogerse63db272014-07-15 15:36:11 -0700304 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
305 ASSERT_EQ(0, rmdir_cache_result);
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700306 TearDownAndroidData(android_data_, true);
Ian Rogerse63db272014-07-15 15:36:11 -0700307
308 // icu4c has a fixed 10-element array "gCommonICUDataArray".
309 // If we run > 10 tests, we fill that array and u_setCommonData fails.
310 // There's a function to clear the array, but it's not public...
311 typedef void (*IcuCleanupFn)();
312 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
313 CHECK(sym != nullptr) << dlerror();
314 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
315 (*icu_cleanup_fn)();
316
Ian Rogerse63db272014-07-15 15:36:11 -0700317 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
318}
319
320std::string CommonRuntimeTest::GetLibCoreDexFileName() {
321 return GetDexFileName("core-libart");
322}
323
324std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
325 if (IsHost()) {
326 const char* host_dir = getenv("ANDROID_HOST_OUT");
327 CHECK(host_dir != nullptr);
328 return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
329 }
330 return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
331}
332
333std::string CommonRuntimeTest::GetTestAndroidRoot() {
334 if (IsHost()) {
335 const char* host_dir = getenv("ANDROID_HOST_OUT");
336 CHECK(host_dir != nullptr);
337 return host_dir;
338 }
339 return GetAndroidRoot();
340}
341
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700342// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
343#ifdef ART_TARGET
344#ifndef ART_TARGET_NATIVETEST_DIR
345#error "ART_TARGET_NATIVETEST_DIR not set."
346#endif
347// Wrap it as a string literal.
348#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
349#else
350#define ART_TARGET_NATIVETEST_DIR_STRING ""
351#endif
352
Richard Uhler66d874d2015-01-15 09:37:19 -0800353std::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700354 CHECK(name != nullptr);
355 std::string filename;
356 if (IsHost()) {
357 filename += getenv("ANDROID_HOST_OUT");
358 filename += "/framework/";
359 } else {
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700360 filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogerse63db272014-07-15 15:36:11 -0700361 }
362 filename += "art-gtest-";
363 filename += name;
364 filename += ".jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800365 return filename;
366}
367
368std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
369 std::string filename = GetTestDexFileName(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700370 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800371 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700372 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
373 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800374 for (auto& dex_file : dex_files) {
Ian Rogerse63db272014-07-15 15:36:11 -0700375 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
376 CHECK(dex_file->IsReadOnly());
377 }
Ian Rogerse63db272014-07-15 15:36:11 -0700378 return dex_files;
379}
380
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800381std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
382 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700383 EXPECT_EQ(1U, vector.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800384 return std::move(vector[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700385}
386
387jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800388 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
389 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700390 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800391 for (auto& dex_file : dex_files) {
392 class_path.push_back(dex_file.get());
Ian Rogerse63db272014-07-15 15:36:11 -0700393 class_linker_->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800394 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700395 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700396 Thread* self = Thread::Current();
397 JNIEnvExt* env = self->GetJniEnv();
398 ScopedLocalRef<jobject> class_loader_local(env,
399 env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
400 jobject class_loader = env->NewGlobalRef(class_loader_local.get());
401 self->SetClassLoaderOverride(class_loader_local.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800402 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path);
Ian Rogerse63db272014-07-15 15:36:11 -0700403 return class_loader;
404}
405
Igor Murashkin37743352014-11-13 14:38:00 -0800406std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
407 CHECK(suffix != nullptr);
408
409 std::string location;
410 if (IsHost()) {
411 const char* host_dir = getenv("ANDROID_HOST_OUT");
412 CHECK(host_dir != NULL);
413 location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
414 } else {
415 location = StringPrintf("/data/art-test/core.%s", suffix);
416 }
417
418 return location;
419}
420
Ian Rogerse63db272014-07-15 15:36:11 -0700421CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700422 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700423}
424
425CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700426 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700427 EXPECT_TRUE(actual_.empty()) << actual_;
428}
429
430void CheckJniAbortCatcher::Check(const char* expected_text) {
431 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
432 << "Expected to find: " << expected_text << "\n"
433 << "In the output : " << actual_;
434 actual_.clear();
435}
436
437void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
438 // We use += because when we're hooking the aborts like this, multiple problems can be found.
439 *reinterpret_cast<std::string*>(data) += reason;
440}
441
442} // namespace art
443
444namespace std {
445
446template <typename T>
447std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
448os << ::art::ToString(rhs);
449return os;
450}
451
452} // namespace std