blob: d400010e4b210aff821797edf0353181fec34874 [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
Ian Rogerse63db272014-07-15 15:36:11 -0700223
224 RuntimeOptions options;
Richard Uhlerc2752592015-01-02 13:28:22 -0800225 std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
226 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700227 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800228 options.push_back(std::make_pair(min_heap_string, nullptr));
229 options.push_back(std::make_pair(max_heap_string, nullptr));
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700230
231 callbacks_.reset(new NoopCompilerCallbacks());
232
Ian Rogerse63db272014-07-15 15:36:11 -0700233 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800234
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700235 // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them.
236 if (callbacks_.get() != nullptr) {
237 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
238 }
239
Richard Uhler66d874d2015-01-15 09:37:19 -0800240 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700241 if (!Runtime::Create(options, false)) {
242 LOG(FATAL) << "Failed to create runtime";
243 return;
244 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800245 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700246 runtime_.reset(Runtime::Current());
247 class_linker_ = runtime_->GetClassLinker();
248 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700249
250 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
251 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700252 if (!unstarted_initialized_) {
253 interpreter::UnstartedRuntimeInitialize();
254 unstarted_initialized_ = true;
255 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700256
Ian Rogerse63db272014-07-15 15:36:11 -0700257 class_linker_->RunRootClinits();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800258 boot_class_path_ = class_linker_->GetBootClassPath();
259 java_lang_dex_file_ = boot_class_path_[0];
260
Ian Rogerse63db272014-07-15 15:36:11 -0700261
262 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
263 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
264 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
265
266 // We're back in native, take the opportunity to initialize well known classes.
267 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
268
269 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
270 // pool is created by the runtime.
271 runtime_->GetHeap()->CreateThreadPool();
272 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700273 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
274 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
Richard Uhlerc2752592015-01-02 13:28:22 -0800275
276 // Get the boot class path from the runtime so it can be used in tests.
277 boot_class_path_ = class_linker_->GetBootClassPath();
278 ASSERT_FALSE(boot_class_path_.empty());
279 java_lang_dex_file_ = boot_class_path_[0];
Ian Rogerse63db272014-07-15 15:36:11 -0700280}
281
Alex Lighta59dd802014-07-02 16:28:08 -0700282void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
283 ASSERT_TRUE(dirpath != nullptr);
284 DIR* dir = opendir(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700285 ASSERT_TRUE(dir != nullptr);
286 dirent* e;
Alex Lighta59dd802014-07-02 16:28:08 -0700287 struct stat s;
Ian Rogerse63db272014-07-15 15:36:11 -0700288 while ((e = readdir(dir)) != nullptr) {
289 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
290 continue;
291 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700292 std::string filename(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700293 filename.push_back('/');
294 filename.append(e->d_name);
Alex Lighta59dd802014-07-02 16:28:08 -0700295 int stat_result = lstat(filename.c_str(), &s);
296 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
297 if (S_ISDIR(s.st_mode)) {
298 ClearDirectory(filename.c_str());
299 int rmdir_result = rmdir(filename.c_str());
300 ASSERT_EQ(0, rmdir_result) << filename;
301 } else {
302 int unlink_result = unlink(filename.c_str());
303 ASSERT_EQ(0, unlink_result) << filename;
304 }
Ian Rogerse63db272014-07-15 15:36:11 -0700305 }
306 closedir(dir);
Alex Lighta59dd802014-07-02 16:28:08 -0700307}
308
309void CommonRuntimeTest::TearDown() {
310 const char* android_data = getenv("ANDROID_DATA");
311 ASSERT_TRUE(android_data != nullptr);
312 ClearDirectory(dalvik_cache_.c_str());
Ian Rogerse63db272014-07-15 15:36:11 -0700313 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
314 ASSERT_EQ(0, rmdir_cache_result);
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700315 TearDownAndroidData(android_data_, true);
Ian Rogerse63db272014-07-15 15:36:11 -0700316
317 // icu4c has a fixed 10-element array "gCommonICUDataArray".
318 // If we run > 10 tests, we fill that array and u_setCommonData fails.
319 // There's a function to clear the array, but it's not public...
320 typedef void (*IcuCleanupFn)();
321 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
322 CHECK(sym != nullptr) << dlerror();
323 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
324 (*icu_cleanup_fn)();
325
Ian Rogerse63db272014-07-15 15:36:11 -0700326 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
327}
328
329std::string CommonRuntimeTest::GetLibCoreDexFileName() {
330 return GetDexFileName("core-libart");
331}
332
333std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
334 if (IsHost()) {
335 const char* host_dir = getenv("ANDROID_HOST_OUT");
336 CHECK(host_dir != nullptr);
337 return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
338 }
339 return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
340}
341
342std::string CommonRuntimeTest::GetTestAndroidRoot() {
343 if (IsHost()) {
344 const char* host_dir = getenv("ANDROID_HOST_OUT");
345 CHECK(host_dir != nullptr);
346 return host_dir;
347 }
348 return GetAndroidRoot();
349}
350
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700351// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
352#ifdef ART_TARGET
353#ifndef ART_TARGET_NATIVETEST_DIR
354#error "ART_TARGET_NATIVETEST_DIR not set."
355#endif
356// Wrap it as a string literal.
357#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
358#else
359#define ART_TARGET_NATIVETEST_DIR_STRING ""
360#endif
361
Richard Uhler66d874d2015-01-15 09:37:19 -0800362std::string CommonRuntimeTest::GetTestDexFileName(const char* name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700363 CHECK(name != nullptr);
364 std::string filename;
365 if (IsHost()) {
366 filename += getenv("ANDROID_HOST_OUT");
367 filename += "/framework/";
368 } else {
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700369 filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogerse63db272014-07-15 15:36:11 -0700370 }
371 filename += "art-gtest-";
372 filename += name;
373 filename += ".jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800374 return filename;
375}
376
377std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
378 std::string filename = GetTestDexFileName(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700379 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800380 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700381 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
382 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800383 for (auto& dex_file : dex_files) {
Ian Rogerse63db272014-07-15 15:36:11 -0700384 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
385 CHECK(dex_file->IsReadOnly());
386 }
Ian Rogerse63db272014-07-15 15:36:11 -0700387 return dex_files;
388}
389
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800390std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
391 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700392 EXPECT_EQ(1U, vector.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800393 return std::move(vector[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700394}
395
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700396std::vector<const DexFile*> CommonRuntimeTest::GetDexFiles(jobject jclass_loader) {
397 std::vector<const DexFile*> ret;
398
399 ScopedObjectAccess soa(Thread::Current());
400
401 StackHandleScope<4> hs(Thread::Current());
402 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
403 soa.Decode<mirror::ClassLoader*>(jclass_loader));
404
405 DCHECK_EQ(class_loader->GetClass(),
406 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader));
407 DCHECK_EQ(class_loader->GetParent()->GetClass(),
408 soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader));
409
410 // The class loader is a PathClassLoader which inherits from BaseDexClassLoader.
411 // We need to get the DexPathList and loop through it.
412 Handle<mirror::ArtField> cookie_field =
413 hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie));
414 Handle<mirror::ArtField> dex_file_field =
415 hs.NewHandle(
416 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile));
417 mirror::Object* dex_path_list =
418 soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)->
419 GetObject(class_loader.Get());
420 if (dex_path_list != nullptr && dex_file_field.Get() != nullptr &&
421 cookie_field.Get() != nullptr) {
422 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
423 mirror::Object* dex_elements_obj =
424 soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
425 GetObject(dex_path_list);
426 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
427 // at the mCookie which is a DexFile vector.
428 if (dex_elements_obj != nullptr) {
429 Handle<mirror::ObjectArray<mirror::Object>> dex_elements =
430 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>());
431 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
432 mirror::Object* element = dex_elements->GetWithoutChecks(i);
433 if (element == nullptr) {
434 // Should never happen, fall back to java code to throw a NPE.
435 break;
436 }
437 mirror::Object* dex_file = dex_file_field->GetObject(element);
438 if (dex_file != nullptr) {
439 mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray();
440 DCHECK(long_array != nullptr);
441 int32_t long_array_size = long_array->GetLength();
442 for (int32_t j = 0; j < long_array_size; ++j) {
443 const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(
444 long_array->GetWithoutChecks(j)));
445 if (cp_dex_file == nullptr) {
446 LOG(WARNING) << "Null DexFile";
447 continue;
448 }
449 ret.push_back(cp_dex_file);
450 }
451 }
452 }
453 }
454 }
455
456 return ret;
457}
458
459const DexFile* CommonRuntimeTest::GetFirstDexFile(jobject jclass_loader) {
460 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
461 DCHECK(!tmp.empty());
462 const DexFile* ret = tmp[0];
463 DCHECK(ret != nullptr);
464 return ret;
465}
466
Ian Rogerse63db272014-07-15 15:36:11 -0700467jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800468 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
469 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700470 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800471 for (auto& dex_file : dex_files) {
472 class_path.push_back(dex_file.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800473 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700474 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700475
Ian Rogers68d8b422014-07-17 11:09:10 -0700476 Thread* self = Thread::Current();
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700477 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, class_path);
478 self->SetClassLoaderOverride(class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700479 return class_loader;
480}
481
Igor Murashkin37743352014-11-13 14:38:00 -0800482std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
483 CHECK(suffix != nullptr);
484
485 std::string location;
486 if (IsHost()) {
487 const char* host_dir = getenv("ANDROID_HOST_OUT");
488 CHECK(host_dir != NULL);
489 location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
490 } else {
491 location = StringPrintf("/data/art-test/core.%s", suffix);
492 }
493
494 return location;
495}
496
Ian Rogerse63db272014-07-15 15:36:11 -0700497CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700498 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700499}
500
501CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700502 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700503 EXPECT_TRUE(actual_.empty()) << actual_;
504}
505
506void CheckJniAbortCatcher::Check(const char* expected_text) {
507 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
508 << "Expected to find: " << expected_text << "\n"
509 << "In the output : " << actual_;
510 actual_.clear();
511}
512
513void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
514 // We use += because when we're hooking the aborts like this, multiple problems can be found.
515 *reinterpret_cast<std::string*>(data) += reason;
516}
517
518} // namespace art
519
520namespace std {
521
522template <typename T>
523std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
524os << ::art::ToString(rhs);
525return os;
526}
527
528} // namespace std