blob: 7da730f180641a362a06f5c54ba48755a45e44bb [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 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 Carlstrom9f30b382011-08-28 22:41:38 -070031static inline const DexFile* OpenDexFileBase64(const char* base64,
32 const std::string& location) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070033 // decode base64
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034 CHECK(base64 != NULL);
35 size_t length;
Elliott Hughes4d6850c2012-01-18 15:55:06 -080036 UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length));
37 CHECK(dex_bytes.get() != NULL);
Brian Carlstrom33f741e2011-10-03 11:24:05 -070038
39 // write to provided file
40 UniquePtr<File> file(OS::OpenFile(location.c_str(), true));
41 CHECK(file.get() != NULL);
Elliott Hughes4d6850c2012-01-18 15:55:06 -080042 if (!file->WriteFully(dex_bytes.get(), length)) {
Brian Carlstrom33f741e2011-10-03 11:24:05 -070043 PLOG(FATAL) << "Failed to write base64 as dex file";
44 }
45 file.reset();
46
47 // read dex file
Brian Carlstrom58ae9412011-10-04 00:56:06 -070048 const DexFile* dex_file = DexFile::Open(location, "");
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070049 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070050 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070051}
52
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053class ScratchFile {
54 public:
55 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070056 filename_ = getenv("ANDROID_DATA");
57 filename_ += "/TmpFile-XXXXXX";
58 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070059 CHECK_NE(-1, fd_);
Elliott Hughes234da572011-11-03 22:13:06 -070060 file_.reset(OS::FileFromFd(GetFilename(), fd_));
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 }
62
63 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070064 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070065 CHECK_EQ(0, unlink_result);
66 int close_result = close(fd_);
67 CHECK_EQ(0, close_result);
68 }
69
70 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070071 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070072 }
73
Elliott Hughes234da572011-11-03 22:13:06 -070074 File* GetFile() const {
75 return file_.get();
76 }
77
Brian Carlstromdb4d5402011-08-09 12:18:28 -070078 int GetFd() const {
79 return fd_;
80 }
81
82 private:
Elliott Hughes34023802011-08-30 12:06:17 -070083 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070084 int fd_;
Elliott Hughes234da572011-11-03 22:13:06 -070085 UniquePtr<File> file_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086};
87
Brian Carlstromf734cf52011-08-17 16:28:14 -070088class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070089 public:
Brian Carlstrom3320cf42011-10-04 14:58:28 -070090
91 static void MakeExecutable(const ByteArray* code_array) {
92 CHECK(code_array != NULL);
93 MakeExecutable(code_array->GetData(), code_array->GetLength());
94 }
95
96 static void MakeExecutable(const std::vector<uint8_t>& code) {
97 CHECK_NE(code.size(), 0U);
98 MakeExecutable(&code[0], code.size());
99 }
100
Brian Carlstromae826982011-11-09 01:33:42 -0800101 // Create an OatMethod based on pointers (for unit tests)
102 OatFile::OatMethod CreateOatMethod(const void* code,
103 const size_t frame_size_in_bytes,
104 const uint32_t core_spill_mask,
105 const uint32_t fp_spill_mask,
106 const uint32_t* mapping_table,
107 const uint16_t* vmap_table,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800108 const uint8_t* gc_map,
Brian Carlstromae826982011-11-09 01:33:42 -0800109 const Method::InvokeStub* invoke_stub) {
110 return OatFile::OatMethod(NULL,
111 reinterpret_cast<uint32_t>(code),
112 frame_size_in_bytes,
113 core_spill_mask,
114 fp_spill_mask,
115 reinterpret_cast<uint32_t>(mapping_table),
116 reinterpret_cast<uint32_t>(vmap_table),
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800117 reinterpret_cast<uint32_t>(gc_map),
Brian Carlstromae826982011-11-09 01:33:42 -0800118 reinterpret_cast<uint32_t>(invoke_stub));
119 }
120
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700121 void MakeExecutable(Method* method) {
122 CHECK(method != NULL);
123
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124 MethodHelper mh(method);
Ian Rogers0571d352011-11-03 19:51:38 -0700125 const CompiledInvokeStub* compiled_invoke_stub =
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 compiler_->FindInvokeStub(mh.IsStatic(), mh.GetShorty());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 CHECK(compiled_invoke_stub != NULL) << PrettyMethod(method);
128 const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode();
129 MakeExecutable(invoke_stub);
130 const Method::InvokeStub* method_invoke_stub
131 = reinterpret_cast<const Method::InvokeStub*>(&invoke_stub[0]);
132 LOG(INFO) << "MakeExecutable " << PrettyMethod(method)
133 << " invoke_stub=" << reinterpret_cast<void*>(method_invoke_stub);
134
135 if (!method->IsAbstract()) {
Ian Rogers0571d352011-11-03 19:51:38 -0700136 const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
137 const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache);
138 const CompiledMethod* compiled_method =
139 compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file,
140 method->GetDexMethodIndex()));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 CHECK(compiled_method != NULL) << PrettyMethod(method);
142 const std::vector<uint8_t>& code = compiled_method->GetCode();
143 MakeExecutable(code);
144 const void* method_code
145 = CompiledMethod::CodePointer(&code[0], compiled_method->GetInstructionSet());
146 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800147 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
148 compiled_method->GetFrameSizeInBytes(),
149 compiled_method->GetCoreSpillMask(),
150 compiled_method->GetFpSpillMask(),
151 &compiled_method->GetMappingTable()[0],
152 &compiled_method->GetVmapTable()[0],
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800153 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800154 method_invoke_stub);
155 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700156 } else {
157 MakeExecutable(runtime_->GetAbstractMethodErrorStubArray());
158 const void* method_code = runtime_->GetAbstractMethodErrorStubArray()->GetData();
159 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Brian Carlstromae826982011-11-09 01:33:42 -0800160 OatFile::OatMethod oat_method = CreateOatMethod(method_code,
161 kStackAlignment,
162 0,
163 0,
164 NULL,
165 NULL,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800166 NULL,
Brian Carlstromae826982011-11-09 01:33:42 -0800167 method_invoke_stub);
168 oat_method.LinkMethodPointers(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700169 }
170 }
171
172 static void MakeExecutable(const void* code_start, size_t code_length) {
173 CHECK(code_start != NULL);
174 CHECK_NE(code_length, 0U);
175 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700176 uintptr_t base = RoundDown(data, kPageSize);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177 uintptr_t limit = RoundUp(data + code_length, kPageSize);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700178 uintptr_t len = limit - base;
179 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800180
Ian Rogers16341552011-10-10 11:33:06 -0700181 // Flush instruction cache
Shih-wei Liao24782c62012-01-08 12:46:11 -0800182 // Only uses __builtin___clear_cache if GCC >= 4.3.3
183#if GCC_VERSION >= 40303
Ian Rogers16341552011-10-10 11:33:06 -0700184 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
Shih-wei Liao24782c62012-01-08 12:46:11 -0800185#else
186 // Currently, only Mac OS builds use GCC 4.2.*. Those host builds do not
187 // need to generate clear_cache on x86.
188#endif
189
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700190 CHECK_EQ(result, 0);
191 }
192
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700193 protected:
194 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700195 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
196
Elliott Hughes0af55432011-08-17 18:37:28 -0700197 if (is_host_) {
198 // $ANDROID_ROOT is set on the device, but not on the host.
199 // We need to set this so that icu4c can find its locale data.
200 std::string root;
201 root += getenv("ANDROID_BUILD_TOP");
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800202#if defined(__linux__)
Elliott Hughes0af55432011-08-17 18:37:28 -0700203 root += "/out/host/linux-x86";
Elliott Hughesa0cb1202012-01-23 17:34:32 -0800204#elif defined(__APPLE__)
205 root += "/out/host/darwin-x86";
206#else
207#error unsupported OS
208#endif
Elliott Hughes0af55432011-08-17 18:37:28 -0700209 setenv("ANDROID_ROOT", root.c_str(), 1);
210 }
211
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700212 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
213 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700214 if (mkdtemp(&android_data_[0]) == NULL) {
215 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
216 }
Elliott Hughes34023802011-08-30 12:06:17 -0700217 setenv("ANDROID_DATA", android_data_.c_str(), 1);
218 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700219 art_cache_.append("/art-cache");
220 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
221 ASSERT_EQ(mkdir_result, 0);
222
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700223 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700224
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700225 std::string boot_class_path;
226 boot_class_path += "-Xbootclasspath:";
227 boot_class_path += GetLibCoreDexFileName();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700228
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700229 Runtime::Options options;
Brian Carlstroma4a7b482011-10-16 15:29:16 -0700230 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700231 options.push_back(std::make_pair(boot_class_path.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700232 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700233 options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL)));
234 options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700236 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700237 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700238
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700239 InstructionSet instruction_set = kNone;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700240#if defined(__i386__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700241 instruction_set = kX86;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700242#elif defined(__arm__)
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700243 instruction_set = kThumb2;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700244#endif
Elliott Hughes8add92d2012-01-18 18:18:43 -0800245 runtime_->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700246 runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set));
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700247 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700248 Runtime::TrampolineType type = Runtime::TrampolineType(i);
249 if (!runtime_->HasResolutionStubArray(type)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700250 runtime_->SetResolutionStubArray(
251 Compiler::CreateResolutionStub(instruction_set, type), type);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700252 }
253 }
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700254 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700255 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
256 if (!runtime_->HasCalleeSaveMethod(type)) {
257 runtime_->SetCalleeSaveMethod(
258 runtime_->CreateCalleeSaveMethod(instruction_set, type), type);
259 }
260 }
Brian Carlstromae826982011-11-09 01:33:42 -0800261 compiler_.reset(new Compiler(instruction_set, false, NULL));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700262
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700263 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700264 }
265
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700266 virtual void TearDown() {
267 const char* android_data = getenv("ANDROID_DATA");
268 ASSERT_TRUE(android_data != NULL);
269 DIR* dir = opendir(art_cache_.c_str());
270 ASSERT_TRUE(dir != NULL);
271 while (true) {
272 struct dirent entry;
273 struct dirent* entry_ptr;
274 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
275 ASSERT_EQ(0, readdir_result);
276 if (entry_ptr == NULL) {
277 break;
278 }
279 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
280 continue;
281 }
282 std::string filename(art_cache_);
283 filename.push_back('/');
284 filename.append(entry_ptr->d_name);
285 int unlink_result = unlink(filename.c_str());
286 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400287 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700288 closedir(dir);
289 int rmdir_cache_result = rmdir(art_cache_.c_str());
290 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700291 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700292 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700293
294 // icu4c has a fixed 10-element array "gCommonICUDataArray".
295 // If we run > 10 tests, we fill that array and u_setCommonData fails.
296 // There's a function to clear the array, but it's not public...
297 typedef void (*IcuCleanupFn)();
298 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
299 CHECK(sym != NULL);
300 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
301 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302
Ian Rogers0e073f72011-09-09 10:45:46 -0700303 compiler_.reset();
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800304 STLDeleteElements(&opened_dex_files_);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700305
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700307 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400308
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700309 std::string GetLibCoreDexFileName() {
310 if (is_host_) {
311 const char* host_dir = getenv("ANDROID_HOST_OUT");
312 CHECK(host_dir != NULL);
313 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
314 }
315 return std::string("/system/framework/core.jar");
316 }
317
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700318 const DexFile* GetLibCoreDex() {
Elliott Hughes95572412011-12-13 18:14:20 -0800319 std::string libcore_dex_file_name(GetLibCoreDexFileName());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700320 return DexFile::Open(libcore_dex_file_name, "");
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400321 }
322
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700323 const DexFile* OpenTestDexFile(const char* name) {
324 CHECK(name != NULL);
325 std::string filename;
326 if (is_host_) {
327 // on the host, just read target dex file
328 filename += getenv("ANDROID_PRODUCT_OUT");
329 }
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700330 filename += "/data/art-test/art-test-dex-";
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700331 filename += name;
332 filename += ".jar";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700333 const DexFile* dex_file = DexFile::Open(filename, "");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700334 CHECK(dex_file != NULL) << "Failed to open " << filename;
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800335 opened_dex_files_.push_back(dex_file);
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700336 return dex_file;
337 }
338
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700339 ClassLoader* LoadDex(const char* dex_name) {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700340 const DexFile* dex_file = OpenTestDexFile(dex_name);
341 CHECK(dex_file != NULL);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700342 class_linker_->RegisterDexFile(*dex_file);
343 std::vector<const DexFile*> class_path;
344 class_path.push_back(dex_file);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700345 SirtRef<ClassLoader> class_loader(PathClassLoader::AllocCompileTime(class_path));
346 CHECK(class_loader.get() != NULL);
347 Thread::Current()->SetClassLoaderOverride(class_loader.get());
348 return class_loader.get();
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700349 }
350
Brian Carlstromaded5f72011-10-07 17:15:04 -0700351 void CompileClass(const ClassLoader* class_loader, const char* class_name) {
Elliott Hughes95572412011-12-13 18:14:20 -0800352 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800353 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700354 CHECK(klass != NULL) << "Class not found " << class_name;
355 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
356 CompileMethod(klass->GetDirectMethod(i));
357 }
358 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
359 CompileMethod(klass->GetVirtualMethod(i));
360 }
361 }
362
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700363 void CompileMethod(Method* method) {
364 CHECK(method != NULL);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700365 compiler_->CompileOne(method);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366 MakeExecutable(method);
367
Ian Rogers169c9a72011-11-13 20:13:17 -0800368 MakeExecutable(runtime_->GetJniDlsymLookupStub());
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700369 }
370
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700371 void CompileDirectMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700372 const char* class_name,
373 const char* method_name,
374 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800375 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800376 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700377 CHECK(klass != NULL) << "Class not found " << class_name;
378 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700379 CHECK(method != NULL) << "Direct method not found: "
380 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700381 CompileMethod(method);
382 }
383
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700384 void CompileVirtualMethod(ClassLoader* class_loader,
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700385 const char* class_name,
386 const char* method_name,
387 const char* signature) {
Elliott Hughes95572412011-12-13 18:14:20 -0800388 std::string class_descriptor(DotToDescriptor(class_name));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800389 Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700390 CHECK(klass != NULL) << "Class not found " << class_name;
391 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700392 CHECK(method != NULL) << "Virtual method not found: "
393 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700394 CompileMethod(method);
395 }
396
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700397 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700398 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700399 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700400 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700401 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700402 UniquePtr<Runtime> runtime_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700403 // Owned by the runtime
Carl Shapiro7a909592011-07-24 19:21:59 -0700404 ClassLinker* class_linker_;
Ian Rogers0e073f72011-09-09 10:45:46 -0700405 UniquePtr<Compiler> compiler_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700406
407 private:
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800408 std::vector<const DexFile*> opened_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700409};
410
Brian Carlstrom934486c2011-07-12 23:42:50 -0700411} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700412
413namespace std {
414
415// TODO: isn't gtest supposed to be able to print STL types for itself?
416template <typename T>
417std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
Elliott Hughes14134a12011-09-30 16:55:51 -0700418 os << ::art::ToString(rhs);
Elliott Hughes34023802011-08-30 12:06:17 -0700419 return os;
420}
421
422} // namespace std