Add CommonArtTest
Add a test framework that does not start up a Runtime object.
Bug: 78651010
Test: make -j 40 test-art-host
Change-Id: I6c8af384af5fe1289c6cf137635e94934ac3795d
diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc
index f6a5efc..f5b15ec 100644
--- a/runtime/common_runtime_test.cc
+++ b/runtime/common_runtime_test.cc
@@ -67,6 +67,7 @@
art::Locks::Init();
art::InitLogging(argv, art::Runtime::Abort);
+ art::MemMap::Init();
LOG(INFO) << "Running main() from common_runtime_test.cc...";
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
@@ -76,69 +77,6 @@
using android::base::StringPrintf;
-ScratchFile::ScratchFile() {
- // ANDROID_DATA needs to be set
- CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
- "Are you subclassing RuntimeTest?";
- filename_ = getenv("ANDROID_DATA");
- filename_ += "/TmpFile-XXXXXX";
- int fd = mkstemp(&filename_[0]);
- CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_;
- file_.reset(new File(fd, GetFilename(), true));
-}
-
-ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix)
- : ScratchFile(other.GetFilename() + suffix) {}
-
-ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) {
- int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
- CHECK_NE(-1, fd);
- file_.reset(new File(fd, GetFilename(), true));
-}
-
-ScratchFile::ScratchFile(File* file) {
- CHECK(file != nullptr);
- filename_ = file->GetPath();
- file_.reset(file);
-}
-
-ScratchFile::ScratchFile(ScratchFile&& other) {
- *this = std::move(other);
-}
-
-ScratchFile& ScratchFile::operator=(ScratchFile&& other) {
- if (GetFile() != other.GetFile()) {
- std::swap(filename_, other.filename_);
- std::swap(file_, other.file_);
- }
- return *this;
-}
-
-ScratchFile::~ScratchFile() {
- Unlink();
-}
-
-int ScratchFile::GetFd() const {
- return file_->Fd();
-}
-
-void ScratchFile::Close() {
- if (file_.get() != nullptr) {
- if (file_->FlushCloseOrErase() != 0) {
- PLOG(WARNING) << "Error closing scratch file.";
- }
- }
-}
-
-void ScratchFile::Unlink() {
- if (!OS::FileExists(filename_.c_str())) {
- return;
- }
- Close();
- int unlink_result = unlink(filename_.c_str());
- CHECK_EQ(0, unlink_result);
-}
-
static bool unstarted_initialized_ = false;
CommonRuntimeTestImpl::CommonRuntimeTestImpl()
@@ -151,124 +89,6 @@
runtime_.reset();
}
-void CommonRuntimeTestImpl::SetUpAndroidRoot() {
- if (IsHost()) {
- // $ANDROID_ROOT is set on the device, but not necessarily on the host.
- // But it needs to be set so that icu4c can find its locale data.
- const char* android_root_from_env = getenv("ANDROID_ROOT");
- if (android_root_from_env == nullptr) {
- // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
- const char* android_host_out = getenv("ANDROID_HOST_OUT");
- if (android_host_out != nullptr) {
- setenv("ANDROID_ROOT", android_host_out, 1);
- } else {
- // Build it from ANDROID_BUILD_TOP or cwd
- std::string root;
- const char* android_build_top = getenv("ANDROID_BUILD_TOP");
- if (android_build_top != nullptr) {
- root += android_build_top;
- } else {
- // Not set by build server, so default to current directory
- char* cwd = getcwd(nullptr, 0);
- setenv("ANDROID_BUILD_TOP", cwd, 1);
- root += cwd;
- free(cwd);
- }
-#if defined(__linux__)
- root += "/out/host/linux-x86";
-#elif defined(__APPLE__)
- root += "/out/host/darwin-x86";
-#else
-#error unsupported OS
-#endif
- setenv("ANDROID_ROOT", root.c_str(), 1);
- }
- }
- setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
-
- // Not set by build server, so default
- if (getenv("ANDROID_HOST_OUT") == nullptr) {
- setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
- }
- }
-}
-
-void CommonRuntimeTestImpl::SetUpAndroidData(std::string& android_data) {
- // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
- if (IsHost()) {
- const char* tmpdir = getenv("TMPDIR");
- if (tmpdir != nullptr && tmpdir[0] != 0) {
- android_data = tmpdir;
- } else {
- android_data = "/tmp";
- }
- } else {
- android_data = "/data/dalvik-cache";
- }
- android_data += "/art-data-XXXXXX";
- if (mkdtemp(&android_data[0]) == nullptr) {
- PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
- }
- setenv("ANDROID_DATA", android_data.c_str(), 1);
-}
-
-void CommonRuntimeTestImpl::TearDownAndroidData(const std::string& android_data,
- bool fail_on_error) {
- if (fail_on_error) {
- ASSERT_EQ(rmdir(android_data.c_str()), 0);
- } else {
- rmdir(android_data.c_str());
- }
-}
-
-// Helper - find directory with the following format:
-// ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
-static std::string GetAndroidToolsDir(const std::string& subdir1,
- const std::string& subdir2,
- const std::string& subdir3) {
- std::string root;
- const char* android_build_top = getenv("ANDROID_BUILD_TOP");
- if (android_build_top != nullptr) {
- root = android_build_top;
- } else {
- // Not set by build server, so default to current directory
- char* cwd = getcwd(nullptr, 0);
- setenv("ANDROID_BUILD_TOP", cwd, 1);
- root = cwd;
- free(cwd);
- }
-
- std::string toolsdir = root + "/" + subdir1;
- std::string founddir;
- DIR* dir;
- if ((dir = opendir(toolsdir.c_str())) != nullptr) {
- float maxversion = 0;
- struct dirent* entry;
- while ((entry = readdir(dir)) != nullptr) {
- std::string format = subdir2 + "-%f";
- float version;
- if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) {
- if (version > maxversion) {
- maxversion = version;
- founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/";
- }
- }
- }
- closedir(dir);
- }
-
- if (founddir.empty()) {
- ADD_FAILURE() << "Cannot find Android tools directory.";
- }
- return founddir;
-}
-
-std::string CommonRuntimeTestImpl::GetAndroidHostToolsDir() {
- return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host",
- "x86_64-linux-glibc2.15",
- "x86_64-linux");
-}
-
std::string CommonRuntimeTestImpl::GetAndroidTargetToolsDir(InstructionSet isa) {
switch (isa) {
case InstructionSet::kArm:
@@ -297,38 +117,8 @@
return "";
}
-std::string CommonRuntimeTestImpl::GetCoreArtLocation() {
- return GetCoreFileLocation("art");
-}
-
-std::string CommonRuntimeTestImpl::GetCoreOatLocation() {
- return GetCoreFileLocation("oat");
-}
-
-std::unique_ptr<const DexFile> CommonRuntimeTestImpl::LoadExpectSingleDexFile(
- const char* location) {
- std::vector<std::unique_ptr<const DexFile>> dex_files;
- std::string error_msg;
- MemMap::Init();
- static constexpr bool kVerifyChecksum = true;
- const ArtDexFileLoader dex_file_loader;
- if (!dex_file_loader.Open(
- location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) {
- LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
- UNREACHABLE();
- } else {
- CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
- return std::move(dex_files[0]);
- }
-}
-
void CommonRuntimeTestImpl::SetUp() {
- SetUpAndroidRoot();
- SetUpAndroidData(android_data_);
- dalvik_cache_.append(android_data_.c_str());
- dalvik_cache_.append("/dalvik-cache");
- int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
- ASSERT_EQ(mkdir_result, 0);
+ CommonArtTestImpl::SetUp();
std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
@@ -406,80 +196,13 @@
runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
}
-void CommonRuntimeTestImpl::ClearDirectory(const char* dirpath, bool recursive) {
- ASSERT_TRUE(dirpath != nullptr);
- DIR* dir = opendir(dirpath);
- ASSERT_TRUE(dir != nullptr);
- dirent* e;
- struct stat s;
- while ((e = readdir(dir)) != nullptr) {
- if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
- continue;
- }
- std::string filename(dirpath);
- filename.push_back('/');
- filename.append(e->d_name);
- int stat_result = lstat(filename.c_str(), &s);
- ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
- if (S_ISDIR(s.st_mode)) {
- if (recursive) {
- ClearDirectory(filename.c_str());
- int rmdir_result = rmdir(filename.c_str());
- ASSERT_EQ(0, rmdir_result) << filename;
- }
- } else {
- int unlink_result = unlink(filename.c_str());
- ASSERT_EQ(0, unlink_result) << filename;
- }
- }
- closedir(dir);
-}
-
void CommonRuntimeTestImpl::TearDown() {
- const char* android_data = getenv("ANDROID_DATA");
- ASSERT_TRUE(android_data != nullptr);
- ClearDirectory(dalvik_cache_.c_str());
- int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
- ASSERT_EQ(0, rmdir_cache_result);
- TearDownAndroidData(android_data_, true);
- dalvik_cache_.clear();
-
+ CommonArtTestImpl::TearDown();
if (runtime_ != nullptr) {
runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
}
}
-static std::string GetDexFileName(const std::string& jar_prefix, bool host) {
- std::string path;
- if (host) {
- const char* host_dir = getenv("ANDROID_HOST_OUT");
- CHECK(host_dir != nullptr);
- path = host_dir;
- } else {
- path = GetAndroidRoot();
- }
-
- std::string suffix = host
- ? "-hostdex" // The host version.
- : "-testdex"; // The unstripped target version.
-
- return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str());
-}
-
-std::vector<std::string> CommonRuntimeTestImpl::GetLibCoreDexFileNames() {
- return std::vector<std::string>({GetDexFileName("core-oj", IsHost()),
- GetDexFileName("core-libart", IsHost())});
-}
-
-std::string CommonRuntimeTestImpl::GetTestAndroidRoot() {
- if (IsHost()) {
- const char* host_dir = getenv("ANDROID_HOST_OUT");
- CHECK(host_dir != nullptr);
- return host_dir;
- }
- return GetAndroidRoot();
-}
-
// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
#ifdef ART_TARGET
#ifndef ART_TARGET_NATIVETEST_DIR
@@ -491,47 +214,6 @@
#define ART_TARGET_NATIVETEST_DIR_STRING ""
#endif
-std::string CommonRuntimeTestImpl::GetTestDexFileName(const char* name) const {
- CHECK(name != nullptr);
- std::string filename;
- if (IsHost()) {
- filename += getenv("ANDROID_HOST_OUT");
- filename += "/framework/";
- } else {
- filename += ART_TARGET_NATIVETEST_DIR_STRING;
- }
- filename += "art-gtest-";
- filename += name;
- filename += ".jar";
- return filename;
-}
-
-std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTestImpl::OpenTestDexFiles(
- const char* name) {
- std::string filename = GetTestDexFileName(name);
- static constexpr bool kVerifyChecksum = true;
- std::string error_msg;
- const ArtDexFileLoader dex_file_loader;
- std::vector<std::unique_ptr<const DexFile>> dex_files;
- bool success = dex_file_loader.Open(filename.c_str(),
- filename.c_str(),
- /* verify */ true,
- kVerifyChecksum,
- &error_msg, &dex_files);
- CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
- for (auto& dex_file : dex_files) {
- CHECK_EQ(PROT_READ, dex_file->GetPermissions());
- CHECK(dex_file->IsReadOnly());
- }
- return dex_files;
-}
-
-std::unique_ptr<const DexFile> CommonRuntimeTestImpl::OpenTestDexFile(const char* name) {
- std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
- EXPECT_EQ(1U, vector.size());
- return std::move(vector[0]);
-}
-
std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) {
ScopedObjectAccess soa(Thread::Current());
@@ -658,43 +340,6 @@
parent_loader);
}
-std::string CommonRuntimeTestImpl::GetCoreFileLocation(const char* suffix) {
- CHECK(suffix != nullptr);
-
- std::string location;
- if (IsHost()) {
- const char* host_dir = getenv("ANDROID_HOST_OUT");
- CHECK(host_dir != nullptr);
- location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
- } else {
- location = StringPrintf("/data/art-test/core.%s", suffix);
- }
-
- return location;
-}
-
-std::string CommonRuntimeTestImpl::CreateClassPath(
- const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
- CHECK(!dex_files.empty());
- std::string classpath = dex_files[0]->GetLocation();
- for (size_t i = 1; i < dex_files.size(); i++) {
- classpath += ":" + dex_files[i]->GetLocation();
- }
- return classpath;
-}
-
-std::string CommonRuntimeTestImpl::CreateClassPathWithChecksums(
- const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
- CHECK(!dex_files.empty());
- std::string classpath = dex_files[0]->GetLocation() + "*" +
- std::to_string(dex_files[0]->GetLocationChecksum());
- for (size_t i = 1; i < dex_files.size(); i++) {
- classpath += ":" + dex_files[i]->GetLocation() + "*" +
- std::to_string(dex_files[i]->GetLocationChecksum());
- }
- return classpath;
-}
-
void CommonRuntimeTestImpl::FillHeap(Thread* self,
ClassLinker* class_linker,
VariableSizedHandleScope* handle_scope) {