blob: 84b97e9783b9752e9dcd03070ea60a7662d058a8 [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"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070016#include "file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "gtest/gtest.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "heap.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070019#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include "runtime.h"
Elliott Hughes14134a12011-09-30 16:55:51 -070021#include "stl_util.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "stringprintf.h"
23#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070024#include "unicode/uclean.h"
25#include "unicode/uvernum.h"
26
Brian Carlstrom934486c2011-07-12 23:42:50 -070027namespace art {
28
Brian Carlstrom9f30b382011-08-28 22:41:38 -070029static inline const DexFile* OpenDexFileBase64(const char* base64,
30 const std::string& location) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070031 // decode base64
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032 CHECK(base64 != NULL);
33 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070034 byte* dex_bytes = DecodeBase64(base64, &length);
35 CHECK(dex_bytes != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070036
37 // write to provided file
38 UniquePtr<File> file(OS::OpenFile(location.c_str(), true));
39 CHECK(file.get() != NULL);
40 if (!file->WriteFully(dex_bytes, length)) {
41 PLOG(FATAL) << "Failed to write base64 as dex file";
42 }
43 file.reset();
44
45 // read dex file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070046 const DexFile* dex_file = DexFile::Open(location, "");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070047 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070048 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070049}
50
Brian Carlstromdb4d5402011-08-09 12:18:28 -070051class ScratchFile {
52 public:
53 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070054 filename_ = getenv("ANDROID_DATA");
55 filename_ += "/TmpFile-XXXXXX";
56 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070057 CHECK_NE(-1, fd_);
58 }
59
60 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070061 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070062 CHECK_EQ(0, unlink_result);
63 int close_result = close(fd_);
64 CHECK_EQ(0, close_result);
65 }
66
67 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070068 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070069 }
70
71 int GetFd() const {
72 return fd_;
73 }
74
75 private:
Elliott Hughes34023802011-08-30 12:06:17 -070076 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070077 int fd_;
78};
79
Brian Carlstromf734cf52011-08-17 16:28:14 -070080class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070081 public:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070082 static void MakeExecutable(const ByteArray* byte_array) {
83 uintptr_t data = reinterpret_cast<uintptr_t>(byte_array->GetData());
84 uintptr_t base = RoundDown(data, kPageSize);
85 uintptr_t limit = RoundUp(data + byte_array->GetLength(), kPageSize);
86 uintptr_t len = limit - base;
87 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
88 CHECK_EQ(result, 0);
89 }
90
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 protected:
92 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070093 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
94
Elliott Hughes0af55432011-08-17 18:37:28 -070095 if (is_host_) {
96 // $ANDROID_ROOT is set on the device, but not on the host.
97 // We need to set this so that icu4c can find its locale data.
98 std::string root;
99 root += getenv("ANDROID_BUILD_TOP");
100 root += "/out/host/linux-x86";
101 setenv("ANDROID_ROOT", root.c_str(), 1);
102 }
103
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700104 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
105 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700106 if (mkdtemp(&android_data_[0]) == NULL) {
107 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
108 }
Elliott Hughes34023802011-08-30 12:06:17 -0700109 setenv("ANDROID_DATA", android_data_.c_str(), 1);
110 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700111 art_cache_.append("/art-cache");
112 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
113 ASSERT_EQ(mkdir_result, 0);
114
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700115 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700116
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700117 std::string boot_class_path;
118 boot_class_path += "-Xbootclasspath:";
119 boot_class_path += GetLibCoreDexFileName();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700120
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700121 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700122 options.push_back(std::make_pair(boot_class_path.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700123 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL)));
125 options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700126 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700127 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700128 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700129
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700130 InstructionSet instruction_set = kNone;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700131#if defined(__i386__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700132 instruction_set = kX86;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700133#elif defined(__arm__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700134 instruction_set = kThumb2;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700135#endif
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700136 runtime_->SetJniStubArray(JniCompiler::CreateJniStub(instruction_set));
137 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
138 for (int i=0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700139 Runtime::TrampolineType type = Runtime::TrampolineType(i);
140 if (!runtime_->HasResolutionStubArray(type)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700141 runtime_->SetResolutionStubArray(
142 Compiler::CreateResolutionStub(instruction_set, type), type);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700143 }
144 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700145 for (int i=0; i < Runtime::kLastCalleeSaveType; i++) {
146 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
147 if (!runtime_->HasCalleeSaveMethod(type)) {
148 runtime_->SetCalleeSaveMethod(
149 runtime_->CreateCalleeSaveMethod(instruction_set, type), type);
150 }
151 }
152 compiler_.reset(new Compiler(instruction_set));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700153
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700155 }
156
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700157 virtual void TearDown() {
158 const char* android_data = getenv("ANDROID_DATA");
159 ASSERT_TRUE(android_data != NULL);
160 DIR* dir = opendir(art_cache_.c_str());
161 ASSERT_TRUE(dir != NULL);
162 while (true) {
163 struct dirent entry;
164 struct dirent* entry_ptr;
165 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
166 ASSERT_EQ(0, readdir_result);
167 if (entry_ptr == NULL) {
168 break;
169 }
170 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
171 continue;
172 }
173 std::string filename(art_cache_);
174 filename.push_back('/');
175 filename.append(entry_ptr->d_name);
176 int unlink_result = unlink(filename.c_str());
177 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400178 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700179 closedir(dir);
180 int rmdir_cache_result = rmdir(art_cache_.c_str());
181 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700182 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700183 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700184
185 // icu4c has a fixed 10-element array "gCommonICUDataArray".
186 // If we run > 10 tests, we fill that array and u_setCommonData fails.
187 // There's a function to clear the array, but it's not public...
188 typedef void (*IcuCleanupFn)();
189 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
190 CHECK(sym != NULL);
191 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
192 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193
Ian Rogers0e073f72011-09-09 10:45:46 -0700194 compiler_.reset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700195
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700197 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400198
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700199 std::string GetLibCoreDexFileName() {
200 if (is_host_) {
201 const char* host_dir = getenv("ANDROID_HOST_OUT");
202 CHECK(host_dir != NULL);
203 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
204 }
205 return std::string("/system/framework/core.jar");
206 }
207
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700208 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700209 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700210 return DexFile::Open(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400211 }
212
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700213 uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) {
214 for (size_t i = 0; i < dex_file.NumTypeIds(); i++) {
215 const DexFile::TypeId& type_id = dex_file.GetTypeId(i);
216 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
217 return i;
218 }
219 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700220 CHECK(false) << "Failed to find type index for " << descriptor;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700221 return 0;
222 }
223
224 uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file,
225 const StringPiece& class_descriptor,
226 const StringPiece& field_name) {
227 for (size_t i = 0; i < dex_file.NumFieldIds(); i++) {
228 const DexFile::FieldId& field_id = dex_file.GetFieldId(i);
229 if (class_descriptor == dex_file.GetFieldClassDescriptor(field_id)
230 && field_name == dex_file.GetFieldName(field_id)) {
231 return i;
232 }
233 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700235 return 0;
236 }
237
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700238 const ClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700239 CHECK(dex_file != NULL);
240 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700241 std::vector<const DexFile*> dex_files;
242 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700243 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700244 }
245
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700246 const DexFile* OpenTestDexFile(const char* name) {
247 CHECK(name != NULL);
248 std::string filename;
249 if (is_host_) {
250 // on the host, just read target dex file
251 filename += getenv("ANDROID_PRODUCT_OUT");
252 }
253 filename += "/system/framework/art-test-dex-";
254 filename += name;
255 filename += ".jar";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700256 const DexFile* dex_file = DexFile::Open(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700257 CHECK(dex_file != NULL) << "Failed to open " << filename;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700258 return dex_file;
259 }
260
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700261 const ClassLoader* LoadDex(const char* dex_name) {
262 const DexFile* dex_file = OpenTestDexFile(dex_name);
263 CHECK(dex_file != NULL);
264 loaded_dex_files_.push_back(dex_file);
265 class_linker_->RegisterDexFile(*dex_file);
266 std::vector<const DexFile*> class_path;
267 class_path.push_back(dex_file);
268 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
269 CHECK(class_loader != NULL);
270 Thread::Current()->SetClassLoaderOverride(class_loader);
271 return class_loader;
272 }
273
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700274 void CompileMethod(Method* method) {
275 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700276 compiler_->CompileOne(method);
Brian Carlstrom16192862011-09-12 17:50:06 -0700277 MakeExecutable(runtime_->GetJniStubArray());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700278 MakeExecutable(method->GetCodeArray());
279 MakeExecutable(method->GetInvokeStubArray());
280 }
281
282 void CompileDirectMethod(const ClassLoader* class_loader,
283 const char* class_name,
284 const char* method_name,
285 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700286 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700287 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
288 CHECK(klass != NULL) << "Class not found " << class_name;
289 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700290 CHECK(method != NULL) << "Direct method not found: "
291 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700292 CompileMethod(method);
293 }
294
295 void CompileVirtualMethod(const ClassLoader* class_loader,
296 const char* class_name,
297 const char* method_name,
298 const char* signature) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700299 std::string class_descriptor = DotToDescriptor(class_name);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700300 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
301 CHECK(klass != NULL) << "Class not found " << class_name;
302 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700303 CHECK(method != NULL) << "Virtual method not found: "
304 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700305 CompileMethod(method);
306 }
307
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700308 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700309 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700310 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700311 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700312 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700313 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700314 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700315 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700316 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700317
318 private:
319 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700320};
321
Brian Carlstrom934486c2011-07-12 23:42:50 -0700322} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700323
324namespace std {
325
326// TODO: isn't gtest supposed to be able to print STL types for itself?
327template <typename T>
328std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700329 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700330 return os;
331}
332
333} // namespace std