David Sehr | d5f8de8 | 2018-04-27 14:12:03 -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 | |
| 17 | #include "common_art_test.h" |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <dlfcn.h> |
| 21 | #include <fcntl.h> |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 22 | #include <ftw.h> |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 25 | #include <cstdio> |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 26 | #include <filesystem> |
| 27 | #include "android-base/file.h" |
| 28 | #include "android-base/logging.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 29 | #include "nativehelper/scoped_local_ref.h" |
| 30 | |
| 31 | #include "android-base/stringprintf.h" |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 32 | #include "android-base/strings.h" |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 33 | #include "android-base/unique_fd.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 34 | |
| 35 | #include "art_field-inl.h" |
| 36 | #include "base/file_utils.h" |
| 37 | #include "base/logging.h" |
| 38 | #include "base/macros.h" |
| 39 | #include "base/mem_map.h" |
| 40 | #include "base/mutex.h" |
| 41 | #include "base/os.h" |
| 42 | #include "base/runtime_debug.h" |
| 43 | #include "base/stl_util.h" |
| 44 | #include "base/unix_file/fd_file.h" |
| 45 | #include "dex/art_dex_file_loader.h" |
| 46 | #include "dex/dex_file-inl.h" |
| 47 | #include "dex/dex_file_loader.h" |
| 48 | #include "dex/primitive.h" |
| 49 | #include "gtest/gtest.h" |
| 50 | |
| 51 | namespace art { |
| 52 | |
| 53 | using android::base::StringPrintf; |
| 54 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 55 | ScratchDir::ScratchDir() { |
| 56 | // ANDROID_DATA needs to be set |
| 57 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 58 | "Are you subclassing RuntimeTest?"; |
| 59 | path_ = getenv("ANDROID_DATA"); |
| 60 | path_ += "/tmp-XXXXXX"; |
| 61 | bool ok = (mkdtemp(&path_[0]) != nullptr); |
| 62 | CHECK(ok) << strerror(errno) << " for " << path_; |
| 63 | path_ += "/"; |
| 64 | } |
| 65 | |
| 66 | ScratchDir::~ScratchDir() { |
| 67 | // Recursively delete the directory and all its content. |
| 68 | nftw(path_.c_str(), [](const char* name, const struct stat*, int type, struct FTW *) { |
| 69 | if (type == FTW_F) { |
| 70 | unlink(name); |
| 71 | } else if (type == FTW_DP) { |
| 72 | rmdir(name); |
| 73 | } |
| 74 | return 0; |
| 75 | }, 256 /* max open file descriptors */, FTW_DEPTH); |
| 76 | } |
| 77 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 78 | ScratchFile::ScratchFile() { |
| 79 | // ANDROID_DATA needs to be set |
| 80 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 81 | "Are you subclassing RuntimeTest?"; |
| 82 | filename_ = getenv("ANDROID_DATA"); |
| 83 | filename_ += "/TmpFile-XXXXXX"; |
| 84 | int fd = mkstemp(&filename_[0]); |
| 85 | CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_; |
| 86 | file_.reset(new File(fd, GetFilename(), true)); |
| 87 | } |
| 88 | |
| 89 | ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) |
| 90 | : ScratchFile(other.GetFilename() + suffix) {} |
| 91 | |
| 92 | ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) { |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 93 | int fd = open(filename_.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 94 | CHECK_NE(-1, fd); |
| 95 | file_.reset(new File(fd, GetFilename(), true)); |
| 96 | } |
| 97 | |
| 98 | ScratchFile::ScratchFile(File* file) { |
| 99 | CHECK(file != nullptr); |
| 100 | filename_ = file->GetPath(); |
| 101 | file_.reset(file); |
| 102 | } |
| 103 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 104 | ScratchFile::ScratchFile(ScratchFile&& other) noexcept { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 105 | *this = std::move(other); |
| 106 | } |
| 107 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 108 | ScratchFile& ScratchFile::operator=(ScratchFile&& other) noexcept { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 109 | if (GetFile() != other.GetFile()) { |
| 110 | std::swap(filename_, other.filename_); |
| 111 | std::swap(file_, other.file_); |
| 112 | } |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | ScratchFile::~ScratchFile() { |
| 117 | Unlink(); |
| 118 | } |
| 119 | |
| 120 | int ScratchFile::GetFd() const { |
| 121 | return file_->Fd(); |
| 122 | } |
| 123 | |
| 124 | void ScratchFile::Close() { |
| 125 | if (file_.get() != nullptr) { |
| 126 | if (file_->FlushCloseOrErase() != 0) { |
| 127 | PLOG(WARNING) << "Error closing scratch file."; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void ScratchFile::Unlink() { |
| 133 | if (!OS::FileExists(filename_.c_str())) { |
| 134 | return; |
| 135 | } |
| 136 | Close(); |
| 137 | int unlink_result = unlink(filename_.c_str()); |
| 138 | CHECK_EQ(0, unlink_result); |
| 139 | } |
| 140 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 141 | void CommonArtTestImpl::SetUpAndroidRootEnvVars() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 142 | if (IsHost()) { |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 143 | // Make sure that ANDROID_BUILD_TOP is set. If not, set it from CWD. |
| 144 | const char* android_build_top_from_env = getenv("ANDROID_BUILD_TOP"); |
| 145 | if (android_build_top_from_env == nullptr) { |
| 146 | // Not set by build server, so default to current directory. |
| 147 | char* cwd = getcwd(nullptr, 0); |
| 148 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 149 | free(cwd); |
| 150 | android_build_top_from_env = getenv("ANDROID_BUILD_TOP"); |
| 151 | } |
| 152 | |
| 153 | const char* android_host_out_from_env = getenv("ANDROID_HOST_OUT"); |
| 154 | if (android_host_out_from_env == nullptr) { |
| 155 | // Not set by build server, so default to the usual value of |
| 156 | // ANDROID_HOST_OUT. |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 157 | std::string android_host_out; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 158 | #if defined(__linux__) |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 159 | // Fallback |
| 160 | android_host_out = std::string(android_build_top_from_env) + "/out/host/linux-x86"; |
| 161 | // Look at how we were invoked |
| 162 | std::string argv; |
| 163 | if (android::base::ReadFileToString("/proc/self/cmdline", &argv)) { |
| 164 | // /proc/self/cmdline is the programs 'argv' with elements delimited by '\0'. |
| 165 | std::string cmdpath(argv.substr(0, argv.find('\0'))); |
| 166 | std::filesystem::path path(cmdpath); |
| 167 | // If the path is relative then prepend the android_build_top_from_env to it |
| 168 | if (path.is_relative()) { |
| 169 | path = std::filesystem::path(android_build_top_from_env).append(cmdpath); |
| 170 | DCHECK(path.is_absolute()) << path; |
| 171 | } |
| 172 | // Walk up until we find the linux-x86 directory or we hit the root directory. |
| 173 | while (path.has_parent_path() && path.parent_path() != path && |
| 174 | path.filename() != std::filesystem::path("linux-x86")) { |
| 175 | path = path.parent_path(); |
| 176 | } |
| 177 | // If we found a linux-x86 directory path is now android_host_out |
| 178 | if (path.filename() == std::filesystem::path("linux-x86")) { |
| 179 | android_host_out = path.string(); |
| 180 | } |
| 181 | } |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 182 | #elif defined(__APPLE__) |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 183 | android_host_out = std::string(android_build_top_from_env) + "/out/host/darwin-x86"; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 184 | #else |
| 185 | #error unsupported OS |
| 186 | #endif |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 187 | setenv("ANDROID_HOST_OUT", android_host_out.c_str(), 1); |
| 188 | android_host_out_from_env = getenv("ANDROID_HOST_OUT"); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 189 | } |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 190 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 191 | // Environment variable ANDROID_ROOT is set on the device, but not |
| 192 | // necessarily on the host. |
| 193 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 194 | if (android_root_from_env == nullptr) { |
| 195 | // Use ANDROID_HOST_OUT for ANDROID_ROOT. |
| 196 | setenv("ANDROID_ROOT", android_host_out_from_env, 1); |
| 197 | android_root_from_env = getenv("ANDROID_ROOT"); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 198 | } |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 199 | |
Victor Chang | 6461124 | 2019-07-05 16:32:41 +0100 | [diff] [blame] | 200 | // Environment variable ANDROID_I18N_ROOT is set on the device, but not |
| 201 | // necessarily on the host. It needs to be set so that various libraries |
| 202 | // like libcore / icu4j / icu4c can find their data files. |
| 203 | const char* android_i18n_root_from_env = getenv("ANDROID_I18N_ROOT"); |
| 204 | if (android_i18n_root_from_env == nullptr) { |
| 205 | // Use ${ANDROID_I18N_OUT}/com.android.i18n for ANDROID_I18N_ROOT. |
| 206 | std::string android_i18n_root = android_host_out_from_env; |
| 207 | android_i18n_root += "/com.android.i18n"; |
| 208 | setenv("ANDROID_I18N_ROOT", android_i18n_root.c_str(), 1); |
| 209 | } |
| 210 | |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 211 | // Environment variable ANDROID_ART_ROOT is set on the device, but not |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 212 | // necessarily on the host. It needs to be set so that various libraries |
Neil Fuller | 90ffe12 | 2019-06-06 17:25:48 +0100 | [diff] [blame] | 213 | // like libcore / icu4j / icu4c can find their data files. |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 214 | const char* android_art_root_from_env = getenv("ANDROID_ART_ROOT"); |
| 215 | if (android_art_root_from_env == nullptr) { |
| 216 | // Use ${ANDROID_HOST_OUT}/com.android.art for ANDROID_ART_ROOT. |
| 217 | std::string android_art_root = android_host_out_from_env; |
| 218 | android_art_root += "/com.android.art"; |
| 219 | setenv("ANDROID_ART_ROOT", android_art_root.c_str(), 1); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 222 | // Environment variable ANDROID_TZDATA_ROOT is set on the device, but not |
| 223 | // necessarily on the host. It needs to be set so that various libraries |
Neil Fuller | 90ffe12 | 2019-06-06 17:25:48 +0100 | [diff] [blame] | 224 | // like libcore / icu4j / icu4c can find their data files. |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 225 | const char* android_tzdata_root_from_env = getenv("ANDROID_TZDATA_ROOT"); |
| 226 | if (android_tzdata_root_from_env == nullptr) { |
| 227 | // Use ${ANDROID_HOST_OUT}/com.android.tzdata for ANDROID_TZDATA_ROOT. |
| 228 | std::string android_tzdata_root = android_host_out_from_env; |
| 229 | android_tzdata_root += "/com.android.tzdata"; |
| 230 | setenv("ANDROID_TZDATA_ROOT", android_tzdata_root.c_str(), 1); |
| 231 | } |
| 232 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 233 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 237 | void CommonArtTestImpl::SetUpAndroidDataDir(std::string& android_data) { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 238 | // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache |
| 239 | if (IsHost()) { |
| 240 | const char* tmpdir = getenv("TMPDIR"); |
| 241 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 242 | android_data = tmpdir; |
| 243 | } else { |
| 244 | android_data = "/tmp"; |
| 245 | } |
| 246 | } else { |
| 247 | android_data = "/data/dalvik-cache"; |
| 248 | } |
| 249 | android_data += "/art-data-XXXXXX"; |
| 250 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 251 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 252 | } |
| 253 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 254 | } |
| 255 | |
| 256 | void CommonArtTestImpl::SetUp() { |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 257 | SetUpAndroidRootEnvVars(); |
| 258 | SetUpAndroidDataDir(android_data_); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 259 | dalvik_cache_.append(android_data_.c_str()); |
| 260 | dalvik_cache_.append("/dalvik-cache"); |
| 261 | int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
| 262 | ASSERT_EQ(mkdir_result, 0); |
Andreas Gampe | 591b1d2 | 2019-06-25 13:08:16 -0700 | [diff] [blame] | 263 | |
| 264 | static bool gSlowDebugTestFlag = false; |
| 265 | RegisterRuntimeDebugFlag(&gSlowDebugTestFlag); |
| 266 | SetRuntimeDebugFlagsEnabled(true); |
| 267 | CHECK(gSlowDebugTestFlag); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 270 | void CommonArtTestImpl::TearDownAndroidDataDir(const std::string& android_data, |
| 271 | bool fail_on_error) { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 272 | if (fail_on_error) { |
| 273 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 274 | } else { |
| 275 | rmdir(android_data.c_str()); |
| 276 | } |
| 277 | } |
| 278 | |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 279 | std::string CommonArtTestImpl::GetAndroidBuildTop() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 280 | std::string root; |
| 281 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 282 | if (android_build_top != nullptr) { |
| 283 | root = android_build_top; |
| 284 | } else { |
| 285 | // Not set by build server, so default to current directory |
| 286 | char* cwd = getcwd(nullptr, 0); |
| 287 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 288 | root = cwd; |
| 289 | free(cwd); |
| 290 | } |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 291 | CHECK(!root.empty()); |
| 292 | if (root.back() != '/') { |
| 293 | root += '/'; |
| 294 | } |
| 295 | return root; |
| 296 | } |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 297 | |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 298 | // Helper - find directory with the following format: |
| 299 | // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ |
| 300 | std::string CommonArtTestImpl::GetAndroidToolsDir(const std::string& subdir1, |
| 301 | const std::string& subdir2, |
| 302 | const std::string& subdir3) { |
| 303 | std::string toolsdir = GetAndroidBuildTop() + subdir1; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 304 | std::string founddir; |
| 305 | DIR* dir; |
| 306 | if ((dir = opendir(toolsdir.c_str())) != nullptr) { |
| 307 | float maxversion = 0; |
| 308 | struct dirent* entry; |
| 309 | while ((entry = readdir(dir)) != nullptr) { |
| 310 | std::string format = subdir2 + "-%f"; |
| 311 | float version; |
| 312 | if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) { |
| 313 | if (version > maxversion) { |
| 314 | maxversion = version; |
| 315 | founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/"; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | closedir(dir); |
| 320 | } |
| 321 | |
| 322 | if (founddir.empty()) { |
| 323 | ADD_FAILURE() << "Cannot find Android tools directory."; |
| 324 | } |
| 325 | return founddir; |
| 326 | } |
| 327 | |
| 328 | std::string CommonArtTestImpl::GetAndroidHostToolsDir() { |
| 329 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host", |
Nicolas Geoffray | de3e70f | 2019-02-22 12:07:29 +0000 | [diff] [blame] | 330 | "x86_64-linux-glibc2.17", |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 331 | "x86_64-linux"); |
| 332 | } |
| 333 | |
| 334 | std::string CommonArtTestImpl::GetCoreArtLocation() { |
| 335 | return GetCoreFileLocation("art"); |
| 336 | } |
| 337 | |
| 338 | std::string CommonArtTestImpl::GetCoreOatLocation() { |
| 339 | return GetCoreFileLocation("oat"); |
| 340 | } |
| 341 | |
| 342 | std::unique_ptr<const DexFile> CommonArtTestImpl::LoadExpectSingleDexFile(const char* location) { |
| 343 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 344 | std::string error_msg; |
| 345 | MemMap::Init(); |
| 346 | static constexpr bool kVerifyChecksum = true; |
| 347 | const ArtDexFileLoader dex_file_loader; |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 348 | std::string filename(location); |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 349 | if (!dex_file_loader.Open(filename.c_str(), |
| 350 | std::string(location), |
| 351 | /* verify= */ true, |
| 352 | kVerifyChecksum, |
| 353 | &error_msg, |
| 354 | &dex_files)) { |
| 355 | LOG(FATAL) << "Could not open .dex file '" << filename << "': " << error_msg << "\n"; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 356 | UNREACHABLE(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 357 | } |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 358 | CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << filename; |
| 359 | return std::move(dex_files[0]); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) { |
| 363 | ASSERT_TRUE(dirpath != nullptr); |
| 364 | DIR* dir = opendir(dirpath); |
| 365 | ASSERT_TRUE(dir != nullptr); |
| 366 | dirent* e; |
| 367 | struct stat s; |
| 368 | while ((e = readdir(dir)) != nullptr) { |
| 369 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 370 | continue; |
| 371 | } |
| 372 | std::string filename(dirpath); |
| 373 | filename.push_back('/'); |
| 374 | filename.append(e->d_name); |
| 375 | int stat_result = lstat(filename.c_str(), &s); |
| 376 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 377 | if (S_ISDIR(s.st_mode)) { |
| 378 | if (recursive) { |
| 379 | ClearDirectory(filename.c_str()); |
| 380 | int rmdir_result = rmdir(filename.c_str()); |
| 381 | ASSERT_EQ(0, rmdir_result) << filename; |
| 382 | } |
| 383 | } else { |
| 384 | int unlink_result = unlink(filename.c_str()); |
| 385 | ASSERT_EQ(0, unlink_result) << filename; |
| 386 | } |
| 387 | } |
| 388 | closedir(dir); |
| 389 | } |
| 390 | |
| 391 | void CommonArtTestImpl::TearDown() { |
| 392 | const char* android_data = getenv("ANDROID_DATA"); |
| 393 | ASSERT_TRUE(android_data != nullptr); |
| 394 | ClearDirectory(dalvik_cache_.c_str()); |
| 395 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 396 | ASSERT_EQ(0, rmdir_cache_result); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 397 | TearDownAndroidDataDir(android_data_, true); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 398 | dalvik_cache_.clear(); |
| 399 | } |
| 400 | |
| 401 | static std::string GetDexFileName(const std::string& jar_prefix, bool host) { |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 402 | std::string prefix(host ? GetAndroidRoot() : ""); |
| 403 | const char* apexPath = (jar_prefix == "conscrypt") |
| 404 | ? kAndroidConscryptApexDefaultPath |
| 405 | : kAndroidArtApexDefaultPath; |
| 406 | return StringPrintf("%s%s/javalib/%s.jar", prefix.c_str(), apexPath, jar_prefix.c_str()); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 409 | std::vector<std::string> CommonArtTestImpl::GetLibCoreModuleNames() const { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 410 | // Note: This must start with the CORE_IMG_JARS in Android.common_path.mk |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 411 | // because that's what we use for compiling the core.art image. |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 412 | // It may contain additional modules from TEST_CORE_JARS. |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 413 | return { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 414 | // CORE_IMG_JARS modules. |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 415 | "core-oj", |
| 416 | "core-libart", |
Victor Chang | 759845f | 2019-08-06 16:04:36 +0100 | [diff] [blame] | 417 | "core-icu4j", |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 418 | "okhttp", |
| 419 | "bouncycastle", |
Vladimir Marko | ea65b21 | 2018-12-13 13:32:13 +0000 | [diff] [blame] | 420 | "apache-xml", |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 421 | // Additional modules. |
| 422 | "conscrypt", |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 423 | }; |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 424 | } |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 425 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 426 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames( |
| 427 | const std::vector<std::string>& modules) const { |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 428 | std::vector<std::string> result; |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 429 | result.reserve(modules.size()); |
| 430 | for (const std::string& module : modules) { |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 431 | result.push_back(GetDexFileName(module, IsHost())); |
| 432 | } |
| 433 | return result; |
| 434 | } |
| 435 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 436 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames() const { |
| 437 | std::vector<std::string> modules = GetLibCoreModuleNames(); |
| 438 | return GetLibCoreDexFileNames(modules); |
| 439 | } |
| 440 | |
| 441 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations( |
| 442 | const std::vector<std::string>& modules) const { |
| 443 | std::vector<std::string> result = GetLibCoreDexFileNames(modules); |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 444 | if (IsHost()) { |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 445 | // Strip the ANDROID_BUILD_TOP directory including the directory separator '/'. |
| 446 | std::string prefix = GetAndroidBuildTop(); |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 447 | for (std::string& location : result) { |
| 448 | CHECK_GT(location.size(), prefix.size()); |
| 449 | CHECK_EQ(location.compare(0u, prefix.size(), prefix), 0); |
| 450 | location.erase(0u, prefix.size()); |
| 451 | } |
| 452 | } |
| 453 | return result; |
| 454 | } |
| 455 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 456 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations() const { |
| 457 | std::vector<std::string> modules = GetLibCoreModuleNames(); |
| 458 | return GetLibCoreDexLocations(modules); |
| 459 | } |
| 460 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 461 | std::string CommonArtTestImpl::GetClassPathOption(const char* option, |
| 462 | const std::vector<std::string>& class_path) { |
| 463 | return option + android::base::Join(class_path, ':'); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 464 | } |
| 465 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 466 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 467 | #ifdef ART_TARGET |
| 468 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 469 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 470 | #endif |
| 471 | // Wrap it as a string literal. |
| 472 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 473 | #else |
| 474 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 475 | #endif |
| 476 | |
| 477 | std::string CommonArtTestImpl::GetTestDexFileName(const char* name) const { |
| 478 | CHECK(name != nullptr); |
| 479 | std::string filename; |
| 480 | if (IsHost()) { |
Roland Levillain | dd8e222 | 2019-04-05 15:31:55 +0100 | [diff] [blame] | 481 | filename += GetAndroidRoot() + "/framework/"; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 482 | } else { |
| 483 | filename += ART_TARGET_NATIVETEST_DIR_STRING; |
| 484 | } |
| 485 | filename += "art-gtest-"; |
| 486 | filename += name; |
| 487 | filename += ".jar"; |
| 488 | return filename; |
| 489 | } |
| 490 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 491 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenDexFiles(const char* filename) { |
| 492 | static constexpr bool kVerify = true; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 493 | static constexpr bool kVerifyChecksum = true; |
| 494 | std::string error_msg; |
| 495 | const ArtDexFileLoader dex_file_loader; |
| 496 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 497 | bool success = dex_file_loader.Open(filename, |
| 498 | filename, |
| 499 | kVerify, |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 500 | kVerifyChecksum, |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 501 | &error_msg, |
| 502 | &dex_files); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 503 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
| 504 | for (auto& dex_file : dex_files) { |
| 505 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 506 | CHECK(dex_file->IsReadOnly()); |
| 507 | } |
| 508 | return dex_files; |
| 509 | } |
| 510 | |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 511 | std::unique_ptr<const DexFile> CommonArtTestImpl::OpenDexFile(const char* filename) { |
| 512 | std::vector<std::unique_ptr<const DexFile>> dex_files(OpenDexFiles(filename)); |
| 513 | CHECK_EQ(dex_files.size(), 1u) << "Expected only one dex file"; |
| 514 | return std::move(dex_files[0]); |
| 515 | } |
| 516 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 517 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenTestDexFiles( |
| 518 | const char* name) { |
| 519 | return OpenDexFiles(GetTestDexFileName(name).c_str()); |
| 520 | } |
| 521 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 522 | std::unique_ptr<const DexFile> CommonArtTestImpl::OpenTestDexFile(const char* name) { |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 523 | return OpenDexFile(GetTestDexFileName(name).c_str()); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) { |
| 527 | CHECK(suffix != nullptr); |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 528 | std::string prefix(IsHost() ? GetAndroidRoot() : ""); |
| 529 | return StringPrintf("%s%s/javalib/boot.%s", prefix.c_str(), kAndroidArtApexDefaultPath, suffix); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | std::string CommonArtTestImpl::CreateClassPath( |
| 533 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 534 | CHECK(!dex_files.empty()); |
| 535 | std::string classpath = dex_files[0]->GetLocation(); |
| 536 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 537 | classpath += ":" + dex_files[i]->GetLocation(); |
| 538 | } |
| 539 | return classpath; |
| 540 | } |
| 541 | |
| 542 | std::string CommonArtTestImpl::CreateClassPathWithChecksums( |
| 543 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 544 | CHECK(!dex_files.empty()); |
| 545 | std::string classpath = dex_files[0]->GetLocation() + "*" + |
| 546 | std::to_string(dex_files[0]->GetLocationChecksum()); |
| 547 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 548 | classpath += ":" + dex_files[i]->GetLocation() + "*" + |
| 549 | std::to_string(dex_files[i]->GetLocationChecksum()); |
| 550 | } |
| 551 | return classpath; |
| 552 | } |
| 553 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 554 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 555 | const std::vector<std::string>& argv, |
| 556 | const PostForkFn& post_fork, |
| 557 | const OutputHandlerFn& handler) { |
| 558 | ForkAndExecResult result; |
| 559 | result.status_code = 0; |
| 560 | result.stage = ForkAndExecResult::kLink; |
| 561 | |
| 562 | std::vector<const char*> c_args; |
| 563 | for (const std::string& str : argv) { |
| 564 | c_args.push_back(str.c_str()); |
| 565 | } |
| 566 | c_args.push_back(nullptr); |
| 567 | |
| 568 | android::base::unique_fd link[2]; |
| 569 | { |
| 570 | int link_fd[2]; |
| 571 | |
| 572 | if (pipe(link_fd) == -1) { |
| 573 | return result; |
| 574 | } |
| 575 | link[0].reset(link_fd[0]); |
| 576 | link[1].reset(link_fd[1]); |
| 577 | } |
| 578 | |
| 579 | result.stage = ForkAndExecResult::kFork; |
| 580 | |
| 581 | pid_t pid = fork(); |
| 582 | if (pid == -1) { |
| 583 | return result; |
| 584 | } |
| 585 | |
| 586 | if (pid == 0) { |
| 587 | if (!post_fork()) { |
| 588 | LOG(ERROR) << "Failed post-fork function"; |
| 589 | exit(1); |
| 590 | UNREACHABLE(); |
| 591 | } |
| 592 | |
| 593 | // Redirect stdout and stderr. |
| 594 | dup2(link[1].get(), STDOUT_FILENO); |
| 595 | dup2(link[1].get(), STDERR_FILENO); |
| 596 | |
| 597 | link[0].reset(); |
| 598 | link[1].reset(); |
| 599 | |
| 600 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 601 | exit(1); |
| 602 | UNREACHABLE(); |
| 603 | } |
| 604 | |
| 605 | result.stage = ForkAndExecResult::kWaitpid; |
| 606 | link[1].reset(); |
| 607 | |
| 608 | char buffer[128] = { 0 }; |
| 609 | ssize_t bytes_read = 0; |
| 610 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0].get(), buffer, 128)) > 0) { |
| 611 | handler(buffer, bytes_read); |
| 612 | } |
| 613 | handler(buffer, 0u); // End with a virtual write of zero length to simplify clients. |
| 614 | |
| 615 | link[0].reset(); |
| 616 | |
| 617 | if (waitpid(pid, &result.status_code, 0) == -1) { |
| 618 | return result; |
| 619 | } |
| 620 | |
| 621 | result.stage = ForkAndExecResult::kFinished; |
| 622 | return result; |
| 623 | } |
| 624 | |
| 625 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 626 | const std::vector<std::string>& argv, const PostForkFn& post_fork, std::string* output) { |
| 627 | auto string_collect_fn = [output](char* buf, size_t len) { |
| 628 | *output += std::string(buf, len); |
| 629 | }; |
| 630 | return ForkAndExec(argv, post_fork, string_collect_fn); |
| 631 | } |
| 632 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 633 | } // namespace art |