blob: 3da2f37d8918affa416b69e4817a530c32326e86 [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 "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070012#include "compiler.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070013#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "dex_file.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070015#include "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"
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -080018#include "macros.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070019#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "object_utils.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070021#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070022#include "runtime.h"
Elliott Hughes14134a12011-09-30 16:55:51 -070023#include "stl_util.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070024#include "stringprintf.h"
25#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070026#include "unicode/uclean.h"
27#include "unicode/uvernum.h"
28
Brian Carlstrom934486c2011-07-12 23:42:50 -070029namespace art {
30
Brian Carlstromb9cc1ca2012-01-27 00:57:42 -080031static const byte kBase64Map[256] = {
32 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
33 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
36 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
37 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
38 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
39 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
40 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
41 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
42 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
44 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
45 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
46 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
47 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
48 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
49 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
50 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
51 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
52 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
53 255, 255, 255, 255
54};
55
56byte* DecodeBase64(const char* src, size_t* dst_size) {
57 std::vector<byte> tmp;
58 unsigned long t = 0, y = 0;
59 int g = 3;
60 for (size_t i = 0; src[i] != '\0'; ++i) {
61 byte c = kBase64Map[src[i] & 0xFF];
62 if (c == 255) continue;
63 // the final = symbols are read and used to trim the remaining bytes
64 if (c == 254) {
65 c = 0;
66 // prevent g < 0 which would potentially allow an overflow later
67 if (--g < 0) {
68 return NULL;
69 }
70 } else if (g != 3) {
71 // we only allow = to be at the end
72 return NULL;
73 }
74 t = (t << 6) | c;
75 if (++y == 4) {
76 tmp.push_back((t >> 16) & 255);
77 if (g > 1) {
78 tmp.push_back((t >> 8) & 255);
79 }
80 if (g > 2) {
81 tmp.push_back(t & 255);
82 }
83 y = t = 0;
84 }
85 }
86 if (y != 0) {
87 return NULL;
88 }
89 UniquePtr<byte[]> dst(new byte[tmp.size()]);
90 if (dst_size != NULL) {
91 *dst_size = tmp.size();
92 }
93 std::copy(tmp.begin(), tmp.end(), dst.get());
94 return dst.release();
95}
96
Brian Carlstrom9f30b382011-08-28 22:41:38 -070097static inline const DexFile* OpenDexFileBase64(const char* base64,
98 const std::string& location) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070099 // decode base64
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700100 CHECK(base64 != NULL);
101 size_t length;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800102 UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length));
103 CHECK(dex_bytes.get() != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700104
105 // write to provided file
106 UniquePtr<File> file(OS::OpenFile(location.c_str(), true));
107 CHECK(file.get() != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800108 if (!file->WriteFully(dex_bytes.get(), length)) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -0700109 PLOG(FATAL) << "Failed to write base64 as dex file";
110 }
111 file.reset();
112
113 // read dex file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700114 const DexFile* dex_file = DexFile::Open(location, "");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700115 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700116 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700117}
118
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700119class ScratchFile {
120 public:
121 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -0700122 filename_ = getenv("ANDROID_DATA");
123 filename_ += "/TmpFile-XXXXXX";
124 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700125 CHECK_NE(-1, fd_);
Elliott Hughes234da572011-11-03 22:13:06 -0700126 file_.reset(OS::FileFromFd(GetFilename(), fd_));
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700127 }
128
129 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -0700130 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700131 CHECK_EQ(0, unlink_result);
132 int close_result = close(fd_);
133 CHECK_EQ(0, close_result);
134 }
135
136 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -0700137 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700138 }
139
Elliott Hughes234da572011-11-03 22:13:06 -0700140 File* GetFile() const {
141 return file_.get();
142 }
143
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700144 int GetFd() const {
145 return fd_;
146 }
147
148 private:
Elliott Hughes34023802011-08-30 12:06:17 -0700149 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700150 int fd_;
Elliott Hughes234da572011-11-03 22:13:06 -0700151 UniquePtr<File> file_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700152};
153
Brian Carlstromf734cf52011-08-17 16:28:14 -0700154class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700155 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700156
157 static void MakeExecutable(const ByteArray* code_array) {
158 CHECK(code_array != NULL);
159 MakeExecutable(code_array->GetData(), code_array->GetLength());
160 }
161
162 static void MakeExecutable(const std::vector<uint8_t>& code) {
163 CHECK_NE(code.size(), 0U);
164 MakeExecutable(&code[0], code.size());
165 }
166
Brian Carlstromae826982011-11-09 01:33:42 -0800167 // Create an OatMethod based on pointers (for unit tests)
168 OatFile::OatMethod CreateOatMethod(const void* code,
169 const size_t frame_size_in_bytes,
170 const uint32_t core_spill_mask,
171 const uint32_t fp_spill_mask,
172 const uint32_t* mapping_table,
173 const uint16_t* vmap_table,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800174 const uint8_t* gc_map,
Brian Carlstromae826982011-11-09 01:33:42 -0800175 const Method::InvokeStub* invoke_stub) {
176 return OatFile::OatMethod(NULL,
177 reinterpret_cast<uint32_t>(code),
178 frame_size_in_bytes,
179 core_spill_mask,
180 fp_spill_mask,
181 reinterpret_cast<uint32_t>(mapping_table),
182 reinterpret_cast<uint32_t>(vmap_table),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800183 reinterpret_cast<uint32_t>(gc_map),
Brian Carlstromae826982011-11-09 01:33:42 -0800184 reinterpret_cast<uint32_t>(invoke_stub));
185 }
186
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700187 void MakeExecutable(Method* method) {
188 CHECK(method != NULL);
189
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 MethodHelper mh(method);
Ian Rogers0571d352011-11-03 19:51:38 -0700191 const CompiledInvokeStub* compiled_invoke_stub =
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 compiler_->FindInvokeStub(mh.IsStatic(), mh.GetShorty());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700193 CHECK(compiled_invoke_stub != NULL) << PrettyMethod(method);
194 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
195 MakeExecutable(invoke_stub);
196 const Method::InvokeStub* method_invoke_stub
197 = reinterpret_cast<const Method::InvokeStub*>(&invoke_stub[0]);
198 LOG(INFO) << "MakeExecutable " << PrettyMethod(method)
199 << " invoke_stub=" << reinterpret_cast<void*>(method_invoke_stub);
200
201 if (!method->IsAbstract()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700202 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
203 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
204 const CompiledMethod* compiled_method =
205 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
206 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700207 CHECK(compiled_method != NULL) << PrettyMethod(method);
208 const std::vector<uint8_t>& code = compiled_method->GetCode();
209 MakeExecutable(code);
210 const void* method_code
211 = CompiledMethod::CodePointer(&code[0], compiled_method->GetInstructionSet());
212 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800213 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
214 compiled_method->GetFrameSizeInBytes(),
215 compiled_method->GetCoreSpillMask(),
216 compiled_method->GetFpSpillMask(),
217 &compiled_method->GetMappingTable()[0],
218 &compiled_method->GetVmapTable()[0],
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800219 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800220 method_invoke_stub);
221 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700222 } else {
223 MakeExecutable(runtime_->GetAbstractMethodErrorStubArray());
224 const void* method_code = runtime_->GetAbstractMethodErrorStubArray()->GetData();
225 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800226 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
227 kStackAlignment,
228 0,
229 0,
230 NULL,
231 NULL,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800232 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800233 method_invoke_stub);
234 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700235 }
236 }
237
238 static void MakeExecutable(const void* code_start, size_t code_length) {
239 CHECK(code_start != NULL);
240 CHECK_NE(code_length, 0U);
241 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700242 uintptr_t base = RoundDown(data, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700243 uintptr_t limit = RoundUp(data + code_length, kPageSize);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700244 uintptr_t len = limit - base;
245 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800246 CHECK_EQ(result, 0);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800247
Ian Rogers16341552011-10-10 11:33:06 -0700248 // Flush instruction cache
Shih-wei Liao24782c62012-01-08 12:46:11 -0800249 // Only uses __builtin___clear_cache if GCC >= 4.3.3
250#if GCC_VERSION >= 40303
Ian Rogers16341552011-10-10 11:33:06 -0700251 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800252#elif defined(__APPLE__)
Shih-wei Liao24782c62012-01-08 12:46:11 -0800253 // Currently, only Mac OS builds use GCC 4.2.*. Those host builds do not
254 // need to generate clear_cache on x86.
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800255#else
256#error unsupported
Shih-wei Liao24782c62012-01-08 12:46:11 -0800257#endif
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700258 }
259
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700260 protected:
261 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700262 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
263
Elliott Hughes0af55432011-08-17 18:37:28 -0700264 if (is_host_) {
265 // $ANDROID_ROOT is set on the device, but not on the host.
266 // We need to set this so that icu4c can find its locale data.
267 std::string root;
268 root += getenv("ANDROID_BUILD_TOP");
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800269#if defined(__linux__)
Elliott Hughes0af55432011-08-17 18:37:28 -0700270 root += "/out/host/linux-x86";
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800271#elif defined(__APPLE__)
272 root += "/out/host/darwin-x86";
273#else
274#error unsupported OS
275#endif
Elliott Hughes0af55432011-08-17 18:37:28 -0700276 setenv("ANDROID_ROOT", root.c_str(), 1);
277 }
278
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700279 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
280 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700281 if (mkdtemp(&android_data_[0]) == NULL) {
282 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
283 }
Elliott Hughes34023802011-08-30 12:06:17 -0700284 setenv("ANDROID_DATA", android_data_.c_str(), 1);
285 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700286 art_cache_.append("/art-cache");
287 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
288 ASSERT_EQ(mkdir_result, 0);
289
Elliott Hughes9b06a0d2012-01-27 16:08:55 -0800290 java_lang_dex_file_.reset(DexFile::Open(GetLibCoreDexFileName(), ""));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700291
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700292 std::string boot_class_path;
293 boot_class_path += "-Xbootclasspath:";
294 boot_class_path += GetLibCoreDexFileName();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700295
Elliott Hughes699f9f42012-01-27 13:04:16 -0800296 std::string min_heap_string(StringPrintf("-Xms%zdm", Heap::kInitialSize / MB));
297 std::string max_heap_string(StringPrintf("-Xmx%zdm", Heap::kMaximumSize / MB));
Ian Rogers30fab402012-01-23 15:43:46 -0800298
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 Runtime::Options options;
Brian Carlstroma4a7b482011-10-16 15:29:16 -0700300 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700301 options.push_back(std::make_pair(boot_class_path.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700302 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Ian Rogers30fab402012-01-23 15:43:46 -0800303 options.push_back(std::make_pair(min_heap_string.c_str(), reinterpret_cast<void*>(NULL)));
304 options.push_back(std::make_pair(max_heap_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700305 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700306 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700307 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700309 InstructionSet instruction_set = kNone;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700310#if defined(__i386__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700311 instruction_set = kX86;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700312#elif defined(__arm__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700313 instruction_set = kThumb2;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700314#endif
Elliott Hughes8add92d2012-01-18 18:18:43 -0800315 runtime_->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700316 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700317 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700318 Runtime::TrampolineType type = Runtime::TrampolineType(i);
319 if (!runtime_->HasResolutionStubArray(type)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700320 runtime_->SetResolutionStubArray(
321 Compiler::CreateResolutionStub(instruction_set, type), type);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700322 }
323 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700324 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700325 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
326 if (!runtime_->HasCalleeSaveMethod(type)) {
327 runtime_->SetCalleeSaveMethod(
328 runtime_->CreateCalleeSaveMethod(instruction_set, type), type);
329 }
330 }
Brian Carlstromae826982011-11-09 01:33:42 -0800331 compiler_.reset(new Compiler(instruction_set, false, NULL));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700332
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700334 }
335
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700336 virtual void TearDown() {
337 const char* android_data = getenv("ANDROID_DATA");
338 ASSERT_TRUE(android_data != NULL);
339 DIR* dir = opendir(art_cache_.c_str());
340 ASSERT_TRUE(dir != NULL);
341 while (true) {
342 struct dirent entry;
343 struct dirent* entry_ptr;
344 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
345 ASSERT_EQ(0, readdir_result);
346 if (entry_ptr == NULL) {
347 break;
348 }
349 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
350 continue;
351 }
352 std::string filename(art_cache_);
353 filename.push_back('/');
354 filename.append(entry_ptr->d_name);
355 int unlink_result = unlink(filename.c_str());
356 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400357 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700358 closedir(dir);
359 int rmdir_cache_result = rmdir(art_cache_.c_str());
360 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700361 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700362 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700363
364 // icu4c has a fixed 10-element array "gCommonICUDataArray".
365 // If we run > 10 tests, we fill that array and u_setCommonData fails.
366 // There's a function to clear the array, but it's not public...
367 typedef void (*IcuCleanupFn)();
368 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
369 CHECK(sym != NULL);
370 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
371 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700372
Ian Rogers0e073f72011-09-09 10:45:46 -0700373 compiler_.reset();
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800374 STLDeleteElements(&opened_dex_files_);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700377 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400378
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700379 std::string GetLibCoreDexFileName() {
380 if (is_host_) {
381 const char* host_dir = getenv("ANDROID_HOST_OUT");
382 CHECK(host_dir != NULL);
383 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
384 }
385 return std::string("/system/framework/core.jar");
386 }
387
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700388 const DexFile* OpenTestDexFile(const char* name) {
389 CHECK(name != NULL);
390 std::string filename;
391 if (is_host_) {
392 // on the host, just read target dex file
393 filename += getenv("ANDROID_PRODUCT_OUT");
394 }
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700395 filename += "/data/art-test/art-test-dex-";
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700396 filename += name;
397 filename += ".jar";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700398 const DexFile* dex_file = DexFile::Open(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700399 CHECK(dex_file != NULL) << "Failed to open " << filename;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800400 opened_dex_files_.push_back(dex_file);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700401 return dex_file;
402 }
403
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700404 ClassLoader* LoadDex(const char* dex_name) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700405 const DexFile* dex_file = OpenTestDexFile(dex_name);
406 CHECK(dex_file != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700407 class_linker_->RegisterDexFile(*dex_file);
408 std::vector<const DexFile*> class_path;
409 class_path.push_back(dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700410 SirtRef<ClassLoader> class_loader(PathClassLoader::AllocCompileTime(class_path));
411 CHECK(class_loader.get() != NULL);
412 Thread::Current()->SetClassLoaderOverride(class_loader.get());
413 return class_loader.get();
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700414 }
415
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416 void CompileClass(const ClassLoader* class_loader, const char* class_name) {
Elliott Hughes95572412011-12-13 18:14:20 -0800417 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800418 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700419 CHECK(klass != NULL) << "Class not found " << class_name;
420 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
421 CompileMethod(klass->GetDirectMethod(i));
422 }
423 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
424 CompileMethod(klass->GetVirtualMethod(i));
425 }
426 }
427
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700428 void CompileMethod(Method* method) {
429 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700430 compiler_->CompileOne(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700431 MakeExecutable(method);
432
Ian Rogers169c9a72011-11-13 20:13:17 -0800433 MakeExecutable(runtime_->GetJniDlsymLookupStub());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700434 }
435
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700436 void CompileDirectMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700437 const char* class_name,
438 const char* method_name,
439 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800440 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800441 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700442 CHECK(klass != NULL) << "Class not found " << class_name;
443 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700444 CHECK(method != NULL) << "Direct method not found: "
445 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700446 CompileMethod(method);
447 }
448
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700449 void CompileVirtualMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700450 const char* class_name,
451 const char* method_name,
452 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800453 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800454 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700455 CHECK(klass != NULL) << "Class not found " << class_name;
456 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700457 CHECK(method != NULL) << "Virtual method not found: "
458 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700459 CompileMethod(method);
460 }
461
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700462 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700463 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700464 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700465 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700466 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700467 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700468 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700469 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700470 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700471
472 private:
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800473 std::vector<const DexFile*> opened_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700474};
475
Brian Carlstrom934486c2011-07-12 23:42:50 -0700476} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700477
478namespace std {
479
480// TODO: isn't gtest supposed to be able to print STL types for itself?
481template <typename T>
482std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700483 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700484 return os;
485}
486
487} // namespace std