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