common_runtime_test.cc revision 9b5cba401aae6d34450112bdcf9bab5e19da90f2
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"
379b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe#include "interpreter/unstarted_runtime.h"
38e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "jni_internal.h"
39e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "mirror/class_loader.h"
4066d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler#include "mem_map.h"
41e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "noop_compiler_callbacks.h"
42e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "os.h"
43e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "runtime-inl.h"
44e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "scoped_thread_state_change.h"
45e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "thread.h"
46e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include "well_known_classes.h"
47eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes
48eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughesint main(int argc, char **argv) {
49369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
50369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
51369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  // everything else. In case you want to see all messages, comment out the line.
52892fc96694b51ac7a896dacd75af5b235f955825Richard Uhler  setenv("ANDROID_LOG_TAGS", "*:e", 1);
53369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe
54eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  art::InitLogging(argv);
55c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
56eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  testing::InitGoogleTest(&argc, argv);
57eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes  return RUN_ALL_TESTS();
58eb02a124afcd6869ada475712bca8cbe0de20222Elliott Hughes}
59e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
60e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace art {
61e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
62e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile() {
63e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // ANDROID_DATA needs to be set
64e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
65e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      "Are you subclassing RuntimeTest?";
66e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = getenv("ANDROID_DATA");
67e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ += "/TmpFile-XXXXXX";
68e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int fd = mkstemp(&filename_[0]);
69e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(-1, fd);
704303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  file_.reset(new File(fd, GetFilename(), true));
71e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
72e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
73e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
74e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = other.GetFilename();
75e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ += suffix;
76e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
77e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(-1, fd);
784303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  file_.reset(new File(fd, GetFilename(), true));
79e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
80e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
81e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::ScratchFile(File* file) {
82e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(file != NULL);
83e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename_ = file->GetPath();
84e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  file_.reset(file);
85e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
86e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
87e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersScratchFile::~ScratchFile() {
88e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Unlink();
89e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
90e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
91e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersint ScratchFile::GetFd() const {
92e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return file_->Fd();
93e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
94e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
95e21dc3db191df04c100620965bee4617b3b24397Andreas Gampevoid ScratchFile::Close() {
964303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  if (file_.get() != nullptr) {
974303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    if (file_->FlushCloseOrErase() != 0) {
984303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe      PLOG(WARNING) << "Error closing scratch file.";
994303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    }
1004303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe  }
101e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe}
102e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe
103e21dc3db191df04c100620965bee4617b3b24397Andreas Gampevoid ScratchFile::Unlink() {
104e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  if (!OS::FileExists(filename_.c_str())) {
105e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe    return;
106e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  }
107e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  Close();
108e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int unlink_result = unlink(filename_.c_str());
109e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_EQ(0, unlink_result);
110e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
111e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1129b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampestatic bool unstarted_initialized_ = false;
1139b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe
114e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCommonRuntimeTest::CommonRuntimeTest() {}
115fbef44de596d298dc6430f482dffc933a046dd28Richard UhlerCommonRuntimeTest::~CommonRuntimeTest() {
116fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  // Ensure the dex files are cleaned up before the runtime.
117fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  loaded_dex_files_.clear();
118fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  runtime_.reset();
119fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler}
120e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1217747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::SetUpAndroidRoot() {
122e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
123e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // $ANDROID_ROOT is set on the device, but not necessarily on the host.
124e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // But it needs to be set so that icu4c can find its locale data.
125e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* android_root_from_env = getenv("ANDROID_ROOT");
126e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if (android_root_from_env == nullptr) {
127e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
128e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      const char* android_host_out = getenv("ANDROID_HOST_OUT");
129e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      if (android_host_out != nullptr) {
130e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        setenv("ANDROID_ROOT", android_host_out, 1);
131e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      } else {
132e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        // Build it from ANDROID_BUILD_TOP or cwd
133e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        std::string root;
134e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        const char* android_build_top = getenv("ANDROID_BUILD_TOP");
135e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        if (android_build_top != nullptr) {
136e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          root += android_build_top;
137e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        } else {
138e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          // Not set by build server, so default to current directory
139e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          char* cwd = getcwd(nullptr, 0);
140e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          setenv("ANDROID_BUILD_TOP", cwd, 1);
141e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          root += cwd;
142e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers          free(cwd);
143e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        }
144e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#if defined(__linux__)
145e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        root += "/out/host/linux-x86";
146e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#elif defined(__APPLE__)
147e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        root += "/out/host/darwin-x86";
148e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#else
149e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#error unsupported OS
150e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#endif
151e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers        setenv("ANDROID_ROOT", root.c_str(), 1);
152e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      }
153e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
154e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    setenv("LD_LIBRARY_PATH", ":", 0);  // Required by java.lang.System.<clinit>.
155e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
156e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    // Not set by build server, so default
157e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if (getenv("ANDROID_HOST_OUT") == nullptr) {
158e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
159e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
160e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
1617747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe}
162e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1637747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
164e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
1655a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  if (IsHost()) {
1665a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    const char* tmpdir = getenv("TMPDIR");
1675a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    if (tmpdir != nullptr && tmpdir[0] != 0) {
1685a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe      android_data = tmpdir;
1695a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    } else {
1705a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe      android_data = "/tmp";
1715a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    }
1725a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  } else {
1735a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe    android_data = "/data/dalvik-cache";
1745a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  }
1755a79fdecffbea657ebecd4cf19078925239eb1c3Andreas Gampe  android_data += "/art-data-XXXXXX";
176e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (mkdtemp(&android_data[0]) == nullptr) {
177e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
178e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
179e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  setenv("ANDROID_DATA", android_data.c_str(), 1);
180e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
181e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
1827747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampevoid CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
1837747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  if (fail_on_error) {
1847747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe    ASSERT_EQ(rmdir(android_data.c_str()), 0);
1857747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  } else {
1867747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe    rmdir(android_data.c_str());
1877747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  }
1887747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe}
1897747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe
1903774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreArtLocation() {
1913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return GetCoreFileLocation("art");
1923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
1933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
1943774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreOatLocation() {
1953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return GetCoreFileLocation("oat");
1963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
1977747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe
198fbef44de596d298dc6430f482dffc933a046dd28Richard Uhlerstd::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
199fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
200e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string error_msg;
20166d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  MemMap::Init();
202e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
203e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
204fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    UNREACHABLE();
205e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  } else {
206e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
207fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    return std::move(dex_files[0]);
208e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
209e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
210e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
211e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CommonRuntimeTest::SetUp() {
2127747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  SetUpAndroidRoot();
2137747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  SetUpAndroidData(android_data_);
214e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dalvik_cache_.append(android_data_.c_str());
215e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dalvik_cache_.append("/dalvik-cache");
216e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
217e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_EQ(mkdir_result, 0);
218e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
219e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
220e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
221e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
222e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  callbacks_.reset(new NoopCompilerCallbacks());
223e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
224e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  RuntimeOptions options;
225c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
226c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(boot_class_path_string, nullptr));
227e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  options.push_back(std::make_pair("-Xcheck:jni", nullptr));
228c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(min_heap_string, nullptr));
229c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  options.push_back(std::make_pair(max_heap_string, nullptr));
230e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
231e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  SetUpRuntimeOptions(&options);
232aaebaa0121be3b9d9f13630585304482cbcaeb4bIgor Murashkin
23366d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  PreRuntimeCreate();
234e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (!Runtime::Create(options, false)) {
235e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    LOG(FATAL) << "Failed to create runtime";
236e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return;
237e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
23866d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  PostRuntimeCreate();
239e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_.reset(Runtime::Current());
240e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_ = runtime_->GetClassLinker();
241e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
2422969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe
2432969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe  // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
2442969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe  // set up.
2459b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe  if (!unstarted_initialized_) {
2469b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe    interpreter::UnstartedRuntimeInitialize();
2479b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe    unstarted_initialized_ = true;
2489b5cba401aae6d34450112bdcf9bab5e19da90f2Andreas Gampe  }
2492969bcdcd80624e4a4fef696b54c2c76b44b6853Andreas Gampe
250e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class_linker_->RunRootClinits();
251fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  boot_class_path_ = class_linker_->GetBootClassPath();
252fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  java_lang_dex_file_ = boot_class_path_[0];
253fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler
254e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
255e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Runtime::Create acquired the mutator_lock_ that is normally given away when we
256e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
257e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
258e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
259e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // We're back in native, take the opportunity to initialize well known classes.
260e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  WellKnownClasses::Init(Thread::Current()->GetJniEnv());
261e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
262e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
263e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // pool is created by the runtime.
264e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_->GetHeap()->CreateThreadPool();
265e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  runtime_->GetHeap()->VerifyHeap();  // Check for heap corruption before the test
266c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler
267c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  // Get the boot class path from the runtime so it can be used in tests.
268c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  boot_class_path_ = class_linker_->GetBootClassPath();
269c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  ASSERT_FALSE(boot_class_path_.empty());
270c275259449ec57987e52d3ab1eda3272b994488fRichard Uhler  java_lang_dex_file_ = boot_class_path_[0];
271e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
272e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
273a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightvoid CommonRuntimeTest::ClearDirectory(const char* dirpath) {
274a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ASSERT_TRUE(dirpath != nullptr);
275a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  DIR* dir = opendir(dirpath);
276e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_TRUE(dir != nullptr);
277e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  dirent* e;
278a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  struct stat s;
279e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  while ((e = readdir(dir)) != nullptr) {
280e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
281e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      continue;
282e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    }
283f0a3f09c3d54646166a55c05a6b39c7dd504129cJeff Hao    std::string filename(dirpath);
284e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename.push_back('/');
285e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename.append(e->d_name);
286a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    int stat_result = lstat(filename.c_str(), &s);
287a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
288a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (S_ISDIR(s.st_mode)) {
289a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ClearDirectory(filename.c_str());
290a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      int rmdir_result = rmdir(filename.c_str());
291a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ASSERT_EQ(0, rmdir_result) << filename;
292a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    } else {
293a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      int unlink_result = unlink(filename.c_str());
294a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      ASSERT_EQ(0, unlink_result) << filename;
295a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    }
296e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
297e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  closedir(dir);
298a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light}
299a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
300a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightvoid CommonRuntimeTest::TearDown() {
301a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  const char* android_data = getenv("ANDROID_DATA");
302a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ASSERT_TRUE(android_data != nullptr);
303a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light  ClearDirectory(dalvik_cache_.c_str());
304e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
305e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  ASSERT_EQ(0, rmdir_cache_result);
3067747c8de402f64e3009ca3bcccebddbb70f617eeAndreas Gampe  TearDownAndroidData(android_data_, true);
307e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
308e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // icu4c has a fixed 10-element array "gCommonICUDataArray".
309e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // If we run > 10 tests, we fill that array and u_setCommonData fails.
310e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // There's a function to clear the array, but it's not public...
311e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  typedef void (*IcuCleanupFn)();
312e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
313e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(sym != nullptr) << dlerror();
314e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
315e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  (*icu_cleanup_fn)();
316e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
317e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  Runtime::Current()->GetHeap()->VerifyHeap();  // Check for heap corruption after the test
318e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
319e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
320e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetLibCoreDexFileName() {
321e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return GetDexFileName("core-libart");
322e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
323e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
324e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
325e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
326e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* host_dir = getenv("ANDROID_HOST_OUT");
327e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(host_dir != nullptr);
328e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
329e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
330e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
331e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
332e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
333e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::string CommonRuntimeTest::GetTestAndroidRoot() {
334e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
335e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    const char* host_dir = getenv("ANDROID_HOST_OUT");
336e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(host_dir != nullptr);
337e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    return host_dir;
338e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
339e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return GetAndroidRoot();
340e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
341e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
3421fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
3431fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#ifdef ART_TARGET
3441fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#ifndef ART_TARGET_NATIVETEST_DIR
3451fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#error "ART_TARGET_NATIVETEST_DIR not set."
3461fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#endif
3471fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe// Wrap it as a string literal.
3481fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
3491fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#else
3501fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#define ART_TARGET_NATIVETEST_DIR_STRING ""
3511fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe#endif
3521fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe
35366d874d96d5699bb090c59f47a5a528956ca053eRichard Uhlerstd::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
354e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(name != nullptr);
355e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string filename;
356e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  if (IsHost()) {
357e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename += getenv("ANDROID_HOST_OUT");
358e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    filename += "/framework/";
359e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  } else {
3601fe5e5cce85a3cdc64a8d226a8ef4e6134c2b9ebAndreas Gampe    filename += ART_TARGET_NATIVETEST_DIR_STRING;
361e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
362e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += "art-gtest-";
363e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += name;
364e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  filename += ".jar";
36566d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  return filename;
36666d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler}
36766d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler
36866d874d96d5699bb090c59f47a5a528956ca053eRichard Uhlerstd::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
36966d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  std::string filename = GetTestDexFileName(name);
370e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  std::string error_msg;
371fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
372e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
373e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
374fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
375e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK_EQ(PROT_READ, dex_file->GetPermissions());
376e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    CHECK(dex_file->IsReadOnly());
377e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
378e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return dex_files;
379e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
380e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
381fbef44de596d298dc6430f482dffc933a046dd28Richard Uhlerstd::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
382fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
383e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_EQ(1U, vector.size());
384fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  return std::move(vector[0]);
385e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
386e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
387e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersjobject CommonRuntimeTest::LoadDex(const char* dex_name) {
388fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
389fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
390e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  CHECK_NE(0U, dex_files.size());
391fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
392fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
393e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers    class_linker_->RegisterDexFile(*dex_file);
394fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    loaded_dex_files_.push_back(std::move(dex_file));
395e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  }
39668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  Thread* self = Thread::Current();
39768d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  JNIEnvExt* env = self->GetJniEnv();
39868d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  ScopedLocalRef<jobject> class_loader_local(env,
39968d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers      env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
40068d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  jobject class_loader = env->NewGlobalRef(class_loader_local.get());
40168d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  self->SetClassLoaderOverride(class_loader_local.get());
402fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path);
403e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  return class_loader;
404e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
405e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
4063774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstd::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
4073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  CHECK(suffix != nullptr);
4083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
4093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::string location;
4103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  if (IsHost()) {
4113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    const char* host_dir = getenv("ANDROID_HOST_OUT");
4123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(host_dir != NULL);
4133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
4143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  } else {
4153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    location = StringPrintf("/data/art-test/core.%s", suffix);
4163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
4173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
4183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return location;
4193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin}
4203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
421e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
42268d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  vm_->SetCheckJniAbortHook(Hook, &actual_);
423e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
424e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
425e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian RogersCheckJniAbortCatcher::~CheckJniAbortCatcher() {
42668d8b42ddec39ec0174162d90d4abaa004d1983eIan Rogers  vm_->SetCheckJniAbortHook(nullptr, nullptr);
427e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_TRUE(actual_.empty()) << actual_;
428e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
429e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
430e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CheckJniAbortCatcher::Check(const char* expected_text) {
431e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
432e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      << "Expected to find: " << expected_text << "\n"
433e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers      << "In the output   : " << actual_;
434e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  actual_.clear();
435e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
436e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
437e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersvoid CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
438e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  // We use += because when we're hooking the aborts like this, multiple problems can be found.
439e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  *reinterpret_cast<std::string*>(data) += reason;
440e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
441e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
442e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace art
443e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
444e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace std {
445e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
446e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogerstemplate <typename T>
447e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersstd::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
448e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersos << ::art::ToString(rhs);
449e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersreturn os;
450e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}
451e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
452e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace std
453