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" |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 27 | #include "android-base/unique_fd.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 28 | #include <unicode/uvernum.h> |
| 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) { |
| 65 | int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); |
| 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 | |
| 113 | void CommonArtTestImpl::SetUpAndroidRoot() { |
| 114 | if (IsHost()) { |
| 115 | // $ANDROID_ROOT is set on the device, but not necessarily on the host. |
| 116 | // But it needs to be set so that icu4c can find its locale data. |
| 117 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 118 | if (android_root_from_env == nullptr) { |
| 119 | // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. |
| 120 | const char* android_host_out = getenv("ANDROID_HOST_OUT"); |
| 121 | if (android_host_out != nullptr) { |
| 122 | setenv("ANDROID_ROOT", android_host_out, 1); |
| 123 | } else { |
| 124 | // Build it from ANDROID_BUILD_TOP or cwd |
| 125 | std::string root; |
| 126 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 127 | if (android_build_top != nullptr) { |
| 128 | root += android_build_top; |
| 129 | } else { |
| 130 | // Not set by build server, so default to current directory |
| 131 | char* cwd = getcwd(nullptr, 0); |
| 132 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 133 | root += cwd; |
| 134 | free(cwd); |
| 135 | } |
| 136 | #if defined(__linux__) |
| 137 | root += "/out/host/linux-x86"; |
| 138 | #elif defined(__APPLE__) |
| 139 | root += "/out/host/darwin-x86"; |
| 140 | #else |
| 141 | #error unsupported OS |
| 142 | #endif |
| 143 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 144 | } |
| 145 | } |
| 146 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
| 147 | |
| 148 | // Not set by build server, so default |
| 149 | if (getenv("ANDROID_HOST_OUT") == nullptr) { |
| 150 | setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void CommonArtTestImpl::SetUpAndroidData(std::string& android_data) { |
| 156 | // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache |
| 157 | if (IsHost()) { |
| 158 | const char* tmpdir = getenv("TMPDIR"); |
| 159 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 160 | android_data = tmpdir; |
| 161 | } else { |
| 162 | android_data = "/tmp"; |
| 163 | } |
| 164 | } else { |
| 165 | android_data = "/data/dalvik-cache"; |
| 166 | } |
| 167 | android_data += "/art-data-XXXXXX"; |
| 168 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 169 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 170 | } |
| 171 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 172 | } |
| 173 | |
| 174 | void CommonArtTestImpl::SetUp() { |
| 175 | SetUpAndroidRoot(); |
| 176 | SetUpAndroidData(android_data_); |
| 177 | dalvik_cache_.append(android_data_.c_str()); |
| 178 | dalvik_cache_.append("/dalvik-cache"); |
| 179 | int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
| 180 | ASSERT_EQ(mkdir_result, 0); |
| 181 | } |
| 182 | |
| 183 | void CommonArtTestImpl::TearDownAndroidData(const std::string& android_data, bool fail_on_error) { |
| 184 | if (fail_on_error) { |
| 185 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 186 | } else { |
| 187 | rmdir(android_data.c_str()); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Helper - find directory with the following format: |
| 192 | // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ |
| 193 | std::string CommonArtTestImpl::GetAndroidToolsDir(const std::string& subdir1, |
| 194 | const std::string& subdir2, |
| 195 | const std::string& subdir3) { |
| 196 | std::string root; |
| 197 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 198 | if (android_build_top != nullptr) { |
| 199 | root = android_build_top; |
| 200 | } else { |
| 201 | // Not set by build server, so default to current directory |
| 202 | char* cwd = getcwd(nullptr, 0); |
| 203 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 204 | root = cwd; |
| 205 | free(cwd); |
| 206 | } |
| 207 | |
| 208 | std::string toolsdir = root + "/" + subdir1; |
| 209 | std::string founddir; |
| 210 | DIR* dir; |
| 211 | if ((dir = opendir(toolsdir.c_str())) != nullptr) { |
| 212 | float maxversion = 0; |
| 213 | struct dirent* entry; |
| 214 | while ((entry = readdir(dir)) != nullptr) { |
| 215 | std::string format = subdir2 + "-%f"; |
| 216 | float version; |
| 217 | if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) { |
| 218 | if (version > maxversion) { |
| 219 | maxversion = version; |
| 220 | founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/"; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | closedir(dir); |
| 225 | } |
| 226 | |
| 227 | if (founddir.empty()) { |
| 228 | ADD_FAILURE() << "Cannot find Android tools directory."; |
| 229 | } |
| 230 | return founddir; |
| 231 | } |
| 232 | |
| 233 | std::string CommonArtTestImpl::GetAndroidHostToolsDir() { |
| 234 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host", |
| 235 | "x86_64-linux-glibc2.15", |
| 236 | "x86_64-linux"); |
| 237 | } |
| 238 | |
| 239 | std::string CommonArtTestImpl::GetCoreArtLocation() { |
| 240 | return GetCoreFileLocation("art"); |
| 241 | } |
| 242 | |
| 243 | std::string CommonArtTestImpl::GetCoreOatLocation() { |
| 244 | return GetCoreFileLocation("oat"); |
| 245 | } |
| 246 | |
| 247 | std::unique_ptr<const DexFile> CommonArtTestImpl::LoadExpectSingleDexFile(const char* location) { |
| 248 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 249 | std::string error_msg; |
| 250 | MemMap::Init(); |
| 251 | static constexpr bool kVerifyChecksum = true; |
| 252 | const ArtDexFileLoader dex_file_loader; |
| 253 | if (!dex_file_loader.Open( |
| 254 | location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) { |
| 255 | LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; |
| 256 | UNREACHABLE(); |
| 257 | } else { |
| 258 | CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; |
| 259 | return std::move(dex_files[0]); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) { |
| 264 | ASSERT_TRUE(dirpath != nullptr); |
| 265 | DIR* dir = opendir(dirpath); |
| 266 | ASSERT_TRUE(dir != nullptr); |
| 267 | dirent* e; |
| 268 | struct stat s; |
| 269 | while ((e = readdir(dir)) != nullptr) { |
| 270 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 271 | continue; |
| 272 | } |
| 273 | std::string filename(dirpath); |
| 274 | filename.push_back('/'); |
| 275 | filename.append(e->d_name); |
| 276 | int stat_result = lstat(filename.c_str(), &s); |
| 277 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 278 | if (S_ISDIR(s.st_mode)) { |
| 279 | if (recursive) { |
| 280 | ClearDirectory(filename.c_str()); |
| 281 | int rmdir_result = rmdir(filename.c_str()); |
| 282 | ASSERT_EQ(0, rmdir_result) << filename; |
| 283 | } |
| 284 | } else { |
| 285 | int unlink_result = unlink(filename.c_str()); |
| 286 | ASSERT_EQ(0, unlink_result) << filename; |
| 287 | } |
| 288 | } |
| 289 | closedir(dir); |
| 290 | } |
| 291 | |
| 292 | void CommonArtTestImpl::TearDown() { |
| 293 | const char* android_data = getenv("ANDROID_DATA"); |
| 294 | ASSERT_TRUE(android_data != nullptr); |
| 295 | ClearDirectory(dalvik_cache_.c_str()); |
| 296 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 297 | ASSERT_EQ(0, rmdir_cache_result); |
| 298 | TearDownAndroidData(android_data_, true); |
| 299 | dalvik_cache_.clear(); |
| 300 | } |
| 301 | |
| 302 | static std::string GetDexFileName(const std::string& jar_prefix, bool host) { |
| 303 | std::string path; |
| 304 | if (host) { |
| 305 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 306 | CHECK(host_dir != nullptr); |
| 307 | path = host_dir; |
| 308 | } else { |
| 309 | path = GetAndroidRoot(); |
| 310 | } |
| 311 | |
| 312 | std::string suffix = host |
| 313 | ? "-hostdex" // The host version. |
| 314 | : "-testdex"; // The unstripped target version. |
| 315 | |
| 316 | return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str()); |
| 317 | } |
| 318 | |
| 319 | std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames() { |
| 320 | return std::vector<std::string>({GetDexFileName("core-oj", IsHost()), |
Neil Fuller | 1391390 | 2018-08-24 14:19:06 +0100 | [diff] [blame] | 321 | GetDexFileName("core-libart", IsHost()), |
| 322 | GetDexFileName("core-simple", IsHost())}); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | std::string CommonArtTestImpl::GetTestAndroidRoot() { |
| 326 | if (IsHost()) { |
| 327 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 328 | CHECK(host_dir != nullptr); |
| 329 | return host_dir; |
| 330 | } |
| 331 | return GetAndroidRoot(); |
| 332 | } |
| 333 | |
| 334 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 335 | #ifdef ART_TARGET |
| 336 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 337 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 338 | #endif |
| 339 | // Wrap it as a string literal. |
| 340 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 341 | #else |
| 342 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 343 | #endif |
| 344 | |
| 345 | std::string CommonArtTestImpl::GetTestDexFileName(const char* name) const { |
| 346 | CHECK(name != nullptr); |
| 347 | std::string filename; |
| 348 | if (IsHost()) { |
| 349 | filename += getenv("ANDROID_HOST_OUT"); |
| 350 | filename += "/framework/"; |
| 351 | } else { |
| 352 | filename += ART_TARGET_NATIVETEST_DIR_STRING; |
| 353 | } |
| 354 | filename += "art-gtest-"; |
| 355 | filename += name; |
| 356 | filename += ".jar"; |
| 357 | return filename; |
| 358 | } |
| 359 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 360 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenDexFiles(const char* filename) { |
| 361 | static constexpr bool kVerify = true; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 362 | static constexpr bool kVerifyChecksum = true; |
| 363 | std::string error_msg; |
| 364 | const ArtDexFileLoader dex_file_loader; |
| 365 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 366 | bool success = dex_file_loader.Open(filename, |
| 367 | filename, |
| 368 | kVerify, |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 369 | kVerifyChecksum, |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 370 | &error_msg, |
| 371 | &dex_files); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 372 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
| 373 | for (auto& dex_file : dex_files) { |
| 374 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 375 | CHECK(dex_file->IsReadOnly()); |
| 376 | } |
| 377 | return dex_files; |
| 378 | } |
| 379 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 380 | std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenTestDexFiles( |
| 381 | const char* name) { |
| 382 | return OpenDexFiles(GetTestDexFileName(name).c_str()); |
| 383 | } |
| 384 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 385 | std::unique_ptr<const DexFile> CommonArtTestImpl::OpenTestDexFile(const char* name) { |
| 386 | std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name); |
| 387 | EXPECT_EQ(1U, vector.size()); |
| 388 | return std::move(vector[0]); |
| 389 | } |
| 390 | |
| 391 | std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) { |
| 392 | CHECK(suffix != nullptr); |
| 393 | |
| 394 | std::string location; |
| 395 | if (IsHost()) { |
| 396 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 397 | CHECK(host_dir != nullptr); |
| 398 | location = StringPrintf("%s/framework/core.%s", host_dir, suffix); |
| 399 | } else { |
| 400 | location = StringPrintf("/data/art-test/core.%s", suffix); |
| 401 | } |
| 402 | |
| 403 | return location; |
| 404 | } |
| 405 | |
| 406 | std::string CommonArtTestImpl::CreateClassPath( |
| 407 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 408 | CHECK(!dex_files.empty()); |
| 409 | std::string classpath = dex_files[0]->GetLocation(); |
| 410 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 411 | classpath += ":" + dex_files[i]->GetLocation(); |
| 412 | } |
| 413 | return classpath; |
| 414 | } |
| 415 | |
| 416 | std::string CommonArtTestImpl::CreateClassPathWithChecksums( |
| 417 | const std::vector<std::unique_ptr<const DexFile>>& dex_files) { |
| 418 | CHECK(!dex_files.empty()); |
| 419 | std::string classpath = dex_files[0]->GetLocation() + "*" + |
| 420 | std::to_string(dex_files[0]->GetLocationChecksum()); |
| 421 | for (size_t i = 1; i < dex_files.size(); i++) { |
| 422 | classpath += ":" + dex_files[i]->GetLocation() + "*" + |
| 423 | std::to_string(dex_files[i]->GetLocationChecksum()); |
| 424 | } |
| 425 | return classpath; |
| 426 | } |
| 427 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 428 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 429 | const std::vector<std::string>& argv, |
| 430 | const PostForkFn& post_fork, |
| 431 | const OutputHandlerFn& handler) { |
| 432 | ForkAndExecResult result; |
| 433 | result.status_code = 0; |
| 434 | result.stage = ForkAndExecResult::kLink; |
| 435 | |
| 436 | std::vector<const char*> c_args; |
| 437 | for (const std::string& str : argv) { |
| 438 | c_args.push_back(str.c_str()); |
| 439 | } |
| 440 | c_args.push_back(nullptr); |
| 441 | |
| 442 | android::base::unique_fd link[2]; |
| 443 | { |
| 444 | int link_fd[2]; |
| 445 | |
| 446 | if (pipe(link_fd) == -1) { |
| 447 | return result; |
| 448 | } |
| 449 | link[0].reset(link_fd[0]); |
| 450 | link[1].reset(link_fd[1]); |
| 451 | } |
| 452 | |
| 453 | result.stage = ForkAndExecResult::kFork; |
| 454 | |
| 455 | pid_t pid = fork(); |
| 456 | if (pid == -1) { |
| 457 | return result; |
| 458 | } |
| 459 | |
| 460 | if (pid == 0) { |
| 461 | if (!post_fork()) { |
| 462 | LOG(ERROR) << "Failed post-fork function"; |
| 463 | exit(1); |
| 464 | UNREACHABLE(); |
| 465 | } |
| 466 | |
| 467 | // Redirect stdout and stderr. |
| 468 | dup2(link[1].get(), STDOUT_FILENO); |
| 469 | dup2(link[1].get(), STDERR_FILENO); |
| 470 | |
| 471 | link[0].reset(); |
| 472 | link[1].reset(); |
| 473 | |
| 474 | execv(c_args[0], const_cast<char* const*>(c_args.data())); |
| 475 | exit(1); |
| 476 | UNREACHABLE(); |
| 477 | } |
| 478 | |
| 479 | result.stage = ForkAndExecResult::kWaitpid; |
| 480 | link[1].reset(); |
| 481 | |
| 482 | char buffer[128] = { 0 }; |
| 483 | ssize_t bytes_read = 0; |
| 484 | while (TEMP_FAILURE_RETRY(bytes_read = read(link[0].get(), buffer, 128)) > 0) { |
| 485 | handler(buffer, bytes_read); |
| 486 | } |
| 487 | handler(buffer, 0u); // End with a virtual write of zero length to simplify clients. |
| 488 | |
| 489 | link[0].reset(); |
| 490 | |
| 491 | if (waitpid(pid, &result.status_code, 0) == -1) { |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | result.stage = ForkAndExecResult::kFinished; |
| 496 | return result; |
| 497 | } |
| 498 | |
| 499 | CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec( |
| 500 | const std::vector<std::string>& argv, const PostForkFn& post_fork, std::string* output) { |
| 501 | auto string_collect_fn = [output](char* buf, size_t len) { |
| 502 | *output += std::string(buf, len); |
| 503 | }; |
| 504 | return ForkAndExec(argv, post_fork, string_collect_fn); |
| 505 | } |
| 506 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 507 | } // namespace art |