blob: 14adfbe182d1843e9ca4db24ab0fa032a0852c76 [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 Carlstrom7a00a3c2012-01-25 18:38:03 -080019#include "macros.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070020#include "oat_file.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Brian Carlstrom33f741e2011-10-03 11:24:05 -070022#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "runtime.h"
Elliott Hughes14134a12011-09-30 16:55:51 -070024#include "stl_util.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070025#include "stringprintf.h"
26#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070027#include "unicode/uclean.h"
28#include "unicode/uvernum.h"
29
Brian Carlstrom934486c2011-07-12 23:42:50 -070030namespace art {
31
Brian Carlstrom9f30b382011-08-28 22:41:38 -070032static inline const DexFile* OpenDexFileBase64(const char* base64,
33 const std::string& location) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070034 // decode base64
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035 CHECK(base64 != NULL);
36 size_t length;
Elliott Hughes4d6850c2012-01-18 15:55:06 -080037 UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length));
38 CHECK(dex_bytes.get() != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070039
40 // write to provided file
41 UniquePtr<File> file(OS::OpenFile(location.c_str(), true));
42 CHECK(file.get() != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -080043 if (!file->WriteFully(dex_bytes.get(), length)) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070044 PLOG(FATAL) << "Failed to write base64 as dex file";
45 }
46 file.reset();
47
48 // read dex file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070049 const DexFile* dex_file = DexFile::Open(location, "");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070050 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070051 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052}
53
Brian Carlstromdb4d5402011-08-09 12:18:28 -070054class ScratchFile {
55 public:
56 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070057 filename_ = getenv("ANDROID_DATA");
58 filename_ += "/TmpFile-XXXXXX";
59 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070060 CHECK_NE(-1, fd_);
Elliott Hughes234da572011-11-03 22:13:06 -070061 file_.reset(OS::FileFromFd(GetFilename(), fd_));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070062 }
63
64 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070065 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070066 CHECK_EQ(0, unlink_result);
67 int close_result = close(fd_);
68 CHECK_EQ(0, close_result);
69 }
70
71 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070072 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070073 }
74
Elliott Hughes234da572011-11-03 22:13:06 -070075 File* GetFile() const {
76 return file_.get();
77 }
78
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 int GetFd() const {
80 return fd_;
81 }
82
83 private:
Elliott Hughes34023802011-08-30 12:06:17 -070084 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 int fd_;
Elliott Hughes234da572011-11-03 22:13:06 -070086 UniquePtr<File> file_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070087};
88
Brian Carlstromf734cf52011-08-17 16:28:14 -070089class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070090 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070091
92 static void MakeExecutable(const ByteArray* code_array) {
93 CHECK(code_array != NULL);
94 MakeExecutable(code_array->GetData(), code_array->GetLength());
95 }
96
97 static void MakeExecutable(const std::vector<uint8_t>& code) {
98 CHECK_NE(code.size(), 0U);
99 MakeExecutable(&code[0], code.size());
100 }
101
Brian Carlstromae826982011-11-09 01:33:42 -0800102 // Create an OatMethod based on pointers (for unit tests)
103 OatFile::OatMethod CreateOatMethod(const void* code,
104 const size_t frame_size_in_bytes,
105 const uint32_t core_spill_mask,
106 const uint32_t fp_spill_mask,
107 const uint32_t* mapping_table,
108 const uint16_t* vmap_table,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800109 const uint8_t* gc_map,
Brian Carlstromae826982011-11-09 01:33:42 -0800110 const Method::InvokeStub* invoke_stub) {
111 return OatFile::OatMethod(NULL,
112 reinterpret_cast<uint32_t>(code),
113 frame_size_in_bytes,
114 core_spill_mask,
115 fp_spill_mask,
116 reinterpret_cast<uint32_t>(mapping_table),
117 reinterpret_cast<uint32_t>(vmap_table),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800118 reinterpret_cast<uint32_t>(gc_map),
Brian Carlstromae826982011-11-09 01:33:42 -0800119 reinterpret_cast<uint32_t>(invoke_stub));
120 }
121
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700122 void MakeExecutable(Method* method) {
123 CHECK(method != NULL);
124
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125 MethodHelper mh(method);
Ian Rogers0571d352011-11-03 19:51:38 -0700126 const CompiledInvokeStub* compiled_invoke_stub =
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 compiler_->FindInvokeStub(mh.IsStatic(), mh.GetShorty());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700128 CHECK(compiled_invoke_stub != NULL) << PrettyMethod(method);
129 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
130 MakeExecutable(invoke_stub);
131 const Method::InvokeStub* method_invoke_stub
132 = reinterpret_cast<const Method::InvokeStub*>(&invoke_stub[0]);
133 LOG(INFO) << "MakeExecutable " << PrettyMethod(method)
134 << " invoke_stub=" << reinterpret_cast<void*>(method_invoke_stub);
135
136 if (!method->IsAbstract()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700137 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
138 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
139 const CompiledMethod* compiled_method =
140 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
141 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142 CHECK(compiled_method != NULL) << PrettyMethod(method);
143 const std::vector<uint8_t>& code = compiled_method->GetCode();
144 MakeExecutable(code);
145 const void* method_code
146 = CompiledMethod::CodePointer(&code[0], compiled_method->GetInstructionSet());
147 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800148 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
149 compiled_method->GetFrameSizeInBytes(),
150 compiled_method->GetCoreSpillMask(),
151 compiled_method->GetFpSpillMask(),
152 &compiled_method->GetMappingTable()[0],
153 &compiled_method->GetVmapTable()[0],
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800154 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800155 method_invoke_stub);
156 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700157 } else {
158 MakeExecutable(runtime_->GetAbstractMethodErrorStubArray());
159 const void* method_code = runtime_->GetAbstractMethodErrorStubArray()->GetData();
160 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800161 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
162 kStackAlignment,
163 0,
164 0,
165 NULL,
166 NULL,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800167 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800168 method_invoke_stub);
169 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700170 }
171 }
172
173 static void MakeExecutable(const void* code_start, size_t code_length) {
174 CHECK(code_start != NULL);
175 CHECK_NE(code_length, 0U);
176 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700177 uintptr_t base = RoundDown(data, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178 uintptr_t limit = RoundUp(data + code_length, kPageSize);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700179 uintptr_t len = limit - base;
180 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800181 CHECK_EQ(result, 0);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800182
Ian Rogers16341552011-10-10 11:33:06 -0700183 // Flush instruction cache
Shih-wei Liao24782c62012-01-08 12:46:11 -0800184 // Only uses __builtin___clear_cache if GCC >= 4.3.3
185#if GCC_VERSION >= 40303
Ian Rogers16341552011-10-10 11:33:06 -0700186 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800187#elif defined(__APPLE__)
Shih-wei Liao24782c62012-01-08 12:46:11 -0800188 // Currently, only Mac OS builds use GCC 4.2.*. Those host builds do not
189 // need to generate clear_cache on x86.
Brian Carlstrom7a00a3c2012-01-25 18:38:03 -0800190#else
191#error unsupported
Shih-wei Liao24782c62012-01-08 12:46:11 -0800192#endif
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700193 }
194
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700195 protected:
196 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700197 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
198
Elliott Hughes0af55432011-08-17 18:37:28 -0700199 if (is_host_) {
200 // $ANDROID_ROOT is set on the device, but not on the host.
201 // We need to set this so that icu4c can find its locale data.
202 std::string root;
203 root += getenv("ANDROID_BUILD_TOP");
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800204#if defined(__linux__)
Elliott Hughes0af55432011-08-17 18:37:28 -0700205 root += "/out/host/linux-x86";
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800206#elif defined(__APPLE__)
207 root += "/out/host/darwin-x86";
208#else
209#error unsupported OS
210#endif
Elliott Hughes0af55432011-08-17 18:37:28 -0700211 setenv("ANDROID_ROOT", root.c_str(), 1);
212 }
213
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700214 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
215 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700216 if (mkdtemp(&android_data_[0]) == NULL) {
217 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
218 }
Elliott Hughes34023802011-08-30 12:06:17 -0700219 setenv("ANDROID_DATA", android_data_.c_str(), 1);
220 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700221 art_cache_.append("/art-cache");
222 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
223 ASSERT_EQ(mkdir_result, 0);
224
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700225 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700226
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700227 std::string boot_class_path;
228 boot_class_path += "-Xbootclasspath:";
229 boot_class_path += GetLibCoreDexFileName();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700230
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700231 Runtime::Options options;
Brian Carlstroma4a7b482011-10-16 15:29:16 -0700232 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700233 options.push_back(std::make_pair(boot_class_path.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700234 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700235 options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL)));
236 options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700237 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700238 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700239 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700241 InstructionSet instruction_set = kNone;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700242#if defined(__i386__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700243 instruction_set = kX86;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700244#elif defined(__arm__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700245 instruction_set = kThumb2;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700246#endif
Elliott Hughes8add92d2012-01-18 18:18:43 -0800247 runtime_->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700248 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700249 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700250 Runtime::TrampolineType type = Runtime::TrampolineType(i);
251 if (!runtime_->HasResolutionStubArray(type)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700252 runtime_->SetResolutionStubArray(
253 Compiler::CreateResolutionStub(instruction_set, type), type);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700254 }
255 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700256 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700257 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
258 if (!runtime_->HasCalleeSaveMethod(type)) {
259 runtime_->SetCalleeSaveMethod(
260 runtime_->CreateCalleeSaveMethod(instruction_set, type), type);
261 }
262 }
Brian Carlstromae826982011-11-09 01:33:42 -0800263 compiler_.reset(new Compiler(instruction_set, false, NULL));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700264
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700265 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700266 }
267
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700268 virtual void TearDown() {
269 const char* android_data = getenv("ANDROID_DATA");
270 ASSERT_TRUE(android_data != NULL);
271 DIR* dir = opendir(art_cache_.c_str());
272 ASSERT_TRUE(dir != NULL);
273 while (true) {
274 struct dirent entry;
275 struct dirent* entry_ptr;
276 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
277 ASSERT_EQ(0, readdir_result);
278 if (entry_ptr == NULL) {
279 break;
280 }
281 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
282 continue;
283 }
284 std::string filename(art_cache_);
285 filename.push_back('/');
286 filename.append(entry_ptr->d_name);
287 int unlink_result = unlink(filename.c_str());
288 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400289 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700290 closedir(dir);
291 int rmdir_cache_result = rmdir(art_cache_.c_str());
292 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700293 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700294 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700295
296 // icu4c has a fixed 10-element array "gCommonICUDataArray".
297 // If we run > 10 tests, we fill that array and u_setCommonData fails.
298 // There's a function to clear the array, but it's not public...
299 typedef void (*IcuCleanupFn)();
300 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
301 CHECK(sym != NULL);
302 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
303 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304
Ian Rogers0e073f72011-09-09 10:45:46 -0700305 compiler_.reset();
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800306 STLDeleteElements(&opened_dex_files_);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700307
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700309 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400310
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700311 std::string GetLibCoreDexFileName() {
312 if (is_host_) {
313 const char* host_dir = getenv("ANDROID_HOST_OUT");
314 CHECK(host_dir != NULL);
315 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
316 }
317 return std::string("/system/framework/core.jar");
318 }
319
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700320 const DexFile* GetLibCoreDex() {
Elliott Hughes95572412011-12-13 18:14:20 -0800321 std::string libcore_dex_file_name(GetLibCoreDexFileName());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700322 return DexFile::Open(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400323 }
324
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700325 const DexFile* OpenTestDexFile(const char* name) {
326 CHECK(name != NULL);
327 std::string filename;
328 if (is_host_) {
329 // on the host, just read target dex file
330 filename += getenv("ANDROID_PRODUCT_OUT");
331 }
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700332 filename += "/data/art-test/art-test-dex-";
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700333 filename += name;
334 filename += ".jar";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700335 const DexFile* dex_file = DexFile::Open(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700336 CHECK(dex_file != NULL) << "Failed to open " << filename;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800337 opened_dex_files_.push_back(dex_file);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700338 return dex_file;
339 }
340
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700341 ClassLoader* LoadDex(const char* dex_name) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700342 const DexFile* dex_file = OpenTestDexFile(dex_name);
343 CHECK(dex_file != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700344 class_linker_->RegisterDexFile(*dex_file);
345 std::vector<const DexFile*> class_path;
346 class_path.push_back(dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700347 SirtRef<ClassLoader> class_loader(PathClassLoader::AllocCompileTime(class_path));
348 CHECK(class_loader.get() != NULL);
349 Thread::Current()->SetClassLoaderOverride(class_loader.get());
350 return class_loader.get();
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700351 }
352
Brian Carlstromaded5f72011-10-07 17:15:04 -0700353 void CompileClass(const ClassLoader* class_loader, const char* class_name) {
Elliott Hughes95572412011-12-13 18:14:20 -0800354 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800355 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700356 CHECK(klass != NULL) << "Class not found " << class_name;
357 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
358 CompileMethod(klass->GetDirectMethod(i));
359 }
360 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
361 CompileMethod(klass->GetVirtualMethod(i));
362 }
363 }
364
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700365 void CompileMethod(Method* method) {
366 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700367 compiler_->CompileOne(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700368 MakeExecutable(method);
369
Ian Rogers169c9a72011-11-13 20:13:17 -0800370 MakeExecutable(runtime_->GetJniDlsymLookupStub());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700371 }
372
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700373 void CompileDirectMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700374 const char* class_name,
375 const char* method_name,
376 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800377 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800378 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700379 CHECK(klass != NULL) << "Class not found " << class_name;
380 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700381 CHECK(method != NULL) << "Direct method not found: "
382 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700383 CompileMethod(method);
384 }
385
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700386 void CompileVirtualMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700387 const char* class_name,
388 const char* method_name,
389 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800390 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800391 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700392 CHECK(klass != NULL) << "Class not found " << class_name;
393 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700394 CHECK(method != NULL) << "Virtual method not found: "
395 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700396 CompileMethod(method);
397 }
398
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700399 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700400 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700401 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700402 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700403 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700404 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700405 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700406 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700407 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700408
409 private:
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800410 std::vector<const DexFile*> opened_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700411};
412
Brian Carlstrom934486c2011-07-12 23:42:50 -0700413} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700414
415namespace std {
416
417// TODO: isn't gtest supposed to be able to print STL types for itself?
418template <typename T>
419std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700420 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700421 return os;
422}
423
424} // namespace std