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