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