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