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