blob: 4104509608b4ef24ba1b1735e9119af835e07b91 [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
19#include <dirent.h>
20#include <dlfcn.h>
21#include <fcntl.h>
22#include <ScopedLocalRef.h>
Andreas Gampe369810a2015-01-14 19:53:31 -080023#include <stdlib.h>
Ian Rogerse63db272014-07-15 15:36:11 -070024
25#include "../../external/icu/icu4c/source/common/unicode/uvernum.h"
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -070026#include "base/macros.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080027#include "base/logging.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "base/stl_util.h"
29#include "base/stringprintf.h"
30#include "base/unix_file/fd_file.h"
31#include "class_linker.h"
32#include "compiler_callbacks.h"
33#include "dex_file.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070034#include "gc_root-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "gc/heap.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070036#include "gtest/gtest.h"
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070037#include "handle_scope-inl.h"
Andreas Gampe9b5cba42015-03-11 09:53:50 -070038#include "interpreter/unstarted_runtime.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "jni_internal.h"
40#include "mirror/class_loader.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080041#include "mem_map.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "noop_compiler_callbacks.h"
43#include "os.h"
44#include "runtime-inl.h"
45#include "scoped_thread_state_change.h"
46#include "thread.h"
47#include "well_known_classes.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070048
49int main(int argc, char **argv) {
Andreas Gampe369810a2015-01-14 19:53:31 -080050 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
51 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
52 // everything else. In case you want to see all messages, comment out the line.
Richard Uhler892fc962015-03-10 16:57:05 +000053 setenv("ANDROID_LOG_TAGS", "*:e", 1);
Andreas Gampe369810a2015-01-14 19:53:31 -080054
Elliott Hugheseb02a122012-06-12 11:35:40 -070055 art::InitLogging(argv);
Ian Rogersc7dd2952014-10-21 23:31:19 -070056 LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
Elliott Hugheseb02a122012-06-12 11:35:40 -070057 testing::InitGoogleTest(&argc, argv);
58 return RUN_ALL_TESTS();
59}
Ian Rogerse63db272014-07-15 15:36:11 -070060
61namespace art {
62
63ScratchFile::ScratchFile() {
64 // ANDROID_DATA needs to be set
65 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
66 "Are you subclassing RuntimeTest?";
67 filename_ = getenv("ANDROID_DATA");
68 filename_ += "/TmpFile-XXXXXX";
69 int fd = mkstemp(&filename_[0]);
70 CHECK_NE(-1, fd);
Andreas Gampe4303ba92014-11-06 01:00:46 -080071 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070072}
73
74ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
75 filename_ = other.GetFilename();
76 filename_ += suffix;
77 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
78 CHECK_NE(-1, fd);
Andreas Gampe4303ba92014-11-06 01:00:46 -080079 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070080}
81
82ScratchFile::ScratchFile(File* file) {
83 CHECK(file != NULL);
84 filename_ = file->GetPath();
85 file_.reset(file);
86}
87
88ScratchFile::~ScratchFile() {
89 Unlink();
90}
91
92int ScratchFile::GetFd() const {
93 return file_->Fd();
94}
95
Andreas Gampee21dc3d2014-12-08 16:59:43 -080096void ScratchFile::Close() {
Andreas Gampe4303ba92014-11-06 01:00:46 -080097 if (file_.get() != nullptr) {
98 if (file_->FlushCloseOrErase() != 0) {
99 PLOG(WARNING) << "Error closing scratch file.";
100 }
101 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800102}
103
104void ScratchFile::Unlink() {
105 if (!OS::FileExists(filename_.c_str())) {
106 return;
107 }
108 Close();
Ian Rogerse63db272014-07-15 15:36:11 -0700109 int unlink_result = unlink(filename_.c_str());
110 CHECK_EQ(0, unlink_result);
111}
112
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700113static bool unstarted_initialized_ = false;
114
Ian Rogerse63db272014-07-15 15:36:11 -0700115CommonRuntimeTest::CommonRuntimeTest() {}
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800116CommonRuntimeTest::~CommonRuntimeTest() {
117 // Ensure the dex files are cleaned up before the runtime.
118 loaded_dex_files_.clear();
119 runtime_.reset();
120}
Ian Rogerse63db272014-07-15 15:36:11 -0700121
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700122void CommonRuntimeTest::SetUpAndroidRoot() {
Ian Rogerse63db272014-07-15 15:36:11 -0700123 if (IsHost()) {
124 // $ANDROID_ROOT is set on the device, but not necessarily on the host.
125 // But it needs to be set so that icu4c can find its locale data.
126 const char* android_root_from_env = getenv("ANDROID_ROOT");
127 if (android_root_from_env == nullptr) {
128 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
129 const char* android_host_out = getenv("ANDROID_HOST_OUT");
130 if (android_host_out != nullptr) {
131 setenv("ANDROID_ROOT", android_host_out, 1);
132 } else {
133 // Build it from ANDROID_BUILD_TOP or cwd
134 std::string root;
135 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
136 if (android_build_top != nullptr) {
137 root += android_build_top;
138 } else {
139 // Not set by build server, so default to current directory
140 char* cwd = getcwd(nullptr, 0);
141 setenv("ANDROID_BUILD_TOP", cwd, 1);
142 root += cwd;
143 free(cwd);
144 }
145#if defined(__linux__)
146 root += "/out/host/linux-x86";
147#elif defined(__APPLE__)
148 root += "/out/host/darwin-x86";
149#else
150#error unsupported OS
151#endif
152 setenv("ANDROID_ROOT", root.c_str(), 1);
153 }
154 }
155 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
156
157 // Not set by build server, so default
158 if (getenv("ANDROID_HOST_OUT") == nullptr) {
159 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
160 }
161 }
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700162}
Ian Rogerse63db272014-07-15 15:36:11 -0700163
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700164void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
Ian Rogerse63db272014-07-15 15:36:11 -0700165 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
Andreas Gampe5a79fde2014-08-06 13:12:26 -0700166 if (IsHost()) {
167 const char* tmpdir = getenv("TMPDIR");
168 if (tmpdir != nullptr && tmpdir[0] != 0) {
169 android_data = tmpdir;
170 } else {
171 android_data = "/tmp";
172 }
173 } else {
174 android_data = "/data/dalvik-cache";
175 }
176 android_data += "/art-data-XXXXXX";
Ian Rogerse63db272014-07-15 15:36:11 -0700177 if (mkdtemp(&android_data[0]) == nullptr) {
178 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
179 }
180 setenv("ANDROID_DATA", android_data.c_str(), 1);
181}
182
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700183void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
184 if (fail_on_error) {
185 ASSERT_EQ(rmdir(android_data.c_str()), 0);
186 } else {
187 rmdir(android_data.c_str());
188 }
189}
190
Igor Murashkin37743352014-11-13 14:38:00 -0800191std::string CommonRuntimeTest::GetCoreArtLocation() {
192 return GetCoreFileLocation("art");
193}
194
195std::string CommonRuntimeTest::GetCoreOatLocation() {
196 return GetCoreFileLocation("oat");
197}
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700198
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800199std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
200 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700201 std::string error_msg;
Richard Uhler66d874d2015-01-15 09:37:19 -0800202 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700203 if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
204 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800205 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700206 } else {
207 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800208 return std::move(dex_files[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700209 }
210}
211
212void CommonRuntimeTest::SetUp() {
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700213 SetUpAndroidRoot();
214 SetUpAndroidData(android_data_);
Ian Rogerse63db272014-07-15 15:36:11 -0700215 dalvik_cache_.append(android_data_.c_str());
216 dalvik_cache_.append("/dalvik-cache");
217 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
218 ASSERT_EQ(mkdir_result, 0);
219
Ian Rogerse63db272014-07-15 15:36:11 -0700220 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
221 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
222
223 callbacks_.reset(new NoopCompilerCallbacks());
224
225 RuntimeOptions options;
Richard Uhlerc2752592015-01-02 13:28:22 -0800226 std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
227 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700228 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800229 options.push_back(std::make_pair(min_heap_string, nullptr));
230 options.push_back(std::make_pair(max_heap_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700231 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
232 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800233
Richard Uhler66d874d2015-01-15 09:37:19 -0800234 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700235 if (!Runtime::Create(options, false)) {
236 LOG(FATAL) << "Failed to create runtime";
237 return;
238 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800239 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700240 runtime_.reset(Runtime::Current());
241 class_linker_ = runtime_->GetClassLinker();
242 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700243
244 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
245 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700246 if (!unstarted_initialized_) {
247 interpreter::UnstartedRuntimeInitialize();
248 unstarted_initialized_ = true;
249 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700250
Ian Rogerse63db272014-07-15 15:36:11 -0700251 class_linker_->RunRootClinits();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800252 boot_class_path_ = class_linker_->GetBootClassPath();
253 java_lang_dex_file_ = boot_class_path_[0];
254
Ian Rogerse63db272014-07-15 15:36:11 -0700255
256 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
257 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
258 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
259
260 // We're back in native, take the opportunity to initialize well known classes.
261 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
262
263 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
264 // pool is created by the runtime.
265 runtime_->GetHeap()->CreateThreadPool();
266 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700267 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
268 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
Richard Uhlerc2752592015-01-02 13:28:22 -0800269
270 // Get the boot class path from the runtime so it can be used in tests.
271 boot_class_path_ = class_linker_->GetBootClassPath();
272 ASSERT_FALSE(boot_class_path_.empty());
273 java_lang_dex_file_ = boot_class_path_[0];
Ian Rogerse63db272014-07-15 15:36:11 -0700274}
275
Alex Lighta59dd802014-07-02 16:28:08 -0700276void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
277 ASSERT_TRUE(dirpath != nullptr);
278 DIR* dir = opendir(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700279 ASSERT_TRUE(dir != nullptr);
280 dirent* e;
Alex Lighta59dd802014-07-02 16:28:08 -0700281 struct stat s;
Ian Rogerse63db272014-07-15 15:36:11 -0700282 while ((e = readdir(dir)) != nullptr) {
283 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
284 continue;
285 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700286 std::string filename(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700287 filename.push_back('/');
288 filename.append(e->d_name);
Alex Lighta59dd802014-07-02 16:28:08 -0700289 int stat_result = lstat(filename.c_str(), &s);
290 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
291 if (S_ISDIR(s.st_mode)) {
292 ClearDirectory(filename.c_str());
293 int rmdir_result = rmdir(filename.c_str());
294 ASSERT_EQ(0, rmdir_result) << filename;
295 } else {
296 int unlink_result = unlink(filename.c_str());
297 ASSERT_EQ(0, unlink_result) << filename;
298 }
Ian Rogerse63db272014-07-15 15:36:11 -0700299 }
300 closedir(dir);
Alex Lighta59dd802014-07-02 16:28:08 -0700301}
302
303void CommonRuntimeTest::TearDown() {
304 const char* android_data = getenv("ANDROID_DATA");
305 ASSERT_TRUE(android_data != nullptr);
306 ClearDirectory(dalvik_cache_.c_str());
Ian Rogerse63db272014-07-15 15:36:11 -0700307 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
308 ASSERT_EQ(0, rmdir_cache_result);
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700309 TearDownAndroidData(android_data_, true);
Ian Rogerse63db272014-07-15 15:36:11 -0700310
311 // icu4c has a fixed 10-element array "gCommonICUDataArray".
312 // If we run > 10 tests, we fill that array and u_setCommonData fails.
313 // There's a function to clear the array, but it's not public...
314 typedef void (*IcuCleanupFn)();
315 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
316 CHECK(sym != nullptr) << dlerror();
317 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
318 (*icu_cleanup_fn)();
319
Ian Rogerse63db272014-07-15 15:36:11 -0700320 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
321}
322
323std::string CommonRuntimeTest::GetLibCoreDexFileName() {
324 return GetDexFileName("core-libart");
325}
326
327std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
328 if (IsHost()) {
329 const char* host_dir = getenv("ANDROID_HOST_OUT");
330 CHECK(host_dir != nullptr);
331 return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
332 }
333 return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
334}
335
336std::string CommonRuntimeTest::GetTestAndroidRoot() {
337 if (IsHost()) {
338 const char* host_dir = getenv("ANDROID_HOST_OUT");
339 CHECK(host_dir != nullptr);
340 return host_dir;
341 }
342 return GetAndroidRoot();
343}
344
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700345// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
346#ifdef ART_TARGET
347#ifndef ART_TARGET_NATIVETEST_DIR
348#error "ART_TARGET_NATIVETEST_DIR not set."
349#endif
350// Wrap it as a string literal.
351#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
352#else
353#define ART_TARGET_NATIVETEST_DIR_STRING ""
354#endif
355
Richard Uhler66d874d2015-01-15 09:37:19 -0800356std::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700357 CHECK(name != nullptr);
358 std::string filename;
359 if (IsHost()) {
360 filename += getenv("ANDROID_HOST_OUT");
361 filename += "/framework/";
362 } else {
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700363 filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogerse63db272014-07-15 15:36:11 -0700364 }
365 filename += "art-gtest-";
366 filename += name;
367 filename += ".jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800368 return filename;
369}
370
371std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
372 std::string filename = GetTestDexFileName(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700373 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800374 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700375 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
376 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800377 for (auto& dex_file : dex_files) {
Ian Rogerse63db272014-07-15 15:36:11 -0700378 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
379 CHECK(dex_file->IsReadOnly());
380 }
Ian Rogerse63db272014-07-15 15:36:11 -0700381 return dex_files;
382}
383
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800384std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
385 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700386 EXPECT_EQ(1U, vector.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800387 return std::move(vector[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700388}
389
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700390std::vector<const DexFile*> CommonRuntimeTest::GetDexFiles(jobject jclass_loader) {
391 std::vector<const DexFile*> ret;
392
393 ScopedObjectAccess soa(Thread::Current());
394
395 StackHandleScope<4> hs(Thread::Current());
396 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
397 soa.Decode<mirror::ClassLoader*>(jclass_loader));
398
399 DCHECK_EQ(class_loader->GetClass(),
400 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader));
401 DCHECK_EQ(class_loader->GetParent()->GetClass(),
402 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader));
403
404 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader.
405 // We need to get the DexPathList and loop through it.
406 Handle<mirror::ArtField> cookie_field =
407 hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie));
408 Handle<mirror::ArtField> dex_file_field =
409 hs.NewHandle(
410 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile));
411 mirror::Object* dex_path_list =
412 soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)->
413 GetObject(class_loader.Get());
414 if (dex_path_list != nullptr && dex_file_field.Get() != nullptr &&
415 cookie_field.Get() != nullptr) {
416 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
417 mirror::Object* dex_elements_obj =
418 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
419 GetObject(dex_path_list);
420 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
421 // at the mCookie which is a DexFile vector.
422 if (dex_elements_obj != nullptr) {
423 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
424 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
425 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
426 mirror::Object* element = dex_elements->GetWithoutChecks(i);
427 if (element == nullptr) {
428 // Should never happen, fall back to java code to throw a NPE.
429 break;
430 }
431 mirror::Object* dex_file = dex_file_field->GetObject(element);
432 if (dex_file != nullptr) {
433 mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray();
434 DCHECK(long_array != nullptr);
435 int32_t long_array_size = long_array->GetLength();
436 for (int32_t j = 0; j < long_array_size; ++j) {
437 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
438 long_array->GetWithoutChecks(j)));
439 if (cp_dex_file == nullptr) {
440 LOG(WARNING) << "Null DexFile";
441 continue;
442 }
443 ret.push_back(cp_dex_file);
444 }
445 }
446 }
447 }
448 }
449
450 return ret;
451}
452
453const DexFile* CommonRuntimeTest::GetFirstDexFile(jobject jclass_loader) {
454 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
455 DCHECK(!tmp.empty());
456 const DexFile* ret = tmp[0];
457 DCHECK(ret != nullptr);
458 return ret;
459}
460
Ian Rogerse63db272014-07-15 15:36:11 -0700461jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800462 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
463 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700464 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800465 for (auto& dex_file : dex_files) {
466 class_path.push_back(dex_file.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800467 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700468 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700469
Ian Rogers68d8b422014-07-17 11:09:10 -0700470 Thread* self = Thread::Current();
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700471 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, class_path);
472 self->SetClassLoaderOverride(class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700473 return class_loader;
474}
475
Igor Murashkin37743352014-11-13 14:38:00 -0800476std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
477 CHECK(suffix != nullptr);
478
479 std::string location;
480 if (IsHost()) {
481 const char* host_dir = getenv("ANDROID_HOST_OUT");
482 CHECK(host_dir != NULL);
483 location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
484 } else {
485 location = StringPrintf("/data/art-test/core.%s", suffix);
486 }
487
488 return location;
489}
490
Ian Rogerse63db272014-07-15 15:36:11 -0700491CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700492 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700493}
494
495CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700496 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700497 EXPECT_TRUE(actual_.empty()) << actual_;
498}
499
500void CheckJniAbortCatcher::Check(const char* expected_text) {
501 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
502 << "Expected to find: " << expected_text << "\n"
503 << "In the output : " << actual_;
504 actual_.clear();
505}
506
507void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
508 // We use += because when we're hooking the aborts like this, multiple problems can be found.
509 *reinterpret_cast<std::string*>(data) += reason;
510}
511
512} // namespace art
513
514namespace std {
515
516template <typename T>
517std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
518os << ::art::ToString(rhs);
519return os;
520}
521
522} // namespace std