Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 3 | #include <dirent.h> |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 4 | #include <dlfcn.h> |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 5 | #include <sys/mman.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 9 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "base64.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 12 | #include "class_loader.h" |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 13 | #include "compiler.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 14 | #include "constants.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 15 | #include "dex_file.h" |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 16 | #include "file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 18 | #include "heap.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 19 | #include "oat_file.h" |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 20 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 21 | #include "runtime.h" |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 22 | #include "stl_util.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 23 | #include "stringprintf.h" |
| 24 | #include "thread.h" |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 25 | #include "unicode/uclean.h" |
| 26 | #include "unicode/uvernum.h" |
| 27 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 30 | static inline const DexFile* OpenDexFileBase64(const char* base64, |
| 31 | const std::string& location) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 32 | // decode base64 |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | CHECK(base64 != NULL); |
| 34 | size_t length; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 35 | byte* dex_bytes = DecodeBase64(base64, &length); |
| 36 | CHECK(dex_bytes != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 37 | |
| 38 | // write to provided file |
| 39 | UniquePtr<File> file(OS::OpenFile(location.c_str(), true)); |
| 40 | CHECK(file.get() != NULL); |
| 41 | if (!file->WriteFully(dex_bytes, length)) { |
| 42 | PLOG(FATAL) << "Failed to write base64 as dex file"; |
| 43 | } |
| 44 | file.reset(); |
| 45 | |
| 46 | // read dex file |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 47 | const DexFile* dex_file = DexFile::Open(location, ""); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 48 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 49 | return dex_file; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 52 | class ScratchFile { |
| 53 | public: |
| 54 | ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 55 | filename_ = getenv("ANDROID_DATA"); |
| 56 | filename_ += "/TmpFile-XXXXXX"; |
| 57 | fd_ = mkstemp(&filename_[0]); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 58 | CHECK_NE(-1, fd_); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 59 | file_.reset(OS::FileFromFd(GetFilename(), fd_)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ~ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 63 | int unlink_result = unlink(filename_.c_str()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 64 | CHECK_EQ(0, unlink_result); |
| 65 | int close_result = close(fd_); |
| 66 | CHECK_EQ(0, close_result); |
| 67 | } |
| 68 | |
| 69 | const char* GetFilename() const { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 70 | return filename_.c_str(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 73 | File* GetFile() const { |
| 74 | return file_.get(); |
| 75 | } |
| 76 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 77 | int GetFd() const { |
| 78 | return fd_; |
| 79 | } |
| 80 | |
| 81 | private: |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 82 | std::string filename_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 83 | int fd_; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 84 | UniquePtr<File> file_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 87 | class CommonTest : public testing::Test { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 88 | public: |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 89 | |
| 90 | static void MakeExecutable(const ByteArray* code_array) { |
| 91 | CHECK(code_array != NULL); |
| 92 | MakeExecutable(code_array->GetData(), code_array->GetLength()); |
| 93 | } |
| 94 | |
| 95 | static void MakeExecutable(const std::vector<uint8_t>& code) { |
| 96 | CHECK_NE(code.size(), 0U); |
| 97 | MakeExecutable(&code[0], code.size()); |
| 98 | } |
| 99 | |
| 100 | void MakeExecutable(Method* method) { |
| 101 | CHECK(method != NULL); |
| 102 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 103 | const CompiledInvokeStub* compiled_invoke_stub = |
| 104 | compiler_->FindInvokeStub(method->IsStatic(), |
| 105 | method->GetShorty()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 106 | CHECK(compiled_invoke_stub != NULL) << PrettyMethod(method); |
| 107 | const std::vector<uint8_t>& invoke_stub = compiled_invoke_stub->GetCode(); |
| 108 | MakeExecutable(invoke_stub); |
| 109 | const Method::InvokeStub* method_invoke_stub |
| 110 | = reinterpret_cast<const Method::InvokeStub*>(&invoke_stub[0]); |
| 111 | LOG(INFO) << "MakeExecutable " << PrettyMethod(method) |
| 112 | << " invoke_stub=" << reinterpret_cast<void*>(method_invoke_stub); |
| 113 | |
| 114 | if (!method->IsAbstract()) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame^] | 115 | const DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 116 | const DexFile& dex_file = Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache); |
| 117 | const CompiledMethod* compiled_method = |
| 118 | compiler_->GetCompiledMethod(Compiler::MethodReference(&dex_file, |
| 119 | method->GetDexMethodIndex())); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 120 | CHECK(compiled_method != NULL) << PrettyMethod(method); |
| 121 | const std::vector<uint8_t>& code = compiled_method->GetCode(); |
| 122 | MakeExecutable(code); |
| 123 | const void* method_code |
| 124 | = CompiledMethod::CodePointer(&code[0], compiled_method->GetInstructionSet()); |
| 125 | LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; |
| 126 | OatFile::OatMethod oat_method(method_code, |
| 127 | compiled_method->GetFrameSizeInBytes(), |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 128 | compiled_method->GetCoreSpillMask(), |
| 129 | compiled_method->GetFpSpillMask(), |
| 130 | &compiled_method->GetMappingTable()[0], |
| 131 | &compiled_method->GetVmapTable()[0], |
| 132 | method_invoke_stub); |
| 133 | oat_method.LinkMethod(method); |
| 134 | } else { |
| 135 | MakeExecutable(runtime_->GetAbstractMethodErrorStubArray()); |
| 136 | const void* method_code = runtime_->GetAbstractMethodErrorStubArray()->GetData(); |
| 137 | LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; |
| 138 | OatFile::OatMethod oat_method(method_code, |
| 139 | kStackAlignment, |
| 140 | 0, |
| 141 | 0, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 142 | NULL, |
| 143 | NULL, |
| 144 | method_invoke_stub); |
| 145 | oat_method.LinkMethod(method); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static void MakeExecutable(const void* code_start, size_t code_length) { |
| 150 | CHECK(code_start != NULL); |
| 151 | CHECK_NE(code_length, 0U); |
| 152 | uintptr_t data = reinterpret_cast<uintptr_t>(code_start); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 153 | uintptr_t base = RoundDown(data, kPageSize); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 154 | uintptr_t limit = RoundUp(data + code_length, kPageSize); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 155 | uintptr_t len = limit - base; |
| 156 | int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); |
Ian Rogers | 1634155 | 2011-10-10 11:33:06 -0700 | [diff] [blame] | 157 | // Flush instruction cache |
| 158 | __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len)); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 159 | CHECK_EQ(result, 0); |
| 160 | } |
| 161 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 162 | protected: |
| 163 | virtual void SetUp() { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 164 | is_host_ = getenv("ANDROID_BUILD_TOP") != NULL; |
| 165 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 166 | if (is_host_) { |
| 167 | // $ANDROID_ROOT is set on the device, but not on the host. |
| 168 | // We need to set this so that icu4c can find its locale data. |
| 169 | std::string root; |
| 170 | root += getenv("ANDROID_BUILD_TOP"); |
| 171 | root += "/out/host/linux-x86"; |
| 172 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 173 | } |
| 174 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 175 | // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache |
| 176 | android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX"); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 177 | if (mkdtemp(&android_data_[0]) == NULL) { |
| 178 | PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed"; |
| 179 | } |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 180 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
| 181 | art_cache_.append(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 182 | art_cache_.append("/art-cache"); |
| 183 | int mkdir_result = mkdir(art_cache_.c_str(), 0700); |
| 184 | ASSERT_EQ(mkdir_result, 0); |
| 185 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 186 | java_lang_dex_file_.reset(GetLibCoreDex()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 187 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 188 | std::string boot_class_path; |
| 189 | boot_class_path += "-Xbootclasspath:"; |
| 190 | boot_class_path += GetLibCoreDexFileName(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 191 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 192 | Runtime::Options options; |
Brian Carlstrom | a4a7b48 | 2011-10-16 15:29:16 -0700 | [diff] [blame] | 193 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 194 | options.push_back(std::make_pair(boot_class_path.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 195 | options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 196 | options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL))); |
| 197 | options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 198 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 199 | ASSERT_TRUE(runtime_.get() != NULL); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 200 | class_linker_ = runtime_->GetClassLinker(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 201 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 202 | InstructionSet instruction_set = kNone; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 203 | #if defined(__i386__) |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 204 | instruction_set = kX86; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 205 | #elif defined(__arm__) |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 206 | instruction_set = kThumb2; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 207 | #endif |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 208 | runtime_->SetJniStubArray(JniCompiler::CreateJniStub(instruction_set)); |
| 209 | runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set)); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 210 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 211 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 212 | if (!runtime_->HasResolutionStubArray(type)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 213 | runtime_->SetResolutionStubArray( |
| 214 | Compiler::CreateResolutionStub(instruction_set, type), type); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 217 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 218 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 219 | if (!runtime_->HasCalleeSaveMethod(type)) { |
| 220 | runtime_->SetCalleeSaveMethod( |
| 221 | runtime_->CreateCalleeSaveMethod(instruction_set, type), type); |
| 222 | } |
| 223 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 224 | compiler_.reset(new Compiler(instruction_set, false)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 225 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 226 | Heap::VerifyHeap(); // Check for heap corruption before the test |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 229 | virtual void TearDown() { |
| 230 | const char* android_data = getenv("ANDROID_DATA"); |
| 231 | ASSERT_TRUE(android_data != NULL); |
| 232 | DIR* dir = opendir(art_cache_.c_str()); |
| 233 | ASSERT_TRUE(dir != NULL); |
| 234 | while (true) { |
| 235 | struct dirent entry; |
| 236 | struct dirent* entry_ptr; |
| 237 | int readdir_result = readdir_r(dir, &entry, &entry_ptr); |
| 238 | ASSERT_EQ(0, readdir_result); |
| 239 | if (entry_ptr == NULL) { |
| 240 | break; |
| 241 | } |
| 242 | if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) { |
| 243 | continue; |
| 244 | } |
| 245 | std::string filename(art_cache_); |
| 246 | filename.push_back('/'); |
| 247 | filename.append(entry_ptr->d_name); |
| 248 | int unlink_result = unlink(filename.c_str()); |
| 249 | ASSERT_EQ(0, unlink_result); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 250 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 251 | closedir(dir); |
| 252 | int rmdir_cache_result = rmdir(art_cache_.c_str()); |
| 253 | ASSERT_EQ(0, rmdir_cache_result); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 254 | int rmdir_data_result = rmdir(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 255 | ASSERT_EQ(0, rmdir_data_result); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 256 | |
| 257 | // icu4c has a fixed 10-element array "gCommonICUDataArray". |
| 258 | // If we run > 10 tests, we fill that array and u_setCommonData fails. |
| 259 | // There's a function to clear the array, but it's not public... |
| 260 | typedef void (*IcuCleanupFn)(); |
| 261 | void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); |
| 262 | CHECK(sym != NULL); |
| 263 | IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); |
| 264 | (*icu_cleanup_fn)(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 265 | |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 266 | compiler_.reset(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 267 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 268 | Heap::VerifyHeap(); // Check for heap corruption after the test |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 269 | } |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 270 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 271 | std::string GetLibCoreDexFileName() { |
| 272 | if (is_host_) { |
| 273 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 274 | CHECK(host_dir != NULL); |
| 275 | return StringPrintf("%s/framework/core-hostdex.jar", host_dir); |
| 276 | } |
| 277 | return std::string("/system/framework/core.jar"); |
| 278 | } |
| 279 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 280 | const DexFile* GetLibCoreDex() { |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 281 | std::string libcore_dex_file_name = GetLibCoreDexFileName(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 282 | return DexFile::Open(libcore_dex_file_name, ""); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 285 | uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) { |
| 286 | for (size_t i = 0; i < dex_file.NumTypeIds(); i++) { |
| 287 | const DexFile::TypeId& type_id = dex_file.GetTypeId(i); |
| 288 | if (descriptor == dex_file.GetTypeDescriptor(type_id)) { |
| 289 | return i; |
| 290 | } |
| 291 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 292 | CHECK(false) << "Failed to find type index for " << descriptor; |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file, |
| 297 | const StringPiece& class_descriptor, |
| 298 | const StringPiece& field_name) { |
| 299 | for (size_t i = 0; i < dex_file.NumFieldIds(); i++) { |
| 300 | const DexFile::FieldId& field_id = dex_file.GetFieldId(i); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 301 | if (class_descriptor == dex_file.GetFieldDeclaringClassDescriptor(field_id) |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 302 | && field_name == dex_file.GetFieldName(field_id)) { |
| 303 | return i; |
| 304 | } |
| 305 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 306 | CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name; |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 310 | const DexFile* OpenTestDexFile(const char* name) { |
| 311 | CHECK(name != NULL); |
| 312 | std::string filename; |
| 313 | if (is_host_) { |
| 314 | // on the host, just read target dex file |
| 315 | filename += getenv("ANDROID_PRODUCT_OUT"); |
| 316 | } |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 317 | filename += "/data/art-test/art-test-dex-"; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 318 | filename += name; |
| 319 | filename += ".jar"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 320 | const DexFile* dex_file = DexFile::Open(filename, ""); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 321 | CHECK(dex_file != NULL) << "Failed to open " << filename; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 322 | return dex_file; |
| 323 | } |
| 324 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 325 | ClassLoader* LoadDex(const char* dex_name) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 326 | const DexFile* dex_file = OpenTestDexFile(dex_name); |
| 327 | CHECK(dex_file != NULL); |
| 328 | loaded_dex_files_.push_back(dex_file); |
| 329 | class_linker_->RegisterDexFile(*dex_file); |
| 330 | std::vector<const DexFile*> class_path; |
| 331 | class_path.push_back(dex_file); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 332 | SirtRef<ClassLoader> class_loader(PathClassLoader::AllocCompileTime(class_path)); |
| 333 | CHECK(class_loader.get() != NULL); |
| 334 | Thread::Current()->SetClassLoaderOverride(class_loader.get()); |
| 335 | return class_loader.get(); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 338 | void CompileClass(const ClassLoader* class_loader, const char* class_name) { |
| 339 | std::string class_descriptor = DotToDescriptor(class_name); |
| 340 | Class* klass = class_linker_->FindClass(class_descriptor, class_loader); |
| 341 | CHECK(klass != NULL) << "Class not found " << class_name; |
| 342 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 343 | CompileMethod(klass->GetDirectMethod(i)); |
| 344 | } |
| 345 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 346 | CompileMethod(klass->GetVirtualMethod(i)); |
| 347 | } |
| 348 | } |
| 349 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 350 | void CompileMethod(Method* method) { |
| 351 | CHECK(method != NULL); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 352 | compiler_->CompileOne(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 353 | MakeExecutable(method); |
| 354 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 355 | MakeExecutable(runtime_->GetJniStubArray()); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 358 | void CompileDirectMethod(ClassLoader* class_loader, |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 359 | const char* class_name, |
| 360 | const char* method_name, |
| 361 | const char* signature) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 362 | std::string class_descriptor = DotToDescriptor(class_name); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 363 | Class* klass = class_linker_->FindClass(class_descriptor, class_loader); |
| 364 | CHECK(klass != NULL) << "Class not found " << class_name; |
| 365 | Method* method = klass->FindDirectMethod(method_name, signature); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 366 | CHECK(method != NULL) << "Direct method not found: " |
| 367 | << class_name << "." << method_name << signature; |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 368 | CompileMethod(method); |
| 369 | } |
| 370 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 371 | void CompileVirtualMethod(ClassLoader* class_loader, |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 372 | const char* class_name, |
| 373 | const char* method_name, |
| 374 | const char* signature) { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 375 | std::string class_descriptor = DotToDescriptor(class_name); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 376 | Class* klass = class_linker_->FindClass(class_descriptor, class_loader); |
| 377 | CHECK(klass != NULL) << "Class not found " << class_name; |
| 378 | Method* method = klass->FindVirtualMethod(method_name, signature); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 379 | CHECK(method != NULL) << "Virtual method not found: " |
| 380 | << class_name << "." << method_name << signature; |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 381 | CompileMethod(method); |
| 382 | } |
| 383 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 384 | bool is_host_; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 385 | std::string android_data_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 386 | std::string art_cache_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 387 | UniquePtr<const DexFile> java_lang_dex_file_; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 388 | std::vector<const DexFile*> boot_class_path_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 389 | UniquePtr<Runtime> runtime_; |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 390 | // Owned by the runtime |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 391 | ClassLinker* class_linker_; |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 392 | UniquePtr<Compiler> compiler_; |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 393 | |
| 394 | private: |
| 395 | std::vector<const DexFile*> loaded_dex_files_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 396 | }; |
| 397 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 398 | } // namespace art |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 399 | |
| 400 | namespace std { |
| 401 | |
| 402 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 403 | template <typename T> |
| 404 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 405 | os << ::art::ToString(rhs); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 406 | return os; |
| 407 | } |
| 408 | |
| 409 | } // namespace std |