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