blob: 844fa00a55ded15c1d5ad7965b64e39d82429624 [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 Hughes90a33692011-08-30 13:27:07 -070019#include "stringprintf.h"
20#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070021#include "unicode/uclean.h"
22#include "unicode/uvernum.h"
23
Brian Carlstrom934486c2011-07-12 23:42:50 -070024namespace art {
25
Brian Carlstrom9f30b382011-08-28 22:41:38 -070026static inline const DexFile* OpenDexFileBase64(const char* base64,
27 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028 CHECK(base64 != NULL);
29 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070030 byte* dex_bytes = DecodeBase64(base64, &length);
31 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070032 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070034 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035}
36
Brian Carlstromdb4d5402011-08-09 12:18:28 -070037class ScratchFile {
38 public:
39 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070040 filename_ = getenv("ANDROID_DATA");
41 filename_ += "/TmpFile-XXXXXX";
42 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043 CHECK_NE(-1, fd_);
44 }
45
46 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070047 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048 CHECK_EQ(0, unlink_result);
49 int close_result = close(fd_);
50 CHECK_EQ(0, close_result);
51 }
52
53 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070054 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070055 }
56
57 int GetFd() const {
58 return fd_;
59 }
60
61 private:
Elliott Hughes34023802011-08-30 12:06:17 -070062 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070063 int fd_;
64};
65
Brian Carlstromf734cf52011-08-17 16:28:14 -070066class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070067 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070068 static void MakeExecutable(const ByteArray* byte_array) {
69 uintptr_t data = reinterpret_cast<uintptr_t>(byte_array->GetData());
70 uintptr_t base = RoundDown(data, kPageSize);
71 uintptr_t limit = RoundUp(data + byte_array->GetLength(), kPageSize);
72 uintptr_t len = limit - base;
73 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
74 CHECK_EQ(result, 0);
75 }
76
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070077 protected:
78 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070079 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
80
Elliott Hughes0af55432011-08-17 18:37:28 -070081 if (is_host_) {
82 // $ANDROID_ROOT is set on the device, but not on the host.
83 // We need to set this so that icu4c can find its locale data.
84 std::string root;
85 root += getenv("ANDROID_BUILD_TOP");
86 root += "/out/host/linux-x86";
87 setenv("ANDROID_ROOT", root.c_str(), 1);
88 }
89
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070090 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
91 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -070092 if (mkdtemp(&android_data_[0]) == NULL) {
93 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
94 }
Elliott Hughes34023802011-08-30 12:06:17 -070095 setenv("ANDROID_DATA", android_data_.c_str(), 1);
96 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070097 art_cache_.append("/art-cache");
98 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
99 ASSERT_EQ(mkdir_result, 0);
100
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700101 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700102
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700105 Runtime::Options options;
106 options.push_back(std::make_pair("bootclasspath", &boot_class_path_));
107 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700108 options.push_back(std::make_pair("-Xms16m", reinterpret_cast<void*>(NULL)));
109 options.push_back(std::make_pair("-Xmx16m", reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700110 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700111 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700112 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700113
Ian Rogers2c8f6532011-09-02 17:16:34 -0700114#if defined(__i386__)
Brian Carlstrom16192862011-09-12 17:50:06 -0700115 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(kX86));
Ian Rogersff1ed472011-09-20 13:46:24 -0700116 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kX86));
Ian Rogers0e073f72011-09-09 10:45:46 -0700117 compiler_.reset(new Compiler(kX86));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118#elif defined(__arm__)
Brian Carlstrom16192862011-09-12 17:50:06 -0700119 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
Ian Rogersff1ed472011-09-20 13:46:24 -0700120 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kThumb2));
Ian Rogers0e073f72011-09-09 10:45:46 -0700121 compiler_.reset(new Compiler(kThumb2));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700122#endif
123
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700124 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700125 }
126
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700127 virtual void TearDown() {
128 const char* android_data = getenv("ANDROID_DATA");
129 ASSERT_TRUE(android_data != NULL);
130 DIR* dir = opendir(art_cache_.c_str());
131 ASSERT_TRUE(dir != NULL);
132 while (true) {
133 struct dirent entry;
134 struct dirent* entry_ptr;
135 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
136 ASSERT_EQ(0, readdir_result);
137 if (entry_ptr == NULL) {
138 break;
139 }
140 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
141 continue;
142 }
143 std::string filename(art_cache_);
144 filename.push_back('/');
145 filename.append(entry_ptr->d_name);
146 int unlink_result = unlink(filename.c_str());
147 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400148 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700149 closedir(dir);
150 int rmdir_cache_result = rmdir(art_cache_.c_str());
151 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700152 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700153 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700154
155 // icu4c has a fixed 10-element array "gCommonICUDataArray".
156 // If we run > 10 tests, we fill that array and u_setCommonData fails.
157 // There's a function to clear the array, but it's not public...
158 typedef void (*IcuCleanupFn)();
159 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
160 CHECK(sym != NULL);
161 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
162 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163
Ian Rogers0e073f72011-09-09 10:45:46 -0700164 compiler_.reset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700167 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400168
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700169 std::string GetLibCoreDexFileName() {
170 if (is_host_) {
171 const char* host_dir = getenv("ANDROID_HOST_OUT");
172 CHECK(host_dir != NULL);
173 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
174 }
175 return std::string("/system/framework/core.jar");
176 }
177
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700178 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700179 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Brian Carlstrom16192862011-09-12 17:50:06 -0700180 return DexFile::OpenZip(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400181 }
182
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700183 uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) {
184 for (size_t i = 0; i < dex_file.NumTypeIds(); i++) {
185 const DexFile::TypeId& type_id = dex_file.GetTypeId(i);
186 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
187 return i;
188 }
189 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700190 CHECK(false) << "Failed to find type index for " << descriptor;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700191 return 0;
192 }
193
194 uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file,
195 const StringPiece& class_descriptor,
196 const StringPiece& field_name) {
197 for (size_t i = 0; i < dex_file.NumFieldIds(); i++) {
198 const DexFile::FieldId& field_id = dex_file.GetFieldId(i);
199 if (class_descriptor == dex_file.GetFieldClassDescriptor(field_id)
200 && field_name == dex_file.GetFieldName(field_id)) {
201 return i;
202 }
203 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700204 CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700205 return 0;
206 }
207
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700208 const ClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700209 CHECK(dex_file != NULL);
210 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700211 std::vector<const DexFile*> dex_files;
212 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700213 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700214 }
215
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700216 const DexFile* OpenTestDexFile(const char* name) {
217 CHECK(name != NULL);
218 std::string filename;
219 if (is_host_) {
220 // on the host, just read target dex file
221 filename += getenv("ANDROID_PRODUCT_OUT");
222 }
223 filename += "/system/framework/art-test-dex-";
224 filename += name;
225 filename += ".jar";
Brian Carlstrom16192862011-09-12 17:50:06 -0700226 const DexFile* dex_file = DexFile::OpenZip(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700227 CHECK(dex_file != NULL) << "Failed to open " << filename;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700228 return dex_file;
229 }
230
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700231 const ClassLoader* LoadDex(const char* dex_name) {
232 const DexFile* dex_file = OpenTestDexFile(dex_name);
233 CHECK(dex_file != NULL);
234 loaded_dex_files_.push_back(dex_file);
235 class_linker_->RegisterDexFile(*dex_file);
236 std::vector<const DexFile*> class_path;
237 class_path.push_back(dex_file);
238 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
239 CHECK(class_loader != NULL);
240 Thread::Current()->SetClassLoaderOverride(class_loader);
241 return class_loader;
242 }
243
244 std::string ConvertClassNameToClassDescriptor(const char* class_name) {
245 std::string desc;
246 desc += "L";
247 desc += class_name;
248 desc += ";";
249 std::replace(desc.begin(), desc.end(), '.', '/');
250 return desc;
251 }
252
253 void CompileMethod(Method* method) {
254 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700255 compiler_->CompileOne(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700256 MakeExecutable(runtime_->GetJniStubArray());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700257 MakeExecutable(method->GetCodeArray());
258 MakeExecutable(method->GetInvokeStubArray());
259 }
260
261 void CompileDirectMethod(const ClassLoader* class_loader,
262 const char* class_name,
263 const char* method_name,
264 const char* signature) {
265 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
266 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
267 CHECK(klass != NULL) << "Class not found " << class_name;
268 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700269 CHECK(method != NULL) << "Direct method not found: "
270 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700271 CompileMethod(method);
272 }
273
274 void CompileVirtualMethod(const ClassLoader* class_loader,
275 const char* class_name,
276 const char* method_name,
277 const char* signature) {
278 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
279 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
280 CHECK(klass != NULL) << "Class not found " << class_name;
281 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700282 CHECK(method != NULL) << "Virtual method not found: "
283 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700284 CompileMethod(method);
285 }
286
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700287 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700288 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700289 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700290 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700291 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700292 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700293 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700294 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700295 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700296
297 private:
298 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700299};
300
Brian Carlstrom934486c2011-07-12 23:42:50 -0700301} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700302
303namespace std {
304
305// TODO: isn't gtest supposed to be able to print STL types for itself?
306template <typename T>
307std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
308 os << "[";
309 for (size_t i = 0; i < rhs.size(); ++i) {
310 os << rhs[i];
311 if (i < rhs.size() - 1) {
312 os << ", ";
313 }
314 }
315 os << "]";
316 return os;
317}
318
319} // namespace std