blob: c0d15f3439048ad78ef5d773634515697c08460b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrome24fa612011-09-29 00:53:55 -070016
Ian Rogersd582fa42014-11-05 23:46:43 -080017#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070018#include "art_method-inl.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080019#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070020#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080021#include "common_compiler_test.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000022#include "compiled_method.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "compiler.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080024#include "dex/pass_manager.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "dex/quick/dex_file_to_method_inliner_map.h"
26#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080027#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_driver.h"
29#include "driver/compiler_options.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000030#include "dwarf/method_debug_info.h"
31#include "elf_writer.h"
32#include "elf_writer_quick.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010033#include "entrypoints/quick/quick_entrypoints.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000034#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080036#include "mirror/object_array-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "mirror/object-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010038#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070039#include "oat_writer.h"
40#include "scoped_thread_state_change.h"
Vladimir Marko9bdf1082016-01-21 12:15:52 +000041#include "utils/test_dex_file_builder.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070042
Brian Carlstrome24fa612011-09-29 00:53:55 -070043namespace art {
44
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080045NO_RETURN static void Usage(const char* fmt, ...) {
46 va_list ap;
47 va_start(ap, fmt);
48 std::string error;
49 StringAppendV(&error, fmt, ap);
50 LOG(FATAL) << error;
51 va_end(ap);
52 UNREACHABLE();
53}
54
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080055class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080056 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070057 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
58
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080060 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080061 const DexFile& dex_file)
Mathieu Chartier90443472015-07-16 20:32:27 -070062 SHARED_REQUIRES(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080063 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080064 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070065 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080066
Ian Rogersd4c4d952014-10-16 20:31:53 -070067 if (compiled_method == nullptr) {
68 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << PrettyMethod(method) << " "
69 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080071 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
72 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
73 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 const void* quick_oat_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -080075 EXPECT_TRUE(quick_oat_code != nullptr) << PrettyMethod(method);
76 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), compiled_method->GetFrameSizeInBytes());
77 EXPECT_EQ(oat_method.GetCoreSpillMask(), compiled_method->GetCoreSpillMask());
78 EXPECT_EQ(oat_method.GetFpSpillMask(), compiled_method->GetFpSpillMask());
79 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
80 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010081 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
82 EXPECT_FALSE(quick_code.empty());
83 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
85 << PrettyMethod(method) << " " << code_size;
86 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080087 }
88 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080089
90 void SetupCompiler(Compiler::Kind compiler_kind,
91 InstructionSet insn_set,
92 const std::vector<std::string>& compiler_options,
93 /*out*/std::string* error_msg) {
94 ASSERT_TRUE(error_msg != nullptr);
95 insn_features_.reset(InstructionSetFeatures::FromVariant(insn_set, "default", error_msg));
96 ASSERT_TRUE(insn_features_ != nullptr) << error_msg;
97 compiler_options_.reset(new CompilerOptions);
98 for (const std::string& option : compiler_options) {
99 compiler_options_->ParseCompilerOption(option, Usage);
100 }
101 verification_results_.reset(new VerificationResults(compiler_options_.get()));
102 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
103 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
104 method_inliner_map_.get(),
105 CompilerCallbacks::CallbackMode::kCompileApp));
106 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
107 timer_.reset(new CumulativeLogger("Compilation times"));
108 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
109 verification_results_.get(),
110 method_inliner_map_.get(),
111 compiler_kind,
112 insn_set,
113 insn_features_.get(),
114 false,
115 nullptr,
116 nullptr,
117 nullptr,
118 2,
119 true,
120 true,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800121 timer_.get(),
122 -1,
Calin Juravle998c2162015-12-21 15:39:33 +0200123 nullptr,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800124 nullptr));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800125 }
126
127 bool WriteElf(File* file,
128 const std::vector<const DexFile*>& dex_files,
129 SafeMap<std::string, std::string>& key_value_store) {
130 TimingLogger timings("WriteElf", false, false);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000131 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
132 for (const DexFile* dex_file : dex_files) {
133 ArrayRef<const uint8_t> raw_dex_file(
134 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
135 dex_file->GetHeader().file_size_);
136 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
137 dex_file->GetLocation().c_str(),
138 dex_file->GetLocationChecksum())) {
139 return false;
140 }
141 }
142 return DoWriteElf(file, oat_writer, key_value_store);
143 }
144
145 bool WriteElf(File* file,
146 const std::vector<const char*>& dex_filenames,
147 SafeMap<std::string, std::string>& key_value_store) {
148 TimingLogger timings("WriteElf", false, false);
149 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
150 for (const char* dex_filename : dex_filenames) {
151 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
152 return false;
153 }
154 }
155 return DoWriteElf(file, oat_writer, key_value_store);
156 }
157
158 bool WriteElf(File* file,
159 ScopedFd&& zip_fd,
160 const char* location,
161 SafeMap<std::string, std::string>& key_value_store) {
162 TimingLogger timings("WriteElf", false, false);
163 OatWriter oat_writer(/*compiling_boot_image*/false, &timings);
164 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
165 return false;
166 }
167 return DoWriteElf(file, oat_writer, key_value_store);
168 }
169
170 bool DoWriteElf(File* file,
171 OatWriter& oat_writer,
172 SafeMap<std::string, std::string>& key_value_store) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000173 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
174 compiler_driver_->GetInstructionSet(),
175 &compiler_driver_->GetCompilerOptions(),
176 file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000177 elf_writer->Start();
Vladimir Marko10c13562015-11-25 14:33:36 +0000178 OutputStream* rodata = elf_writer->StartRoData();
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000179 std::unique_ptr<MemMap> opened_dex_files_map;
180 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
181 if (!oat_writer.WriteAndOpenDexFiles(rodata,
182 file,
183 compiler_driver_->GetInstructionSet(),
184 compiler_driver_->GetInstructionSetFeatures(),
185 &key_value_store,
186 &opened_dex_files_map,
187 &opened_dex_files)) {
188 return false;
189 }
190 Runtime* runtime = Runtime::Current();
191 ClassLinker* const class_linker = runtime->GetClassLinker();
192 std::vector<const DexFile*> dex_files;
193 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
194 dex_files.push_back(dex_file.get());
195 ScopedObjectAccess soa(Thread::Current());
196 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
197 }
198 oat_writer.PrepareLayout(compiler_driver_.get(), nullptr, dex_files);
Vladimir Marko10c13562015-11-25 14:33:36 +0000199 if (!oat_writer.WriteRodata(rodata)) {
200 return false;
201 }
202 elf_writer->EndRoData(rodata);
203
204 OutputStream* text = elf_writer->StartText();
205 if (!oat_writer.WriteCode(text)) {
206 return false;
207 }
208 elf_writer->EndText(text);
209
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000210 if (!oat_writer.WriteHeader(elf_writer->GetStream(), 42U, 4096U, 0)) {
211 return false;
212 }
213
Vladimir Marko10c13562015-11-25 14:33:36 +0000214 elf_writer->SetBssSize(oat_writer.GetBssSize());
Vladimir Marko10c13562015-11-25 14:33:36 +0000215 elf_writer->WriteDynamicSection();
Vladimir Marko49b0f452015-12-10 13:49:19 +0000216 elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo());
217 elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations());
Vladimir Marko10c13562015-11-25 14:33:36 +0000218
219 return elf_writer->End();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800220 }
221
222 std::unique_ptr<const InstructionSetFeatures> insn_features_;
223 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800224};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700225
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000226class ZipBuilder {
227 public:
228 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
229
230 bool AddFile(const char* location, const void* data, size_t size) {
231 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
232 if (offset == static_cast<off_t>(-1)) {
233 return false;
234 }
235
236 ZipFileHeader file_header;
237 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
238 file_header.compressed_size = size;
239 file_header.uncompressed_size = size;
240 file_header.filename_length = strlen(location);
241
242 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
243 !zip_file_->WriteFully(location, file_header.filename_length) ||
244 !zip_file_->WriteFully(data, size)) {
245 return false;
246 }
247
248 CentralDirectoryFileHeader cdfh;
249 cdfh.crc32 = file_header.crc32;
250 cdfh.compressed_size = size;
251 cdfh.uncompressed_size = size;
252 cdfh.filename_length = file_header.filename_length;
253 cdfh.relative_offset_of_local_file_header = offset;
254 file_data_.push_back(FileData { cdfh, location });
255 return true;
256 }
257
258 bool Finish() {
259 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
260 if (offset == static_cast<off_t>(-1)) {
261 return false;
262 }
263
264 size_t central_directory_size = 0u;
265 for (const FileData& file_data : file_data_) {
266 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
267 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
268 return false;
269 }
270 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
271 }
272 EndOfCentralDirectoryRecord eocd_record;
273 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
274 eocd_record.total_number_of_central_directory_records = file_data_.size();
275 eocd_record.size_of_central_directory = central_directory_size;
276 eocd_record.offset_of_start_of_central_directory = offset;
277 return
278 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
279 zip_file_->Flush() == 0;
280 }
281
282 private:
283 struct PACKED(1) ZipFileHeader {
284 uint32_t signature = 0x04034b50;
285 uint16_t version_needed_to_extract = 10;
286 uint16_t general_purpose_bit_flag = 0;
287 uint16_t compression_method = 0; // 0 = store only.
288 uint16_t file_last_modification_time = 0u;
289 uint16_t file_last_modification_date = 0u;
290 uint32_t crc32;
291 uint32_t compressed_size;
292 uint32_t uncompressed_size;
293 uint16_t filename_length;
294 uint16_t extra_field_length = 0u; // No extra fields.
295 };
296
297 struct PACKED(1) CentralDirectoryFileHeader {
298 uint32_t signature = 0x02014b50;
299 uint16_t version_made_by = 10;
300 uint16_t version_needed_to_extract = 10;
301 uint16_t general_purpose_bit_flag = 0;
302 uint16_t compression_method = 0; // 0 = store only.
303 uint16_t file_last_modification_time = 0u;
304 uint16_t file_last_modification_date = 0u;
305 uint32_t crc32;
306 uint32_t compressed_size;
307 uint32_t uncompressed_size;
308 uint16_t filename_length;
309 uint16_t extra_field_length = 0u; // No extra fields.
310 uint16_t file_comment_length = 0u; // No file comment.
311 uint16_t disk_number_where_file_starts = 0u;
312 uint16_t internal_file_attributes = 0u;
313 uint32_t external_file_attributes = 0u;
314 uint32_t relative_offset_of_local_file_header;
315 };
316
317 struct PACKED(1) EndOfCentralDirectoryRecord {
318 uint32_t signature = 0x06054b50;
319 uint16_t number_of_this_disk = 0u;
320 uint16_t disk_where_central_directory_starts = 0u;
321 uint16_t number_of_central_directory_records_on_this_disk;
322 uint16_t total_number_of_central_directory_records;
323 uint32_t size_of_central_directory;
324 uint32_t offset_of_start_of_central_directory;
325 uint16_t comment_length = 0u; // No file comment.
326 };
327
328 struct FileData {
329 CentralDirectoryFileHeader cdfh;
330 const char* location;
331 };
332
333 File* zip_file_;
334 std::vector<FileData> file_data_;
335};
336
Brian Carlstrome24fa612011-09-29 00:53:55 -0700337TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800338 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500339 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700340
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341 // TODO: make selectable.
Elliott Hughes956af0f2014-12-11 14:34:28 -0800342 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogersa073c672013-07-30 17:43:55 -0700343 InstructionSet insn_set = kIsTargetBuild ? kThumb2 : kX86;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700344 std::string error_msg;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800345 SetupCompiler(compiler_kind, insn_set, std::vector<std::string>(), /*out*/ &error_msg);
346
Ian Rogersd4c4d952014-10-16 20:31:53 -0700347 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700348 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800349 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000350 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800351 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700352 }
353
354 ScratchFile tmp;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700355 SafeMap<std::string, std::string> key_value_store;
356 key_value_store.Put(OatHeader::kImageLocationKey, "lue.art");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800357 bool success = WriteElf(tmp.GetFile(), class_linker->GetBootClassPath(), key_value_store);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700358 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700359
Brian Carlstromba150c32013-08-27 17:31:03 -0700360 if (kCompile) { // OatWriter strips the code, regenerate to compare
Ian Rogers3d504072014-03-01 09:16:49 -0800361 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700362 }
Ian Rogersd4c4d952014-10-16 20:31:53 -0700363 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(), tmp.GetFilename(), nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700364 nullptr, false, nullptr, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700365 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700366 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700367 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000368 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Brian Carlstrom28db0122012-10-18 16:20:41 -0700369 ASSERT_EQ(42U, oat_header.GetImageFileLocationOatChecksum());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800370 ASSERT_EQ(4096U, oat_header.GetImageFileLocationOatDataBegin());
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700371 ASSERT_EQ("lue.art", std::string(oat_header.GetStoreValueByKey(OatHeader::kImageLocationKey)));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700372
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800373 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
374 const DexFile& dex_file = *java_lang_dex_file_;
375 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
376 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700377 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700378 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800379 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100380 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 auto pointer_size = class_linker->GetImagePointerSize();
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800382 for (size_t i = 0; i < dex_file.NumClassDefs(); i++) {
383 const DexFile::ClassDef& class_def = dex_file.GetClassDef(i);
384 const uint8_t* class_data = dex_file.GetClassData(class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385
Brian Carlstromba150c32013-08-27 17:31:03 -0700386 size_t num_virtual_methods = 0;
Ian Rogersd4c4d952014-10-16 20:31:53 -0700387 if (class_data != nullptr) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800388 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers0571d352011-11-03 19:51:38 -0700389 num_virtual_methods = it.NumVirtualMethods();
390 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800392 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800393 mirror::Class* klass = class_linker->FindClass(soa.Self(),
394 descriptor,
395 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700396
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100397 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(i);
398 CHECK_EQ(mirror::Class::Status::kStatusNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700399 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100400 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700401
402 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 for (auto& m : klass->GetDirectMethods(pointer_size)) {
404 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
405 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700406 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800408 // TODO We should also check copied methods in this test.
409 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
410 EXPECT_FALSE(m.IsMiranda());
411 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
412 ++method_index;
413 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700414 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700416 }
417}
418
Brian Carlstrom341df942012-06-27 12:29:22 -0700419TEST_F(OatTest, OatHeaderSizeCheck) {
420 // If this test is failing and you have to update these constants,
421 // it is time to update OatHeader::kOatVersion
Elliott Hughes956af0f2014-12-11 14:34:28 -0800422 EXPECT_EQ(72U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800423 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
424 EXPECT_EQ(28U, sizeof(OatQuickMethodHeader));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000425 EXPECT_EQ(132 * GetInstructionSetPointerSize(kRuntimeISA), sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700426}
427
Brian Carlstromf852fb22012-10-19 11:01:58 -0700428TEST_F(OatTest, OatHeaderIsValid) {
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700429 InstructionSet insn_set = kX86;
430 std::string error_msg;
431 std::unique_ptr<const InstructionSetFeatures> insn_features(
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700432 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700433 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700434 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
435 insn_features.get(),
Vladimir Marko49b0f452015-12-10 13:49:19 +0000436 0u,
Andreas Gampe928f72b2014-09-09 19:53:48 -0700437 nullptr));
438 ASSERT_NE(oat_header.get(), nullptr);
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700439 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700440
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700441 char* magic = const_cast<char*>(oat_header->GetMagic());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700442 strcpy(magic, ""); // bad magic
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700443 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700444 strcpy(magic, "oat\n000"); // bad version
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700445 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700446}
447
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800448TEST_F(OatTest, EmptyTextSection) {
449 TimingLogger timings("OatTest::EmptyTextSection", false, false);
450
451 // TODO: make selectable.
452 Compiler::Kind compiler_kind = Compiler::kQuick;
453 InstructionSet insn_set = kRuntimeISA;
454 if (insn_set == kArm) insn_set = kThumb2;
455 std::string error_msg;
456 std::vector<std::string> compiler_options;
457 compiler_options.push_back("--compiler-filter=verify-at-runtime");
458 SetupCompiler(compiler_kind, insn_set, compiler_options, /*out*/ &error_msg);
459
460 jobject class_loader;
461 {
462 ScopedObjectAccess soa(Thread::Current());
463 class_loader = LoadDex("Main");
464 }
465 ASSERT_TRUE(class_loader != nullptr);
466 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
467 ASSERT_TRUE(!dex_files.empty());
468
469 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
470 for (const DexFile* dex_file : dex_files) {
471 ScopedObjectAccess soa(Thread::Current());
472 class_linker->RegisterDexFile(
473 *dex_file,
474 class_linker->GetOrCreateAllocatorForClassLoader(
475 soa.Decode<mirror::ClassLoader*>(class_loader)));
476 }
477 compiler_driver_->SetDexFilesForOatFile(dex_files);
478 compiler_driver_->CompileAll(class_loader, dex_files, &timings);
479
480 ScratchFile tmp;
481 SafeMap<std::string, std::string> key_value_store;
482 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
483 bool success = WriteElf(tmp.GetFile(), dex_files, key_value_store);
484 ASSERT_TRUE(success);
485
486 std::unique_ptr<OatFile> oat_file(OatFile::Open(tmp.GetFilename(),
487 tmp.GetFilename(),
488 nullptr,
489 nullptr,
490 false,
491 nullptr,
492 &error_msg));
493 ASSERT_TRUE(oat_file != nullptr);
494 EXPECT_LT(static_cast<size_t>(oat_file->Size()), static_cast<size_t>(tmp.GetFile()->GetLength()));
495}
496
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000497TEST_F(OatTest, DexFileInput) {
498 TimingLogger timings("OatTest::DexFileInput", false, false);
499
500 std::vector<const char*> input_filenames;
501
502 ScratchFile dex_file1;
503 TestDexFileBuilder builder1;
504 builder1.AddField("Lsome.TestClass;", "int", "someField");
505 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
506 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
507 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
508 dex_file1_data->GetHeader().file_size_);
509 ASSERT_TRUE(success);
510 success = dex_file1.GetFile()->Flush() == 0;
511 ASSERT_TRUE(success);
512 input_filenames.push_back(dex_file1.GetFilename().c_str());
513
514 ScratchFile dex_file2;
515 TestDexFileBuilder builder2;
516 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
517 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
518 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
519 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
520 dex_file2_data->GetHeader().file_size_);
521 ASSERT_TRUE(success);
522 success = dex_file2.GetFile()->Flush() == 0;
523 ASSERT_TRUE(success);
524 input_filenames.push_back(dex_file2.GetFilename().c_str());
525
526 ScratchFile oat_file;
527 SafeMap<std::string, std::string> key_value_store;
528 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
529 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store);
530 ASSERT_TRUE(success);
531
532 std::string error_msg;
533 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
534 oat_file.GetFilename(),
535 nullptr,
536 nullptr,
537 false,
538 nullptr,
539 &error_msg));
540 ASSERT_TRUE(opened_oat_file != nullptr);
541 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
542 std::unique_ptr<const DexFile> opened_dex_file1 =
543 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
544 std::unique_ptr<const DexFile> opened_dex_file2 =
545 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
546
547 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
548 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
549 &opened_dex_file1->GetHeader(),
550 dex_file1_data->GetHeader().file_size_));
551 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
552
553 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
554 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
555 &opened_dex_file2->GetHeader(),
556 dex_file2_data->GetHeader().file_size_));
557 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
558}
559
560TEST_F(OatTest, ZipFileInput) {
561 TimingLogger timings("OatTest::DexFileInput", false, false);
562
563 ScratchFile zip_file;
564 ZipBuilder zip_builder(zip_file.GetFile());
565
566 ScratchFile dex_file1;
567 TestDexFileBuilder builder1;
568 builder1.AddField("Lsome.TestClass;", "long", "someField");
569 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
570 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
571 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
572 dex_file1_data->GetHeader().file_size_);
573 ASSERT_TRUE(success);
574 success = dex_file1.GetFile()->Flush() == 0;
575 ASSERT_TRUE(success);
576 success = zip_builder.AddFile("classes.dex",
577 &dex_file1_data->GetHeader(),
578 dex_file1_data->GetHeader().file_size_);
579 ASSERT_TRUE(success);
580
581 ScratchFile dex_file2;
582 TestDexFileBuilder builder2;
583 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
584 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
585 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
586 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
587 dex_file2_data->GetHeader().file_size_);
588 ASSERT_TRUE(success);
589 success = dex_file2.GetFile()->Flush() == 0;
590 ASSERT_TRUE(success);
591 success = zip_builder.AddFile("classes2.dex",
592 &dex_file2_data->GetHeader(),
593 dex_file2_data->GetHeader().file_size_);
594 ASSERT_TRUE(success);
595
596 success = zip_builder.Finish();
597 ASSERT_TRUE(success) << strerror(errno);
598
599 SafeMap<std::string, std::string> key_value_store;
600 key_value_store.Put(OatHeader::kImageLocationKey, "test.art");
601 {
602 // Test using the AddDexFileSource() interface with the zip file.
603 std::vector<const char*> input_filenames { zip_file.GetFilename().c_str() }; // NOLINT [readability/braces] [4]
604
605 ScratchFile oat_file;
606 success = WriteElf(oat_file.GetFile(), input_filenames, key_value_store);
607 ASSERT_TRUE(success);
608
609 std::string error_msg;
610 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
611 oat_file.GetFilename(),
612 nullptr,
613 nullptr,
614 false,
615 nullptr,
616 &error_msg));
617 ASSERT_TRUE(opened_oat_file != nullptr);
618 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
619 std::unique_ptr<const DexFile> opened_dex_file1 =
620 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
621 std::unique_ptr<const DexFile> opened_dex_file2 =
622 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
623
624 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
625 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
626 &opened_dex_file1->GetHeader(),
627 dex_file1_data->GetHeader().file_size_));
628 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
629 opened_dex_file1->GetLocation());
630
631 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
632 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
633 &opened_dex_file2->GetHeader(),
634 dex_file2_data->GetHeader().file_size_));
635 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
636 opened_dex_file2->GetLocation());
637 }
638
639 {
640 // Test using the AddZipDexFileSource() interface with the zip file handle.
641 ScopedFd zip_fd(dup(zip_file.GetFd()));
642 ASSERT_NE(-1, zip_fd.get());
643
644 ScratchFile oat_file;
645 success = WriteElf(oat_file.GetFile(),
646 std::move(zip_fd),
647 zip_file.GetFilename().c_str(),
648 key_value_store);
649 ASSERT_TRUE(success);
650
651 std::string error_msg;
652 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(oat_file.GetFilename(),
653 oat_file.GetFilename(),
654 nullptr,
655 nullptr,
656 false,
657 nullptr,
658 &error_msg));
659 ASSERT_TRUE(opened_oat_file != nullptr);
660 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
661 std::unique_ptr<const DexFile> opened_dex_file1 =
662 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
663 std::unique_ptr<const DexFile> opened_dex_file2 =
664 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
665
666 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
667 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
668 &opened_dex_file1->GetHeader(),
669 dex_file1_data->GetHeader().file_size_));
670 ASSERT_EQ(DexFile::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
671 opened_dex_file1->GetLocation());
672
673 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
674 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
675 &opened_dex_file2->GetHeader(),
676 dex_file2_data->GetHeader().file_size_));
677 ASSERT_EQ(DexFile::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
678 opened_dex_file2->GetLocation());
679 }
680}
681
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682} // namespace art