blob: 08bf31c4bd3e9207c1e6ab79db207cbca583fe79 [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 Srbecky23ae5322018-06-06 16:02:21 +0100448// These tests check that a boot image relocated using patchoat can be unrelocated
449// using the .rel file created by patchoat.
450//
451// The tests don't work when heap poisoning is enabled because some of the
452// references are negated. b/72117833 is tracking the effort to have patchoat
453// and its tests support heap poisoning.
David Srbeckyb16e4a32018-06-05 13:20:48 +0100454class PatchoatVerificationTest : public PatchoatTest {
455 protected:
David Srbecky23ae5322018-06-06 16:02:21 +0100456 void CreateRelocatedBootImage() {
David Srbeckyb16e4a32018-06-05 13:20:48 +0100457 // Compile boot image into a random directory using dex2oat
458 ScratchFile dex2oat_orig_scratch;
459 dex2oat_orig_scratch.Unlink();
460 dex2oat_orig_dir_ = dex2oat_orig_scratch.GetFilename();
461 ASSERT_EQ(0, mkdir(dex2oat_orig_dir_.c_str(), 0700));
462 const uint32_t orig_base_addr = 0x60000000;
463 std::vector<std::string> dex2oat_extra_args;
464 std::string error_msg;
465 if (!CompileBootImageToDir(dex2oat_orig_dir_, dex2oat_extra_args, orig_base_addr, &error_msg)) {
466 FAIL() << "CompileBootImage1 failed: " << error_msg;
467 }
468
469 // Generate image relocation file for the original boot image
470 std::string dex2oat_orig_with_arch_dir =
471 dex2oat_orig_dir_ + "/" + GetInstructionSetString(kRuntimeISA);
472 // The arch-including symlink is needed by patchoat
473 ASSERT_EQ(0, symlink(dex2oat_orig_dir_.c_str(), dex2oat_orig_with_arch_dir.c_str()));
474 base_addr_delta_ = 0x100000;
475 if (!GenerateBootImageRelFile(
476 dex2oat_orig_dir_ + "/boot.art",
477 dex2oat_orig_dir_,
478 base_addr_delta_,
479 &error_msg)) {
480 FAIL() << "RelocateBootImage failed: " << error_msg;
481 }
482
483 // Relocate the original boot image using patchoat
484 ScratchFile relocated_scratch;
485 relocated_scratch.Unlink();
486 relocated_dir_ = relocated_scratch.GetFilename();
487 ASSERT_EQ(0, mkdir(relocated_dir_.c_str(), 0700));
488 // Use a different relocation delta from the one used when generating .rel files above. This is
489 // to make sure .rel files are not specific to a particular relocation delta.
490 base_addr_delta_ -= 0x10000;
491 if (!RelocateBootImage(
492 dex2oat_orig_dir_ + "/boot.art",
493 relocated_dir_,
494 base_addr_delta_,
495 &error_msg)) {
496 FAIL() << "RelocateBootImage failed: " << error_msg;
497 }
498
499 // Assert that patchoat created the same set of .art and .art.rel files
500 std::vector<std::string> rel_basenames;
501 std::vector<std::string> relocated_image_basenames;
502 if (!ListDirFilesEndingWith(dex2oat_orig_dir_, ".rel", &rel_basenames, &error_msg)) {
503 FAIL() << "Failed to list *.art.rel files in " << dex2oat_orig_dir_ << ": " << error_msg;
504 }
505 if (!ListDirFilesEndingWith(relocated_dir_, ".art", &relocated_image_basenames, &error_msg)) {
506 FAIL() << "Failed to list *.art files in " << relocated_dir_ << ": " << error_msg;
507 }
508 std::sort(rel_basenames.begin(), rel_basenames.end());
509 std::sort(relocated_image_basenames.begin(), relocated_image_basenames.end());
510
511 // .art and .art.rel file names output by patchoat look like
512 // tmp@art-data-<random>-<random>@boot*.art, encoding the name of the directory in their name.
513 // To compare these with each other, we retain only the part of the file name after the last @,
514 // and we also drop the extension.
515 std::vector<std::string> rel_shortened_basenames(rel_basenames.size());
516 std::vector<std::string> relocated_image_shortened_basenames(relocated_image_basenames.size());
517 for (size_t i = 0; i < rel_basenames.size(); i++) {
518 rel_shortened_basenames[i] = rel_basenames[i].substr(rel_basenames[i].find_last_of("@") + 1);
519 rel_shortened_basenames[i] =
520 rel_shortened_basenames[i].substr(0, rel_shortened_basenames[i].find("."));
521 }
522 for (size_t i = 0; i < relocated_image_basenames.size(); i++) {
523 relocated_image_shortened_basenames[i] =
524 relocated_image_basenames[i].substr(relocated_image_basenames[i].find_last_of("@") + 1);
525 relocated_image_shortened_basenames[i] =
526 relocated_image_shortened_basenames[i].substr(
527 0, relocated_image_shortened_basenames[i].find("."));
528 }
529 ASSERT_EQ(rel_shortened_basenames, relocated_image_shortened_basenames);
530 }
531
532 virtual void TearDown() {
David Srbecky23ae5322018-06-06 16:02:21 +0100533 if (!dex2oat_orig_dir_.empty()) {
534 ClearDirectory(dex2oat_orig_dir_.c_str(), /*recursive*/ true);
535 rmdir(dex2oat_orig_dir_.c_str());
536 }
537 if (!relocated_dir_.empty()) {
538 ClearDirectory(relocated_dir_.c_str(), /*recursive*/ true);
539 rmdir(relocated_dir_.c_str());
540 }
David Srbeckyb16e4a32018-06-05 13:20:48 +0100541 PatchoatTest::TearDown();
542 }
543
544 std::string dex2oat_orig_dir_;
545 std::string relocated_dir_;
546 off_t base_addr_delta_;
547};
548
549// Assert that verification works with the .rel files.
550TEST_F(PatchoatVerificationTest, Sucessful) {
David Srbecky23ae5322018-06-06 16:02:21 +0100551 TEST_DISABLED_FOR_HEAP_POISONING();
552 CreateRelocatedBootImage();
553
Alex Klyubin3856af02017-10-23 13:53:13 -0700554 std::string error_msg;
Chris Morin754b7572018-01-19 18:04:46 -0800555 if (!VerifyBootImage(
David Srbeckyb16e4a32018-06-05 13:20:48 +0100556 dex2oat_orig_dir_ + "/boot.art",
557 relocated_dir_,
558 base_addr_delta_,
Chris Morin754b7572018-01-19 18:04:46 -0800559 &error_msg)) {
560 FAIL() << "VerifyBootImage failed: " << error_msg;
Alex Klyubin3856af02017-10-23 13:53:13 -0700561 }
David Srbeckyb16e4a32018-06-05 13:20:48 +0100562}
Alex Klyubin3856af02017-10-23 13:53:13 -0700563
David Srbeckyb16e4a32018-06-05 13:20:48 +0100564// Corrupt the image file and check that the verification fails gracefully.
565TEST_F(PatchoatVerificationTest, CorruptedImage) {
David Srbecky23ae5322018-06-06 16:02:21 +0100566 TEST_DISABLED_FOR_HEAP_POISONING();
567 CreateRelocatedBootImage();
568
David Srbeckyb16e4a32018-06-05 13:20:48 +0100569 std::string error_msg;
570 std::string relocated_image_filename;
571 if (!GetDalvikCacheFilename((dex2oat_orig_dir_ + "/boot.art").c_str(),
572 relocated_dir_.c_str(),
573 &relocated_image_filename,
574 &error_msg)) {
575 FAIL() << "Failed to find relocated image file name: " << error_msg;
576 }
577 ASSERT_EQ(truncate(relocated_image_filename.c_str(), sizeof(ImageHeader)), 0)
578 << relocated_image_filename;
Alex Klyubin3856af02017-10-23 13:53:13 -0700579
David Srbeckyb16e4a32018-06-05 13:20:48 +0100580 if (VerifyBootImage(
581 dex2oat_orig_dir_ + "/boot.art",
582 relocated_dir_,
583 base_addr_delta_,
584 &error_msg)) {
585 FAIL() << "VerifyBootImage should have failed since the image was intentionally corrupted";
586 }
587}
588
589// Corrupt the relocation file and check that the verification fails gracefully.
590TEST_F(PatchoatVerificationTest, CorruptedRelFile) {
David Srbecky23ae5322018-06-06 16:02:21 +0100591 TEST_DISABLED_FOR_HEAP_POISONING();
592 CreateRelocatedBootImage();
593
David Srbeckyb16e4a32018-06-05 13:20:48 +0100594 std::string error_msg;
595 std::string art_filename = dex2oat_orig_dir_ + "/boot.art";
596 std::string rel_filename = dex2oat_orig_dir_ + "/boot.art.rel";
597 std::unique_ptr<File> art_file(OS::OpenFileForReading(art_filename.c_str()));
598 std::unique_ptr<File> rel_file(OS::OpenFileReadWrite(rel_filename.c_str()));
599 rel_file->ClearContent();
600 uint8_t buffer[64] = {};
601 ASSERT_TRUE(rel_file->WriteFully(&buffer, SHA256_DIGEST_LENGTH));
602 // Encode single relocation which is just past the end of the image file.
603 size_t leb_size = EncodeUnsignedLeb128(buffer, art_file->GetLength()) - buffer;
604 ASSERT_TRUE(rel_file->WriteFully(&buffer, leb_size));
605 ASSERT_EQ(rel_file->FlushClose(), 0);
606 ASSERT_EQ(art_file->Close(), 0);
607
608 if (VerifyBootImage(
609 dex2oat_orig_dir_ + "/boot.art",
610 relocated_dir_,
611 base_addr_delta_,
612 &error_msg)) {
613 FAIL() << "VerifyBootImage should have failed since the rel file was intentionally corrupted";
614 }
Alex Klyubin3856af02017-10-23 13:53:13 -0700615}
616
Alex Klyubind1d5c952017-12-15 12:57:33 -0800617} // namespace art