blob: 67413eb85c6f4954fac7cea4d48ebbc4560d4ff4 [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>
23#include <cstdio>
24#include "nativehelper/scoped_local_ref.h"
25
26#include "android-base/stringprintf.h"
27#include <unicode/uvernum.h>
28
29#include "art_field-inl.h"
30#include "base/file_utils.h"
31#include "base/logging.h"
32#include "base/macros.h"
33#include "base/mem_map.h"
34#include "base/mutex.h"
35#include "base/os.h"
36#include "base/runtime_debug.h"
37#include "base/stl_util.h"
38#include "base/unix_file/fd_file.h"
39#include "dex/art_dex_file_loader.h"
40#include "dex/dex_file-inl.h"
41#include "dex/dex_file_loader.h"
42#include "dex/primitive.h"
43#include "gtest/gtest.h"
44
45namespace art {
46
47using android::base::StringPrintf;
48
49ScratchFile::ScratchFile() {
50 // ANDROID_DATA needs to be set
51 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
52 "Are you subclassing RuntimeTest?";
53 filename_ = getenv("ANDROID_DATA");
54 filename_ += "/TmpFile-XXXXXX";
55 int fd = mkstemp(&filename_[0]);
56 CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_;
57 file_.reset(new File(fd, GetFilename(), true));
58}
59
60ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix)
61 : ScratchFile(other.GetFilename() + suffix) {}
62
63ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) {
64 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
65 CHECK_NE(-1, fd);
66 file_.reset(new File(fd, GetFilename(), true));
67}
68
69ScratchFile::ScratchFile(File* file) {
70 CHECK(file != nullptr);
71 filename_ = file->GetPath();
72 file_.reset(file);
73}
74
75ScratchFile::ScratchFile(ScratchFile&& other) {
76 *this = std::move(other);
77}
78
79ScratchFile& ScratchFile::operator=(ScratchFile&& other) {
80 if (GetFile() != other.GetFile()) {
81 std::swap(filename_, other.filename_);
82 std::swap(file_, other.file_);
83 }
84 return *this;
85}
86
87ScratchFile::~ScratchFile() {
88 Unlink();
89}
90
91int ScratchFile::GetFd() const {
92 return file_->Fd();
93}
94
95void ScratchFile::Close() {
96 if (file_.get() != nullptr) {
97 if (file_->FlushCloseOrErase() != 0) {
98 PLOG(WARNING) << "Error closing scratch file.";
99 }
100 }
101}
102
103void ScratchFile::Unlink() {
104 if (!OS::FileExists(filename_.c_str())) {
105 return;
106 }
107 Close();
108 int unlink_result = unlink(filename_.c_str());
109 CHECK_EQ(0, unlink_result);
110}
111
112void CommonArtTestImpl::SetUpAndroidRoot() {
113 if (IsHost()) {
114 // $ANDROID_ROOT is set on the device, but not necessarily on the host.
115 // But it needs to be set so that icu4c can find its locale data.
116 const char* android_root_from_env = getenv("ANDROID_ROOT");
117 if (android_root_from_env == nullptr) {
118 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
119 const char* android_host_out = getenv("ANDROID_HOST_OUT");
120 if (android_host_out != nullptr) {
121 setenv("ANDROID_ROOT", android_host_out, 1);
122 } else {
123 // Build it from ANDROID_BUILD_TOP or cwd
124 std::string root;
125 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
126 if (android_build_top != nullptr) {
127 root += android_build_top;
128 } else {
129 // Not set by build server, so default to current directory
130 char* cwd = getcwd(nullptr, 0);
131 setenv("ANDROID_BUILD_TOP", cwd, 1);
132 root += cwd;
133 free(cwd);
134 }
135#if defined(__linux__)
136 root += "/out/host/linux-x86";
137#elif defined(__APPLE__)
138 root += "/out/host/darwin-x86";
139#else
140#error unsupported OS
141#endif
142 setenv("ANDROID_ROOT", root.c_str(), 1);
143 }
144 }
145 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
146
147 // Not set by build server, so default
148 if (getenv("ANDROID_HOST_OUT") == nullptr) {
149 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
150 }
151 }
152}
153
154void CommonArtTestImpl::SetUpAndroidData(std::string& android_data) {
155 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
156 if (IsHost()) {
157 const char* tmpdir = getenv("TMPDIR");
158 if (tmpdir != nullptr && tmpdir[0] != 0) {
159 android_data = tmpdir;
160 } else {
161 android_data = "/tmp";
162 }
163 } else {
164 android_data = "/data/dalvik-cache";
165 }
166 android_data += "/art-data-XXXXXX";
167 if (mkdtemp(&android_data[0]) == nullptr) {
168 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
169 }
170 setenv("ANDROID_DATA", android_data.c_str(), 1);
171}
172
173void CommonArtTestImpl::SetUp() {
174 SetUpAndroidRoot();
175 SetUpAndroidData(android_data_);
176 dalvik_cache_.append(android_data_.c_str());
177 dalvik_cache_.append("/dalvik-cache");
178 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
179 ASSERT_EQ(mkdir_result, 0);
180}
181
182void CommonArtTestImpl::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
183 if (fail_on_error) {
184 ASSERT_EQ(rmdir(android_data.c_str()), 0);
185 } else {
186 rmdir(android_data.c_str());
187 }
188}
189
190// Helper - find directory with the following format:
191// ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
192std::string CommonArtTestImpl::GetAndroidToolsDir(const std::string& subdir1,
193 const std::string& subdir2,
194 const std::string& subdir3) {
195 std::string root;
196 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
197 if (android_build_top != nullptr) {
198 root = android_build_top;
199 } else {
200 // Not set by build server, so default to current directory
201 char* cwd = getcwd(nullptr, 0);
202 setenv("ANDROID_BUILD_TOP", cwd, 1);
203 root = cwd;
204 free(cwd);
205 }
206
207 std::string toolsdir = root + "/" + subdir1;
208 std::string founddir;
209 DIR* dir;
210 if ((dir = opendir(toolsdir.c_str())) != nullptr) {
211 float maxversion = 0;
212 struct dirent* entry;
213 while ((entry = readdir(dir)) != nullptr) {
214 std::string format = subdir2 + "-%f";
215 float version;
216 if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) {
217 if (version > maxversion) {
218 maxversion = version;
219 founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/";
220 }
221 }
222 }
223 closedir(dir);
224 }
225
226 if (founddir.empty()) {
227 ADD_FAILURE() << "Cannot find Android tools directory.";
228 }
229 return founddir;
230}
231
232std::string CommonArtTestImpl::GetAndroidHostToolsDir() {
233 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host",
234 "x86_64-linux-glibc2.15",
235 "x86_64-linux");
236}
237
238std::string CommonArtTestImpl::GetCoreArtLocation() {
239 return GetCoreFileLocation("art");
240}
241
242std::string CommonArtTestImpl::GetCoreOatLocation() {
243 return GetCoreFileLocation("oat");
244}
245
246std::unique_ptr<const DexFile> CommonArtTestImpl::LoadExpectSingleDexFile(const char* location) {
247 std::vector<std::unique_ptr<const DexFile>> dex_files;
248 std::string error_msg;
249 MemMap::Init();
250 static constexpr bool kVerifyChecksum = true;
251 const ArtDexFileLoader dex_file_loader;
252 if (!dex_file_loader.Open(
253 location, location, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) {
254 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
255 UNREACHABLE();
256 } else {
257 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
258 return std::move(dex_files[0]);
259 }
260}
261
262void CommonArtTestImpl::ClearDirectory(const char* dirpath, bool recursive) {
263 ASSERT_TRUE(dirpath != nullptr);
264 DIR* dir = opendir(dirpath);
265 ASSERT_TRUE(dir != nullptr);
266 dirent* e;
267 struct stat s;
268 while ((e = readdir(dir)) != nullptr) {
269 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
270 continue;
271 }
272 std::string filename(dirpath);
273 filename.push_back('/');
274 filename.append(e->d_name);
275 int stat_result = lstat(filename.c_str(), &s);
276 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
277 if (S_ISDIR(s.st_mode)) {
278 if (recursive) {
279 ClearDirectory(filename.c_str());
280 int rmdir_result = rmdir(filename.c_str());
281 ASSERT_EQ(0, rmdir_result) << filename;
282 }
283 } else {
284 int unlink_result = unlink(filename.c_str());
285 ASSERT_EQ(0, unlink_result) << filename;
286 }
287 }
288 closedir(dir);
289}
290
291void CommonArtTestImpl::TearDown() {
292 const char* android_data = getenv("ANDROID_DATA");
293 ASSERT_TRUE(android_data != nullptr);
294 ClearDirectory(dalvik_cache_.c_str());
295 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
296 ASSERT_EQ(0, rmdir_cache_result);
297 TearDownAndroidData(android_data_, true);
298 dalvik_cache_.clear();
299}
300
301static std::string GetDexFileName(const std::string& jar_prefix, bool host) {
302 std::string path;
303 if (host) {
304 const char* host_dir = getenv("ANDROID_HOST_OUT");
305 CHECK(host_dir != nullptr);
306 path = host_dir;
307 } else {
308 path = GetAndroidRoot();
309 }
310
311 std::string suffix = host
312 ? "-hostdex" // The host version.
313 : "-testdex"; // The unstripped target version.
314
315 return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str());
316}
317
318std::vector<std::string> CommonArtTestImpl::GetLibCoreDexFileNames() {
319 return std::vector<std::string>({GetDexFileName("core-oj", IsHost()),
320 GetDexFileName("core-libart", IsHost())});
321}
322
323std::string CommonArtTestImpl::GetTestAndroidRoot() {
324 if (IsHost()) {
325 const char* host_dir = getenv("ANDROID_HOST_OUT");
326 CHECK(host_dir != nullptr);
327 return host_dir;
328 }
329 return GetAndroidRoot();
330}
331
332// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
333#ifdef ART_TARGET
334#ifndef ART_TARGET_NATIVETEST_DIR
335#error "ART_TARGET_NATIVETEST_DIR not set."
336#endif
337// Wrap it as a string literal.
338#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
339#else
340#define ART_TARGET_NATIVETEST_DIR_STRING ""
341#endif
342
343std::string CommonArtTestImpl::GetTestDexFileName(const char* name) const {
344 CHECK(name != nullptr);
345 std::string filename;
346 if (IsHost()) {
347 filename += getenv("ANDROID_HOST_OUT");
348 filename += "/framework/";
349 } else {
350 filename += ART_TARGET_NATIVETEST_DIR_STRING;
351 }
352 filename += "art-gtest-";
353 filename += name;
354 filename += ".jar";
355 return filename;
356}
357
David Sehr7d432422018-05-25 10:49:02 -0700358std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenDexFiles(const char* filename) {
359 static constexpr bool kVerify = true;
David Sehrd5f8de82018-04-27 14:12:03 -0700360 static constexpr bool kVerifyChecksum = true;
361 std::string error_msg;
362 const ArtDexFileLoader dex_file_loader;
363 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr7d432422018-05-25 10:49:02 -0700364 bool success = dex_file_loader.Open(filename,
365 filename,
366 kVerify,
David Sehrd5f8de82018-04-27 14:12:03 -0700367 kVerifyChecksum,
David Sehr7d432422018-05-25 10:49:02 -0700368 &error_msg,
369 &dex_files);
David Sehrd5f8de82018-04-27 14:12:03 -0700370 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
371 for (auto& dex_file : dex_files) {
372 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
373 CHECK(dex_file->IsReadOnly());
374 }
375 return dex_files;
376}
377
David Sehr7d432422018-05-25 10:49:02 -0700378std::vector<std::unique_ptr<const DexFile>> CommonArtTestImpl::OpenTestDexFiles(
379 const char* name) {
380 return OpenDexFiles(GetTestDexFileName(name).c_str());
381}
382
David Sehrd5f8de82018-04-27 14:12:03 -0700383std::unique_ptr<const DexFile> CommonArtTestImpl::OpenTestDexFile(const char* name) {
384 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
385 EXPECT_EQ(1U, vector.size());
386 return std::move(vector[0]);
387}
388
389std::string CommonArtTestImpl::GetCoreFileLocation(const char* suffix) {
390 CHECK(suffix != nullptr);
391
392 std::string location;
393 if (IsHost()) {
394 const char* host_dir = getenv("ANDROID_HOST_OUT");
395 CHECK(host_dir != nullptr);
396 location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
397 } else {
398 location = StringPrintf("/data/art-test/core.%s", suffix);
399 }
400
401 return location;
402}
403
404std::string CommonArtTestImpl::CreateClassPath(
405 const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
406 CHECK(!dex_files.empty());
407 std::string classpath = dex_files[0]->GetLocation();
408 for (size_t i = 1; i < dex_files.size(); i++) {
409 classpath += ":" + dex_files[i]->GetLocation();
410 }
411 return classpath;
412}
413
414std::string CommonArtTestImpl::CreateClassPathWithChecksums(
415 const std::vector<std::unique_ptr<const DexFile>>& dex_files) {
416 CHECK(!dex_files.empty());
417 std::string classpath = dex_files[0]->GetLocation() + "*" +
418 std::to_string(dex_files[0]->GetLocationChecksum());
419 for (size_t i = 1; i < dex_files.size(); i++) {
420 classpath += ":" + dex_files[i]->GetLocation() + "*" +
421 std::to_string(dex_files[i]->GetLocationChecksum());
422 }
423 return classpath;
424}
425
426} // namespace art