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 Srbecky | 4a88a5a | 2020-05-05 16:21:57 +0100 | [diff] [blame] | 23 | #include <libgen.h> |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 25 | #include <sys/capability.h> |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 27 | |
| 28 | #include <cstdio> |
| 29 | #include <filesystem> |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 30 | #include <functional> |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 31 | |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 32 | #include "android-base/file.h" |
| 33 | #include "android-base/logging.h" |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 34 | #include "android-base/process.h" |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 35 | #include "android-base/scopeguard.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 36 | #include "android-base/stringprintf.h" |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 37 | #include "android-base/strings.h" |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 38 | #include "android-base/unique_fd.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 39 | #include "art_field-inl.h" |
| 40 | #include "base/file_utils.h" |
| 41 | #include "base/logging.h" |
| 42 | #include "base/macros.h" |
| 43 | #include "base/mem_map.h" |
| 44 | #include "base/mutex.h" |
| 45 | #include "base/os.h" |
| 46 | #include "base/runtime_debug.h" |
Jiakai Zhang | 6c68c74 | 2022-07-19 18:01:19 +0100 | [diff] [blame] | 47 | #include "base/scoped_cap.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 48 | #include "base/stl_util.h" |
David Srbecky | e2a9bb7 | 2020-09-28 12:24:39 +0100 | [diff] [blame] | 49 | #include "base/string_view_cpp20.h" |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 50 | #include "base/testing.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 51 | #include "base/unix_file/fd_file.h" |
| 52 | #include "dex/art_dex_file_loader.h" |
| 53 | #include "dex/dex_file-inl.h" |
| 54 | #include "dex/dex_file_loader.h" |
| 55 | #include "dex/primitive.h" |
| 56 | #include "gtest/gtest.h" |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 57 | #include "nativehelper/scoped_local_ref.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 58 | |
| 59 | namespace art { |
| 60 | |
| 61 | using android::base::StringPrintf; |
| 62 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 63 | ScratchDir::ScratchDir(bool keep_files) : keep_files_(keep_files) { |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 64 | // ANDROID_DATA needs to be set |
| 65 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 66 | "Are you subclassing RuntimeTest?"; |
| 67 | path_ = getenv("ANDROID_DATA"); |
| 68 | path_ += "/tmp-XXXXXX"; |
| 69 | bool ok = (mkdtemp(&path_[0]) != nullptr); |
| 70 | CHECK(ok) << strerror(errno) << " for " << path_; |
| 71 | path_ += "/"; |
| 72 | } |
| 73 | |
| 74 | ScratchDir::~ScratchDir() { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 75 | if (!keep_files_) { |
Jiakai Zhang | 7421603 | 2022-09-29 15:53:14 +0100 | [diff] [blame] | 76 | std::filesystem::remove_all(path_); |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 77 | } |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 80 | ScratchFile::ScratchFile() { |
| 81 | // ANDROID_DATA needs to be set |
| 82 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 83 | "Are you subclassing RuntimeTest?"; |
| 84 | filename_ = getenv("ANDROID_DATA"); |
| 85 | filename_ += "/TmpFile-XXXXXX"; |
| 86 | int fd = mkstemp(&filename_[0]); |
| 87 | CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_; |
| 88 | file_.reset(new File(fd, GetFilename(), true)); |
| 89 | } |
| 90 | |
| 91 | ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) |
| 92 | : ScratchFile(other.GetFilename() + suffix) {} |
| 93 | |
| 94 | ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) { |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 95 | 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] | 96 | CHECK_NE(-1, fd); |
| 97 | file_.reset(new File(fd, GetFilename(), true)); |
| 98 | } |
| 99 | |
| 100 | ScratchFile::ScratchFile(File* file) { |
| 101 | CHECK(file != nullptr); |
| 102 | filename_ = file->GetPath(); |
| 103 | file_.reset(file); |
| 104 | } |
| 105 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 106 | ScratchFile::ScratchFile(ScratchFile&& other) noexcept { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 107 | *this = std::move(other); |
| 108 | } |
| 109 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 110 | ScratchFile& ScratchFile::operator=(ScratchFile&& other) noexcept { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 111 | if (GetFile() != other.GetFile()) { |
| 112 | std::swap(filename_, other.filename_); |
| 113 | std::swap(file_, other.file_); |
| 114 | } |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | ScratchFile::~ScratchFile() { |
| 119 | Unlink(); |
| 120 | } |
| 121 | |
| 122 | int ScratchFile::GetFd() const { |
| 123 | return file_->Fd(); |
| 124 | } |
| 125 | |
| 126 | void ScratchFile::Close() { |
Vladimir Marko | a220980 | 2021-04-23 13:28:29 +0000 | [diff] [blame] | 127 | if (file_ != nullptr) { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 128 | if (file_->FlushCloseOrErase() != 0) { |
| 129 | PLOG(WARNING) << "Error closing scratch file."; |
| 130 | } |
Vladimir Marko | a220980 | 2021-04-23 13:28:29 +0000 | [diff] [blame] | 131 | file_.reset(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | void ScratchFile::Unlink() { |
| 136 | if (!OS::FileExists(filename_.c_str())) { |
| 137 | return; |
| 138 | } |
| 139 | Close(); |
| 140 | int unlink_result = unlink(filename_.c_str()); |
| 141 | CHECK_EQ(0, unlink_result); |
| 142 | } |
| 143 | |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 144 | // Temporarily drops all root capabilities when the test is run as root. This is a noop otherwise. |
| 145 | android::base::ScopeGuard<std::function<void()>> ScopedUnroot() { |
| 146 | ScopedCap old_cap(cap_get_proc()); |
Jiakai Zhang | 6c68c74 | 2022-07-19 18:01:19 +0100 | [diff] [blame] | 147 | CHECK_NE(old_cap.Get(), nullptr); |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 148 | ScopedCap new_cap(cap_dup(old_cap.Get())); |
Jiakai Zhang | 6c68c74 | 2022-07-19 18:01:19 +0100 | [diff] [blame] | 149 | CHECK_NE(new_cap.Get(), nullptr); |
Jiakai Zhang | 19c766b | 2022-07-04 14:24:06 +0100 | [diff] [blame] | 150 | CHECK_EQ(cap_clear_flag(new_cap.Get(), CAP_EFFECTIVE), 0); |
| 151 | CHECK_EQ(cap_set_proc(new_cap.Get()), 0); |
| 152 | // `old_cap` is actually not shared with anyone else, but we have to wrap it with a `shared_ptr` |
| 153 | // because `std::function` requires captures to be copyable. |
| 154 | return android::base::make_scope_guard( |
| 155 | [old_cap = std::make_shared<ScopedCap>(std::move(old_cap))]() { |
| 156 | CHECK_EQ(cap_set_proc(old_cap->Get()), 0); |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | // Temporarily drops write permission on a file/directory. |
| 161 | android::base::ScopeGuard<std::function<void()>> ScopedInaccessible(const std::string& path) { |
| 162 | std::filesystem::perms old_perms = std::filesystem::status(path).permissions(); |
| 163 | std::filesystem::permissions(path, std::filesystem::perms::none); |
| 164 | return android::base::make_scope_guard([=]() { std::filesystem::permissions(path, old_perms); }); |
| 165 | } |
| 166 | |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 167 | std::string CommonArtTestImpl::GetAndroidBuildTop() { |
| 168 | CHECK(IsHost()); |
| 169 | std::string android_build_top; |
| 170 | |
| 171 | // Look at how we were invoked to find the expected directory. |
| 172 | std::string argv; |
| 173 | if (android::base::ReadFileToString("/proc/self/cmdline", &argv)) { |
| 174 | // /proc/self/cmdline is the programs 'argv' with elements delimited by '\0'. |
| 175 | std::filesystem::path path(argv.substr(0, argv.find('\0'))); |
| 176 | path = std::filesystem::absolute(path); |
| 177 | // Walk up until we find the one of the well-known directories. |
| 178 | for (; path.parent_path() != path; path = path.parent_path()) { |
| 179 | // We are running tests from out/host/linux-x86 on developer machine. |
| 180 | if (path.filename() == std::filesystem::path("linux-x86")) { |
| 181 | android_build_top = path.parent_path().parent_path().parent_path(); |
| 182 | break; |
| 183 | } |
| 184 | // We are running tests from testcases (extracted from zip) on tradefed. |
David Srbecky | e2a9bb7 | 2020-09-28 12:24:39 +0100 | [diff] [blame] | 185 | // The first path is for remote runs and the second path for local runs. |
| 186 | if (path.filename() == std::filesystem::path("testcases") || |
| 187 | StartsWith(path.filename().string(), "host_testcases")) { |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 188 | android_build_top = path.append("art_common"); |
| 189 | break; |
Alex Light | 5515310 | 2019-05-08 17:03:10 -0700 | [diff] [blame] | 190 | } |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 191 | } |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 192 | } |
| 193 | CHECK(!android_build_top.empty()); |
| 194 | |
| 195 | // Check that the expected directory matches the environment variable. |
| 196 | const char* android_build_top_from_env = getenv("ANDROID_BUILD_TOP"); |
Alex Light | 75c8b63 | 2020-07-14 15:31:02 -0700 | [diff] [blame] | 197 | android_build_top = std::filesystem::path(android_build_top).string(); |
| 198 | CHECK(!android_build_top.empty()); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 199 | if (android_build_top_from_env != nullptr) { |
Alex Light | 1faacf5 | 2020-08-31 15:48:25 -0700 | [diff] [blame] | 200 | if (std::filesystem::weakly_canonical(android_build_top).string() != |
| 201 | std::filesystem::weakly_canonical(android_build_top_from_env).string()) { |
| 202 | LOG(WARNING) << "Execution path (" << argv << ") not below ANDROID_BUILD_TOP (" |
| 203 | << android_build_top_from_env << ")! Using env-var."; |
| 204 | android_build_top = android_build_top_from_env; |
| 205 | } |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 206 | } else { |
| 207 | setenv("ANDROID_BUILD_TOP", android_build_top.c_str(), /*overwrite=*/0); |
| 208 | } |
| 209 | if (android_build_top.back() != '/') { |
| 210 | android_build_top += '/'; |
| 211 | } |
| 212 | return android_build_top; |
| 213 | } |
| 214 | |
| 215 | std::string CommonArtTestImpl::GetAndroidHostOut() { |
| 216 | CHECK(IsHost()); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 217 | |
| 218 | // Check that the expected directory matches the environment variable. |
Alex Light | 75c8b63 | 2020-07-14 15:31:02 -0700 | [diff] [blame] | 219 | // ANDROID_HOST_OUT is set by envsetup or unset and is the full path to host binaries/libs |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 220 | const char* android_host_out_from_env = getenv("ANDROID_HOST_OUT"); |
Alex Light | 75c8b63 | 2020-07-14 15:31:02 -0700 | [diff] [blame] | 221 | // OUT_DIR is a user-settable ENV_VAR that controls where soong puts build artifacts. It can |
| 222 | // either be relative to ANDROID_BUILD_TOP or a concrete path. |
| 223 | const char* android_out_dir = getenv("OUT_DIR"); |
| 224 | // Take account of OUT_DIR setting. |
| 225 | if (android_out_dir == nullptr) { |
| 226 | android_out_dir = "out"; |
| 227 | } |
| 228 | std::string android_host_out; |
| 229 | if (android_out_dir[0] == '/') { |
| 230 | android_host_out = (std::filesystem::path(android_out_dir) / "host" / "linux-x86").string(); |
| 231 | } else { |
| 232 | android_host_out = |
| 233 | (std::filesystem::path(GetAndroidBuildTop()) / android_out_dir / "host" / "linux-x86") |
| 234 | .string(); |
| 235 | } |
| 236 | std::filesystem::path expected(android_host_out); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 237 | if (android_host_out_from_env != nullptr) { |
Alex Light | 75c8b63 | 2020-07-14 15:31:02 -0700 | [diff] [blame] | 238 | std::filesystem::path from_env(std::filesystem::weakly_canonical(android_host_out_from_env)); |
Alex Light | 2734999 | 2020-10-06 15:26:58 -0700 | [diff] [blame] | 239 | if (std::filesystem::weakly_canonical(expected).string() != from_env.string()) { |
| 240 | LOG(WARNING) << "Execution path (" << expected << ") not below ANDROID_HOST_OUT (" |
| 241 | << from_env << ")! Using env-var."; |
| 242 | expected = from_env; |
| 243 | } |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 244 | } else { |
| 245 | setenv("ANDROID_HOST_OUT", android_host_out.c_str(), /*overwrite=*/0); |
| 246 | } |
Alex Light | 75c8b63 | 2020-07-14 15:31:02 -0700 | [diff] [blame] | 247 | return expected.string(); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void CommonArtTestImpl::SetUpAndroidRootEnvVars() { |
| 251 | if (IsHost()) { |
| 252 | std::string android_host_out = GetAndroidHostOut(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 253 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 254 | // Environment variable ANDROID_ROOT is set on the device, but not |
| 255 | // necessarily on the host. |
| 256 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 257 | if (android_root_from_env == nullptr) { |
| 258 | // Use ANDROID_HOST_OUT for ANDROID_ROOT. |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 259 | setenv("ANDROID_ROOT", android_host_out.c_str(), 1); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 260 | android_root_from_env = getenv("ANDROID_ROOT"); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 261 | } |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 262 | |
Victor Chang | 6461124 | 2019-07-05 16:32:41 +0100 | [diff] [blame] | 263 | // Environment variable ANDROID_I18N_ROOT is set on the device, but not |
| 264 | // necessarily on the host. It needs to be set so that various libraries |
| 265 | // like libcore / icu4j / icu4c can find their data files. |
| 266 | const char* android_i18n_root_from_env = getenv("ANDROID_I18N_ROOT"); |
| 267 | if (android_i18n_root_from_env == nullptr) { |
| 268 | // Use ${ANDROID_I18N_OUT}/com.android.i18n for ANDROID_I18N_ROOT. |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 269 | std::string android_i18n_root = android_host_out.c_str(); |
Victor Chang | 6461124 | 2019-07-05 16:32:41 +0100 | [diff] [blame] | 270 | android_i18n_root += "/com.android.i18n"; |
| 271 | setenv("ANDROID_I18N_ROOT", android_i18n_root.c_str(), 1); |
| 272 | } |
| 273 | |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 274 | // Environment variable ANDROID_ART_ROOT is set on the device, but not |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 275 | // 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] | 276 | // like libcore / icu4j / icu4c can find their data files. |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 277 | const char* android_art_root_from_env = getenv("ANDROID_ART_ROOT"); |
| 278 | if (android_art_root_from_env == nullptr) { |
| 279 | // Use ${ANDROID_HOST_OUT}/com.android.art for ANDROID_ART_ROOT. |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 280 | std::string android_art_root = android_host_out.c_str(); |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 281 | android_art_root += "/com.android.art"; |
| 282 | setenv("ANDROID_ART_ROOT", android_art_root.c_str(), 1); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 285 | // Environment variable ANDROID_TZDATA_ROOT is set on the device, but not |
| 286 | // 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] | 287 | // like libcore / icu4j / icu4c can find their data files. |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 288 | const char* android_tzdata_root_from_env = getenv("ANDROID_TZDATA_ROOT"); |
| 289 | if (android_tzdata_root_from_env == nullptr) { |
| 290 | // Use ${ANDROID_HOST_OUT}/com.android.tzdata for ANDROID_TZDATA_ROOT. |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 291 | std::string android_tzdata_root = android_host_out.c_str(); |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 292 | android_tzdata_root += "/com.android.tzdata"; |
| 293 | setenv("ANDROID_TZDATA_ROOT", android_tzdata_root.c_str(), 1); |
| 294 | } |
| 295 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 296 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 300 | void CommonArtTestImpl::SetUpAndroidDataDir(std::string& android_data) { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 301 | if (IsHost()) { |
| 302 | const char* tmpdir = getenv("TMPDIR"); |
| 303 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 304 | android_data = tmpdir; |
| 305 | } else { |
| 306 | android_data = "/tmp"; |
| 307 | } |
| 308 | } else { |
Roland Levillain | 7ffab81 | 2021-06-30 11:13:19 +0100 | [diff] [blame] | 309 | // On target, we cannot use `/mnt/sdcard` because it is mounted `noexec`, |
| 310 | // nor `/data/dalvik-cache` as it is not accessible on `user` builds. |
| 311 | // Instead, use `/data/local/tmp`, which does not require any special |
| 312 | // permission. |
| 313 | android_data = "/data/local/tmp"; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 314 | } |
| 315 | android_data += "/art-data-XXXXXX"; |
| 316 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 317 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 318 | } |
| 319 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 320 | } |
| 321 | |
| 322 | void CommonArtTestImpl::SetUp() { |
Alex Light | 7302a74 | 2021-02-05 09:31:14 -0800 | [diff] [blame] | 323 | // Some tests clear these and when running with --no_isolate this can cause |
| 324 | // later tests to fail |
| 325 | Locks::Init(); |
| 326 | MemMap::Init(); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 327 | SetUpAndroidRootEnvVars(); |
| 328 | SetUpAndroidDataDir(android_data_); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 329 | |
| 330 | // Re-use the data temporary directory for /system_ext tests |
| 331 | android_system_ext_.append(android_data_.c_str()); |
| 332 | android_system_ext_.append("/system_ext"); |
| 333 | int mkdir_result = mkdir(android_system_ext_.c_str(), 0700); |
| 334 | ASSERT_EQ(mkdir_result, 0); |
Victor Hsieh | 5ff45d6 | 2022-09-21 10:00:11 -0700 | [diff] [blame] | 335 | setenv("SYSTEM_EXT_ROOT", android_system_ext_.c_str(), 1); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 336 | |
| 337 | std::string system_ext_framework = android_system_ext_ + "/framework"; |
| 338 | mkdir_result = mkdir(system_ext_framework.c_str(), 0700); |
| 339 | ASSERT_EQ(mkdir_result, 0); |
| 340 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 341 | dalvik_cache_.append(android_data_.c_str()); |
| 342 | dalvik_cache_.append("/dalvik-cache"); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 343 | mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 344 | ASSERT_EQ(mkdir_result, 0); |
Andreas Gampe | 591b1d2 | 2019-06-25 13:08:16 -0700 | [diff] [blame] | 345 | |
Roland Levillain | f040914 | 2021-03-22 15:45:03 +0000 | [diff] [blame] | 346 | if (kIsDebugBuild) { |
| 347 | static bool gSlowDebugTestFlag = false; |
| 348 | RegisterRuntimeDebugFlag(&gSlowDebugTestFlag); |
| 349 | SetRuntimeDebugFlagsEnabled(true); |
| 350 | CHECK(gSlowDebugTestFlag); |
| 351 | } |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 354 | void CommonArtTestImpl::TearDownAndroidDataDir(const std::string& android_data, |
| 355 | bool fail_on_error) { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 356 | if (fail_on_error) { |
| 357 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 358 | } else { |
| 359 | rmdir(android_data.c_str()); |
| 360 | } |
| 361 | } |
| 362 | |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 363 | // Get prebuilt binary tool. |
| 364 | // The paths need to be updated when Android prebuilts update. |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 365 | std::string CommonArtTestImpl::GetAndroidTool(const char* name, InstructionSet) { |
David Srbecky | c607092 | 2020-07-16 11:06:32 +0100 | [diff] [blame] | 366 | #ifndef ART_CLANG_PATH |
David Srbecky | 7400a54 | 2020-07-09 13:40:57 +0100 | [diff] [blame] | 367 | UNUSED(name); |
David Srbecky | c607092 | 2020-07-16 11:06:32 +0100 | [diff] [blame] | 368 | LOG(FATAL) << "There are no prebuilt tools available."; |
David Srbecky | 7400a54 | 2020-07-09 13:40:57 +0100 | [diff] [blame] | 369 | UNREACHABLE(); |
| 370 | #else |
| 371 | std::string path = GetAndroidBuildTop() + ART_CLANG_PATH + "/bin/"; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 372 | CHECK(OS::DirectoryExists(path.c_str())) << path; |
| 373 | path += name; |
| 374 | CHECK(OS::FileExists(path.c_str())) << path; |
| 375 | return path; |
David Srbecky | 7400a54 | 2020-07-09 13:40:57 +0100 | [diff] [blame] | 376 | #endif |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | std::string CommonArtTestImpl::GetCoreArtLocation() { |
| 380 | return GetCoreFileLocation("art"); |
| 381 | } |
| 382 | |
| 383 | std::string CommonArtTestImpl::GetCoreOatLocation() { |
| 384 | return GetCoreFileLocation("oat"); |
| 385 | } |
| 386 | |
| 387 | std::unique_ptr<const DexFile> CommonArtTestImpl::LoadExpectSingleDexFile(const char* location) { |
| 388 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 389 | std::string error_msg; |
| 390 | MemMap::Init(); |
| 391 | static constexpr bool kVerifyChecksum = true; |
| 392 | const ArtDexFileLoader dex_file_loader; |
David Srbecky | 7400a54 | 2020-07-09 13:40:57 +0100 | [diff] [blame] | 393 | std::string filename(IsHost() ? GetAndroidBuildTop() + location : location); |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 394 | if (!dex_file_loader.Open(filename.c_str(), |
| 395 | std::string(location), |
| 396 | /* verify= */ true, |
| 397 | kVerifyChecksum, |
| 398 | &error_msg, |
| 399 | &dex_files)) { |
| 400 | LOG(FATAL) << "Could not open .dex file '" << filename << "': " << error_msg << "\n"; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 401 | UNREACHABLE(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 402 | } |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 403 | CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << filename; |
| 404 | return std::move(dex_files[0]); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) { |
| 408 | ASSERT_TRUE(dirpath != nullptr); |
| 409 | DIR* dir = opendir(dirpath); |
| 410 | ASSERT_TRUE(dir != nullptr); |
| 411 | dirent* e; |
| 412 | struct stat s; |
| 413 | while ((e = readdir(dir)) != nullptr) { |
| 414 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 415 | continue; |
| 416 | } |
| 417 | std::string filename(dirpath); |
| 418 | filename.push_back('/'); |
| 419 | filename.append(e->d_name); |
| 420 | int stat_result = lstat(filename.c_str(), &s); |
| 421 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 422 | if (S_ISDIR(s.st_mode)) { |
| 423 | if (recursive) { |
| 424 | ClearDirectory(filename.c_str()); |
| 425 | int rmdir_result = rmdir(filename.c_str()); |
| 426 | ASSERT_EQ(0, rmdir_result) << filename; |
| 427 | } |
| 428 | } else { |
| 429 | int unlink_result = unlink(filename.c_str()); |
| 430 | ASSERT_EQ(0, unlink_result) << filename; |
| 431 | } |
| 432 | } |
| 433 | closedir(dir); |
| 434 | } |
| 435 | |
| 436 | void CommonArtTestImpl::TearDown() { |
| 437 | const char* android_data = getenv("ANDROID_DATA"); |
| 438 | ASSERT_TRUE(android_data != nullptr); |
| 439 | ClearDirectory(dalvik_cache_.c_str()); |
| 440 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 441 | ASSERT_EQ(0, rmdir_cache_result); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 442 | ClearDirectory(android_system_ext_.c_str(), true); |
| 443 | rmdir_cache_result = rmdir(android_system_ext_.c_str()); |
| 444 | ASSERT_EQ(0, rmdir_cache_result); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 445 | TearDownAndroidDataDir(android_data_, true); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 446 | dalvik_cache_.clear(); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 447 | android_system_ext_.clear(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 450 | std::vector<std::string> CommonArtTestImpl::GetLibCoreModuleNames() const { |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 451 | return art::testing::GetLibCoreModuleNames(); |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 452 | } |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 453 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 454 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames( |
| 455 | const std::vector<std::string>& modules) const { |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 456 | return art::testing::GetLibCoreDexFileNames(modules); |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 459 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames() const { |
| 460 | std::vector<std::string> modules = GetLibCoreModuleNames(); |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 461 | return art::testing::GetLibCoreDexFileNames(modules); |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations( |
| 465 | const std::vector<std::string>& modules) const { |
| 466 | std::vector<std::string> result = GetLibCoreDexFileNames(modules); |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 467 | if (IsHost()) { |
David Srbecky | d31def5 | 2020-03-30 17:57:10 +0100 | [diff] [blame] | 468 | // Strip the ANDROID_BUILD_TOP directory including the directory separator '/'. |
| 469 | std::string prefix = GetAndroidBuildTop(); |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 470 | for (std::string& location : result) { |
| 471 | CHECK_GT(location.size(), prefix.size()); |
David Srbecky | 883c134 | 2020-05-11 23:30:29 +0000 | [diff] [blame] | 472 | CHECK_EQ(location.compare(0u, prefix.size(), prefix), 0) |
| 473 | << " prefix=" << prefix << " location=" << location; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 474 | location.erase(0u, prefix.size()); |
| 475 | } |
| 476 | } |
| 477 | return result; |
| 478 | } |
| 479 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 480 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations() const { |
| 481 | std::vector<std::string> modules = GetLibCoreModuleNames(); |
| 482 | return GetLibCoreDexLocations(modules); |
| 483 | } |
| 484 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 485 | std::string CommonArtTestImpl::GetClassPathOption(const char* option, |
| 486 | const std::vector<std::string>& class_path) { |
| 487 | return option + android::base::Join(class_path, ':'); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 488 | } |
| 489 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 490 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 491 | #ifdef ART_TARGET |
| 492 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 493 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 494 | #endif |
| 495 | // Wrap it as a string literal. |
| 496 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 497 | #else |
| 498 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 499 | #endif |
| 500 | |
| 501 | std::string CommonArtTestImpl::GetTestDexFileName(const char* name) const { |
| 502 | CHECK(name != nullptr); |
David Srbecky | 4a88a5a | 2020-05-05 16:21:57 +0100 | [diff] [blame] | 503 | // The needed jar files for gtest are located next to the gtest binary itself. |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 504 | std::string executable_dir = android::base::GetExecutableDirectory(); |
David Srbecky | edda3ca | 2020-07-08 00:41:08 +0100 | [diff] [blame] | 505 | for (auto ext : {".jar", ".dex"}) { |
| 506 | std::string path = executable_dir + "/art-gtest-jars-" + name + ext; |
| 507 | if (OS::FileExists(path.c_str())) { |
| 508 | return path; |
| 509 | } |
| 510 | } |
| 511 | LOG(FATAL) << "Test file " << name << " not found"; |
| 512 | UNREACHABLE(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 513 | } |
| 514 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 515 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenDexFiles(const char* filename) { |
| 516 | static constexpr bool kVerify = true; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 517 | static constexpr bool kVerifyChecksum = true; |
| 518 | std::string error_msg; |
| 519 | const ArtDexFileLoader dex_file_loader; |
| 520 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 521 | bool success = dex_file_loader.Open(filename, |
| 522 | filename, |
| 523 | kVerify, |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 524 | kVerifyChecksum, |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 525 | &error_msg, |
| 526 | &dex_files); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 527 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
| 528 | for (auto& dex_file : dex_files) { |
| 529 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 530 | CHECK(dex_file->IsReadOnly()); |
| 531 | } |
| 532 | return dex_files; |
| 533 | } |
| 534 | |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 535 | std::unique_ptr<const DexFile> CommonArtTestImpl::OpenDexFile(const char* filename) { |
| 536 | std::vector<std::unique_ptr<const DexFile>> dex_files(OpenDexFiles(filename)); |
| 537 | CHECK_EQ(dex_files.size(), 1u) << "Expected only one dex file"; |
| 538 | return std::move(dex_files[0]); |
| 539 | } |
| 540 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 541 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenTestDexFiles( |
| 542 | const char* name) { |
| 543 | return OpenDexFiles(GetTestDexFileName(name).c_str()); |
| 544 | } |
| 545 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 546 | std::unique_ptr<const DexFile> CommonArtTestImpl::OpenTestDexFile(const char* name) { |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 547 | return OpenDexFile(GetTestDexFileName(name).c_str()); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Martin Stjernholm | 9fde2db | 2020-10-15 16:03:29 +0100 | [diff] [blame] | 550 | std::string CommonArtTestImpl::GetImageDirectory() { |
Martin Stjernholm | 9fde2db | 2020-10-15 16:03:29 +0100 | [diff] [blame] | 551 | if (IsHost()) { |
| 552 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 553 | CHECK(host_dir != nullptr); |
Jiakai Zhang | a329345 | 2022-01-12 20:04:11 +0000 | [diff] [blame] | 554 | return std::string(host_dir) + "/apex/art_boot_images/javalib"; |
Martin Stjernholm | 9fde2db | 2020-10-15 16:03:29 +0100 | [diff] [blame] | 555 | } |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 556 | // On device, the boot image is generated by `generate-boot-image`. |
| 557 | // In a standalone test, the boot image is located next to the gtest binary itself. |
| 558 | std::string path = android::base::GetExecutableDirectory() + "/art_boot_images"; |
| 559 | if (OS::DirectoryExists(path.c_str())) { |
| 560 | return path; |
| 561 | } |
Jiakai Zhang | 61c0d22 | 2022-02-16 10:28:38 +0000 | [diff] [blame] | 562 | // In a chroot environment prepared by scripts, the boot image is located in a predefined |
| 563 | // location on /system. |
| 564 | path = "/system/framework/art_boot_images"; |
| 565 | if (OS::DirectoryExists(path.c_str())) { |
| 566 | return path; |
| 567 | } |
| 568 | // In art-target-gtest-chroot, the boot image is located in a predefined location on /data because |
| 569 | // /system is a mount point that replicates the real one on device. |
Jiakai Zhang | d539f86 | 2022-02-08 14:56:05 +0000 | [diff] [blame] | 570 | path = "/data/local/tmp/art_boot_images"; |
| 571 | if (OS::DirectoryExists(path.c_str())) { |
| 572 | return path; |
| 573 | } |
| 574 | LOG(FATAL) << "Boot image not found"; |
| 575 | UNREACHABLE(); |
Martin Stjernholm | 9fde2db | 2020-10-15 16:03:29 +0100 | [diff] [blame] | 576 | } |
| 577 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 578 | std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) { |
| 579 | CHECK(suffix != nullptr); |
Martin Stjernholm | 9fde2db | 2020-10-15 16:03:29 +0100 | [diff] [blame] | 580 | return GetImageDirectory() + "/boot." + suffix; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | std::string CommonArtTestImpl::CreateClassPath( |
| 584 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 585 | CHECK(!dex_files.empty()); |
| 586 | std::string classpath = dex_files[0]->GetLocation(); |
| 587 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 588 | classpath += ":" + dex_files[i]->GetLocation(); |
| 589 | } |
| 590 | return classpath; |
| 591 | } |
| 592 | |
| 593 | std::string CommonArtTestImpl::CreateClassPathWithChecksums( |
| 594 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 595 | CHECK(!dex_files.empty()); |
| 596 | std::string classpath = dex_files[0]->GetLocation() + "*" + |
| 597 | std::to_string(dex_files[0]->GetLocationChecksum()); |
| 598 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 599 | classpath += ":" + dex_files[i]->GetLocation() + "*" + |
| 600 | std::to_string(dex_files[i]->GetLocationChecksum()); |
| 601 | } |
| 602 | return classpath; |
| 603 | } |
| 604 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 605 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 606 | const std::vector<std::string>& argv, |
| 607 | const PostForkFn& post_fork, |
| 608 | const OutputHandlerFn& handler) { |
| 609 | ForkAndExecResult result; |
| 610 | result.status_code = 0; |
| 611 | result.stage = ForkAndExecResult::kLink; |
| 612 | |
| 613 | std::vector<const char*> c_args; |
Stefano Cianciulli | d59d4d1 | 2023-01-11 11:21:59 +0000 | [diff] [blame] | 614 | c_args.reserve(argv.size() + 1); |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 615 | for (const std::string& str : argv) { |
| 616 | c_args.push_back(str.c_str()); |
| 617 | } |
| 618 | c_args.push_back(nullptr); |
| 619 | |
| 620 | android::base::unique_fd link[2]; |
| 621 | { |
| 622 | int link_fd[2]; |
| 623 | |
| 624 | if (pipe(link_fd) == -1) { |
| 625 | return result; |
| 626 | } |
| 627 | link[0].reset(link_fd[0]); |
| 628 | link[1].reset(link_fd[1]); |
| 629 | } |
| 630 | |
| 631 | result.stage = ForkAndExecResult::kFork; |
| 632 | |
| 633 | pid_t pid = fork(); |
| 634 | if (pid == -1) { |
| 635 | return result; |
| 636 | } |
| 637 | |
| 638 | if (pid == 0) { |
| 639 | if (!post_fork()) { |
| 640 | LOG(ERROR) << "Failed post-fork function"; |
| 641 | exit(1); |
| 642 | UNREACHABLE(); |
| 643 | } |
| 644 | |
| 645 | // Redirect stdout and stderr. |
| 646 | dup2(link[1].get(), STDOUT_FILENO); |
| 647 | dup2(link[1].get(), STDERR_FILENO); |
| 648 | |
| 649 | link[0].reset(); |
| 650 | link[1].reset(); |
| 651 | |
| 652 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 653 | exit(1); |
| 654 | UNREACHABLE(); |
| 655 | } |
| 656 | |
| 657 | result.stage = ForkAndExecResult::kWaitpid; |
| 658 | link[1].reset(); |
| 659 | |
| 660 | char buffer[128] = { 0 }; |
| 661 | ssize_t bytes_read = 0; |
| 662 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0].get(), buffer, 128)) > 0) { |
| 663 | handler(buffer, bytes_read); |
| 664 | } |
| 665 | handler(buffer, 0u); // End with a virtual write of zero length to simplify clients. |
| 666 | |
| 667 | link[0].reset(); |
| 668 | |
| 669 | if (waitpid(pid, &result.status_code, 0) == -1) { |
| 670 | return result; |
| 671 | } |
| 672 | |
| 673 | result.stage = ForkAndExecResult::kFinished; |
| 674 | return result; |
| 675 | } |
| 676 | |
| 677 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 678 | const std::vector<std::string>& argv, const PostForkFn& post_fork, std::string* output) { |
| 679 | auto string_collect_fn = [output](char* buf, size_t len) { |
| 680 | *output += std::string(buf, len); |
| 681 | }; |
| 682 | return ForkAndExec(argv, post_fork, string_collect_fn); |
| 683 | } |
| 684 | |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 685 | std::vector<pid_t> GetPidByName(const std::string& process_name) { |
| 686 | std::vector<pid_t> results; |
| 687 | for (pid_t pid : android::base::AllPids{}) { |
Jiakai Zhang | c7ea9b1 | 2021-12-24 19:49:51 +0000 | [diff] [blame] | 688 | std::string cmdline; |
| 689 | if (!android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline)) { |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 690 | continue; |
| 691 | } |
Jiakai Zhang | c7ea9b1 | 2021-12-24 19:49:51 +0000 | [diff] [blame] | 692 | // Take the first argument. |
| 693 | size_t pos = cmdline.find('\0'); |
| 694 | if (pos != std::string::npos) { |
| 695 | cmdline.resize(pos); |
| 696 | } |
| 697 | if (cmdline == process_name) { |
Jiakai Zhang | 5514c8e | 2021-10-19 05:40:32 +0000 | [diff] [blame] | 698 | results.push_back(pid); |
| 699 | } |
| 700 | } |
| 701 | return results; |
| 702 | } |
| 703 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 704 | } // namespace art |