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