blob: b7ffd609b78019233b4493c5a5555be29de589a3 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "jni_internal.h"
38#include "mirror/class_loader.h"
39#include "noop_compiler_callbacks.h"
40#include "os.h"
41#include "runtime-inl.h"
42#include "scoped_thread_state_change.h"
43#include "thread.h"
44#include "well_known_classes.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070045
46int main(int argc, char **argv) {
Andreas Gampe369810a2015-01-14 19:53:31 -080047 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
48 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
49 // everything else. In case you want to see all messages, comment out the line.
50 setenv("ANDROID_LOG_TAGS", "*:e", 1);
51
Elliott Hugheseb02a122012-06-12 11:35:40 -070052 art::InitLogging(argv);
Ian Rogersc7dd2952014-10-21 23:31:19 -070053 LOG(::art::INFO) << "Running main() from common_runtime_test.cc...";
Elliott Hugheseb02a122012-06-12 11:35:40 -070054 testing::InitGoogleTest(&argc, argv);
55 return RUN_ALL_TESTS();
56}
Ian Rogerse63db272014-07-15 15:36:11 -070057
58namespace art {
59
60ScratchFile::ScratchFile() {
61 // ANDROID_DATA needs to be set
62 CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) <<
63 "Are you subclassing RuntimeTest?";
64 filename_ = getenv("ANDROID_DATA");
65 filename_ += "/TmpFile-XXXXXX";
66 int fd = mkstemp(&filename_[0]);
67 CHECK_NE(-1, fd);
Andreas Gampe4303ba92014-11-06 01:00:46 -080068 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070069}
70
71ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) {
72 filename_ = other.GetFilename();
73 filename_ += suffix;
74 int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666);
75 CHECK_NE(-1, fd);
Andreas Gampe4303ba92014-11-06 01:00:46 -080076 file_.reset(new File(fd, GetFilename(), true));
Ian Rogerse63db272014-07-15 15:36:11 -070077}
78
79ScratchFile::ScratchFile(File* file) {
80 CHECK(file != NULL);
81 filename_ = file->GetPath();
82 file_.reset(file);
83}
84
85ScratchFile::~ScratchFile() {
86 Unlink();
87}
88
89int ScratchFile::GetFd() const {
90 return file_->Fd();
91}
92
Andreas Gampee21dc3d2014-12-08 16:59:43 -080093void ScratchFile::Close() {
Andreas Gampe4303ba92014-11-06 01:00:46 -080094 if (file_.get() != nullptr) {
95 if (file_->FlushCloseOrErase() != 0) {
96 PLOG(WARNING) << "Error closing scratch file.";
97 }
98 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -080099}
100
101void ScratchFile::Unlink() {
102 if (!OS::FileExists(filename_.c_str())) {
103 return;
104 }
105 Close();
Ian Rogerse63db272014-07-15 15:36:11 -0700106 int unlink_result = unlink(filename_.c_str());
107 CHECK_EQ(0, unlink_result);
108}
109
110CommonRuntimeTest::CommonRuntimeTest() {}
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800111CommonRuntimeTest::~CommonRuntimeTest() {
112 // Ensure the dex files are cleaned up before the runtime.
113 loaded_dex_files_.clear();
114 runtime_.reset();
115}
Ian Rogerse63db272014-07-15 15:36:11 -0700116
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700117void CommonRuntimeTest::SetUpAndroidRoot() {
Ian Rogerse63db272014-07-15 15:36:11 -0700118 if (IsHost()) {
119 // $ANDROID_ROOT is set on the device, but not necessarily on the host.
120 // But it needs to be set so that icu4c can find its locale data.
121 const char* android_root_from_env = getenv("ANDROID_ROOT");
122 if (android_root_from_env == nullptr) {
123 // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set.
124 const char* android_host_out = getenv("ANDROID_HOST_OUT");
125 if (android_host_out != nullptr) {
126 setenv("ANDROID_ROOT", android_host_out, 1);
127 } else {
128 // Build it from ANDROID_BUILD_TOP or cwd
129 std::string root;
130 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
131 if (android_build_top != nullptr) {
132 root += android_build_top;
133 } else {
134 // Not set by build server, so default to current directory
135 char* cwd = getcwd(nullptr, 0);
136 setenv("ANDROID_BUILD_TOP", cwd, 1);
137 root += cwd;
138 free(cwd);
139 }
140#if defined(__linux__)
141 root += "/out/host/linux-x86";
142#elif defined(__APPLE__)
143 root += "/out/host/darwin-x86";
144#else
145#error unsupported OS
146#endif
147 setenv("ANDROID_ROOT", root.c_str(), 1);
148 }
149 }
150 setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>.
151
152 // Not set by build server, so default
153 if (getenv("ANDROID_HOST_OUT") == nullptr) {
154 setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1);
155 }
156 }
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700157}
Ian Rogerse63db272014-07-15 15:36:11 -0700158
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700159void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) {
Ian Rogerse63db272014-07-15 15:36:11 -0700160 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache
Andreas Gampe5a79fde2014-08-06 13:12:26 -0700161 if (IsHost()) {
162 const char* tmpdir = getenv("TMPDIR");
163 if (tmpdir != nullptr && tmpdir[0] != 0) {
164 android_data = tmpdir;
165 } else {
166 android_data = "/tmp";
167 }
168 } else {
169 android_data = "/data/dalvik-cache";
170 }
171 android_data += "/art-data-XXXXXX";
Ian Rogerse63db272014-07-15 15:36:11 -0700172 if (mkdtemp(&android_data[0]) == nullptr) {
173 PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed";
174 }
175 setenv("ANDROID_DATA", android_data.c_str(), 1);
176}
177
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700178void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) {
179 if (fail_on_error) {
180 ASSERT_EQ(rmdir(android_data.c_str()), 0);
181 } else {
182 rmdir(android_data.c_str());
183 }
184}
185
Igor Murashkin37743352014-11-13 14:38:00 -0800186std::string CommonRuntimeTest::GetCoreArtLocation() {
187 return GetCoreFileLocation("art");
188}
189
190std::string CommonRuntimeTest::GetCoreOatLocation() {
191 return GetCoreFileLocation("oat");
192}
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700193
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800194std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
195 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700196 std::string error_msg;
197 if (!DexFile::Open(location, location, &error_msg, &dex_files)) {
198 LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n";
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800199 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700200 } else {
201 CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800202 return std::move(dex_files[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700203 }
204}
205
206void CommonRuntimeTest::SetUp() {
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700207 SetUpAndroidRoot();
208 SetUpAndroidData(android_data_);
Ian Rogerse63db272014-07-15 15:36:11 -0700209 dalvik_cache_.append(android_data_.c_str());
210 dalvik_cache_.append("/dalvik-cache");
211 int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700);
212 ASSERT_EQ(mkdir_result, 0);
213
Ian Rogerse63db272014-07-15 15:36:11 -0700214 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
215 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
216
217 callbacks_.reset(new NoopCompilerCallbacks());
218
219 RuntimeOptions options;
Richard Uhlerc2752592015-01-02 13:28:22 -0800220 std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName();
221 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700222 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800223 options.push_back(std::make_pair(min_heap_string, nullptr));
224 options.push_back(std::make_pair(max_heap_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700225 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
226 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800227
Ian Rogerse63db272014-07-15 15:36:11 -0700228 if (!Runtime::Create(options, false)) {
229 LOG(FATAL) << "Failed to create runtime";
230 return;
231 }
232 runtime_.reset(Runtime::Current());
233 class_linker_ = runtime_->GetClassLinker();
234 class_linker_->FixupDexCaches(runtime_->GetResolutionMethod());
235 class_linker_->RunRootClinits();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800236 boot_class_path_ = class_linker_->GetBootClassPath();
237 java_lang_dex_file_ = boot_class_path_[0];
238
Ian Rogerse63db272014-07-15 15:36:11 -0700239
240 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
241 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
242 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
243
244 // We're back in native, take the opportunity to initialize well known classes.
245 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
246
247 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
248 // pool is created by the runtime.
249 runtime_->GetHeap()->CreateThreadPool();
250 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Richard Uhlerc2752592015-01-02 13:28:22 -0800251
252 // Get the boot class path from the runtime so it can be used in tests.
253 boot_class_path_ = class_linker_->GetBootClassPath();
254 ASSERT_FALSE(boot_class_path_.empty());
255 java_lang_dex_file_ = boot_class_path_[0];
Ian Rogerse63db272014-07-15 15:36:11 -0700256}
257
Alex Lighta59dd802014-07-02 16:28:08 -0700258void CommonRuntimeTest::ClearDirectory(const char* dirpath) {
259 ASSERT_TRUE(dirpath != nullptr);
260 DIR* dir = opendir(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700261 ASSERT_TRUE(dir != nullptr);
262 dirent* e;
Alex Lighta59dd802014-07-02 16:28:08 -0700263 struct stat s;
Ian Rogerse63db272014-07-15 15:36:11 -0700264 while ((e = readdir(dir)) != nullptr) {
265 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
266 continue;
267 }
Jeff Haof0a3f092014-07-24 16:26:09 -0700268 std::string filename(dirpath);
Ian Rogerse63db272014-07-15 15:36:11 -0700269 filename.push_back('/');
270 filename.append(e->d_name);
Alex Lighta59dd802014-07-02 16:28:08 -0700271 int stat_result = lstat(filename.c_str(), &s);
272 ASSERT_EQ(0, stat_result) << "unable to stat " << filename;
273 if (S_ISDIR(s.st_mode)) {
274 ClearDirectory(filename.c_str());
275 int rmdir_result = rmdir(filename.c_str());
276 ASSERT_EQ(0, rmdir_result) << filename;
277 } else {
278 int unlink_result = unlink(filename.c_str());
279 ASSERT_EQ(0, unlink_result) << filename;
280 }
Ian Rogerse63db272014-07-15 15:36:11 -0700281 }
282 closedir(dir);
Alex Lighta59dd802014-07-02 16:28:08 -0700283}
284
285void CommonRuntimeTest::TearDown() {
286 const char* android_data = getenv("ANDROID_DATA");
287 ASSERT_TRUE(android_data != nullptr);
288 ClearDirectory(dalvik_cache_.c_str());
Ian Rogerse63db272014-07-15 15:36:11 -0700289 int rmdir_cache_result = rmdir(dalvik_cache_.c_str());
290 ASSERT_EQ(0, rmdir_cache_result);
Andreas Gampe7747c8d2014-08-06 14:53:03 -0700291 TearDownAndroidData(android_data_, true);
Ian Rogerse63db272014-07-15 15:36:11 -0700292
293 // icu4c has a fixed 10-element array "gCommonICUDataArray".
294 // If we run > 10 tests, we fill that array and u_setCommonData fails.
295 // There's a function to clear the array, but it's not public...
296 typedef void (*IcuCleanupFn)();
297 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
298 CHECK(sym != nullptr) << dlerror();
299 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
300 (*icu_cleanup_fn)();
301
Ian Rogerse63db272014-07-15 15:36:11 -0700302 Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
303}
304
305std::string CommonRuntimeTest::GetLibCoreDexFileName() {
306 return GetDexFileName("core-libart");
307}
308
309std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) {
310 if (IsHost()) {
311 const char* host_dir = getenv("ANDROID_HOST_OUT");
312 CHECK(host_dir != nullptr);
313 return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str());
314 }
315 return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str());
316}
317
318std::string CommonRuntimeTest::GetTestAndroidRoot() {
319 if (IsHost()) {
320 const char* host_dir = getenv("ANDROID_HOST_OUT");
321 CHECK(host_dir != nullptr);
322 return host_dir;
323 }
324 return GetAndroidRoot();
325}
326
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700327// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
328#ifdef ART_TARGET
329#ifndef ART_TARGET_NATIVETEST_DIR
330#error "ART_TARGET_NATIVETEST_DIR not set."
331#endif
332// Wrap it as a string literal.
333#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
334#else
335#define ART_TARGET_NATIVETEST_DIR_STRING ""
336#endif
337
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800338std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700339 CHECK(name != nullptr);
340 std::string filename;
341 if (IsHost()) {
342 filename += getenv("ANDROID_HOST_OUT");
343 filename += "/framework/";
344 } else {
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700345 filename += ART_TARGET_NATIVETEST_DIR_STRING;
Ian Rogerse63db272014-07-15 15:36:11 -0700346 }
347 filename += "art-gtest-";
348 filename += name;
349 filename += ".jar";
350 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800351 std::vector<std::unique_ptr<const DexFile>> dex_files;
Ian Rogerse63db272014-07-15 15:36:11 -0700352 bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files);
353 CHECK(success) << "Failed to open '" << filename << "': " << error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800354 for (auto& dex_file : dex_files) {
Ian Rogerse63db272014-07-15 15:36:11 -0700355 CHECK_EQ(PROT_READ, dex_file->GetPermissions());
356 CHECK(dex_file->IsReadOnly());
357 }
Ian Rogerse63db272014-07-15 15:36:11 -0700358 return dex_files;
359}
360
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800361std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) {
362 std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name);
Ian Rogerse63db272014-07-15 15:36:11 -0700363 EXPECT_EQ(1U, vector.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800364 return std::move(vector[0]);
Ian Rogerse63db272014-07-15 15:36:11 -0700365}
366
367jobject CommonRuntimeTest::LoadDex(const char* dex_name) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800368 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name);
369 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700370 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800371 for (auto& dex_file : dex_files) {
372 class_path.push_back(dex_file.get());
Ian Rogerse63db272014-07-15 15:36:11 -0700373 class_linker_->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800374 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700375 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700376 Thread* self = Thread::Current();
377 JNIEnvExt* env = self->GetJniEnv();
378 ScopedLocalRef<jobject> class_loader_local(env,
379 env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
380 jobject class_loader = env->NewGlobalRef(class_loader_local.get());
381 self->SetClassLoaderOverride(class_loader_local.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800382 Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path);
Ian Rogerse63db272014-07-15 15:36:11 -0700383 return class_loader;
384}
385
Igor Murashkin37743352014-11-13 14:38:00 -0800386std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
387 CHECK(suffix != nullptr);
388
389 std::string location;
390 if (IsHost()) {
391 const char* host_dir = getenv("ANDROID_HOST_OUT");
392 CHECK(host_dir != NULL);
393 location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
394 } else {
395 location = StringPrintf("/data/art-test/core.%s", suffix);
396 }
397
398 return location;
399}
400
Ian Rogerse63db272014-07-15 15:36:11 -0700401CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700402 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700403}
404
405CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700406 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700407 EXPECT_TRUE(actual_.empty()) << actual_;
408}
409
410void CheckJniAbortCatcher::Check(const char* expected_text) {
411 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
412 << "Expected to find: " << expected_text << "\n"
413 << "In the output : " << actual_;
414 actual_.clear();
415}
416
417void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
418 // We use += because when we're hooking the aborts like this, multiple problems can be found.
419 *reinterpret_cast<std::string*>(data) += reason;
420}
421
422} // namespace art
423
424namespace std {
425
426template <typename T>
427std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
428os << ::art::ToString(rhs);
429return os;
430}
431
432} // namespace std