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" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 20 | #include "object_utils.h" |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 21 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 22 | #include "runtime.h" |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 23 | #include "stl_util.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 24 | #include "stringprintf.h" |
| 25 | #include "thread.h" |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 26 | #include "unicode/uclean.h" |
| 27 | #include "unicode/uvernum.h" |
| 28 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 31 | static inline const DexFile* OpenDexFileBase64(const char* base64, |
| 32 | const std::string& location) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 33 | // decode base64 |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 34 | CHECK(base64 != NULL); |
| 35 | size_t length; |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 36 | UniquePtr<byte[]> dex_bytes(DecodeBase64(base64, &length)); |
| 37 | CHECK(dex_bytes.get() != NULL); |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 38 | |
| 39 | // write to provided file |
| 40 | UniquePtr<File> file(OS::OpenFile(location.c_str(), true)); |
| 41 | CHECK(file.get() != NULL); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 42 | if (!file->WriteFully(dex_bytes.get(), length)) { |
Brian Carlstrom | 33f741e | 2011-10-03 11:24:05 -0700 | [diff] [blame] | 43 | PLOG(FATAL) << "Failed to write base64 as dex file"; |
| 44 | } |
| 45 | file.reset(); |
| 46 | |
| 47 | // read dex file |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 48 | const DexFile* dex_file = DexFile::Open(location, ""); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 49 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 50 | return dex_file; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 53 | class ScratchFile { |
| 54 | public: |
| 55 | ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 56 | filename_ = getenv("ANDROID_DATA"); |
| 57 | filename_ += "/TmpFile-XXXXXX"; |
| 58 | fd_ = mkstemp(&filename_[0]); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 59 | CHECK_NE(-1, fd_); |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 60 | file_.reset(OS::FileFromFd(GetFilename(), fd_)); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | ~ScratchFile() { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 64 | int unlink_result = unlink(filename_.c_str()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 65 | 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 Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 71 | return filename_.c_str(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 74 | File* GetFile() const { |
| 75 | return file_.get(); |
| 76 | } |
| 77 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 78 | int GetFd() const { |
| 79 | return fd_; |
| 80 | } |
| 81 | |
| 82 | private: |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 83 | std::string filename_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 84 | int fd_; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 85 | UniquePtr<File> file_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 88 | class CommonTest : public testing::Test { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 89 | public: |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 90 | |
| 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 Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 101 | // 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 108 | const uint8_t* gc_map, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 109 | 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 117 | reinterpret_cast<uint32_t>(gc_map), |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 118 | reinterpret_cast<uint32_t>(invoke_stub)); |
| 119 | } |
| 120 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 121 | void MakeExecutable(Method* method) { |
| 122 | CHECK(method != NULL); |
| 123 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 124 | MethodHelper mh(method); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 125 | const CompiledInvokeStub* compiled_invoke_stub = |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 126 | compiler_->FindInvokeStub(mh.IsStatic(), mh.GetShorty()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 127 | 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 Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 136 | 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 Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 141 | 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 Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 147 | 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 Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 153 | NULL, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 154 | method_invoke_stub); |
| 155 | oat_method.LinkMethodPointers(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 156 | } else { |
| 157 | MakeExecutable(runtime_->GetAbstractMethodErrorStubArray()); |
| 158 | const void* method_code = runtime_->GetAbstractMethodErrorStubArray()->GetData(); |
| 159 | LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 160 | OatFile::OatMethod oat_method = CreateOatMethod(method_code, |
| 161 | kStackAlignment, |
| 162 | 0, |
| 163 | 0, |
| 164 | NULL, |
| 165 | NULL, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 166 | NULL, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 167 | method_invoke_stub); |
| 168 | oat_method.LinkMethodPointers(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 169 | } |
| 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 Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 176 | uintptr_t base = RoundDown(data, kPageSize); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 177 | uintptr_t limit = RoundUp(data + code_length, kPageSize); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 178 | uintptr_t len = limit - base; |
| 179 | int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 180 | |
Ian Rogers | 1634155 | 2011-10-10 11:33:06 -0700 | [diff] [blame] | 181 | // Flush instruction cache |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 182 | // Only uses __builtin___clear_cache if GCC >= 4.3.3 |
| 183 | #if GCC_VERSION >= 40303 |
Ian Rogers | 1634155 | 2011-10-10 11:33:06 -0700 | [diff] [blame] | 184 | __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len)); |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 185 | #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 Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 190 | CHECK_EQ(result, 0); |
| 191 | } |
| 192 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 193 | protected: |
| 194 | virtual void SetUp() { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 195 | is_host_ = getenv("ANDROID_BUILD_TOP") != NULL; |
| 196 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 197 | 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 Hughes | a0cb120 | 2012-01-23 17:34:32 -0800 | [diff] [blame] | 202 | #if defined(__linux__) |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 203 | root += "/out/host/linux-x86"; |
Elliott Hughes | a0cb120 | 2012-01-23 17:34:32 -0800 | [diff] [blame] | 204 | #elif defined(__APPLE__) |
| 205 | root += "/out/host/darwin-x86"; |
| 206 | #else |
| 207 | #error unsupported OS |
| 208 | #endif |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 209 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 210 | } |
| 211 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 212 | // 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 Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 214 | if (mkdtemp(&android_data_[0]) == NULL) { |
| 215 | PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed"; |
| 216 | } |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 217 | setenv("ANDROID_DATA", android_data_.c_str(), 1); |
| 218 | art_cache_.append(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 219 | art_cache_.append("/art-cache"); |
| 220 | int mkdir_result = mkdir(art_cache_.c_str(), 0700); |
| 221 | ASSERT_EQ(mkdir_result, 0); |
| 222 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 223 | java_lang_dex_file_.reset(GetLibCoreDex()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 224 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 225 | std::string boot_class_path; |
| 226 | boot_class_path += "-Xbootclasspath:"; |
| 227 | boot_class_path += GetLibCoreDexFileName(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 228 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 229 | Runtime::Options options; |
Brian Carlstrom | a4a7b48 | 2011-10-16 15:29:16 -0700 | [diff] [blame] | 230 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 231 | 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] | 232 | options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 233 | options.push_back(std::make_pair("-Xms64m", reinterpret_cast<void*>(NULL))); |
| 234 | options.push_back(std::make_pair("-Xmx64m", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 235 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 236 | ASSERT_TRUE(runtime_.get() != NULL); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 237 | class_linker_ = runtime_->GetClassLinker(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 238 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 239 | InstructionSet instruction_set = kNone; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 240 | #if defined(__i386__) |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 241 | instruction_set = kX86; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 242 | #elif defined(__arm__) |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 243 | instruction_set = kThumb2; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 244 | #endif |
Elliott Hughes | 8add92d | 2012-01-18 18:18:43 -0800 | [diff] [blame] | 245 | runtime_->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set)); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 246 | runtime_->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set)); |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 247 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 248 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 249 | if (!runtime_->HasResolutionStubArray(type)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 250 | runtime_->SetResolutionStubArray( |
| 251 | Compiler::CreateResolutionStub(instruction_set, type), type); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 254 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 255 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 256 | if (!runtime_->HasCalleeSaveMethod(type)) { |
| 257 | runtime_->SetCalleeSaveMethod( |
| 258 | runtime_->CreateCalleeSaveMethod(instruction_set, type), type); |
| 259 | } |
| 260 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 261 | compiler_.reset(new Compiler(instruction_set, false, NULL)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 262 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 263 | Heap::VerifyHeap(); // Check for heap corruption before the test |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 266 | 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 Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 287 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 288 | closedir(dir); |
| 289 | int rmdir_cache_result = rmdir(art_cache_.c_str()); |
| 290 | ASSERT_EQ(0, rmdir_cache_result); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 291 | int rmdir_data_result = rmdir(android_data_.c_str()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 292 | ASSERT_EQ(0, rmdir_data_result); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 293 | |
| 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 Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 302 | |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 303 | compiler_.reset(); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 304 | STLDeleteElements(&opened_dex_files_); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 305 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 306 | Heap::VerifyHeap(); // Check for heap corruption after the test |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 307 | } |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 308 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 309 | 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 Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 318 | const DexFile* GetLibCoreDex() { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 319 | std::string libcore_dex_file_name(GetLibCoreDexFileName()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 320 | return DexFile::Open(libcore_dex_file_name, ""); |
Jesse Wilson | ac5b9e2 | 2011-07-27 15:11:13 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 323 | 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 Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 330 | filename += "/data/art-test/art-test-dex-"; |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 331 | filename += name; |
| 332 | filename += ".jar"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 333 | const DexFile* dex_file = DexFile::Open(filename, ""); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 334 | CHECK(dex_file != NULL) << "Failed to open " << filename; |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 335 | opened_dex_files_.push_back(dex_file); |
Brian Carlstrom | 9f30b38 | 2011-08-28 22:41:38 -0700 | [diff] [blame] | 336 | return dex_file; |
| 337 | } |
| 338 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 339 | ClassLoader* LoadDex(const char* dex_name) { |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 340 | const DexFile* dex_file = OpenTestDexFile(dex_name); |
| 341 | CHECK(dex_file != NULL); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 342 | class_linker_->RegisterDexFile(*dex_file); |
| 343 | std::vector<const DexFile*> class_path; |
| 344 | class_path.push_back(dex_file); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 345 | 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 Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 351 | void CompileClass(const ClassLoader* class_loader, const char* class_name) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 352 | std::string class_descriptor(DotToDescriptor(class_name)); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 353 | Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 354 | 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 Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 363 | void CompileMethod(Method* method) { |
| 364 | CHECK(method != NULL); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 365 | compiler_->CompileOne(method); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 366 | MakeExecutable(method); |
| 367 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 368 | MakeExecutable(runtime_->GetJniDlsymLookupStub()); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 371 | void CompileDirectMethod(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) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 375 | std::string class_descriptor(DotToDescriptor(class_name)); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 376 | Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 377 | CHECK(klass != NULL) << "Class not found " << class_name; |
| 378 | Method* method = klass->FindDirectMethod(method_name, signature); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 379 | CHECK(method != NULL) << "Direct 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 | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 384 | void CompileVirtualMethod(ClassLoader* class_loader, |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 385 | const char* class_name, |
| 386 | const char* method_name, |
| 387 | const char* signature) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 388 | std::string class_descriptor(DotToDescriptor(class_name)); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 389 | Class* klass = class_linker_->FindClass(class_descriptor.c_str(), class_loader); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 390 | CHECK(klass != NULL) << "Class not found " << class_name; |
| 391 | Method* method = klass->FindVirtualMethod(method_name, signature); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 392 | CHECK(method != NULL) << "Virtual method not found: " |
| 393 | << class_name << "." << method_name << signature; |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 394 | CompileMethod(method); |
| 395 | } |
| 396 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 397 | bool is_host_; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 398 | std::string android_data_; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 399 | std::string art_cache_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 400 | UniquePtr<const DexFile> java_lang_dex_file_; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 401 | std::vector<const DexFile*> boot_class_path_; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 402 | UniquePtr<Runtime> runtime_; |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 403 | // Owned by the runtime |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 404 | ClassLinker* class_linker_; |
Ian Rogers | 0e073f7 | 2011-09-09 10:45:46 -0700 | [diff] [blame] | 405 | UniquePtr<Compiler> compiler_; |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 406 | |
| 407 | private: |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 408 | std::vector<const DexFile*> opened_dex_files_; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 409 | }; |
| 410 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 411 | } // namespace art |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 412 | |
| 413 | namespace std { |
| 414 | |
| 415 | // TODO: isn't gtest supposed to be able to print STL types for itself? |
| 416 | template <typename T> |
| 417 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 418 | os << ::art::ToString(rhs); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 419 | return os; |
| 420 | } |
| 421 | |
| 422 | } // namespace std |