blob: bbf2e2f23957cd0c4a8613445397b4ed172614ad [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 Carlstrom27ec9612011-09-19 20:20:38 -0700109 options.push_back(std::make_pair("-Xms16m", reinterpret_cast<void*>(NULL)));
110 options.push_back(std::make_pair("-Xmx16m", 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));
Ian Rogersff1ed472011-09-20 13:46:24 -0700117 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kX86));
Ian Rogers0e073f72011-09-09 10:45:46 -0700118 compiler_.reset(new Compiler(kX86));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700119#elif defined(__arm__)
Brian Carlstrom16192862011-09-12 17:50:06 -0700120 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
Ian Rogersff1ed472011-09-20 13:46:24 -0700121 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(kThumb2));
Ian Rogers0e073f72011-09-09 10:45:46 -0700122 compiler_.reset(new Compiler(kThumb2));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123#endif
124
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700125 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700126 }
127
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700128 virtual void TearDown() {
129 const char* android_data = getenv("ANDROID_DATA");
130 ASSERT_TRUE(android_data != NULL);
131 DIR* dir = opendir(art_cache_.c_str());
132 ASSERT_TRUE(dir != NULL);
133 while (true) {
134 struct dirent entry;
135 struct dirent* entry_ptr;
136 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
137 ASSERT_EQ(0, readdir_result);
138 if (entry_ptr == NULL) {
139 break;
140 }
141 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
142 continue;
143 }
144 std::string filename(art_cache_);
145 filename.push_back('/');
146 filename.append(entry_ptr->d_name);
147 int unlink_result = unlink(filename.c_str());
148 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400149 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700150 closedir(dir);
151 int rmdir_cache_result = rmdir(art_cache_.c_str());
152 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700153 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700154 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700155
156 // icu4c has a fixed 10-element array "gCommonICUDataArray".
157 // If we run > 10 tests, we fill that array and u_setCommonData fails.
158 // There's a function to clear the array, but it's not public...
159 typedef void (*IcuCleanupFn)();
160 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
161 CHECK(sym != NULL);
162 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
163 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164
Ian Rogers0e073f72011-09-09 10:45:46 -0700165 compiler_.reset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700166
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700167 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700168 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400169
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700170 std::string GetLibCoreDexFileName() {
171 if (is_host_) {
172 const char* host_dir = getenv("ANDROID_HOST_OUT");
173 CHECK(host_dir != NULL);
174 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
175 }
176 return std::string("/system/framework/core.jar");
177 }
178
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700179 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700180 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Brian Carlstrom16192862011-09-12 17:50:06 -0700181 return DexFile::OpenZip(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400182 }
183
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700184 uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) {
185 for (size_t i = 0; i < dex_file.NumTypeIds(); i++) {
186 const DexFile::TypeId& type_id = dex_file.GetTypeId(i);
187 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
188 return i;
189 }
190 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700191 CHECK(false) << "Failed to find type index for " << descriptor;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700192 return 0;
193 }
194
195 uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file,
196 const StringPiece& class_descriptor,
197 const StringPiece& field_name) {
198 for (size_t i = 0; i < dex_file.NumFieldIds(); i++) {
199 const DexFile::FieldId& field_id = dex_file.GetFieldId(i);
200 if (class_descriptor == dex_file.GetFieldClassDescriptor(field_id)
201 && field_name == dex_file.GetFieldName(field_id)) {
202 return i;
203 }
204 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700205 CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700206 return 0;
207 }
208
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700209 const ClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700210 CHECK(dex_file != NULL);
211 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700212 std::vector<const DexFile*> dex_files;
213 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700214 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700215 }
216
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700217 const DexFile* OpenTestDexFile(const char* name) {
218 CHECK(name != NULL);
219 std::string filename;
220 if (is_host_) {
221 // on the host, just read target dex file
222 filename += getenv("ANDROID_PRODUCT_OUT");
223 }
224 filename += "/system/framework/art-test-dex-";
225 filename += name;
226 filename += ".jar";
Brian Carlstrom16192862011-09-12 17:50:06 -0700227 const DexFile* dex_file = DexFile::OpenZip(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700228 CHECK(dex_file != NULL) << "Failed to open " << filename;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700229 return dex_file;
230 }
231
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700232 const ClassLoader* LoadDex(const char* dex_name) {
233 const DexFile* dex_file = OpenTestDexFile(dex_name);
234 CHECK(dex_file != NULL);
235 loaded_dex_files_.push_back(dex_file);
236 class_linker_->RegisterDexFile(*dex_file);
237 std::vector<const DexFile*> class_path;
238 class_path.push_back(dex_file);
239 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
240 CHECK(class_loader != NULL);
241 Thread::Current()->SetClassLoaderOverride(class_loader);
242 return class_loader;
243 }
244
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700245 void CompileMethod(Method* method) {
246 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700247 compiler_->CompileOne(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700248 MakeExecutable(runtime_->GetJniStubArray());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700249 MakeExecutable(method->GetCodeArray());
250 MakeExecutable(method->GetInvokeStubArray());
251 }
252
253 void CompileDirectMethod(const ClassLoader* class_loader,
254 const char* class_name,
255 const char* method_name,
256 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700257 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700258 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
259 CHECK(klass != NULL) << "Class not found " << class_name;
260 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700261 CHECK(method != NULL) << "Direct method not found: "
262 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700263 CompileMethod(method);
264 }
265
266 void CompileVirtualMethod(const ClassLoader* class_loader,
267 const char* class_name,
268 const char* method_name,
269 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700270 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700271 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
272 CHECK(klass != NULL) << "Class not found " << class_name;
273 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700274 CHECK(method != NULL) << "Virtual method not found: "
275 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700276 CompileMethod(method);
277 }
278
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700279 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700280 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700281 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700282 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700283 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700284 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700285 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700286 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700287 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700288
289 private:
290 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700291};
292
Brian Carlstrom934486c2011-07-12 23:42:50 -0700293} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700294
295namespace std {
296
297// TODO: isn't gtest supposed to be able to print STL types for itself?
298template <typename T>
299std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700300 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700301 return os;
302}
303
304} // namespace std