blob: 743fcc87eb0e87d47cca145c4fb946f9f8790222 [file] [log] [blame]
Elliott Hugheseb02a122012-06-12 11:35:40 -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
Ian Rogerse63db272014-07-15 15:36:11 -070017#include "common_runtime_test.h"
18
David Srbecky3e52aa42015-04-12 07:45:18 +010019#include <cstdio>
Ian Rogerse63db272014-07-15 15:36:11 -070020#include <dirent.h>
21#include <dlfcn.h>
22#include <fcntl.h>
23#include <ScopedLocalRef.h>
Andreas Gampe369810a2015-01-14 19:53:31 -080024#include <stdlib.h>
Ian Rogerse63db272014-07-15 15:36:11 -070025
26#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027#include "android-base/stringprintf.h"
28
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "art_field-inl.h"
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -070030#include "base/macros.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "base/logging.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "base/stl_util.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "base/unix_file/fd_file.h"
34#include "class_linker.h"
35#include "compiler_callbacks.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "dex_file-inl.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070037#include "gc_root-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "gc/heap.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070039#include "gtest/gtest.h"
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070040#include "handle_scope-inl.h"
Andreas Gampe9b5cba42015-03-11 09:53:50 -070041#include "interpreter/unstarted_runtime.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070043#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070044#include "mirror/class_loader.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080045#include "mem_map.h"
Mathieu Chartiere58991b2015-10-13 07:59:34 -070046#include "native/dalvik_system_DexFile.h"
Ian Rogerse63db272014-07-15 15:36:11 -070047#include "noop_compiler_callbacks.h"
48#include "os.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070049#include "primitive.h"
Ian Rogerse63db272014-07-15 15:36:11 -070050#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070051#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070052#include "thread.h"
53#include "well_known_classes.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070054
55int main(int argc, char **argv) {
Andreas Gampe369810a2015-01-14 19:53:31 -080056 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
57 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
58 // everything else. In case you want to see all messages, comment out the line.
Nicolas Geoffraya7a47592015-11-24 09:17:30 +000059 setenv("ANDROID_LOG_TAGS", "*:e", 1);
Andreas Gampe369810a2015-01-14 19:53:31 -080060
David Sehrf57589f2016-10-17 10:09:33 -070061 art::InitLogging(argv, art::Runtime::Aborter);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070062 LOG(INFO) << "Running main() from common_runtime_test.cc...";
Elliott Hugheseb02a122012-06-12 11:35:40 -070063 testing::InitGoogleTest(&argc, argv);
64 return RUN_ALL_TESTS();
65}
Ian Rogerse63db272014-07-15 15:36:11 -070066
67namespace art {
68
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Ian Rogerse63db272014-07-15 15:36:11 -070071ScratchFile::ScratchFile() {
72 // ANDROID_DATA needs to be set
73 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
74 "Are you subclassing RuntimeTest?";
75 filename_ = getenv("ANDROID_DATA");
76 filename_ += "/TmpFile-XXXXXX";
77 int fd = mkstemp(&filename_[0]);
Andreas Gampe29d38e72016-03-23 15:31:51 +000078 CHECK_NE(-1, fd) << strerror(errno) << " for " << filename_;
Andreas Gampe4303ba92014-11-06 01:00:46 -080079 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070080}
81
Mathieu Chartier866d8742016-09-21 15:24:18 -070082ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix)
83 : ScratchFile(other.GetFilename() + suffix) {}
84
85ScratchFile::ScratchFile(const std::string& filename) : filename_(filename) {
Ian Rogerse63db272014-07-15 15:36:11 -070086 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
87 CHECK_NE(-1, fd);
Andreas Gampe4303ba92014-11-06 01:00:46 -080088 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070089}
90
91ScratchFile::ScratchFile(File* file) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 CHECK(file != nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -070093 filename_ = file->GetPath();
94 file_.reset(file);
95}
96
Mathieu Chartier866d8742016-09-21 15:24:18 -070097ScratchFile::ScratchFile(ScratchFile&& other) {
98 *this = std::move(other);
99}
100
101ScratchFile& ScratchFile::operator=(ScratchFile&& other) {
102 if (GetFile() != other.GetFile()) {
103 std::swap(filename_, other.filename_);
104 std::swap(file_, other.file_);
105 }
106 return *this;
107}
108
Ian Rogerse63db272014-07-15 15:36:11 -0700109ScratchFile::~ScratchFile() {
110 Unlink();
111}
112
113int ScratchFile::GetFd() const {
114 return file_->Fd();
115}
116
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800117void ScratchFile::Close() {
Andreas Gampe4303ba92014-11-06 01:00:46 -0800118 if (file_.get() != nullptr) {
119 if (file_->FlushCloseOrErase() != 0) {
120 PLOG(WARNING) << "Error closing scratch file.";
121 }
122 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800123}
124
125void ScratchFile::Unlink() {
126 if (!OS::FileExists(filename_.c_str())) {
127 return;
128 }
129 Close();
Ian Rogerse63db272014-07-15 15:36:11 -0700130 int unlink_result = unlink(filename_.c_str());
131 CHECK_EQ(0, unlink_result);
132}
133
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700134static bool unstarted_initialized_ = false;
135
Mathieu Chartier91c91162016-01-15 09:48:15 -0800136CommonRuntimeTestImpl::CommonRuntimeTestImpl() {}
137
138CommonRuntimeTestImpl::~CommonRuntimeTestImpl() {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800139 // Ensure the dex files are cleaned up before the runtime.
140 loaded_dex_files_.clear();
141 runtime_.reset();
142}
Ian Rogerse63db272014-07-15 15:36:11 -0700143
Mathieu Chartier91c91162016-01-15 09:48:15 -0800144void CommonRuntimeTestImpl::SetUpAndroidRoot() {
Ian Rogerse63db272014-07-15 15:36:11 -0700145 if (IsHost()) {
146 // $ANDROID_ROOT is set on the device, but not necessarily on the host.
147 // But it needs to be set so that icu4c can find its locale data.
148 const char* android_root_from_env = getenv("ANDROID_ROOT");
149 if (android_root_from_env == nullptr) {
150 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
151 const char* android_host_out = getenv("ANDROID_HOST_OUT");
152 if (android_host_out != nullptr) {
153 setenv("ANDROID_ROOT", android_host_out, 1);
154 } else {
155 // Build it from ANDROID_BUILD_TOP or cwd
156 std::string root;
157 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
158 if (android_build_top != nullptr) {
159 root += android_build_top;
160 } else {
161 // Not set by build server, so default to current directory
162 char* cwd = getcwd(nullptr, 0);
163 setenv("ANDROID_BUILD_TOP", cwd, 1);
164 root += cwd;
165 free(cwd);
166 }
167#if defined(__linux__)
168 root += "/out/host/linux-x86";
169#elif defined(__APPLE__)
170 root += "/out/host/darwin-x86";
171#else
172#error unsupported OS
173#endif
174 setenv("ANDROID_ROOT", root.c_str(), 1);
175 }
176 }
177 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
178
179 // Not set by build server, so default
180 if (getenv("ANDROID_HOST_OUT") == nullptr) {
181 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
182 }
183 }
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700184}
Ian Rogerse63db272014-07-15 15:36:11 -0700185
Mathieu Chartier91c91162016-01-15 09:48:15 -0800186void CommonRuntimeTestImpl::SetUpAndroidData(std::string& android_data) {
Ian Rogerse63db272014-07-15 15:36:11 -0700187 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
Andreas Gampe5a79fde2014-08-06 13:12:26 -0700188 if (IsHost()) {
189 const char* tmpdir = getenv("TMPDIR");
190 if (tmpdir != nullptr && tmpdir[0] != 0) {
191 android_data = tmpdir;
192 } else {
193 android_data = "/tmp";
194 }
195 } else {
196 android_data = "/data/dalvik-cache";
197 }
198 android_data += "/art-data-XXXXXX";
Ian Rogerse63db272014-07-15 15:36:11 -0700199 if (mkdtemp(&android_data[0]) == nullptr) {
200 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
201 }
202 setenv("ANDROID_DATA", android_data.c_str(), 1);
203}
204
Mathieu Chartier91c91162016-01-15 09:48:15 -0800205void CommonRuntimeTestImpl::TearDownAndroidData(const std::string& android_data,
206 bool fail_on_error) {
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700207 if (fail_on_error) {
208 ASSERT_EQ(rmdir(android_data.c_str()), 0);
209 } else {
210 rmdir(android_data.c_str());
211 }
212}
213
David Srbecky3e52aa42015-04-12 07:45:18 +0100214// Helper - find directory with the following format:
215// ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
216static std::string GetAndroidToolsDir(const std::string& subdir1,
217 const std::string& subdir2,
218 const std::string& subdir3) {
219 std::string root;
220 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
221 if (android_build_top != nullptr) {
222 root = android_build_top;
223 } else {
224 // Not set by build server, so default to current directory
225 char* cwd = getcwd(nullptr, 0);
226 setenv("ANDROID_BUILD_TOP", cwd, 1);
227 root = cwd;
228 free(cwd);
229 }
230
231 std::string toolsdir = root + "/" + subdir1;
232 std::string founddir;
233 DIR* dir;
234 if ((dir = opendir(toolsdir.c_str())) != nullptr) {
235 float maxversion = 0;
236 struct dirent* entry;
237 while ((entry = readdir(dir)) != nullptr) {
238 std::string format = subdir2 + "-%f";
239 float version;
240 if (std::sscanf(entry->d_name, format.c_str(), &version) == 1) {
241 if (version > maxversion) {
242 maxversion = version;
243 founddir = toolsdir + "/" + entry->d_name + "/" + subdir3 + "/bin/";
244 }
245 }
246 }
247 closedir(dir);
248 }
249
250 if (founddir.empty()) {
Roland Levillain91d65e02016-01-19 15:59:16 +0000251 ADD_FAILURE() << "Cannot find Android tools directory.";
David Srbecky3e52aa42015-04-12 07:45:18 +0100252 }
253 return founddir;
254}
255
Mathieu Chartier91c91162016-01-15 09:48:15 -0800256std::string CommonRuntimeTestImpl::GetAndroidHostToolsDir() {
David Srbecky3e52aa42015-04-12 07:45:18 +0100257 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/host",
258 "x86_64-linux-glibc2.15",
259 "x86_64-linux");
260}
261
Mathieu Chartier91c91162016-01-15 09:48:15 -0800262std::string CommonRuntimeTestImpl::GetAndroidTargetToolsDir(InstructionSet isa) {
David Srbecky3e52aa42015-04-12 07:45:18 +0100263 switch (isa) {
264 case kArm:
265 case kThumb2:
266 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/arm",
267 "arm-linux-androideabi",
268 "arm-linux-androideabi");
269 case kArm64:
270 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/aarch64",
271 "aarch64-linux-android",
272 "aarch64-linux-android");
273 case kX86:
274 case kX86_64:
275 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/x86",
276 "x86_64-linux-android",
277 "x86_64-linux-android");
278 case kMips:
279 case kMips64:
280 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/mips",
281 "mips64el-linux-android",
282 "mips64el-linux-android");
283 case kNone:
284 break;
285 }
286 ADD_FAILURE() << "Invalid isa " << isa;
287 return "";
288}
289
Mathieu Chartier91c91162016-01-15 09:48:15 -0800290std::string CommonRuntimeTestImpl::GetCoreArtLocation() {
Igor Murashkin37743352014-11-13 14:38:00 -0800291 return GetCoreFileLocation("art");
292}
293
Mathieu Chartier91c91162016-01-15 09:48:15 -0800294std::string CommonRuntimeTestImpl::GetCoreOatLocation() {
Igor Murashkin37743352014-11-13 14:38:00 -0800295 return GetCoreFileLocation("oat");
296}
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700297
Mathieu Chartier91c91162016-01-15 09:48:15 -0800298std::unique_ptr<const DexFile> CommonRuntimeTestImpl::LoadExpectSingleDexFile(
299 const char* location) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800300 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700301 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800302 MemMap::Init();
Aart Bik37d6a3b2016-06-21 18:30:10 -0700303 static constexpr bool kVerifyChecksum = true;
304 if (!DexFile::Open(location, location, kVerifyChecksum, &error_msg, &dex_files)) {
Ian Rogerse63db272014-07-15 15:36:11 -0700305 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800306 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700307 } else {
308 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800309 return std::move(dex_files[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700310 }
311}
312
Mathieu Chartier91c91162016-01-15 09:48:15 -0800313void CommonRuntimeTestImpl::SetUp() {
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700314 SetUpAndroidRoot();
315 SetUpAndroidData(android_data_);
Ian Rogerse63db272014-07-15 15:36:11 -0700316 dalvik_cache_.append(android_data_.c_str());
317 dalvik_cache_.append("/dalvik-cache");
318 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
319 ASSERT_EQ(mkdir_result, 0);
320
Ian Rogerse63db272014-07-15 15:36:11 -0700321 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
322 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
323
Ian Rogerse63db272014-07-15 15:36:11 -0700324
325 RuntimeOptions options;
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000326 std::string boot_class_path_string = "-Xbootclasspath";
327 for (const std::string &core_dex_file_name : GetLibCoreDexFileNames()) {
328 boot_class_path_string += ":";
329 boot_class_path_string += core_dex_file_name;
330 }
331
Richard Uhlerc2752592015-01-02 13:28:22 -0800332 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700333 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800334 options.push_back(std::make_pair(min_heap_string, nullptr));
335 options.push_back(std::make_pair(max_heap_string, nullptr));
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700336
337 callbacks_.reset(new NoopCompilerCallbacks());
338
Ian Rogerse63db272014-07-15 15:36:11 -0700339 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800340
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700341 // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them.
342 if (callbacks_.get() != nullptr) {
343 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
344 }
345
Richard Uhler66d874d2015-01-15 09:37:19 -0800346 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700347 if (!Runtime::Create(options, false)) {
348 LOG(FATAL) << "Failed to create runtime";
349 return;
350 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800351 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700352 runtime_.reset(Runtime::Current());
353 class_linker_ = runtime_->GetClassLinker();
354 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700355
Andreas Gampea00f0122015-12-16 16:54:35 -0800356 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
357 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
358 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
359
360 // Get the boot class path from the runtime so it can be used in tests.
361 boot_class_path_ = class_linker_->GetBootClassPath();
362 ASSERT_FALSE(boot_class_path_.empty());
363 java_lang_dex_file_ = boot_class_path_[0];
364
365 FinalizeSetup();
366}
367
Mathieu Chartier91c91162016-01-15 09:48:15 -0800368void CommonRuntimeTestImpl::FinalizeSetup() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700369 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
370 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700371 if (!unstarted_initialized_) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700372 interpreter::UnstartedRuntime::Initialize();
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700373 unstarted_initialized_ = true;
374 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700375
Andreas Gampea00f0122015-12-16 16:54:35 -0800376 {
377 ScopedObjectAccess soa(Thread::Current());
378 class_linker_->RunRootClinits();
379 }
Ian Rogerse63db272014-07-15 15:36:11 -0700380
381 // We're back in native, take the opportunity to initialize well known classes.
382 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
383
384 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
385 // pool is created by the runtime.
386 runtime_->GetHeap()->CreateThreadPool();
387 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700388 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
389 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
Ian Rogerse63db272014-07-15 15:36:11 -0700390}
391
Mathieu Chartier91c91162016-01-15 09:48:15 -0800392void CommonRuntimeTestImpl::ClearDirectory(const char* dirpath) {
Alex Lighta59dd802014-07-02 16:28:08 -0700393 ASSERT_TRUE(dirpath != nullptr);
394 DIR* dir = opendir(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700395 ASSERT_TRUE(dir != nullptr);
396 dirent* e;
Alex Lighta59dd802014-07-02 16:28:08 -0700397 struct stat s;
Ian Rogerse63db272014-07-15 15:36:11 -0700398 while ((e = readdir(dir)) != nullptr) {
399 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
400 continue;
401 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700402 std::string filename(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700403 filename.push_back('/');
404 filename.append(e->d_name);
Alex Lighta59dd802014-07-02 16:28:08 -0700405 int stat_result = lstat(filename.c_str(), &s);
406 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
407 if (S_ISDIR(s.st_mode)) {
408 ClearDirectory(filename.c_str());
409 int rmdir_result = rmdir(filename.c_str());
410 ASSERT_EQ(0, rmdir_result) << filename;
411 } else {
412 int unlink_result = unlink(filename.c_str());
413 ASSERT_EQ(0, unlink_result) << filename;
414 }
Ian Rogerse63db272014-07-15 15:36:11 -0700415 }
416 closedir(dir);
Alex Lighta59dd802014-07-02 16:28:08 -0700417}
418
Mathieu Chartier91c91162016-01-15 09:48:15 -0800419void CommonRuntimeTestImpl::TearDown() {
Alex Lighta59dd802014-07-02 16:28:08 -0700420 const char* android_data = getenv("ANDROID_DATA");
421 ASSERT_TRUE(android_data != nullptr);
422 ClearDirectory(dalvik_cache_.c_str());
Ian Rogerse63db272014-07-15 15:36:11 -0700423 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
424 ASSERT_EQ(0, rmdir_cache_result);
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700425 TearDownAndroidData(android_data_, true);
Andreas Gampe3f41a012016-02-18 16:53:41 -0800426 dalvik_cache_.clear();
Ian Rogerse63db272014-07-15 15:36:11 -0700427
Ian Rogerse63db272014-07-15 15:36:11 -0700428 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
429}
430
Andreas Gampec7d4a582015-09-30 11:52:02 -0700431static std::string GetDexFileName(const std::string& jar_prefix, bool host) {
432 std::string path;
433 if (host) {
Ian Rogerse63db272014-07-15 15:36:11 -0700434 const char* host_dir = getenv("ANDROID_HOST_OUT");
435 CHECK(host_dir != nullptr);
Andreas Gampec7d4a582015-09-30 11:52:02 -0700436 path = host_dir;
437 } else {
438 path = GetAndroidRoot();
Ian Rogerse63db272014-07-15 15:36:11 -0700439 }
Andreas Gampec7d4a582015-09-30 11:52:02 -0700440
441 std::string suffix = host
442 ? "-hostdex" // The host version.
443 : "-testdex"; // The unstripped target version.
444
445 return StringPrintf("%s/framework/%s%s.jar", path.c_str(), jar_prefix.c_str(), suffix.c_str());
446}
447
Mathieu Chartier91c91162016-01-15 09:48:15 -0800448std::vector<std::string> CommonRuntimeTestImpl::GetLibCoreDexFileNames() {
Andreas Gampec7d4a582015-09-30 11:52:02 -0700449 return std::vector<std::string>({GetDexFileName("core-oj", IsHost()),
450 GetDexFileName("core-libart", IsHost())});
Ian Rogerse63db272014-07-15 15:36:11 -0700451}
452
Mathieu Chartier91c91162016-01-15 09:48:15 -0800453std::string CommonRuntimeTestImpl::GetTestAndroidRoot() {
Ian Rogerse63db272014-07-15 15:36:11 -0700454 if (IsHost()) {
455 const char* host_dir = getenv("ANDROID_HOST_OUT");
456 CHECK(host_dir != nullptr);
457 return host_dir;
458 }
459 return GetAndroidRoot();
460}
461
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700462// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
463#ifdef ART_TARGET
464#ifndef ART_TARGET_NATIVETEST_DIR
465#error "ART_TARGET_NATIVETEST_DIR not set."
466#endif
467// Wrap it as a string literal.
468#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
469#else
470#define ART_TARGET_NATIVETEST_DIR_STRING ""
471#endif
472
Andreas Gampee1459ae2016-06-29 09:36:30 -0700473std::string CommonRuntimeTestImpl::GetTestDexFileName(const char* name) const {
Ian Rogerse63db272014-07-15 15:36:11 -0700474 CHECK(name != nullptr);
475 std::string filename;
476 if (IsHost()) {
477 filename += getenv("ANDROID_HOST_OUT");
478 filename += "/framework/";
479 } else {
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700480 filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogerse63db272014-07-15 15:36:11 -0700481 }
482 filename += "art-gtest-";
483 filename += name;
484 filename += ".jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800485 return filename;
486}
487
Mathieu Chartier91c91162016-01-15 09:48:15 -0800488std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTestImpl::OpenTestDexFiles(
489 const char* name) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800490 std::string filename = GetTestDexFileName(name);
Aart Bik37d6a3b2016-06-21 18:30:10 -0700491 static constexpr bool kVerifyChecksum = true;
Ian Rogerse63db272014-07-15 15:36:11 -0700492 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800493 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -0700494 bool success = DexFile::Open(
495 filename.c_str(), filename.c_str(), kVerifyChecksum, &error_msg, &dex_files);
Ian Rogerse63db272014-07-15 15:36:11 -0700496 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800497 for (auto& dex_file : dex_files) {
Ian Rogerse63db272014-07-15 15:36:11 -0700498 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
499 CHECK(dex_file->IsReadOnly());
500 }
Ian Rogerse63db272014-07-15 15:36:11 -0700501 return dex_files;
502}
503
Mathieu Chartier91c91162016-01-15 09:48:15 -0800504std::unique_ptr<const DexFile> CommonRuntimeTestImpl::OpenTestDexFile(const char* name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800505 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700506 EXPECT_EQ(1U, vector.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800507 return std::move(vector[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700508}
509
Mathieu Chartier91c91162016-01-15 09:48:15 -0800510std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700511 std::vector<const DexFile*> ret;
512
513 ScopedObjectAccess soa(Thread::Current());
514
Mathieu Chartierc7853442015-03-27 14:35:38 -0700515 StackHandleScope<2> hs(soa.Self());
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700516 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700517 soa.Decode<mirror::ClassLoader>(jclass_loader));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700518
519 DCHECK_EQ(class_loader->GetClass(),
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700520 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700521 DCHECK_EQ(class_loader->GetParent()->GetClass(),
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700522 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700523
524 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader.
525 // We need to get the DexPathList and loop through it.
Andreas Gampe08883de2016-11-08 13:20:52 -0800526 ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700527 ArtField* dex_file_field =
Andreas Gampe08883de2016-11-08 13:20:52 -0800528 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
Mathieu Chartier3398c782016-09-30 10:27:43 -0700529 ObjPtr<mirror::Object> dex_path_list =
Andreas Gampe08883de2016-11-08 13:20:52 -0800530 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
531 GetObject(class_loader.Get());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700532 if (dex_path_list != nullptr && dex_file_field!= nullptr && cookie_field != nullptr) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700533 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
Mathieu Chartier3398c782016-09-30 10:27:43 -0700534 ObjPtr<mirror::Object> dex_elements_obj =
Andreas Gampe08883de2016-11-08 13:20:52 -0800535 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
536 GetObject(dex_path_list);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700537 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
538 // at the mCookie which is a DexFile vector.
539 if (dex_elements_obj != nullptr) {
540 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
541 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
542 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700543 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700544 if (element == nullptr) {
545 // Should never happen, fall back to java code to throw a NPE.
546 break;
547 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700548 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700549 if (dex_file != nullptr) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700550 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray();
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700551 DCHECK(long_array != nullptr);
552 int32_t long_array_size = long_array->GetLength();
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700553 for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700554 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
555 long_array->GetWithoutChecks(j)));
556 if (cp_dex_file == nullptr) {
557 LOG(WARNING) << "Null DexFile";
558 continue;
559 }
560 ret.push_back(cp_dex_file);
561 }
562 }
563 }
564 }
565 }
566
567 return ret;
568}
569
Mathieu Chartier91c91162016-01-15 09:48:15 -0800570const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700571 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
572 DCHECK(!tmp.empty());
573 const DexFile* ret = tmp[0];
574 DCHECK(ret != nullptr);
575 return ret;
576}
577
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100578jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name,
579 const char* second_dex_name) {
580 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name);
581 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name);
582 std::vector<const DexFile*> class_path;
583 CHECK_NE(0U, first_dex_files.size());
584 CHECK_NE(0U, second_dex_files.size());
585 for (auto& dex_file : first_dex_files) {
586 class_path.push_back(dex_file.get());
587 loaded_dex_files_.push_back(std::move(dex_file));
588 }
589 for (auto& dex_file : second_dex_files) {
590 class_path.push_back(dex_file.get());
591 loaded_dex_files_.push_back(std::move(dex_file));
592 }
593
594 Thread* self = Thread::Current();
595 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,
596 class_path);
597 self->SetClassLoaderOverride(class_loader);
598 return class_loader;
599}
600
Mathieu Chartier91c91162016-01-15 09:48:15 -0800601jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800602 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
603 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700604 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800605 for (auto& dex_file : dex_files) {
606 class_path.push_back(dex_file.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800607 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700608 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700609
Ian Rogers68d8b422014-07-17 11:09:10 -0700610 Thread* self = Thread::Current();
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700611 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,
Mathieu Chartier966878d2016-01-14 14:33:29 -0800612 class_path);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700613 self->SetClassLoaderOverride(class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700614 return class_loader;
615}
616
Mathieu Chartier91c91162016-01-15 09:48:15 -0800617std::string CommonRuntimeTestImpl::GetCoreFileLocation(const char* suffix) {
Igor Murashkin37743352014-11-13 14:38:00 -0800618 CHECK(suffix != nullptr);
619
620 std::string location;
621 if (IsHost()) {
622 const char* host_dir = getenv("ANDROID_HOST_OUT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700623 CHECK(host_dir != nullptr);
Nicolas Geoffrayb76bc782016-09-14 12:33:34 +0000624 location = StringPrintf("%s/framework/core-npic.%s", host_dir, suffix);
Igor Murashkin37743352014-11-13 14:38:00 -0800625 } else {
Nicolas Geoffrayb76bc782016-09-14 12:33:34 +0000626 location = StringPrintf("/data/art-test/core-npic.%s", suffix);
Igor Murashkin37743352014-11-13 14:38:00 -0800627 }
628
629 return location;
630}
631
Ian Rogerse63db272014-07-15 15:36:11 -0700632CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700633 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700634}
635
636CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700637 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700638 EXPECT_TRUE(actual_.empty()) << actual_;
639}
640
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700641void CheckJniAbortCatcher::Check(const std::string& expected_text) {
642 Check(expected_text.c_str());
643}
644
Ian Rogerse63db272014-07-15 15:36:11 -0700645void CheckJniAbortCatcher::Check(const char* expected_text) {
646 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
647 << "Expected to find: " << expected_text << "\n"
648 << "In the output : " << actual_;
649 actual_.clear();
650}
651
652void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
653 // We use += because when we're hooking the aborts like this, multiple problems can be found.
654 *reinterpret_cast<std::string*>(data) += reason;
655}
656
657} // namespace art
658
659namespace std {
660
661template <typename T>
662std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
663os << ::art::ToString(rhs);
664return os;
665}
666
667} // namespace std