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