common_runtime_test.cc revision 2969bcdcd80624e4a4fef696b54c2c76b44b6853
1eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes/*
2eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes *
4eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * you may not use this file except in compliance with the License.
6eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * You may obtain a copy of the License at
7eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes *
8eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes *
10eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * See the License for the specific language governing permissions and
14eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes * limitations under the License.
15eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes */
16eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes
17e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "common_runtime_test.h"
18e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
19e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include <dirent.h>
20e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include <dlfcn.h>
21e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include <fcntl.h>
22e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include <ScopedLocalRef.h>
23369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe#include <stdlib.h>
24e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
25e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
261fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#include "base/macros.h"
2707ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h"
28e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "base/stl_util.h"
29e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "base/stringprintf.h"
30e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "base/unix_file/fd_file.h"
31e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "class_linker.h"
32e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "compiler_callbacks.h"
33e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "dex_file.h"
3494f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi#include "gc_root-inl.h"
35e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "gc/heap.h"
36eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes#include "gtest/gtest.h"
37e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "jni_internal.h"
38e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "mirror/class_loader.h"
3966d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler#include "mem_map.h"
40e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "noop_compiler_callbacks.h"
41e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "os.h"
42e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "runtime-inl.h"
43e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "scoped_thread_state_change.h"
44e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "thread.h"
45e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "well_known_classes.h"
46eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes
47eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughesint main(int argc, char **argv) {
48369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
49369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
50369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // everything else. In case you want to see all messages, comment out the line.
51f45599d8f92748c5c2d2aec515948932c52444ebRichard Uhler  // setenv("ANDROID_LOG_TAGS", "*:e", 1);
52369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe
53eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  art::InitLogging(argv);
54c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
55eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  testing::InitGoogleTest(&argc, argv);
56eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  return RUN_ALL_TESTS();
57eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes}
58e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
59e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace art {
60e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
61e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile() {
62e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // ANDROID_DATA needs to be set
63e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
64e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      "Are you subclassing RuntimeTest?";
65e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = getenv("ANDROID_DATA");
66e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ += "/TmpFile-XXXXXX";
67e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int fd = mkstemp(&filename_[0]);
68e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(-1, fd);
694303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  file_.reset(new File(fd, GetFilename(), true));
70e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
71e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
72e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
73e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = other.GetFilename();
74e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ += suffix;
75e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
76e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(-1, fd);
774303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  file_.reset(new File(fd, GetFilename(), true));
78e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
79e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
80e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile(File* file) {
81e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(file != NULL);
82e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = file->GetPath();
83e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  file_.reset(file);
84e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
85e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
86e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::~ScratchFile() {
87e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Unlink();
88e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
89e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
90e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersint ScratchFile::GetFd() const {
91e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return file_->Fd();
92e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
93e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
94e21dc3db191df04c100620965bee4617b3b24397Andreas Gampevoid ScratchFile::Close() {
954303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  if (file_.get() != nullptr) {
964303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    if (file_->FlushCloseOrErase() != 0) {
974303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe      PLOG(WARNING) << "Error closing scratch file.";
984303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    }
994303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  }
100e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe}
101e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe
102e21dc3db191df04c100620965bee4617b3b24397Andreas Gampevoid ScratchFile::Unlink() {
103e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  if (!OS::FileExists(filename_.c_str())) {
104e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe    return;
105e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  }
106e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  Close();
107e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int unlink_result = unlink(filename_.c_str());
108e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_EQ(0, unlink_result);
109e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
110e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
111e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCommonRuntimeTest::CommonRuntimeTest() {}
112fbef44de596d298dc6430f482dffc933a046dd28Richard UhlerCommonRuntimeTest::~CommonRuntimeTest() {
113fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  // Ensure the dex files are cleaned up before the runtime.
114fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  loaded_dex_files_.clear();
115fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  runtime_.reset();
116fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler}
117e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1187747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::SetUpAndroidRoot() {
119e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
120e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // $ANDROID_ROOT is set on the device, but not necessarily on the host.
121e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // But it needs to be set so that icu4c can find its locale data.
122e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* android_root_from_env = getenv("ANDROID_ROOT");
123e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if (android_root_from_env == nullptr) {
124e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
125e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      const char* android_host_out = getenv("ANDROID_HOST_OUT");
126e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      if (android_host_out != nullptr) {
127e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        setenv("ANDROID_ROOT", android_host_out, 1);
128e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      } else {
129e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        // Build it from ANDROID_BUILD_TOP or cwd
130e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        std::string root;
131e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        const char* android_build_top = getenv("ANDROID_BUILD_TOP");
132e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        if (android_build_top != nullptr) {
133e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          root += android_build_top;
134e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        } else {
135e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          // Not set by build server, so default to current directory
136e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          char* cwd = getcwd(nullptr, 0);
137e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          setenv("ANDROID_BUILD_TOP", cwd, 1);
138e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          root += cwd;
139e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          free(cwd);
140e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        }
141e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#if defined(__linux__)
142e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        root += "/out/host/linux-x86";
143e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#elif defined(__APPLE__)
144e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        root += "/out/host/darwin-x86";
145e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#else
146e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#error unsupported OS
147e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#endif
148e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        setenv("ANDROID_ROOT", root.c_str(), 1);
149e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      }
150e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
151e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    setenv("LD_LIBRARY_PATH", ":", 0);  // Required by java.lang.System.<clinit>.
152e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
153e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // Not set by build server, so default
154e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if (getenv("ANDROID_HOST_OUT") == nullptr) {
155e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
156e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
157e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
1587747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe}
159e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1607747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
161e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
1625a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  if (IsHost()) {
1635a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    const char* tmpdir = getenv("TMPDIR");
1645a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    if (tmpdir != nullptr && tmpdir[0] != 0) {
1655a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe      android_data = tmpdir;
1665a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    } else {
1675a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe      android_data = "/tmp";
1685a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    }
1695a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  } else {
1705a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    android_data = "/data/dalvik-cache";
1715a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  }
1725a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  android_data += "/art-data-XXXXXX";
173e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (mkdtemp(&android_data[0]) == nullptr) {
174e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
175e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
176e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  setenv("ANDROID_DATA", android_data.c_str(), 1);
177e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
178e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1797747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
1807747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  if (fail_on_error) {
1817747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe    ASSERT_EQ(rmdir(android_data.c_str()), 0);
1827747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  } else {
1837747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe    rmdir(android_data.c_str());
1847747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  }
1857747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe}
1867747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe
1873774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreArtLocation() {
1883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return GetCoreFileLocation("art");
1893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
1903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
1913774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreOatLocation() {
1923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return GetCoreFileLocation("oat");
1933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
1947747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe
195fbef44de596d298dc6430f482dffc933a046dd28Richard Uhlerstd::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
196fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
197e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string error_msg;
19866d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  MemMap::Init();
199e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
200e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
201fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    UNREACHABLE();
202e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  } else {
203e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
204fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    return std::move(dex_files[0]);
205e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
206e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
207e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
208e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CommonRuntimeTest::SetUp() {
2097747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  SetUpAndroidRoot();
2107747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  SetUpAndroidData(android_data_);
211e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dalvik_cache_.append(android_data_.c_str());
212e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dalvik_cache_.append("/dalvik-cache");
213e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
214e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_EQ(mkdir_result, 0);
215e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
216e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
217e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
218e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
219e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  callbacks_.reset(new NoopCompilerCallbacks());
220e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
221e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  RuntimeOptions options;
222c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
223c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(boot_class_path_string, nullptr));
224e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  options.push_back(std::make_pair("-Xcheck:jni", nullptr));
225c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(min_heap_string, nullptr));
226c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(max_heap_string, nullptr));
227e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
228e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  SetUpRuntimeOptions(&options);
229aaebaa0121be3b9d9f13630585304482cbcaeb4bIgor Murashkin
23066d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  PreRuntimeCreate();
231e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (!Runtime::Create(options, false)) {
232e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    LOG(FATAL) << "Failed to create runtime";
233e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return;
234e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
23566d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  PostRuntimeCreate();
236e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_.reset(Runtime::Current());
237e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_ = runtime_->GetClassLinker();
238e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
2392969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe
2402969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe  // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
2412969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe  // set up.
2422969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe  interpreter::UnstartedRuntimeInitialize();
2432969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe
244e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_->RunRootClinits();
245fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  boot_class_path_ = class_linker_->GetBootClassPath();
246fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  java_lang_dex_file_ = boot_class_path_[0];
247fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler
248e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
249e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Runtime::Create acquired the mutator_lock_ that is normally given away when we
250e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
251e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
252e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
253e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // We're back in native, take the opportunity to initialize well known classes.
254e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  WellKnownClasses::Init(Thread::Current()->GetJniEnv());
255e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
256e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
257e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // pool is created by the runtime.
258e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_->GetHeap()->CreateThreadPool();
259e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption before the test
260c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler
261c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  // Get the boot class path from the runtime so it can be used in tests.
262c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  boot_class_path_ = class_linker_->GetBootClassPath();
263c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  ASSERT_FALSE(boot_class_path_.empty());
264c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  java_lang_dex_file_ = boot_class_path_[0];
265e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
266e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
267a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightvoid CommonRuntimeTest::ClearDirectory(const char* dirpath) {
268a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ASSERT_TRUE(dirpath != nullptr);
269a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  DIR* dir = opendir(dirpath);
270e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_TRUE(dir != nullptr);
271e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dirent* e;
272a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  struct stat s;
273e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  while ((e = readdir(dir)) != nullptr) {
274e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
275e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      continue;
276e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
277f0a3f09c3d54646166a55c05a6b39c7dd504129cJeff Hao    std::string filename(dirpath);
278e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename.push_back('/');
279e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename.append(e->d_name);
280a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    int stat_result = lstat(filename.c_str(), &s);
281a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
282a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (S_ISDIR(s.st_mode)) {
283a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ClearDirectory(filename.c_str());
284a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      int rmdir_result = rmdir(filename.c_str());
285a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ASSERT_EQ(0, rmdir_result) << filename;
286a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    } else {
287a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      int unlink_result = unlink(filename.c_str());
288a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ASSERT_EQ(0, unlink_result) << filename;
289a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    }
290e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
291e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  closedir(dir);
292a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light}
293a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
294a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightvoid CommonRuntimeTest::TearDown() {
295a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  const char* android_data = getenv("ANDROID_DATA");
296a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ASSERT_TRUE(android_data != nullptr);
297a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ClearDirectory(dalvik_cache_.c_str());
298e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
299e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_EQ(0, rmdir_cache_result);
3007747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  TearDownAndroidData(android_data_, true);
301e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
302e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // icu4c has a fixed 10-element array "gCommonICUDataArray".
303e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // If we run > 10 tests, we fill that array and u_setCommonData fails.
304e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // There's a function to clear the array, but it's not public...
305e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  typedef void (*IcuCleanupFn)();
306e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
307e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(sym != nullptr) << dlerror();
308e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
309e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  (*icu_cleanup_fn)();
310e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
311e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Runtime::Current()->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
312e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
313e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
314e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetLibCoreDexFileName() {
315e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return GetDexFileName("core-libart");
316e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
317e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
318e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
319e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
320e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* host_dir = getenv("ANDROID_HOST_OUT");
321e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(host_dir != nullptr);
322e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
323e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
324e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
325e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
326e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
327e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetTestAndroidRoot() {
328e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
329e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* host_dir = getenv("ANDROID_HOST_OUT");
330e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(host_dir != nullptr);
331e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return host_dir;
332e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
333e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return GetAndroidRoot();
334e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
335e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
3361fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
3371fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#ifdef ART_TARGET
3381fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#ifndef ART_TARGET_NATIVETEST_DIR
3391fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#error "ART_TARGET_NATIVETEST_DIR not set."
3401fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#endif
3411fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe// Wrap it as a string literal.
3421fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
3431fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#else
3441fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#define ART_TARGET_NATIVETEST_DIR_STRING ""
3451fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#endif
3461fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe
34766d874d96d5699bb090c59f47a5a528956ca053eRichard Uhlerstd::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
348e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(name != nullptr);
349e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string filename;
350e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
351e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename += getenv("ANDROID_HOST_OUT");
352e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename += "/framework/";
353e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  } else {
3541fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe    filename += ART_TARGET_NATIVETEST_DIR_STRING;
355e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
356e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += "art-gtest-";
357e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += name;
358e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += ".jar";
35966d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  return filename;
36066d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler}
36166d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler
36266d874d96d5699bb090c59f47a5a528956ca053eRichard Uhlerstd::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
36366d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  std::string filename = GetTestDexFileName(name);
364e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string error_msg;
365fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
366e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
367e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
368fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
369e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK_EQ(PROT_READ, dex_file->GetPermissions());
370e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(dex_file->IsReadOnly());
371e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
372e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return dex_files;
373e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
374e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
375fbef44de596d298dc6430f482dffc933a046dd28Richard Uhlerstd::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
376fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
377e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_EQ(1U, vector.size());
378fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  return std::move(vector[0]);
379e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
380e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
381e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersjobject CommonRuntimeTest::LoadDex(const char* dex_name) {
382fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
383fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
384e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(0U, dex_files.size());
385fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
386fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
387e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    class_linker_->RegisterDexFile(*dex_file);
388fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    loaded_dex_files_.push_back(std::move(dex_file));
389e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
39068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  Thread* self = Thread::Current();
39168d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  JNIEnvExt* env = self->GetJniEnv();
39268d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  ScopedLocalRef<jobject> class_loader_local(env,
39368d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers      env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
39468d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  jobject class_loader = env->NewGlobalRef(class_loader_local.get());
39568d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  self->SetClassLoaderOverride(class_loader_local.get());
396fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path);
397e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return class_loader;
398e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
399e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
4003774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
4013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  CHECK(suffix != nullptr);
4023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
4033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::string location;
4043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  if (IsHost()) {
4053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    const char* host_dir = getenv("ANDROID_HOST_OUT");
4063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(host_dir != NULL);
4073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
4083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  } else {
4093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    location = StringPrintf("/data/art-test/core.%s", suffix);
4103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
4113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
4123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return location;
4133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
4143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
415e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
41668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  vm_->SetCheckJniAbortHook(Hook, &actual_);
417e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
418e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
419e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCheckJniAbortCatcher::~CheckJniAbortCatcher() {
42068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  vm_->SetCheckJniAbortHook(nullptr, nullptr);
421e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_TRUE(actual_.empty()) << actual_;
422e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
423e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
424e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CheckJniAbortCatcher::Check(const char* expected_text) {
425e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
426e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      << "Expected to find: " << expected_text << "\n"
427e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      << "In the output   : " << actual_;
428e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  actual_.clear();
429e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
430e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
431e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
432e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // We use += because when we're hooking the aborts like this, multiple problems can be found.
433e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  *reinterpret_cast<std::string*>(data) += reason;
434e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
435e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
436e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace art
437e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
438e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace std {
439e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
440e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogerstemplate <typename T>
441e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
442e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersos << ::art::ToString(rhs);
443e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersreturn os;
444e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
445e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
446e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace std
447