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