blob: 8491dbbbb49c9d365e7d4a7f7cf070fc8c4d1238 [file] [log] [blame]
Brian Carlstrom934486c2011-07-12 23:42:50 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstromb0460ea2011-07-29 10:08:05 -07003#include <dirent.h>
Elliott Hughes0af55432011-08-17 18:37:28 -07004#include <dlfcn.h>
Brian Carlstrom27ec9612011-09-19 20:20:38 -07005#include <sys/mman.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -07006#include <sys/stat.h>
7#include <sys/types.h>
8
Elliott Hughes90a33692011-08-30 13:27:07 -07009#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "base64.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070012#include "class_loader.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070013#include "compiler.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070014#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070016#include "gtest/gtest.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070017#include "heap.h"
18#include "runtime.h"
Elliott Hughes14134a12011-09-30 16:55:51 -070019#include "stl_util.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070020#include "stringprintf.h"
21#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070022#include "unicode/uclean.h"
23#include "unicode/uvernum.h"
24
Brian Carlstrom934486c2011-07-12 23:42:50 -070025namespace art {
26
Brian Carlstrom9f30b382011-08-28 22:41:38 -070027static inline const DexFile* OpenDexFileBase64(const char* base64,
28 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029 CHECK(base64 != NULL);
30 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070031 byte* dex_bytes = DecodeBase64(base64, &length);
32 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070033 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070035 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070036}
37
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038class ScratchFile {
39 public:
40 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070041 filename_ = getenv("ANDROID_DATA");
42 filename_ += "/TmpFile-XXXXXX";
43 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 CHECK_NE(-1, fd_);
45 }
46
47 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070048 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070049 CHECK_EQ(0, unlink_result);
50 int close_result = close(fd_);
51 CHECK_EQ(0, close_result);
52 }
53
54 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070055 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070056 }
57
58 int GetFd() const {
59 return fd_;
60 }
61
62 private:
Elliott Hughes34023802011-08-30 12:06:17 -070063 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070064 int fd_;
65};
66
Brian Carlstromf734cf52011-08-17 16:28:14 -070067class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070068 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070069 static void MakeExecutable(const ByteArray* byte_array) {
70 uintptr_t data = reinterpret_cast<uintptr_t>(byte_array->GetData());
71 uintptr_t base = RoundDown(data, kPageSize);
72 uintptr_t limit = RoundUp(data + byte_array->GetLength(), kPageSize);
73 uintptr_t len = limit - base;
74 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
75 CHECK_EQ(result, 0);
76 }
77
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078 protected:
79 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070080 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
81
Elliott Hughes0af55432011-08-17 18:37:28 -070082 if (is_host_) {
83 // $ANDROID_ROOT is set on the device, but not on the host.
84 // We need to set this so that icu4c can find its locale data.
85 std::string root;
86 root += getenv("ANDROID_BUILD_TOP");
87 root += "/out/host/linux-x86";
88 setenv("ANDROID_ROOT", root.c_str(), 1);
89 }
90
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070091 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
92 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -070093 if (mkdtemp(&android_data_[0]) == NULL) {
94 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
95 }
Elliott Hughes34023802011-08-30 12:06:17 -070096 setenv("ANDROID_DATA", android_data_.c_str(), 1);
97 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070098 art_cache_.append("/art-cache");
99 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
100 ASSERT_EQ(mkdir_result, 0);
101
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700102 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700104 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700105
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700106 Runtime::Options options;
107 options.push_back(std::make_pair("bootclasspath", &boot_class_path_));
108 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL)));
110 options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700111 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700112 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700113 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700114
Ian Rogers2c8f6532011-09-02 17:16:34 -0700115#if defined(__i386__)
Brian Carlstrom16192862011-09-12 17:50:06 -0700116 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(kX86));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700117 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kX86));
Ian Rogersff1ed472011-09-20 13:46:24 -0700118 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kX86));
Ian Rogers0e073f72011-09-09 10:45:46 -0700119 compiler_.reset(new Compiler(kX86));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700120#elif defined(__arm__)
Brian Carlstrom16192862011-09-12 17:50:06 -0700121 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
Ian Rogersff1ed472011-09-20 13:46:24 -0700122 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kThumb2));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700123 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
Ian Rogers0e073f72011-09-09 10:45:46 -0700124 compiler_.reset(new Compiler(kThumb2));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700125#endif
126
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700127 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700128 }
129
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700130 virtual void TearDown() {
131 const char* android_data = getenv("ANDROID_DATA");
132 ASSERT_TRUE(android_data != NULL);
133 DIR* dir = opendir(art_cache_.c_str());
134 ASSERT_TRUE(dir != NULL);
135 while (true) {
136 struct dirent entry;
137 struct dirent* entry_ptr;
138 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
139 ASSERT_EQ(0, readdir_result);
140 if (entry_ptr == NULL) {
141 break;
142 }
143 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
144 continue;
145 }
146 std::string filename(art_cache_);
147 filename.push_back('/');
148 filename.append(entry_ptr->d_name);
149 int unlink_result = unlink(filename.c_str());
150 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400151 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700152 closedir(dir);
153 int rmdir_cache_result = rmdir(art_cache_.c_str());
154 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700155 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700156 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700157
158 // icu4c has a fixed 10-element array "gCommonICUDataArray".
159 // If we run > 10 tests, we fill that array and u_setCommonData fails.
160 // There's a function to clear the array, but it's not public...
161 typedef void (*IcuCleanupFn)();
162 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
163 CHECK(sym != NULL);
164 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
165 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166
Ian Rogers0e073f72011-09-09 10:45:46 -0700167 compiler_.reset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700168
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700169 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700170 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400171
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700172 std::string GetLibCoreDexFileName() {
173 if (is_host_) {
174 const char* host_dir = getenv("ANDROID_HOST_OUT");
175 CHECK(host_dir != NULL);
176 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
177 }
178 return std::string("/system/framework/core.jar");
179 }
180
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700181 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700182 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Brian Carlstrom16192862011-09-12 17:50:06 -0700183 return DexFile::OpenZip(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400184 }
185
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700186 uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) {
187 for (size_t i = 0; i < dex_file.NumTypeIds(); i++) {
188 const DexFile::TypeId& type_id = dex_file.GetTypeId(i);
189 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
190 return i;
191 }
192 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700193 CHECK(false) << "Failed to find type index for " << descriptor;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700194 return 0;
195 }
196
197 uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file,
198 const StringPiece& class_descriptor,
199 const StringPiece& field_name) {
200 for (size_t i = 0; i < dex_file.NumFieldIds(); i++) {
201 const DexFile::FieldId& field_id = dex_file.GetFieldId(i);
202 if (class_descriptor == dex_file.GetFieldClassDescriptor(field_id)
203 && field_name == dex_file.GetFieldName(field_id)) {
204 return i;
205 }
206 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700207 CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700208 return 0;
209 }
210
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700211 const ClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700212 CHECK(dex_file != NULL);
213 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700214 std::vector<const DexFile*> dex_files;
215 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700216 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700217 }
218
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700219 const DexFile* OpenTestDexFile(const char* name) {
220 CHECK(name != NULL);
221 std::string filename;
222 if (is_host_) {
223 // on the host, just read target dex file
224 filename += getenv("ANDROID_PRODUCT_OUT");
225 }
226 filename += "/system/framework/art-test-dex-";
227 filename += name;
228 filename += ".jar";
Brian Carlstrom16192862011-09-12 17:50:06 -0700229 const DexFile* dex_file = DexFile::OpenZip(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700230 CHECK(dex_file != NULL) << "Failed to open " << filename;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700231 return dex_file;
232 }
233
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700234 const ClassLoader* LoadDex(const char* dex_name) {
235 const DexFile* dex_file = OpenTestDexFile(dex_name);
236 CHECK(dex_file != NULL);
237 loaded_dex_files_.push_back(dex_file);
238 class_linker_->RegisterDexFile(*dex_file);
239 std::vector<const DexFile*> class_path;
240 class_path.push_back(dex_file);
241 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
242 CHECK(class_loader != NULL);
243 Thread::Current()->SetClassLoaderOverride(class_loader);
244 return class_loader;
245 }
246
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700247 void CompileMethod(Method* method) {
248 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700249 compiler_->CompileOne(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700250 MakeExecutable(runtime_->GetJniStubArray());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700251 MakeExecutable(method->GetCodeArray());
252 MakeExecutable(method->GetInvokeStubArray());
253 }
254
255 void CompileDirectMethod(const ClassLoader* class_loader,
256 const char* class_name,
257 const char* method_name,
258 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700259 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700260 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
261 CHECK(klass != NULL) << "Class not found " << class_name;
262 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700263 CHECK(method != NULL) << "Direct method not found: "
264 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700265 CompileMethod(method);
266 }
267
268 void CompileVirtualMethod(const ClassLoader* class_loader,
269 const char* class_name,
270 const char* method_name,
271 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700272 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700273 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
274 CHECK(klass != NULL) << "Class not found " << class_name;
275 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700276 CHECK(method != NULL) << "Virtual method not found: "
277 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700278 CompileMethod(method);
279 }
280
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700281 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700282 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700283 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700284 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700285 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700286 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700287 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700288 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700289 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700290
291 private:
292 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700293};
294
Brian Carlstrom934486c2011-07-12 23:42:50 -0700295} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700296
297namespace std {
298
299// TODO: isn't gtest supposed to be able to print STL types for itself?
300template <typename T>
301std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700302 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700303 return os;
304}
305
306} // namespace std