blob: 5de154083978fe20ae2e0cc771a1b348151d6ef8 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080017#include "android-base/stringprintf.h"
18
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Andreas Gampedfcd82c2018-10-16 20:22:37 -070022#include "base/file_utils.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070023#include "base/stl_util.h"
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080024#include "base/unix_file/fd_file.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070025#include "class_linker.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080026#include "common_compiler_test.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010027#include "compiled_method-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000029#include "debug/method_debug_info.h"
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -070030#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "dex/quick_compiler_callbacks.h"
David Sehr312f3b22018-03-19 08:39:26 -070033#include "dex/test_dex_file_builder.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080034#include "dex/verification_results.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000035#include "driver/compiler_driver.h"
36#include "driver/compiler_options.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010037#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000038#include "linker/buffered_output_stream.h"
Vladimir Marko74527972016-11-29 15:57:32 +000039#include "linker/elf_writer.h"
40#include "linker/elf_writer_quick.h"
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +000041#include "linker/file_output_stream.h"
Vladimir Marko944da602016-02-19 12:27:55 +000042#include "linker/multi_oat_relative_patcher.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000043#include "linker/vector_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070045#include "mirror/object-inl.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000046#include "mirror/object_array-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010047#include "oat_file-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070048#include "oat_writer.h"
David Sehr82d046e2018-04-23 08:14:19 -070049#include "profile/profile_compilation_info.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070050#include "scoped_thread_state_change-inl.h"
David Brazdil93592f52017-12-08 10:53:27 +000051#include "vdex_file.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070052
Brian Carlstrome24fa612011-09-29 00:53:55 -070053namespace art {
Vladimir Marko74527972016-11-29 15:57:32 +000054namespace linker {
Brian Carlstrome24fa612011-09-29 00:53:55 -070055
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080056class OatTest : public CommonCompilerTest {
Logan Chieneeb7edf2012-03-09 20:38:39 +080057 protected:
Brian Carlstromba150c32013-08-27 17:31:03 -070058 static const bool kCompile = false; // DISABLED_ due to the time to compile libcore
59
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 void CheckMethod(ArtMethod* method,
Logan Chieneeb7edf2012-03-09 20:38:39 +080061 const OatFile::OatMethod& oat_method,
Richard Uhlerfbef44d2014-12-23 09:48:51 -080062 const DexFile& dex_file)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_) {
Logan Chieneeb7edf2012-03-09 20:38:39 +080064 const CompiledMethod* compiled_method =
Richard Uhlerfbef44d2014-12-23 09:48:51 -080065 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
Brian Carlstrom51c24672013-07-11 16:00:56 -070066 method->GetDexMethodIndex()));
Logan Chieneeb7edf2012-03-09 20:38:39 +080067
Ian Rogersd4c4d952014-10-16 20:31:53 -070068 if (compiled_method == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -070069 EXPECT_TRUE(oat_method.GetQuickCode() == nullptr) << method->PrettyMethod() << " "
Ian Rogersd4c4d952014-10-16 20:31:53 -070070 << oat_method.GetQuickCode();
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 EXPECT_EQ(oat_method.GetFrameSizeInBytes(), 0U);
Logan Chieneeb7edf2012-03-09 20:38:39 +080072 EXPECT_EQ(oat_method.GetCoreSpillMask(), 0U);
73 EXPECT_EQ(oat_method.GetFpSpillMask(), 0U);
74 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 const void* quick_oat_code = oat_method.GetQuickCode();
David Sehr709b0702016-10-13 09:12:37 -070076 EXPECT_TRUE(quick_oat_code != nullptr) << method->PrettyMethod();
Elliott Hughes956af0f2014-12-11 14:34:28 -080077 uintptr_t oat_code_aligned = RoundDown(reinterpret_cast<uintptr_t>(quick_oat_code), 2);
78 quick_oat_code = reinterpret_cast<const void*>(oat_code_aligned);
Vladimir Marko35831e82015-09-11 11:59:18 +010079 ArrayRef<const uint8_t> quick_code = compiled_method->GetQuickCode();
80 EXPECT_FALSE(quick_code.empty());
81 size_t code_size = quick_code.size() * sizeof(quick_code[0]);
Elliott Hughes956af0f2014-12-11 14:34:28 -080082 EXPECT_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size))
David Sehr709b0702016-10-13 09:12:37 -070083 << method->PrettyMethod() << " " << code_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 CHECK_EQ(0, memcmp(quick_oat_code, &quick_code[0], code_size));
Logan Chieneeb7edf2012-03-09 20:38:39 +080085 }
86 }
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080087
Vladimir Markoa0431112018-06-25 09:32:54 +010088 void SetupCompiler(const std::vector<std::string>& compiler_options) {
89 std::string error_msg;
Andreas Gampe097f34c2017-08-23 08:57:51 -070090 if (!compiler_options_->ParseCompilerOptions(compiler_options,
Vladimir Markof4efa9e2018-10-17 14:12:45 +010091 /*ignore_unrecognized=*/ false,
Vladimir Markoa0431112018-06-25 09:32:54 +010092 &error_msg)) {
93 LOG(FATAL) << error_msg;
Andreas Gampe097f34c2017-08-23 08:57:51 -070094 UNREACHABLE();
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080095 }
Mathieu Chartiere01b6f62017-07-19 16:55:04 -070096 callbacks_.reset(new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp));
97 callbacks_->SetVerificationResults(verification_results_.get());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080098 Runtime::Current()->SetCompilerCallbacks(callbacks_.get());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -080099 }
100
David Brazdil7b49e6c2016-09-01 11:06:18 +0100101 bool WriteElf(File* vdex_file,
102 File* oat_file,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800103 const std::vector<const DexFile*>& dex_files,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800104 SafeMap<std::string, std::string>& key_value_store,
105 bool verify) {
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800106 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100107 ClearBootImageOption();
108 OatWriter oat_writer(*compiler_options_,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700109 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700110 /*profile_compilation_info*/nullptr,
111 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000112 for (const DexFile* dex_file : dex_files) {
113 ArrayRef<const uint8_t> raw_dex_file(
114 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
115 dex_file->GetHeader().file_size_);
116 if (!oat_writer.AddRawDexFileSource(raw_dex_file,
117 dex_file->GetLocation().c_str(),
118 dex_file->GetLocationChecksum())) {
119 return false;
120 }
121 }
Colin Cross2b41cca2018-11-16 22:43:41 -0800122 return DoWriteElf(
123 vdex_file, oat_file, oat_writer, key_value_store, verify, CopyOption::kOnlyIfCompressed);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000124 }
125
David Brazdil7b49e6c2016-09-01 11:06:18 +0100126 bool WriteElf(File* vdex_file,
127 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000128 const std::vector<const char*>& dex_filenames,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800129 SafeMap<std::string, std::string>& key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800130 bool verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800131 CopyOption copy,
Jeff Haod9df7802017-02-06 16:41:16 -0800132 ProfileCompilationInfo* profile_compilation_info) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000133 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100134 ClearBootImageOption();
135 OatWriter oat_writer(*compiler_options_,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700136 &timings,
137 profile_compilation_info,
138 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000139 for (const char* dex_filename : dex_filenames) {
140 if (!oat_writer.AddDexFileSource(dex_filename, dex_filename)) {
141 return false;
142 }
143 }
Colin Cross2b41cca2018-11-16 22:43:41 -0800144 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000145 }
146
David Brazdil7b49e6c2016-09-01 11:06:18 +0100147 bool WriteElf(File* vdex_file,
148 File* oat_file,
Andreas Gampe43e10b02016-07-15 17:17:34 -0700149 File&& zip_fd,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000150 const char* location,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800151 SafeMap<std::string, std::string>& key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800152 bool verify,
153 CopyOption copy) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000154 TimingLogger timings("WriteElf", false, false);
Vladimir Markoa0431112018-06-25 09:32:54 +0100155 ClearBootImageOption();
156 OatWriter oat_writer(*compiler_options_,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700157 &timings,
Mathieu Chartier603ccab2017-10-20 14:34:28 -0700158 /*profile_compilation_info*/nullptr,
159 CompactDexLevel::kCompactDexLevelNone);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000160 if (!oat_writer.AddZippedDexFilesSource(std::move(zip_fd), location)) {
161 return false;
162 }
Colin Cross2b41cca2018-11-16 22:43:41 -0800163 return DoWriteElf(vdex_file, oat_file, oat_writer, key_value_store, verify, copy);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000164 }
165
David Brazdil7b49e6c2016-09-01 11:06:18 +0100166 bool DoWriteElf(File* vdex_file,
167 File* oat_file,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000168 OatWriter& oat_writer,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800169 SafeMap<std::string, std::string>& key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800170 bool verify,
171 CopyOption copy) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000172 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
Vladimir Markoa0431112018-06-25 09:32:54 +0100173 compiler_driver_->GetCompilerOptions(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100174 oat_file);
Vladimir Marko10c13562015-11-25 14:33:36 +0000175 elf_writer->Start();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100176 OutputStream* oat_rodata = elf_writer->StartRoData();
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100177 std::vector<MemMap> opened_dex_files_maps;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000178 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
Mathieu Chartier792111c2018-02-15 13:02:15 -0800179 if (!oat_writer.WriteAndOpenDexFiles(
180 vdex_file,
181 oat_rodata,
Mathieu Chartier792111c2018-02-15 13:02:15 -0800182 &key_value_store,
183 verify,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100184 /*update_input_vdex=*/ false,
Colin Cross2b41cca2018-11-16 22:43:41 -0800185 copy,
Mathieu Chartier792111c2018-02-15 13:02:15 -0800186 &opened_dex_files_maps,
187 &opened_dex_files)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000188 return false;
189 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000190
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000191 Runtime* runtime = Runtime::Current();
192 ClassLinker* const class_linker = runtime->GetClassLinker();
193 std::vector<const DexFile*> dex_files;
194 for (const std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
195 dex_files.push_back(dex_file.get());
196 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierf284d442016-06-02 11:48:30 -0700197 class_linker->RegisterDexFile(*dex_file, nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000198 }
Vladimir Markoa0431112018-06-25 09:32:54 +0100199 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
200 compiler_options_->GetInstructionSetFeatures(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000201 compiler_driver_->GetCompiledMethodStorage());
Nicolas Geoffray4acefd32016-10-24 13:14:58 +0100202 oat_writer.Initialize(compiler_driver_.get(), nullptr, dex_files);
203 oat_writer.PrepareLayout(&patcher);
Vladimir Markob066d432018-01-03 13:14:37 +0000204 elf_writer->PrepareDynamicSection(oat_writer.GetOatHeader().GetExecutableOffset(),
205 oat_writer.GetCodeSize(),
206 oat_writer.GetDataBimgRelRoSize(),
Vladimir Markoaad75c62016-10-03 08:46:48 +0000207 oat_writer.GetBssSize(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100208 oat_writer.GetBssMethodsOffset(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000209 oat_writer.GetBssRootsOffset(),
210 oat_writer.GetVdexSize());
Vladimir Marko944da602016-02-19 12:27:55 +0000211
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000212 std::unique_ptr<BufferedOutputStream> vdex_out =
213 std::make_unique<BufferedOutputStream>(std::make_unique<FileOutputStream>(vdex_file));
214 if (!oat_writer.WriteVerifierDeps(vdex_out.get(), nullptr)) {
215 return false;
216 }
David Brazdil93592f52017-12-08 10:53:27 +0000217 if (!oat_writer.WriteQuickeningInfo(vdex_out.get())) {
218 return false;
219 }
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000220 if (!oat_writer.WriteChecksumsAndVdexHeader(vdex_out.get())) {
221 return false;
Nicolas Geoffrayf54e5df2016-12-01 10:45:08 +0000222 }
223
David Brazdil7b49e6c2016-09-01 11:06:18 +0100224 if (!oat_writer.WriteRodata(oat_rodata)) {
Vladimir Marko10c13562015-11-25 14:33:36 +0000225 return false;
226 }
David Brazdil7b49e6c2016-09-01 11:06:18 +0100227 elf_writer->EndRoData(oat_rodata);
Vladimir Marko10c13562015-11-25 14:33:36 +0000228
229 OutputStream* text = elf_writer->StartText();
230 if (!oat_writer.WriteCode(text)) {
231 return false;
232 }
233 elf_writer->EndText(text);
234
Vladimir Markob066d432018-01-03 13:14:37 +0000235 if (oat_writer.GetDataBimgRelRoSize() != 0u) {
236 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
237 if (!oat_writer.WriteDataBimgRelRo(data_bimg_rel_ro)) {
238 return false;
239 }
240 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
241 }
242
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000243 if (!oat_writer.WriteHeader(elf_writer->GetStream(), /*boot_image_checksum=*/ 42u)) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000244 return false;
245 }
246
Vladimir Marko10c13562015-11-25 14:33:36 +0000247 elf_writer->WriteDynamicSection();
David Srbecky32210b92017-12-04 14:39:21 +0000248 elf_writer->WriteDebugInfo(oat_writer.GetDebugInfo());
Vladimir Marko10c13562015-11-25 14:33:36 +0000249
Vladimir Markoaad75c62016-10-03 08:46:48 +0000250 if (!elf_writer->End()) {
251 return false;
252 }
253
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100254 for (MemMap& map : opened_dex_files_maps) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000255 opened_dex_files_maps_.emplace_back(std::move(map));
256 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000257 for (std::unique_ptr<const DexFile>& dex_file : opened_dex_files) {
258 opened_dex_files_.emplace_back(dex_file.release());
259 }
260 return true;
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800261 }
262
Jeff Haod9df7802017-02-06 16:41:16 -0800263 void TestDexFileInput(bool verify, bool low_4gb, bool use_profile);
Colin Cross2b41cca2018-11-16 22:43:41 -0800264 void TestZipFileInput(bool verify, CopyOption copy);
Jeff Hao41b2f532017-03-02 16:36:31 -0800265 void TestZipFileInputWithEmptyDex();
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800266
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800267 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000268
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100269 std::vector<MemMap> opened_dex_files_maps_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000270 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Logan Chieneeb7edf2012-03-09 20:38:39 +0800271};
Brian Carlstrome24fa612011-09-29 00:53:55 -0700272
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000273class ZipBuilder {
274 public:
275 explicit ZipBuilder(File* zip_file) : zip_file_(zip_file) { }
276
277 bool AddFile(const char* location, const void* data, size_t size) {
278 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
279 if (offset == static_cast<off_t>(-1)) {
280 return false;
281 }
282
283 ZipFileHeader file_header;
284 file_header.crc32 = crc32(0u, reinterpret_cast<const Bytef*>(data), size);
285 file_header.compressed_size = size;
286 file_header.uncompressed_size = size;
287 file_header.filename_length = strlen(location);
288
289 if (!zip_file_->WriteFully(&file_header, sizeof(file_header)) ||
290 !zip_file_->WriteFully(location, file_header.filename_length) ||
291 !zip_file_->WriteFully(data, size)) {
292 return false;
293 }
294
295 CentralDirectoryFileHeader cdfh;
296 cdfh.crc32 = file_header.crc32;
297 cdfh.compressed_size = size;
298 cdfh.uncompressed_size = size;
299 cdfh.filename_length = file_header.filename_length;
300 cdfh.relative_offset_of_local_file_header = offset;
301 file_data_.push_back(FileData { cdfh, location });
302 return true;
303 }
304
305 bool Finish() {
306 off_t offset = lseek(zip_file_->Fd(), 0, SEEK_CUR);
307 if (offset == static_cast<off_t>(-1)) {
308 return false;
309 }
310
311 size_t central_directory_size = 0u;
312 for (const FileData& file_data : file_data_) {
313 if (!zip_file_->WriteFully(&file_data.cdfh, sizeof(file_data.cdfh)) ||
314 !zip_file_->WriteFully(file_data.location, file_data.cdfh.filename_length)) {
315 return false;
316 }
317 central_directory_size += sizeof(file_data.cdfh) + file_data.cdfh.filename_length;
318 }
319 EndOfCentralDirectoryRecord eocd_record;
320 eocd_record.number_of_central_directory_records_on_this_disk = file_data_.size();
321 eocd_record.total_number_of_central_directory_records = file_data_.size();
322 eocd_record.size_of_central_directory = central_directory_size;
323 eocd_record.offset_of_start_of_central_directory = offset;
324 return
325 zip_file_->WriteFully(&eocd_record, sizeof(eocd_record)) &&
326 zip_file_->Flush() == 0;
327 }
328
329 private:
330 struct PACKED(1) ZipFileHeader {
331 uint32_t signature = 0x04034b50;
332 uint16_t version_needed_to_extract = 10;
333 uint16_t general_purpose_bit_flag = 0;
334 uint16_t compression_method = 0; // 0 = store only.
335 uint16_t file_last_modification_time = 0u;
336 uint16_t file_last_modification_date = 0u;
337 uint32_t crc32;
338 uint32_t compressed_size;
339 uint32_t uncompressed_size;
340 uint16_t filename_length;
341 uint16_t extra_field_length = 0u; // No extra fields.
342 };
343
344 struct PACKED(1) CentralDirectoryFileHeader {
345 uint32_t signature = 0x02014b50;
346 uint16_t version_made_by = 10;
347 uint16_t version_needed_to_extract = 10;
348 uint16_t general_purpose_bit_flag = 0;
349 uint16_t compression_method = 0; // 0 = store only.
350 uint16_t file_last_modification_time = 0u;
351 uint16_t file_last_modification_date = 0u;
352 uint32_t crc32;
353 uint32_t compressed_size;
354 uint32_t uncompressed_size;
355 uint16_t filename_length;
356 uint16_t extra_field_length = 0u; // No extra fields.
357 uint16_t file_comment_length = 0u; // No file comment.
358 uint16_t disk_number_where_file_starts = 0u;
359 uint16_t internal_file_attributes = 0u;
360 uint32_t external_file_attributes = 0u;
361 uint32_t relative_offset_of_local_file_header;
362 };
363
364 struct PACKED(1) EndOfCentralDirectoryRecord {
365 uint32_t signature = 0x06054b50;
366 uint16_t number_of_this_disk = 0u;
367 uint16_t disk_where_central_directory_starts = 0u;
368 uint16_t number_of_central_directory_records_on_this_disk;
369 uint16_t total_number_of_central_directory_records;
370 uint32_t size_of_central_directory;
371 uint32_t offset_of_start_of_central_directory;
372 uint16_t comment_length = 0u; // No file comment.
373 };
374
375 struct FileData {
376 CentralDirectoryFileHeader cdfh;
377 const char* location;
378 };
379
380 File* zip_file_;
381 std::vector<FileData> file_data_;
382};
383
Brian Carlstrome24fa612011-09-29 00:53:55 -0700384TEST_F(OatTest, WriteRead) {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800385 TimingLogger timings("OatTest::WriteRead", false, false);
Jesse Wilson254db0f2011-11-16 16:44:11 -0500386 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700387
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700388 std::string error_msg;
Vladimir Markoa0431112018-06-25 09:32:54 +0100389 SetupCompiler(std::vector<std::string>());
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800390
Ian Rogersd4c4d952014-10-16 20:31:53 -0700391 jobject class_loader = nullptr;
Brian Carlstromba150c32013-08-27 17:31:03 -0700392 if (kCompile) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800393 TimingLogger timings2("OatTest::WriteRead", false, false);
Vladimir Marko2afaff72018-11-30 17:01:50 +0000394 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings2);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700395 }
396
Vladimir Markof0499c92018-03-09 12:42:09 +0000397 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700398 SafeMap<std::string, std::string> key_value_store;
David Brazdil7b49e6c2016-09-01 11:06:18 +0100399 bool success = WriteElf(tmp_vdex.GetFile(),
400 tmp_oat.GetFile(),
401 class_linker->GetBootClassPath(),
402 key_value_store,
403 false);
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700404 ASSERT_TRUE(success);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700405
Brian Carlstromba150c32013-08-27 17:31:03 -0700406 if (kCompile) { // OatWriter strips the code, regenerate to compare
Vladimir Marko2afaff72018-11-30 17:01:50 +0000407 CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700408 }
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100409 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100410 tmp_oat.GetFilename(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100411 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100412 /*executable=*/ false,
413 /*low_4gb=*/ true,
414 /*abs_dex_location=*/ nullptr,
415 /*reservation=*/ nullptr,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800416 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700417 ASSERT_TRUE(oat_file.get() != nullptr) << error_msg;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700418 const OatHeader& oat_header = oat_file->GetOatHeader();
Brian Carlstromf852fb22012-10-19 11:01:58 -0700419 ASSERT_TRUE(oat_header.IsValid());
Yi Kong9515b512015-11-11 14:36:07 +0000420 ASSERT_EQ(class_linker->GetBootClassPath().size(), oat_header.GetDexFileCount()); // core
Vladimir Markoc10a0c62018-11-16 11:39:22 +0000421 ASSERT_EQ(42u, oat_header.GetBootImageChecksum());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700422
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800423 ASSERT_TRUE(java_lang_dex_file_ != nullptr);
424 const DexFile& dex_file = *java_lang_dex_file_;
425 uint32_t dex_file_checksum = dex_file.GetLocationChecksum();
Andreas Gampeb40d3612018-06-26 15:49:42 -0700426 const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(),
427 &dex_file_checksum);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700428 ASSERT_TRUE(oat_dex_file != nullptr);
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800429 CHECK_EQ(dex_file.GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
Vladimir Markof4da6752014-08-01 19:04:18 +0100430 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700431 auto pointer_size = class_linker->GetImagePointerSize();
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700432 for (ClassAccessor accessor : dex_file.GetClasses()) {
433 size_t num_virtual_methods = accessor.NumVirtualMethods();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700434
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700435 const char* descriptor = accessor.GetDescriptor();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100436 ObjPtr<mirror::Class> klass = class_linker->FindClass(soa.Self(),
437 descriptor,
438 ScopedNullHandle<mirror::ClassLoader>());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700439
Mathieu Chartierbee5e2d2018-08-02 11:00:16 -0700440 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
Vladimir Marko2c64a832018-01-04 11:31:56 +0000441 CHECK_EQ(ClassStatus::kNotReady, oat_class.GetStatus()) << descriptor;
Brian Carlstromba150c32013-08-27 17:31:03 -0700442 CHECK_EQ(kCompile ? OatClassType::kOatClassAllCompiled : OatClassType::kOatClassNoneCompiled,
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100443 oat_class.GetType()) << descriptor;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700444
445 size_t method_index = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700446 for (auto& m : klass->GetDirectMethods(pointer_size)) {
447 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
448 ++method_index;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700449 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 size_t visited_virtuals = 0;
Alex Lighte64300b2015-12-15 15:02:47 -0800451 // TODO We should also check copied methods in this test.
452 for (auto& m : klass->GetDeclaredVirtualMethods(pointer_size)) {
Alex Lightfcea56f2016-02-17 11:59:05 -0800453 if (!klass->IsInterface()) {
Alex Light36121492016-02-22 13:43:29 -0800454 EXPECT_FALSE(m.IsCopied());
Alex Lightfcea56f2016-02-17 11:59:05 -0800455 }
Alex Lighte64300b2015-12-15 15:02:47 -0800456 CheckMethod(&m, oat_class.GetOatMethod(method_index), dex_file);
457 ++method_index;
458 ++visited_virtuals;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700459 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460 EXPECT_EQ(visited_virtuals, num_virtual_methods);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700461 }
462}
463
Brian Carlstrom341df942012-06-27 12:29:22 -0700464TEST_F(OatTest, OatHeaderSizeCheck) {
465 // If this test is failing and you have to update these constants,
466 // it is time to update OatHeader::kOatVersion
Vladimir Markoe0669322018-09-03 15:44:54 +0100467 EXPECT_EQ(68U, sizeof(OatHeader));
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800468 EXPECT_EQ(4U, sizeof(OatMethodOffsets));
David Srbecky8cd54542018-07-15 23:58:44 +0100469 EXPECT_EQ(8U, sizeof(OatQuickMethodHeader));
Alex Lightd109e302018-06-27 10:25:41 -0700470 EXPECT_EQ(166 * static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA)),
Andreas Gampe542451c2016-07-26 09:02:02 -0700471 sizeof(QuickEntryPoints));
Brian Carlstrom341df942012-06-27 12:29:22 -0700472}
473
Brian Carlstromf852fb22012-10-19 11:01:58 -0700474TEST_F(OatTest, OatHeaderIsValid) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000475 InstructionSet insn_set = InstructionSet::kX86;
Jeff Hao5d3baf62016-05-23 19:17:04 -0700476 std::string error_msg;
477 std::unique_ptr<const InstructionSetFeatures> insn_features(
478 InstructionSetFeatures::FromVariant(insn_set, "default", &error_msg));
479 ASSERT_TRUE(insn_features.get() != nullptr) << error_msg;
480 std::unique_ptr<OatHeader> oat_header(OatHeader::Create(insn_set,
481 insn_features.get(),
482 0u,
483 nullptr));
484 ASSERT_NE(oat_header.get(), nullptr);
485 ASSERT_TRUE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700486
Jeff Hao5d3baf62016-05-23 19:17:04 -0700487 char* magic = const_cast<char*>(oat_header->GetMagic());
488 strcpy(magic, ""); // bad magic
489 ASSERT_FALSE(oat_header->IsValid());
490 strcpy(magic, "oat\n000"); // bad version
491 ASSERT_FALSE(oat_header->IsValid());
Brian Carlstromf852fb22012-10-19 11:01:58 -0700492}
493
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800494TEST_F(OatTest, EmptyTextSection) {
495 TimingLogger timings("OatTest::EmptyTextSection", false, false);
496
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800497 std::vector<std::string> compiler_options;
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100498 compiler_options.push_back("--compiler-filter=extract");
Vladimir Markoa0431112018-06-25 09:32:54 +0100499 SetupCompiler(compiler_options);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800500
501 jobject class_loader;
502 {
503 ScopedObjectAccess soa(Thread::Current());
504 class_loader = LoadDex("Main");
505 }
506 ASSERT_TRUE(class_loader != nullptr);
507 std::vector<const DexFile*> dex_files = GetDexFiles(class_loader);
508 ASSERT_TRUE(!dex_files.empty());
509
510 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
511 for (const DexFile* dex_file : dex_files) {
512 ScopedObjectAccess soa(Thread::Current());
Vladimir Markobcf17522018-06-01 13:14:32 +0100513 class_linker->RegisterDexFile(*dex_file, soa.Decode<mirror::ClassLoader>(class_loader));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800514 }
Vladimir Marko2afaff72018-11-30 17:01:50 +0000515 CompileAll(class_loader, dex_files, &timings);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800516
Vladimir Markof0499c92018-03-09 12:42:09 +0000517 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800518 SafeMap<std::string, std::string> key_value_store;
Vladimir Markof0499c92018-03-09 12:42:09 +0000519 bool success = WriteElf(tmp_vdex.GetFile(),
520 tmp_oat.GetFile(),
521 dex_files,
522 key_value_store,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100523 /*verify=*/ false);
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800524 ASSERT_TRUE(success);
525
Vladimir Markoa0431112018-06-25 09:32:54 +0100526 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100527 std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100528 tmp_oat.GetFilename(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100529 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100530 /*executable=*/ false,
531 /*low_4gb=*/ false,
532 /*abs_dex_location=*/ nullptr,
533 /*reservation=*/ nullptr,
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800534 &error_msg));
535 ASSERT_TRUE(oat_file != nullptr);
David Brazdil7b49e6c2016-09-01 11:06:18 +0100536 EXPECT_LT(static_cast<size_t>(oat_file->Size()),
537 static_cast<size_t>(tmp_oat.GetFile()->GetLength()));
Mathieu Chartier07ddb6f2015-11-05 11:16:34 -0800538}
539
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800540static void MaybeModifyDexFileToFail(bool verify, std::unique_ptr<const DexFile>& data) {
541 // If in verify mode (= fail the verifier mode), make sure we fail early. We'll fail already
542 // because of the missing map, but that may lead to out of bounds reads.
543 if (verify) {
544 const_cast<DexFile::Header*>(&data->GetHeader())->checksum_++;
545 }
546}
547
Jeff Haod9df7802017-02-06 16:41:16 -0800548void OatTest::TestDexFileInput(bool verify, bool low_4gb, bool use_profile) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000549 TimingLogger timings("OatTest::DexFileInput", false, false);
550
551 std::vector<const char*> input_filenames;
552
553 ScratchFile dex_file1;
554 TestDexFileBuilder builder1;
555 builder1.AddField("Lsome.TestClass;", "int", "someField");
556 builder1.AddMethod("Lsome.TestClass;", "()I", "foo");
557 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800558
559 MaybeModifyDexFileToFail(verify, dex_file1_data);
560
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000561 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
562 dex_file1_data->GetHeader().file_size_);
563 ASSERT_TRUE(success);
564 success = dex_file1.GetFile()->Flush() == 0;
565 ASSERT_TRUE(success);
566 input_filenames.push_back(dex_file1.GetFilename().c_str());
567
568 ScratchFile dex_file2;
569 TestDexFileBuilder builder2;
570 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
571 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
572 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800573
574 MaybeModifyDexFileToFail(verify, dex_file2_data);
575
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000576 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
577 dex_file2_data->GetHeader().file_size_);
578 ASSERT_TRUE(success);
579 success = dex_file2.GetFile()->Flush() == 0;
580 ASSERT_TRUE(success);
581 input_filenames.push_back(dex_file2.GetFilename().c_str());
582
Vladimir Markof0499c92018-03-09 12:42:09 +0000583 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000584 SafeMap<std::string, std::string> key_value_store;
Jeff Haod9df7802017-02-06 16:41:16 -0800585 std::unique_ptr<ProfileCompilationInfo>
586 profile_compilation_info(use_profile ? new ProfileCompilationInfo() : nullptr);
Vladimir Markof0499c92018-03-09 12:42:09 +0000587 success = WriteElf(tmp_vdex.GetFile(),
588 tmp_oat.GetFile(),
David Brazdil7b49e6c2016-09-01 11:06:18 +0100589 input_filenames,
590 key_value_store,
Jeff Haod9df7802017-02-06 16:41:16 -0800591 verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800592 CopyOption::kOnlyIfCompressed,
Jeff Haod9df7802017-02-06 16:41:16 -0800593 profile_compilation_info.get());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800594
595 // In verify mode, we expect failure.
596 if (verify) {
597 ASSERT_FALSE(success);
598 return;
599 }
600
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000601 ASSERT_TRUE(success);
602
603 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100604 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100605 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000606 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100607 /*executable=*/ false,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800608 low_4gb,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100609 /*abs_dex_location=*/ nullptr,
610 /*reservation=*/ nullptr,
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000611 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000612 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800613 if (low_4gb) {
614 uintptr_t begin = reinterpret_cast<uintptr_t>(opened_oat_file->Begin());
615 EXPECT_EQ(begin, static_cast<uint32_t>(begin));
616 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000617 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
618 std::unique_ptr<const DexFile> opened_dex_file1 =
619 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
620 std::unique_ptr<const DexFile> opened_dex_file2 =
621 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
622
Nicolas Geoffray6f73f4a2017-11-23 12:51:47 +0000623 ASSERT_EQ(opened_oat_file->GetOatDexFiles()[0]->GetDexFileLocationChecksum(),
624 dex_file1_data->GetHeader().checksum_);
625 ASSERT_EQ(opened_oat_file->GetOatDexFiles()[1]->GetDexFileLocationChecksum(),
626 dex_file2_data->GetHeader().checksum_);
627
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000628 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
629 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
630 &opened_dex_file1->GetHeader(),
631 dex_file1_data->GetHeader().file_size_));
632 ASSERT_EQ(dex_file1_data->GetLocation(), opened_dex_file1->GetLocation());
633
634 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
635 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
636 &opened_dex_file2->GetHeader(),
637 dex_file2_data->GetHeader().file_size_));
638 ASSERT_EQ(dex_file2_data->GetLocation(), opened_dex_file2->GetLocation());
David Brazdil93592f52017-12-08 10:53:27 +0000639
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000640 const VdexFile::DexSectionHeader &vdex_header =
641 opened_oat_file->GetVdexFile()->GetDexSectionHeader();
Mathieu Chartier210531f2018-01-12 10:15:51 -0800642 if (!compiler_driver_->GetCompilerOptions().IsQuickeningCompilationEnabled()) {
643 // If quickening is enabled we will always write the table since there is no special logic that
644 // checks for all methods not being quickened (not worth the complexity).
645 ASSERT_EQ(vdex_header.GetQuickeningInfoSize(), 0u);
646 }
David Brazdil93592f52017-12-08 10:53:27 +0000647
Vladimir Markof0499c92018-03-09 12:42:09 +0000648 int64_t actual_vdex_size = tmp_vdex.GetFile()->GetLength();
David Brazdil93592f52017-12-08 10:53:27 +0000649 ASSERT_GE(actual_vdex_size, 0);
Nicolas Geoffray3a293552018-03-02 10:52:16 +0000650 ASSERT_EQ((uint64_t) actual_vdex_size, opened_oat_file->GetVdexFile()->GetComputedFileSize());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000651}
652
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800653TEST_F(OatTest, DexFileInputCheckOutput) {
Jeff Haod9df7802017-02-06 16:41:16 -0800654 TestDexFileInput(/*verify*/false, /*low_4gb*/false, /*use_profile*/false);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800655}
656
657TEST_F(OatTest, DexFileInputCheckOutputLow4GB) {
Jeff Haod9df7802017-02-06 16:41:16 -0800658 TestDexFileInput(/*verify*/false, /*low_4gb*/true, /*use_profile*/false);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800659}
660
661TEST_F(OatTest, DexFileInputCheckVerifier) {
Jeff Haod9df7802017-02-06 16:41:16 -0800662 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/false);
663}
664
665TEST_F(OatTest, DexFileFailsVerifierWithLayout) {
666 TestDexFileInput(/*verify*/true, /*low_4gb*/false, /*use_profile*/true);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800667}
668
Colin Cross2b41cca2018-11-16 22:43:41 -0800669void OatTest::TestZipFileInput(bool verify, CopyOption copy) {
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000670 TimingLogger timings("OatTest::DexFileInput", false, false);
671
672 ScratchFile zip_file;
673 ZipBuilder zip_builder(zip_file.GetFile());
674
675 ScratchFile dex_file1;
676 TestDexFileBuilder builder1;
677 builder1.AddField("Lsome.TestClass;", "long", "someField");
678 builder1.AddMethod("Lsome.TestClass;", "()D", "foo");
679 std::unique_ptr<const DexFile> dex_file1_data = builder1.Build(dex_file1.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800680
681 MaybeModifyDexFileToFail(verify, dex_file1_data);
682
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000683 bool success = dex_file1.GetFile()->WriteFully(&dex_file1_data->GetHeader(),
684 dex_file1_data->GetHeader().file_size_);
685 ASSERT_TRUE(success);
686 success = dex_file1.GetFile()->Flush() == 0;
687 ASSERT_TRUE(success);
688 success = zip_builder.AddFile("classes.dex",
689 &dex_file1_data->GetHeader(),
690 dex_file1_data->GetHeader().file_size_);
691 ASSERT_TRUE(success);
692
693 ScratchFile dex_file2;
694 TestDexFileBuilder builder2;
695 builder2.AddField("Land.AnotherTestClass;", "boolean", "someOtherField");
696 builder2.AddMethod("Land.AnotherTestClass;", "()J", "bar");
697 std::unique_ptr<const DexFile> dex_file2_data = builder2.Build(dex_file2.GetFilename());
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800698
699 MaybeModifyDexFileToFail(verify, dex_file2_data);
700
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000701 success = dex_file2.GetFile()->WriteFully(&dex_file2_data->GetHeader(),
702 dex_file2_data->GetHeader().file_size_);
703 ASSERT_TRUE(success);
704 success = dex_file2.GetFile()->Flush() == 0;
705 ASSERT_TRUE(success);
706 success = zip_builder.AddFile("classes2.dex",
707 &dex_file2_data->GetHeader(),
708 dex_file2_data->GetHeader().file_size_);
709 ASSERT_TRUE(success);
710
711 success = zip_builder.Finish();
712 ASSERT_TRUE(success) << strerror(errno);
713
714 SafeMap<std::string, std::string> key_value_store;
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000715 {
716 // Test using the AddDexFileSource() interface with the zip file.
Igor Murashkin5573c372017-11-16 13:34:30 -0800717 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000718
Vladimir Markof0499c92018-03-09 12:42:09 +0000719 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
720 success = WriteElf(tmp_vdex.GetFile(),
721 tmp_oat.GetFile(),
722 input_filenames,
723 key_value_store,
724 verify,
Colin Cross2b41cca2018-11-16 22:43:41 -0800725 copy,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100726 /*profile_compilation_info=*/ nullptr);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000727
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800728 if (verify) {
729 ASSERT_FALSE(success);
730 } else {
731 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000732
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800733 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100734 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100735 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000736 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100737 /*executable=*/ false,
738 /*low_4gb=*/ false,
739 /*abs_dex_location=*/ nullptr,
740 /*reservation=*/ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800741 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000742 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800743 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
744 std::unique_ptr<const DexFile> opened_dex_file1 =
745 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
746 std::unique_ptr<const DexFile> opened_dex_file2 =
747 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000748
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800749 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
750 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
751 &opened_dex_file1->GetHeader(),
752 dex_file1_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700753 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800754 opened_dex_file1->GetLocation());
755
756 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
757 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
758 &opened_dex_file2->GetHeader(),
759 dex_file2_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700760 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800761 opened_dex_file2->GetLocation());
762 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000763 }
764
765 {
766 // Test using the AddZipDexFileSource() interface with the zip file handle.
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700767 File zip_fd(DupCloexec(zip_file.GetFd()), /*check_usage=*/ false);
Andreas Gampe43e10b02016-07-15 17:17:34 -0700768 ASSERT_NE(-1, zip_fd.Fd());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000769
Vladimir Markof0499c92018-03-09 12:42:09 +0000770 ScratchFile tmp_base, tmp_oat(tmp_base, ".oat"), tmp_vdex(tmp_base, ".vdex");
771 success = WriteElf(tmp_vdex.GetFile(),
772 tmp_oat.GetFile(),
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000773 std::move(zip_fd),
774 zip_file.GetFilename().c_str(),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800775 key_value_store,
Colin Cross2b41cca2018-11-16 22:43:41 -0800776 verify,
777 copy);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800778 if (verify) {
779 ASSERT_FALSE(success);
780 } else {
781 ASSERT_TRUE(success);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000782
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800783 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100784 std::unique_ptr<OatFile> opened_oat_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100785 tmp_oat.GetFilename(),
Vladimir Markof0499c92018-03-09 12:42:09 +0000786 tmp_oat.GetFilename(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100787 /*executable=*/ false,
788 /*low_4gb=*/ false,
789 /*abs_dex_location=*/ nullptr,
790 /*reservation=*/ nullptr,
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800791 &error_msg));
Vladimir Markof0499c92018-03-09 12:42:09 +0000792 ASSERT_TRUE(opened_oat_file != nullptr) << error_msg;
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800793 ASSERT_EQ(2u, opened_oat_file->GetOatDexFiles().size());
794 std::unique_ptr<const DexFile> opened_dex_file1 =
795 opened_oat_file->GetOatDexFiles()[0]->OpenDexFile(&error_msg);
796 std::unique_ptr<const DexFile> opened_dex_file2 =
797 opened_oat_file->GetOatDexFiles()[1]->OpenDexFile(&error_msg);
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000798
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800799 ASSERT_EQ(dex_file1_data->GetHeader().file_size_, opened_dex_file1->GetHeader().file_size_);
800 ASSERT_EQ(0, memcmp(&dex_file1_data->GetHeader(),
801 &opened_dex_file1->GetHeader(),
802 dex_file1_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700803 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(0, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800804 opened_dex_file1->GetLocation());
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000805
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800806 ASSERT_EQ(dex_file2_data->GetHeader().file_size_, opened_dex_file2->GetHeader().file_size_);
807 ASSERT_EQ(0, memcmp(&dex_file2_data->GetHeader(),
808 &opened_dex_file2->GetHeader(),
809 dex_file2_data->GetHeader().file_size_));
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700810 ASSERT_EQ(DexFileLoader::GetMultiDexLocation(1, zip_file.GetFilename().c_str()),
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800811 opened_dex_file2->GetLocation());
812 }
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000813 }
814}
815
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800816TEST_F(OatTest, ZipFileInputCheckOutput) {
Colin Cross2b41cca2018-11-16 22:43:41 -0800817 TestZipFileInput(false, CopyOption::kOnlyIfCompressed);
818}
819
820TEST_F(OatTest, ZipFileInputCheckOutputWithoutCopy) {
821 TestZipFileInput(false, CopyOption::kNever);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800822}
823
824TEST_F(OatTest, ZipFileInputCheckVerifier) {
Colin Cross2b41cca2018-11-16 22:43:41 -0800825 TestZipFileInput(true, CopyOption::kOnlyIfCompressed);
Andreas Gampe3a2bd292016-01-26 17:23:47 -0800826}
827
Jeff Hao41b2f532017-03-02 16:36:31 -0800828void OatTest::TestZipFileInputWithEmptyDex() {
829 ScratchFile zip_file;
830 ZipBuilder zip_builder(zip_file.GetFile());
831 bool success = zip_builder.AddFile("classes.dex", nullptr, 0);
832 ASSERT_TRUE(success);
833 success = zip_builder.Finish();
834 ASSERT_TRUE(success) << strerror(errno);
835
836 SafeMap<std::string, std::string> key_value_store;
Igor Murashkin5573c372017-11-16 13:34:30 -0800837 std::vector<const char*> input_filenames = { zip_file.GetFilename().c_str() };
Jeff Hao41b2f532017-03-02 16:36:31 -0800838 ScratchFile oat_file, vdex_file(oat_file, ".vdex");
839 std::unique_ptr<ProfileCompilationInfo> profile_compilation_info(new ProfileCompilationInfo());
Vladimir Markof0499c92018-03-09 12:42:09 +0000840 success = WriteElf(vdex_file.GetFile(),
841 oat_file.GetFile(),
842 input_filenames,
843 key_value_store,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100844 /*verify=*/ false,
Colin Cross2b41cca2018-11-16 22:43:41 -0800845 CopyOption::kOnlyIfCompressed,
Vladimir Markof0499c92018-03-09 12:42:09 +0000846 profile_compilation_info.get());
Jeff Hao41b2f532017-03-02 16:36:31 -0800847 ASSERT_FALSE(success);
848}
849
850TEST_F(OatTest, ZipFileInputWithEmptyDex) {
851 TestZipFileInputWithEmptyDex();
852}
853
Vladimir Marko74527972016-11-29 15:57:32 +0000854} // namespace linker
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855} // namespace art