blob: e2e85f2b6fb1279d74189d49d2b5de43c266855d [file] [log] [blame]
David Sehrd5f8de82018-04-27 14:12:03 -07001/*
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 Light55153102019-05-08 17:03:10 -070023#include <unistd.h>
David Sehrd5f8de82018-04-27 14:12:03 -070024#include <cstdio>
Alex Light55153102019-05-08 17:03:10 -070025#include <filesystem>
26#include "android-base/file.h"
27#include "android-base/logging.h"
David Sehrd5f8de82018-04-27 14:12:03 -070028#include "nativehelper/scoped_local_ref.h"
29
30#include "android-base/stringprintf.h"
Vladimir Marko7a85e702018-12-03 18:47:23 +000031#include "android-base/strings.h"
Andreas Gampe38aa0b52018-07-10 23:26:55 -070032#include "android-base/unique_fd.h"
David Sehrd5f8de82018-04-27 14:12:03 -070033
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
50namespace art {
51
52using android::base::StringPrintf;
53
54ScratchFile::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
65ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix)
66 : ScratchFile(other.GetFilename() + suffix) {}
67
68ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) {
Andreas Gampedfcd82c2018-10-16 20:22:37 -070069 int fd = open(filename_.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
David Sehrd5f8de82018-04-27 14:12:03 -070070 CHECK_NE(-1, fd);
71 file_.reset(new File(fd, GetFilename(), true));
72}
73
74ScratchFile::ScratchFile(File* file) {
75 CHECK(file != nullptr);
76 filename_ = file->GetPath();
77 file_.reset(file);
78}
79
Andreas Gampe44b31742018-10-01 19:30:57 -070080ScratchFile::ScratchFile(ScratchFile&& other) noexcept {
David Sehrd5f8de82018-04-27 14:12:03 -070081 *this = std::move(other);
82}
83
Andreas Gampe44b31742018-10-01 19:30:57 -070084ScratchFile& ScratchFile::operator=(ScratchFile&& other) noexcept {
David Sehrd5f8de82018-04-27 14:12:03 -070085 if (GetFile() != other.GetFile()) {
86 std::swap(filename_, other.filename_);
87 std::swap(file_, other.file_);
88 }
89 return *this;
90}
91
92ScratchFile::~ScratchFile() {
93 Unlink();
94}
95
96int ScratchFile::GetFd() const {
97 return file_->Fd();
98}
99
100void ScratchFile::Close() {
101 if (file_.get() != nullptr) {
102 if (file_->FlushCloseOrErase() != 0) {
103 PLOG(WARNING) << "Error closing scratch file.";
104 }
105 }
106}
107
108void 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 Fuller26c43772018-11-23 17:56:43 +0000117void CommonArtTestImpl::SetUpAndroidRootEnvVars() {
David Sehrd5f8de82018-04-27 14:12:03 -0700118 if (IsHost()) {
Neil Fuller26c43772018-11-23 17:56:43 +0000119 // 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 Light55153102019-05-08 17:03:10 -0700133 std::string android_host_out;
David Sehrd5f8de82018-04-27 14:12:03 -0700134#if defined(__linux__)
Alex Light55153102019-05-08 17:03:10 -0700135 // 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 Sehrd5f8de82018-04-27 14:12:03 -0700158#elif defined(__APPLE__)
Alex Light55153102019-05-08 17:03:10 -0700159 android_host_out = std::string(android_build_top_from_env) + "/out/host/darwin-x86";
David Sehrd5f8de82018-04-27 14:12:03 -0700160#else
161#error unsupported OS
162#endif
Neil Fuller26c43772018-11-23 17:56:43 +0000163 setenv("ANDROID_HOST_OUT", android_host_out.c_str(), 1);
164 android_host_out_from_env = getenv("ANDROID_HOST_OUT");
David Sehrd5f8de82018-04-27 14:12:03 -0700165 }
David Sehrd5f8de82018-04-27 14:12:03 -0700166
Neil Fuller26c43772018-11-23 17:56:43 +0000167 // 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 Sehrd5f8de82018-04-27 14:12:03 -0700174 }
Neil Fuller26c43772018-11-23 17:56:43 +0000175
176 // Environment variable ANDROID_RUNTIME_ROOT is set on the device, but not
177 // necessarily on the host. It needs to be set so that various libraries
Neil Fuller90ffe122019-06-06 17:25:48 +0100178 // like libcore / icu4j / icu4c can find their data files.
Neil Fuller26c43772018-11-23 17:56:43 +0000179 const char* android_runtime_root_from_env = getenv("ANDROID_RUNTIME_ROOT");
180 if (android_runtime_root_from_env == nullptr) {
181 // Use ${ANDROID_HOST_OUT}/com.android.runtime for ANDROID_RUNTIME_ROOT.
182 std::string android_runtime_root = android_host_out_from_env;
183 android_runtime_root += "/com.android.runtime";
184 setenv("ANDROID_RUNTIME_ROOT", android_runtime_root.c_str(), 1);
185 }
186
Neil Fuller26a5dd62019-03-13 15:16:35 +0000187 // Environment variable ANDROID_TZDATA_ROOT is set on the device, but not
188 // necessarily on the host. It needs to be set so that various libraries
Neil Fuller90ffe122019-06-06 17:25:48 +0100189 // like libcore / icu4j / icu4c can find their data files.
Neil Fuller26a5dd62019-03-13 15:16:35 +0000190 const char* android_tzdata_root_from_env = getenv("ANDROID_TZDATA_ROOT");
191 if (android_tzdata_root_from_env == nullptr) {
192 // Use ${ANDROID_HOST_OUT}/com.android.tzdata for ANDROID_TZDATA_ROOT.
193 std::string android_tzdata_root = android_host_out_from_env;
194 android_tzdata_root += "/com.android.tzdata";
195 setenv("ANDROID_TZDATA_ROOT", android_tzdata_root.c_str(), 1);
196 }
197
Neil Fuller26c43772018-11-23 17:56:43 +0000198 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
David Sehrd5f8de82018-04-27 14:12:03 -0700199 }
200}
201
Neil Fuller26c43772018-11-23 17:56:43 +0000202void CommonArtTestImpl::SetUpAndroidDataDir(std::string& android_data) {
David Sehrd5f8de82018-04-27 14:12:03 -0700203 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
204 if (IsHost()) {
205 const char* tmpdir = getenv("TMPDIR");
206 if (tmpdir != nullptr && tmpdir[0] != 0) {
207 android_data = tmpdir;
208 } else {
209 android_data = "/tmp";
210 }
211 } else {
212 android_data = "/data/dalvik-cache";
213 }
214 android_data += "/art-data-XXXXXX";
215 if (mkdtemp(&android_data[0]) == nullptr) {
216 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
217 }
218 setenv("ANDROID_DATA", android_data.c_str(), 1);
219}
220
221void CommonArtTestImpl::SetUp() {
Neil Fuller26c43772018-11-23 17:56:43 +0000222 SetUpAndroidRootEnvVars();
223 SetUpAndroidDataDir(android_data_);
David Sehrd5f8de82018-04-27 14:12:03 -0700224 dalvik_cache_.append(android_data_.c_str());
225 dalvik_cache_.append("/dalvik-cache");
226 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
227 ASSERT_EQ(mkdir_result, 0);
Andreas Gampe591b1d22019-06-25 13:08:16 -0700228
229 static bool gSlowDebugTestFlag = false;
230 RegisterRuntimeDebugFlag(&gSlowDebugTestFlag);
231 SetRuntimeDebugFlagsEnabled(true);
232 CHECK(gSlowDebugTestFlag);
David Sehrd5f8de82018-04-27 14:12:03 -0700233}
234
Neil Fuller26c43772018-11-23 17:56:43 +0000235void CommonArtTestImpl::TearDownAndroidDataDir(const std::string& android_data,
236 bool fail_on_error) {
David Sehrd5f8de82018-04-27 14:12:03 -0700237 if (fail_on_error) {
238 ASSERT_EQ(rmdir(android_data.c_str()), 0);
239 } else {
240 rmdir(android_data.c_str());
241 }
242}
243
244// Helper - find directory with the following format:
245// ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
246std::string CommonArtTestImpl::GetAndroidToolsDir(const std::string& subdir1,
247 const std::string& subdir2,
248 const std::string& subdir3) {
249 std::string root;
250 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
251 if (android_build_top != nullptr) {
252 root = android_build_top;
253 } else {
254 // Not set by build server, so default to current directory
255 char* cwd = getcwd(nullptr, 0);
256 setenv("ANDROID_BUILD_TOP", cwd, 1);
257 root = cwd;
258 free(cwd);
259 }
260
261 std::string toolsdir = root + "/" + subdir1;
262 std::string founddir;
263 DIR* dir;
264 if ((dir = opendir(toolsdir.c_str())) != nullptr) {
265 float maxversion = 0;
266 struct dirent* entry;
267 while ((entry = readdir(dir)) != nullptr) {
268 std::string format = subdir2 + "-%f";
269 float version;
270 if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) {
271 if (version > maxversion) {
272 maxversion = version;
273 founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/";
274 }
275 }
276 }
277 closedir(dir);
278 }
279
280 if (founddir.empty()) {
281 ADD_FAILURE() << "Cannot find Android tools directory.";
282 }
283 return founddir;
284}
285
286std::string CommonArtTestImpl::GetAndroidHostToolsDir() {
287 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host",
Nicolas Geoffrayde3e70f2019-02-22 12:07:29 +0000288 "x86_64-linux-glibc2.17",
David Sehrd5f8de82018-04-27 14:12:03 -0700289 "x86_64-linux");
290}
291
292std::string CommonArtTestImpl::GetCoreArtLocation() {
293 return GetCoreFileLocation("art");
294}
295
296std::string CommonArtTestImpl::GetCoreOatLocation() {
297 return GetCoreFileLocation("oat");
298}
299
300std::unique_ptr<const DexFile> CommonArtTestImpl::LoadExpectSingleDexFile(const char* location) {
301 std::vector<std::unique_ptr<const DexFile>> dex_files;
302 std::string error_msg;
303 MemMap::Init();
304 static constexpr bool kVerifyChecksum = true;
305 const ArtDexFileLoader dex_file_loader;
306 if (!dex_file_loader.Open(
Andreas Gampe0de385f2018-10-11 11:11:13 -0700307 location, location, /* verify= */ true, kVerifyChecksum, &error_msg, &dex_files)) {
David Sehrd5f8de82018-04-27 14:12:03 -0700308 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
309 UNREACHABLE();
310 } else {
311 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
312 return std::move(dex_files[0]);
313 }
314}
315
316void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) {
317 ASSERT_TRUE(dirpath != nullptr);
318 DIR* dir = opendir(dirpath);
319 ASSERT_TRUE(dir != nullptr);
320 dirent* e;
321 struct stat s;
322 while ((e = readdir(dir)) != nullptr) {
323 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
324 continue;
325 }
326 std::string filename(dirpath);
327 filename.push_back('/');
328 filename.append(e->d_name);
329 int stat_result = lstat(filename.c_str(), &s);
330 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
331 if (S_ISDIR(s.st_mode)) {
332 if (recursive) {
333 ClearDirectory(filename.c_str());
334 int rmdir_result = rmdir(filename.c_str());
335 ASSERT_EQ(0, rmdir_result) << filename;
336 }
337 } else {
338 int unlink_result = unlink(filename.c_str());
339 ASSERT_EQ(0, unlink_result) << filename;
340 }
341 }
342 closedir(dir);
343}
344
345void CommonArtTestImpl::TearDown() {
346 const char* android_data = getenv("ANDROID_DATA");
347 ASSERT_TRUE(android_data != nullptr);
348 ClearDirectory(dalvik_cache_.c_str());
349 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
350 ASSERT_EQ(0, rmdir_cache_result);
Neil Fuller26c43772018-11-23 17:56:43 +0000351 TearDownAndroidDataDir(android_data_, true);
David Sehrd5f8de82018-04-27 14:12:03 -0700352 dalvik_cache_.clear();
353}
354
355static std::string GetDexFileName(const std::string& jar_prefix, bool host) {
Roland Levillaindd8e2222019-04-05 15:31:55 +0100356 std::string path = GetAndroidRoot();
David Sehrd5f8de82018-04-27 14:12:03 -0700357
358 std::string suffix = host
359 ? "-hostdex" // The host version.
360 : "-testdex"; // The unstripped target version.
361
362 return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str());
363}
364
Vladimir Markob9c29f62019-03-20 14:22:51 +0000365std::vector<std::string> CommonArtTestImpl::GetLibCoreModuleNames() const {
Vladimir Marko0ace5632018-12-14 11:11:47 +0000366 // Note: This must start with the CORE_IMG_JARS in Android.common_path.mk
Vladimir Marko7a85e702018-12-03 18:47:23 +0000367 // because that's what we use for compiling the core.art image.
Vladimir Marko0ace5632018-12-14 11:11:47 +0000368 // It may contain additional modules from TEST_CORE_JARS.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000369 return {
Vladimir Marko0ace5632018-12-14 11:11:47 +0000370 // CORE_IMG_JARS modules.
Vladimir Marko7a85e702018-12-03 18:47:23 +0000371 "core-oj",
372 "core-libart",
Vladimir Marko7a85e702018-12-03 18:47:23 +0000373 "okhttp",
374 "bouncycastle",
Vladimir Markoea65b212018-12-13 13:32:13 +0000375 "apache-xml",
Vladimir Marko0ace5632018-12-14 11:11:47 +0000376 // Additional modules.
377 "conscrypt",
Vladimir Marko7a85e702018-12-03 18:47:23 +0000378 };
Vladimir Markob9c29f62019-03-20 14:22:51 +0000379}
Vladimir Marko7a85e702018-12-03 18:47:23 +0000380
Vladimir Markob9c29f62019-03-20 14:22:51 +0000381std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames(
382 const std::vector<std::string>& modules) const {
Vladimir Marko7a85e702018-12-03 18:47:23 +0000383 std::vector<std::string> result;
Vladimir Markob9c29f62019-03-20 14:22:51 +0000384 result.reserve(modules.size());
385 for (const std::string& module : modules) {
Vladimir Marko7a85e702018-12-03 18:47:23 +0000386 result.push_back(GetDexFileName(module, IsHost()));
387 }
388 return result;
389}
390
Vladimir Markob9c29f62019-03-20 14:22:51 +0000391std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames() const {
392 std::vector<std::string> modules = GetLibCoreModuleNames();
393 return GetLibCoreDexFileNames(modules);
394}
395
396std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations(
397 const std::vector<std::string>& modules) const {
398 std::vector<std::string> result = GetLibCoreDexFileNames(modules);
Vladimir Marko7a85e702018-12-03 18:47:23 +0000399 if (IsHost()) {
400 // Strip the ANDROID_BUILD_TOP directory including the directory separator '/'.
401 const char* host_dir = getenv("ANDROID_BUILD_TOP");
402 CHECK(host_dir != nullptr);
403 std::string prefix = host_dir;
404 CHECK(!prefix.empty());
405 if (prefix.back() != '/') {
406 prefix += '/';
407 }
408 for (std::string& location : result) {
409 CHECK_GT(location.size(), prefix.size());
410 CHECK_EQ(location.compare(0u, prefix.size(), prefix), 0);
411 location.erase(0u, prefix.size());
412 }
413 }
414 return result;
415}
416
Vladimir Markob9c29f62019-03-20 14:22:51 +0000417std::vector<std::string> CommonArtTestImpl::GetLibCoreDexLocations() const {
418 std::vector<std::string> modules = GetLibCoreModuleNames();
419 return GetLibCoreDexLocations(modules);
420}
421
Vladimir Marko7a85e702018-12-03 18:47:23 +0000422std::string CommonArtTestImpl::GetClassPathOption(const char* option,
423 const std::vector<std::string>& class_path) {
424 return option + android::base::Join(class_path, ':');
David Sehrd5f8de82018-04-27 14:12:03 -0700425}
426
David Sehrd5f8de82018-04-27 14:12:03 -0700427// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
428#ifdef ART_TARGET
429#ifndef ART_TARGET_NATIVETEST_DIR
430#error "ART_TARGET_NATIVETEST_DIR not set."
431#endif
432// Wrap it as a string literal.
433#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
434#else
435#define ART_TARGET_NATIVETEST_DIR_STRING ""
436#endif
437
438std::string CommonArtTestImpl::GetTestDexFileName(const char* name) const {
439 CHECK(name != nullptr);
440 std::string filename;
441 if (IsHost()) {
Roland Levillaindd8e2222019-04-05 15:31:55 +0100442 filename += GetAndroidRoot() + "/framework/";
David Sehrd5f8de82018-04-27 14:12:03 -0700443 } else {
444 filename += ART_TARGET_NATIVETEST_DIR_STRING;
445 }
446 filename += "art-gtest-";
447 filename += name;
448 filename += ".jar";
449 return filename;
450}
451
David Sehr7d432422018-05-25 10:49:02 -0700452std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenDexFiles(const char* filename) {
453 static constexpr bool kVerify = true;
David Sehrd5f8de82018-04-27 14:12:03 -0700454 static constexpr bool kVerifyChecksum = true;
455 std::string error_msg;
456 const ArtDexFileLoader dex_file_loader;
457 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7d432422018-05-25 10:49:02 -0700458 bool success = dex_file_loader.Open(filename,
459 filename,
460 kVerify,
David Sehrd5f8de82018-04-27 14:12:03 -0700461 kVerifyChecksum,
David Sehr7d432422018-05-25 10:49:02 -0700462 &error_msg,
463 &dex_files);
David Sehrd5f8de82018-04-27 14:12:03 -0700464 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
465 for (auto& dex_file : dex_files) {
466 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
467 CHECK(dex_file->IsReadOnly());
468 }
469 return dex_files;
470}
471
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700472std::unique_ptr<const DexFile> CommonArtTestImpl::OpenDexFile(const char* filename) {
473 std::vector<std::unique_ptr<const DexFile>> dex_files(OpenDexFiles(filename));
474 CHECK_EQ(dex_files.size(), 1u) << "Expected only one dex file";
475 return std::move(dex_files[0]);
476}
477
David Sehr7d432422018-05-25 10:49:02 -0700478std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenTestDexFiles(
479 const char* name) {
480 return OpenDexFiles(GetTestDexFileName(name).c_str());
481}
482
David Sehrd5f8de82018-04-27 14:12:03 -0700483std::unique_ptr<const DexFile> CommonArtTestImpl::OpenTestDexFile(const char* name) {
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700484 return OpenDexFile(GetTestDexFileName(name).c_str());
David Sehrd5f8de82018-04-27 14:12:03 -0700485}
486
487std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) {
488 CHECK(suffix != nullptr);
489
490 std::string location;
491 if (IsHost()) {
Roland Levillaindd8e2222019-04-05 15:31:55 +0100492 std::string host_dir = GetAndroidRoot();
493 location = StringPrintf("%s/framework/core.%s", host_dir.c_str(), suffix);
David Sehrd5f8de82018-04-27 14:12:03 -0700494 } else {
495 location = StringPrintf("/data/art-test/core.%s", suffix);
496 }
497
498 return location;
499}
500
501std::string CommonArtTestImpl::CreateClassPath(
502 const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
503 CHECK(!dex_files.empty());
504 std::string classpath = dex_files[0]->GetLocation();
505 for (size_t i = 1; i < dex_files.size(); i++) {
506 classpath += ":" + dex_files[i]->GetLocation();
507 }
508 return classpath;
509}
510
511std::string CommonArtTestImpl::CreateClassPathWithChecksums(
512 const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
513 CHECK(!dex_files.empty());
514 std::string classpath = dex_files[0]->GetLocation() + "*" +
515 std::to_string(dex_files[0]->GetLocationChecksum());
516 for (size_t i = 1; i < dex_files.size(); i++) {
517 classpath += ":" + dex_files[i]->GetLocation() + "*" +
518 std::to_string(dex_files[i]->GetLocationChecksum());
519 }
520 return classpath;
521}
522
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700523CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec(
524 const std::vector<std::string>& argv,
525 const PostForkFn& post_fork,
526 const OutputHandlerFn& handler) {
527 ForkAndExecResult result;
528 result.status_code = 0;
529 result.stage = ForkAndExecResult::kLink;
530
531 std::vector<const char*> c_args;
532 for (const std::string& str : argv) {
533 c_args.push_back(str.c_str());
534 }
535 c_args.push_back(nullptr);
536
537 android::base::unique_fd link[2];
538 {
539 int link_fd[2];
540
541 if (pipe(link_fd) == -1) {
542 return result;
543 }
544 link[0].reset(link_fd[0]);
545 link[1].reset(link_fd[1]);
546 }
547
548 result.stage = ForkAndExecResult::kFork;
549
550 pid_t pid = fork();
551 if (pid == -1) {
552 return result;
553 }
554
555 if (pid == 0) {
556 if (!post_fork()) {
557 LOG(ERROR) << "Failed post-fork function";
558 exit(1);
559 UNREACHABLE();
560 }
561
562 // Redirect stdout and stderr.
563 dup2(link[1].get(), STDOUT_FILENO);
564 dup2(link[1].get(), STDERR_FILENO);
565
566 link[0].reset();
567 link[1].reset();
568
569 execv(c_args[0], const_cast<char* const*>(c_args.data()));
570 exit(1);
571 UNREACHABLE();
572 }
573
574 result.stage = ForkAndExecResult::kWaitpid;
575 link[1].reset();
576
577 char buffer[128] = { 0 };
578 ssize_t bytes_read = 0;
579 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0].get(), buffer, 128)) > 0) {
580 handler(buffer, bytes_read);
581 }
582 handler(buffer, 0u); // End with a virtual write of zero length to simplify clients.
583
584 link[0].reset();
585
586 if (waitpid(pid, &result.status_code, 0) == -1) {
587 return result;
588 }
589
590 result.stage = ForkAndExecResult::kFinished;
591 return result;
592}
593
594CommonArtTestImpl::ForkAndExecResult CommonArtTestImpl::ForkAndExec(
595 const std::vector<std::string>& argv, const PostForkFn& post_fork, std::string* output) {
596 auto string_collect_fn = [output](char* buf, size_t len) {
597 *output += std::string(buf, len);
598 };
599 return ForkAndExec(argv, post_fork, string_collect_fn);
600}
601
David Sehrd5f8de82018-04-27 14:12:03 -0700602} // namespace art