blob: 934936d4b39c195b4142bc273ff40317dc165987 [file] [log] [blame]
Alex Klyubind1d5c952017-12-15 12:57:33 -08001/*
2 * Copyright (C) 2017 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 */
16
Alex Klyubin3856af02017-10-23 13:53:13 -070017#include <openssl/sha.h>
Alex Klyubina46e50b2017-12-19 12:16:11 -080018#include <dirent.h>
19#include <sys/types.h>
20
Alex Klyubind1d5c952017-12-15 12:57:33 -080021#include <string>
22#include <vector>
23
24#include "android-base/stringprintf.h"
25#include "android-base/strings.h"
26
Vladimir Marko3f4a0bc2018-04-12 10:08:58 +010027#include "base/hex_dump.h"
David Sehr67bf42e2018-02-26 16:43:04 -080028#include "base/leb128.h"
Alex Klyubind1d5c952017-12-15 12:57:33 -080029#include "dexopt_test.h"
30#include "runtime.h"
31
32#include <gtest/gtest.h>
33
34namespace art {
35
36using android::base::StringPrintf;
37
38class PatchoatTest : public DexoptTest {
39 public:
Alex Klyubina46e50b2017-12-19 12:16:11 -080040 static bool ListDirFilesEndingWith(
41 const std::string& dir,
42 const std::string& suffix,
43 std::vector<std::string>* filenames,
44 std::string* error_msg) {
45 DIR* d = opendir(dir.c_str());
46 if (d == nullptr) {
47 *error_msg = "Failed to open directory";
48 return false;
49 }
50 dirent* e;
51 struct stat s;
52 size_t suffix_len = suffix.size();
53 while ((e = readdir(d)) != nullptr) {
54 if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) {
55 continue;
56 }
57 size_t name_len = strlen(e->d_name);
58 if ((name_len < suffix_len) || (strcmp(&e->d_name[name_len - suffix_len], suffix.c_str()))) {
59 continue;
60 }
61 std::string basename(e->d_name);
62 std::string filename = dir + "/" + basename;
63 int stat_result = lstat(filename.c_str(), &s);
64 if (stat_result != 0) {
65 *error_msg =
66 StringPrintf("Failed to stat %s: stat returned %d", filename.c_str(), stat_result);
67 return false;
68 }
69 if (S_ISDIR(s.st_mode)) {
70 continue;
71 }
72 filenames->push_back(basename);
73 }
74 closedir(d);
75 return true;
76 }
77
Alex Klyubind1d5c952017-12-15 12:57:33 -080078 static void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
79 args.push_back("--runtime-arg");
80 args.push_back(arg);
81 }
82
83 bool CompileBootImage(const std::vector<std::string>& extra_args,
84 const std::string& image_file_name_prefix,
85 uint32_t base_addr,
86 std::string* error_msg) {
87 Runtime* const runtime = Runtime::Current();
88 std::vector<std::string> argv;
89 argv.push_back(runtime->GetCompilerExecutable());
90 AddRuntimeArg(argv, "-Xms64m");
91 AddRuntimeArg(argv, "-Xmx64m");
92 std::vector<std::string> dex_files = GetLibCoreDexFileNames();
93 for (const std::string& dex_file : dex_files) {
94 argv.push_back("--dex-file=" + dex_file);
95 argv.push_back("--dex-location=" + dex_file);
96 }
97 if (runtime->IsJavaDebuggable()) {
98 argv.push_back("--debuggable");
99 }
100 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
101
102 AddRuntimeArg(argv, "-Xverify:softfail");
103
104 if (!kIsTargetBuild) {
105 argv.push_back("--host");
106 }
107
108 argv.push_back("--image=" + image_file_name_prefix + ".art");
109 argv.push_back("--oat-file=" + image_file_name_prefix + ".oat");
110 argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
111 argv.push_back(StringPrintf("--base=0x%" PRIx32, base_addr));
112 argv.push_back("--compile-pic");
113 argv.push_back("--multi-image");
114 argv.push_back("--no-generate-debug-info");
115
116 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
117 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
118
119 // We must set --android-root.
120 const char* android_root = getenv("ANDROID_ROOT");
121 CHECK(android_root != nullptr);
122 argv.push_back("--android-root=" + std::string(android_root));
123 argv.insert(argv.end(), extra_args.begin(), extra_args.end());
124
125 return RunDex2OatOrPatchoat(argv, error_msg);
126 }
127
Chris Morin754b7572018-01-19 18:04:46 -0800128 static std::vector<std::string> BasePatchoatCommand(const std::string& input_image_location,
129 off_t base_offset_delta) {
Alex Klyubind1d5c952017-12-15 12:57:33 -0800130 Runtime* const runtime = Runtime::Current();
131 std::vector<std::string> argv;
132 argv.push_back(runtime->GetPatchoatExecutable());
133 argv.push_back("--input-image-location=" + input_image_location);
Alex Klyubind1d5c952017-12-15 12:57:33 -0800134 argv.push_back(StringPrintf("--base-offset-delta=0x%jx", (intmax_t) base_offset_delta));
135 argv.push_back(StringPrintf("--instruction-set=%s", GetInstructionSetString(kRuntimeISA)));
136
Chris Morin754b7572018-01-19 18:04:46 -0800137 return argv;
138 }
139
140 bool RelocateBootImage(const std::string& input_image_location,
Chris Morin88c6d262018-02-13 15:26:21 -0800141 const std::string& output_image_directory,
Chris Morin754b7572018-01-19 18:04:46 -0800142 off_t base_offset_delta,
143 std::string* error_msg) {
144 std::vector<std::string> argv = BasePatchoatCommand(input_image_location, base_offset_delta);
Chris Morin88c6d262018-02-13 15:26:21 -0800145 argv.push_back("--output-image-directory=" + output_image_directory);
Chris Morin754b7572018-01-19 18:04:46 -0800146
147 return RunDex2OatOrPatchoat(argv, error_msg);
148 }
149
150 bool VerifyBootImage(const std::string& input_image_location,
Chris Morin88c6d262018-02-13 15:26:21 -0800151 const std::string& output_image_directory,
Chris Morin754b7572018-01-19 18:04:46 -0800152 off_t base_offset_delta,
153 std::string* error_msg) {
154 std::vector<std::string> argv = BasePatchoatCommand(input_image_location, base_offset_delta);
Chris Morin88c6d262018-02-13 15:26:21 -0800155 argv.push_back("--output-image-directory=" + output_image_directory);
Chris Morin754b7572018-01-19 18:04:46 -0800156 argv.push_back("--verify");
157
Alex Klyubind1d5c952017-12-15 12:57:33 -0800158 return RunDex2OatOrPatchoat(argv, error_msg);
159 }
160
Alex Klyubin3856af02017-10-23 13:53:13 -0700161 bool GenerateBootImageRelFile(const std::string& input_image_location,
Chris Morin88c6d262018-02-13 15:26:21 -0800162 const std::string& output_rel_directory,
Alex Klyubin3856af02017-10-23 13:53:13 -0700163 off_t base_offset_delta,
164 std::string* error_msg) {
Chris Morin754b7572018-01-19 18:04:46 -0800165 std::vector<std::string> argv = BasePatchoatCommand(input_image_location, base_offset_delta);
Chris Morin88c6d262018-02-13 15:26:21 -0800166 argv.push_back("--output-image-relocation-directory=" + output_rel_directory);
Alex Klyubin3856af02017-10-23 13:53:13 -0700167
168 return RunDex2OatOrPatchoat(argv, error_msg);
169 }
170
Alex Klyubind1d5c952017-12-15 12:57:33 -0800171 bool RunDex2OatOrPatchoat(const std::vector<std::string>& args, std::string* error_msg) {
172 int link[2];
173
174 if (pipe(link) == -1) {
175 return false;
176 }
177
178 pid_t pid = fork();
179 if (pid == -1) {
180 return false;
181 }
182
183 if (pid == 0) {
184 // We need dex2oat to actually log things.
185 setenv("ANDROID_LOG_TAGS", "*:e", 1);
186 dup2(link[1], STDERR_FILENO);
187 close(link[0]);
188 close(link[1]);
189 std::vector<const char*> c_args;
190 for (const std::string& str : args) {
191 c_args.push_back(str.c_str());
192 }
193 c_args.push_back(nullptr);
194 execv(c_args[0], const_cast<char* const*>(c_args.data()));
195 exit(1);
196 UNREACHABLE();
197 } else {
198 close(link[1]);
199 char buffer[128];
200 memset(buffer, 0, 128);
201 ssize_t bytes_read = 0;
202
203 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
204 *error_msg += std::string(buffer, bytes_read);
205 }
206 close(link[0]);
207 int status = -1;
208 if (waitpid(pid, &status, 0) != -1) {
209 return (status == 0);
210 }
211 return false;
212 }
213 }
214
215 bool CompileBootImageToDir(
216 const std::string& output_dir,
217 const std::vector<std::string>& dex2oat_extra_args,
218 uint32_t base_addr,
219 std::string* error_msg) {
220 return CompileBootImage(dex2oat_extra_args, output_dir + "/boot", base_addr, error_msg);
221 }
222
223 bool CopyImageChecksumAndSetPatchDelta(
224 const std::string& src_image_filename,
225 const std::string& dest_image_filename,
226 off_t dest_patch_delta,
227 std::string* error_msg) {
228 std::unique_ptr<File> src_file(OS::OpenFileForReading(src_image_filename.c_str()));
229 if (src_file.get() == nullptr) {
230 *error_msg = StringPrintf("Failed to open source image file %s", src_image_filename.c_str());
231 return false;
232 }
233 ImageHeader src_header;
234 if (!src_file->ReadFully(&src_header, sizeof(src_header))) {
235 *error_msg = StringPrintf("Failed to read source image file %s", src_image_filename.c_str());
236 return false;
237 }
238
239 std::unique_ptr<File> dest_file(OS::OpenFileReadWrite(dest_image_filename.c_str()));
240 if (dest_file.get() == nullptr) {
241 *error_msg =
242 StringPrintf("Failed to open destination image file %s", dest_image_filename.c_str());
243 return false;
244 }
245 ImageHeader dest_header;
246 if (!dest_file->ReadFully(&dest_header, sizeof(dest_header))) {
247 *error_msg =
248 StringPrintf("Failed to read destination image file %s", dest_image_filename.c_str());
249 return false;
250 }
251 dest_header.SetOatChecksum(src_header.GetOatChecksum());
252 dest_header.SetPatchDelta(dest_patch_delta);
253 if (!dest_file->ResetOffset()) {
254 *error_msg =
255 StringPrintf(
256 "Failed to seek to start of destination image file %s", dest_image_filename.c_str());
257 return false;
258 }
259 if (!dest_file->WriteFully(&dest_header, sizeof(dest_header))) {
260 *error_msg =
261 StringPrintf("Failed to write to destination image file %s", dest_image_filename.c_str());
262 dest_file->Erase();
263 return false;
264 }
265 if (dest_file->FlushCloseOrErase() != 0) {
266 *error_msg =
267 StringPrintf(
268 "Failed to flush/close destination image file %s", dest_image_filename.c_str());
269 return false;
270 }
271
272 return true;
273 }
274
275 bool ReadFully(
276 const std::string& filename, std::vector<uint8_t>* contents, std::string* error_msg) {
277 std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str()));
278 if (file.get() == nullptr) {
279 *error_msg = "Failed to open";
280 return false;
281 }
282 int64_t size = file->GetLength();
283 if (size < 0) {
284 *error_msg = "Failed to get size";
285 return false;
286 }
287 contents->resize(size);
288 if (!file->ReadFully(&(*contents)[0], size)) {
289 *error_msg = "Failed to read";
290 contents->clear();
291 return false;
292 }
293 return true;
294 }
295
296 bool BinaryDiff(
297 const std::string& filename1, const std::string& filename2, std::string* error_msg) {
298 std::string read_error_msg;
299 std::vector<uint8_t> image1;
300 if (!ReadFully(filename1, &image1, &read_error_msg)) {
301 *error_msg = StringPrintf("Failed to read %s: %s", filename1.c_str(), read_error_msg.c_str());
302 return true;
303 }
304 std::vector<uint8_t> image2;
305 if (!ReadFully(filename2, &image2, &read_error_msg)) {
306 *error_msg = StringPrintf("Failed to read %s: %s", filename2.c_str(), read_error_msg.c_str());
307 return true;
308 }
Chris Morin754b7572018-01-19 18:04:46 -0800309 if (image1.size() != image1.size()) {
Alex Klyubind1d5c952017-12-15 12:57:33 -0800310 *error_msg =
311 StringPrintf(
Chris Morin754b7572018-01-19 18:04:46 -0800312 "%s and %s are of different size: %zu vs %zu",
313 filename1.c_str(),
314 filename2.c_str(),
315 image1.size(),
316 image2.size());
317 return true;
Alex Klyubind1d5c952017-12-15 12:57:33 -0800318 }
Chris Morin754b7572018-01-19 18:04:46 -0800319 size_t size = image1.size();
320 for (size_t i = 0; i < size; i++) {
321 if (image1[i] != image2[i]) {
Alex Klyubind1d5c952017-12-15 12:57:33 -0800322 *error_msg =
Chris Morin754b7572018-01-19 18:04:46 -0800323 StringPrintf("%s and %s differ at offset %zu", filename1.c_str(), filename2.c_str(), i);
Vladimir Marko3f4a0bc2018-04-12 10:08:58 +0100324 size_t hexdump_size = std::min<size_t>(16u, size - i);
325 HexDump dump1(&image1[i], hexdump_size, /* show_actual_addresses */ false, /* prefix */ "");
326 HexDump dump2(&image2[i], hexdump_size, /* show_actual_addresses */ false, /* prefix */ "");
327 std::ostringstream oss;
328 oss << "\n" << dump1 << "\n" << dump2;
329 *error_msg += oss.str();
Chris Morin754b7572018-01-19 18:04:46 -0800330 return true;
Alex Klyubind1d5c952017-12-15 12:57:33 -0800331 }
332 }
333
Chris Morin754b7572018-01-19 18:04:46 -0800334 return false;
Alex Klyubind1d5c952017-12-15 12:57:33 -0800335 }
336};
337
338TEST_F(PatchoatTest, PatchoatRelocationSameAsDex2oatRelocation) {
339#if defined(ART_USE_READ_BARRIER)
340 // This test checks that relocating a boot image using patchoat produces the same result as
341 // producing the boot image for that relocated base address using dex2oat. To be precise, these
342 // two files will have two small differences: the OAT checksum and base address. However, this
343 // test takes this into account.
344
345 // Compile boot image into a random directory using dex2oat
346 ScratchFile dex2oat_orig_scratch;
347 dex2oat_orig_scratch.Unlink();
348 std::string dex2oat_orig_dir = dex2oat_orig_scratch.GetFilename();
349 ASSERT_EQ(0, mkdir(dex2oat_orig_dir.c_str(), 0700));
350 const uint32_t orig_base_addr = 0x60000000;
351 // Force deterministic output. We want the boot images created by this dex2oat run and the run
352 // below to differ only in their base address.
353 std::vector<std::string> dex2oat_extra_args;
354 dex2oat_extra_args.push_back("--force-determinism");
355 dex2oat_extra_args.push_back("-j1"); // Might not be needed. Causes a 3-5x slowdown.
356 std::string error_msg;
357 if (!CompileBootImageToDir(dex2oat_orig_dir, dex2oat_extra_args, orig_base_addr, &error_msg)) {
358 FAIL() << "CompileBootImage1 failed: " << error_msg;
359 }
360
361 // Compile a "relocated" boot image into a random directory using dex2oat. This image is relocated
362 // in the sense that it uses a different base address.
363 ScratchFile dex2oat_reloc_scratch;
364 dex2oat_reloc_scratch.Unlink();
365 std::string dex2oat_reloc_dir = dex2oat_reloc_scratch.GetFilename();
366 ASSERT_EQ(0, mkdir(dex2oat_reloc_dir.c_str(), 0700));
367 const uint32_t reloc_base_addr = 0x70000000;
368 if (!CompileBootImageToDir(dex2oat_reloc_dir, dex2oat_extra_args, reloc_base_addr, &error_msg)) {
369 FAIL() << "CompileBootImage2 failed: " << error_msg;
370 }
371 const off_t base_addr_delta = reloc_base_addr - orig_base_addr;
372
373 // Relocate the original boot image using patchoat. The image is relocated by the same amount
374 // as the second/relocated image produced by dex2oat.
375 ScratchFile patchoat_scratch;
376 patchoat_scratch.Unlink();
377 std::string patchoat_dir = patchoat_scratch.GetFilename();
378 ASSERT_EQ(0, mkdir(patchoat_dir.c_str(), 0700));
379 std::string dex2oat_orig_with_arch_dir =
380 dex2oat_orig_dir + "/" + GetInstructionSetString(kRuntimeISA);
381 // The arch-including symlink is needed by patchoat
382 ASSERT_EQ(0, symlink(dex2oat_orig_dir.c_str(), dex2oat_orig_with_arch_dir.c_str()));
383 if (!RelocateBootImage(
384 dex2oat_orig_dir + "/boot.art",
Chris Morin88c6d262018-02-13 15:26:21 -0800385 patchoat_dir,
Alex Klyubind1d5c952017-12-15 12:57:33 -0800386 base_addr_delta,
387 &error_msg)) {
388 FAIL() << "RelocateBootImage failed: " << error_msg;
389 }
390
Alex Klyubina46e50b2017-12-19 12:16:11 -0800391 // Assert that patchoat created the same set of .art files as dex2oat
392 std::vector<std::string> dex2oat_image_basenames;
393 std::vector<std::string> patchoat_image_basenames;
394 if (!ListDirFilesEndingWith(dex2oat_reloc_dir, ".art", &dex2oat_image_basenames, &error_msg)) {
395 FAIL() << "Failed to list *.art files in " << dex2oat_reloc_dir << ": " << error_msg;
396 }
397 if (!ListDirFilesEndingWith(patchoat_dir, ".art", &patchoat_image_basenames, &error_msg)) {
398 FAIL() << "Failed to list *.art files in " << patchoat_dir << ": " << error_msg;
399 }
400 std::sort(dex2oat_image_basenames.begin(), dex2oat_image_basenames.end());
401 std::sort(patchoat_image_basenames.begin(), patchoat_image_basenames.end());
402 // .art file names output by patchoat look like tmp@art-data-<random>-<random>@boot*.art. To
403 // compare these with .art file names output by dex2oat we retain only the part of the file name
404 // after the last @.
405 std::vector<std::string> patchoat_image_shortened_basenames(patchoat_image_basenames.size());
406 for (size_t i = 0; i < patchoat_image_basenames.size(); i++) {
407 patchoat_image_shortened_basenames[i] =
408 patchoat_image_basenames[i].substr(patchoat_image_basenames[i].find_last_of("@") + 1);
409 }
410 ASSERT_EQ(dex2oat_image_basenames, patchoat_image_shortened_basenames);
Alex Klyubind1d5c952017-12-15 12:57:33 -0800411
Alex Klyubina46e50b2017-12-19 12:16:11 -0800412 // Patch up the dex2oat-relocated image files so that it looks as though they were relocated by
413 // patchoat. patchoat preserves the OAT checksum header field and sets patch delta header field.
414 for (const std::string& image_basename : dex2oat_image_basenames) {
415 if (!CopyImageChecksumAndSetPatchDelta(
416 dex2oat_orig_dir + "/" + image_basename,
417 dex2oat_reloc_dir + "/" + image_basename,
418 base_addr_delta,
419 &error_msg)) {
420 FAIL() << "Unable to patch up " << image_basename << ": " << error_msg;
421 }
Alex Klyubind1d5c952017-12-15 12:57:33 -0800422 }
423
Alex Klyubina46e50b2017-12-19 12:16:11 -0800424 // Assert that the patchoat-relocated images are identical to the dex2oat-relocated images
425 for (size_t i = 0; i < dex2oat_image_basenames.size(); i++) {
426 const std::string& dex2oat_image_basename = dex2oat_image_basenames[i];
427 const std::string& dex2oat_image_filename = dex2oat_reloc_dir + "/" + dex2oat_image_basename;
428 const std::string& patchoat_image_filename = patchoat_dir + "/" + patchoat_image_basenames[i];
429 if (BinaryDiff(dex2oat_image_filename, patchoat_image_filename, &error_msg)) {
430 FAIL() << "patchoat- and dex2oat-relocated variants of " << dex2oat_image_basename
431 << " differ: " << error_msg;
432 }
Alex Klyubind1d5c952017-12-15 12:57:33 -0800433 }
434
435 ClearDirectory(dex2oat_orig_dir.c_str(), /*recursive*/ true);
436 ClearDirectory(dex2oat_reloc_dir.c_str(), /*recursive*/ true);
437 ClearDirectory(patchoat_dir.c_str(), /*recursive*/ true);
438 rmdir(dex2oat_orig_dir.c_str());
439 rmdir(dex2oat_reloc_dir.c_str());
440 rmdir(patchoat_dir.c_str());
441#else
442 LOG(INFO) << "Skipping PatchoatRelocationSameAsDex2oatRelocation";
443 // Force-print to std::cout so it's also outside the logcat.
444 std::cout << "Skipping PatchoatRelocationSameAsDex2oatRelocation" << std::endl;
445#endif
446}
447
David Srbeckyb16e4a32018-06-05 13:20:48 +0100448class PatchoatVerificationTest : public PatchoatTest {
449 protected:
450 virtual void SetUp() {
451 PatchoatTest::SetUp();
Alex Klyubin3856af02017-10-23 13:53:13 -0700452
David Srbeckyb16e4a32018-06-05 13:20:48 +0100453 // This test checks that a boot image relocated using patchoat can be unrelocated using the .rel
454 // file created by patchoat.
Alex Klyubin3856af02017-10-23 13:53:13 -0700455
David Srbeckyb16e4a32018-06-05 13:20:48 +0100456 // This test doesn't work when heap poisoning is enabled because some of the
457 // references are negated. b/72117833 is tracking the effort to have patchoat
458 // and its tests support heap poisoning.
459 TEST_DISABLED_FOR_HEAP_POISONING();
460
461 // Compile boot image into a random directory using dex2oat
462 ScratchFile dex2oat_orig_scratch;
463 dex2oat_orig_scratch.Unlink();
464 dex2oat_orig_dir_ = dex2oat_orig_scratch.GetFilename();
465 ASSERT_EQ(0, mkdir(dex2oat_orig_dir_.c_str(), 0700));
466 const uint32_t orig_base_addr = 0x60000000;
467 std::vector<std::string> dex2oat_extra_args;
468 std::string error_msg;
469 if (!CompileBootImageToDir(dex2oat_orig_dir_, dex2oat_extra_args, orig_base_addr, &error_msg)) {
470 FAIL() << "CompileBootImage1 failed: " << error_msg;
471 }
472
473 // Generate image relocation file for the original boot image
474 std::string dex2oat_orig_with_arch_dir =
475 dex2oat_orig_dir_ + "/" + GetInstructionSetString(kRuntimeISA);
476 // The arch-including symlink is needed by patchoat
477 ASSERT_EQ(0, symlink(dex2oat_orig_dir_.c_str(), dex2oat_orig_with_arch_dir.c_str()));
478 base_addr_delta_ = 0x100000;
479 if (!GenerateBootImageRelFile(
480 dex2oat_orig_dir_ + "/boot.art",
481 dex2oat_orig_dir_,
482 base_addr_delta_,
483 &error_msg)) {
484 FAIL() << "RelocateBootImage failed: " << error_msg;
485 }
486
487 // Relocate the original boot image using patchoat
488 ScratchFile relocated_scratch;
489 relocated_scratch.Unlink();
490 relocated_dir_ = relocated_scratch.GetFilename();
491 ASSERT_EQ(0, mkdir(relocated_dir_.c_str(), 0700));
492 // Use a different relocation delta from the one used when generating .rel files above. This is
493 // to make sure .rel files are not specific to a particular relocation delta.
494 base_addr_delta_ -= 0x10000;
495 if (!RelocateBootImage(
496 dex2oat_orig_dir_ + "/boot.art",
497 relocated_dir_,
498 base_addr_delta_,
499 &error_msg)) {
500 FAIL() << "RelocateBootImage failed: " << error_msg;
501 }
502
503 // Assert that patchoat created the same set of .art and .art.rel files
504 std::vector<std::string> rel_basenames;
505 std::vector<std::string> relocated_image_basenames;
506 if (!ListDirFilesEndingWith(dex2oat_orig_dir_, ".rel", &rel_basenames, &error_msg)) {
507 FAIL() << "Failed to list *.art.rel files in " << dex2oat_orig_dir_ << ": " << error_msg;
508 }
509 if (!ListDirFilesEndingWith(relocated_dir_, ".art", &relocated_image_basenames, &error_msg)) {
510 FAIL() << "Failed to list *.art files in " << relocated_dir_ << ": " << error_msg;
511 }
512 std::sort(rel_basenames.begin(), rel_basenames.end());
513 std::sort(relocated_image_basenames.begin(), relocated_image_basenames.end());
514
515 // .art and .art.rel file names output by patchoat look like
516 // tmp@art-data-<random>-<random>@boot*.art, encoding the name of the directory in their name.
517 // To compare these with each other, we retain only the part of the file name after the last @,
518 // and we also drop the extension.
519 std::vector<std::string> rel_shortened_basenames(rel_basenames.size());
520 std::vector<std::string> relocated_image_shortened_basenames(relocated_image_basenames.size());
521 for (size_t i = 0; i < rel_basenames.size(); i++) {
522 rel_shortened_basenames[i] = rel_basenames[i].substr(rel_basenames[i].find_last_of("@") + 1);
523 rel_shortened_basenames[i] =
524 rel_shortened_basenames[i].substr(0, rel_shortened_basenames[i].find("."));
525 }
526 for (size_t i = 0; i < relocated_image_basenames.size(); i++) {
527 relocated_image_shortened_basenames[i] =
528 relocated_image_basenames[i].substr(relocated_image_basenames[i].find_last_of("@") + 1);
529 relocated_image_shortened_basenames[i] =
530 relocated_image_shortened_basenames[i].substr(
531 0, relocated_image_shortened_basenames[i].find("."));
532 }
533 ASSERT_EQ(rel_shortened_basenames, relocated_image_shortened_basenames);
534 }
535
536 virtual void TearDown() {
537 ClearDirectory(dex2oat_orig_dir_.c_str(), /*recursive*/ true);
538 ClearDirectory(relocated_dir_.c_str(), /*recursive*/ true);
539
540 rmdir(dex2oat_orig_dir_.c_str());
541 rmdir(relocated_dir_.c_str());
542
543 PatchoatTest::TearDown();
544 }
545
546 std::string dex2oat_orig_dir_;
547 std::string relocated_dir_;
548 off_t base_addr_delta_;
549};
550
551// Assert that verification works with the .rel files.
552TEST_F(PatchoatVerificationTest, Sucessful) {
Alex Klyubin3856af02017-10-23 13:53:13 -0700553 std::string error_msg;
Chris Morin754b7572018-01-19 18:04:46 -0800554 if (!VerifyBootImage(
David Srbeckyb16e4a32018-06-05 13:20:48 +0100555 dex2oat_orig_dir_ + "/boot.art",
556 relocated_dir_,
557 base_addr_delta_,
Chris Morin754b7572018-01-19 18:04:46 -0800558 &error_msg)) {
559 FAIL() << "VerifyBootImage failed: " << error_msg;
Alex Klyubin3856af02017-10-23 13:53:13 -0700560 }
David Srbeckyb16e4a32018-06-05 13:20:48 +0100561}
Alex Klyubin3856af02017-10-23 13:53:13 -0700562
David Srbeckyb16e4a32018-06-05 13:20:48 +0100563// Corrupt the image file and check that the verification fails gracefully.
564TEST_F(PatchoatVerificationTest, CorruptedImage) {
565 std::string error_msg;
566 std::string relocated_image_filename;
567 if (!GetDalvikCacheFilename((dex2oat_orig_dir_ + "/boot.art").c_str(),
568 relocated_dir_.c_str(),
569 &relocated_image_filename,
570 &error_msg)) {
571 FAIL() << "Failed to find relocated image file name: " << error_msg;
572 }
573 ASSERT_EQ(truncate(relocated_image_filename.c_str(), sizeof(ImageHeader)), 0)
574 << relocated_image_filename;
Alex Klyubin3856af02017-10-23 13:53:13 -0700575
David Srbeckyb16e4a32018-06-05 13:20:48 +0100576 if (VerifyBootImage(
577 dex2oat_orig_dir_ + "/boot.art",
578 relocated_dir_,
579 base_addr_delta_,
580 &error_msg)) {
581 FAIL() << "VerifyBootImage should have failed since the image was intentionally corrupted";
582 }
583}
584
585// Corrupt the relocation file and check that the verification fails gracefully.
586TEST_F(PatchoatVerificationTest, CorruptedRelFile) {
587 std::string error_msg;
588 std::string art_filename = dex2oat_orig_dir_ + "/boot.art";
589 std::string rel_filename = dex2oat_orig_dir_ + "/boot.art.rel";
590 std::unique_ptr<File> art_file(OS::OpenFileForReading(art_filename.c_str()));
591 std::unique_ptr<File> rel_file(OS::OpenFileReadWrite(rel_filename.c_str()));
592 rel_file->ClearContent();
593 uint8_t buffer[64] = {};
594 ASSERT_TRUE(rel_file->WriteFully(&buffer, SHA256_DIGEST_LENGTH));
595 // Encode single relocation which is just past the end of the image file.
596 size_t leb_size = EncodeUnsignedLeb128(buffer, art_file->GetLength()) - buffer;
597 ASSERT_TRUE(rel_file->WriteFully(&buffer, leb_size));
598 ASSERT_EQ(rel_file->FlushClose(), 0);
599 ASSERT_EQ(art_file->Close(), 0);
600
601 if (VerifyBootImage(
602 dex2oat_orig_dir_ + "/boot.art",
603 relocated_dir_,
604 base_addr_delta_,
605 &error_msg)) {
606 FAIL() << "VerifyBootImage should have failed since the rel file was intentionally corrupted";
607 }
Alex Klyubin3856af02017-10-23 13:53:13 -0700608}
609
Alex Klyubind1d5c952017-12-15 12:57:33 -0800610} // namespace art