xref: /aosp_15_r20/external/zucchini/reloc_win32_unittest.cc (revision a03ca8b91e029cd15055c20c78c2e087c84792e4)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/zucchini/reloc_win32.h"
6 
7 #include <stdint.h>
8 
9 #include <algorithm>
10 #include <memory>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
15 #include "base/numerics/safe_conversions.h"
16 #include "base/test/gtest_util.h"
17 #include "components/zucchini/address_translator.h"
18 #include "components/zucchini/algorithm.h"
19 #include "components/zucchini/image_utils.h"
20 #include "components/zucchini/test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace zucchini {
24 
25 class RelocUtilsWin32Test : public testing::Test {
26  protected:
27   using Units = std::vector<RelocUnitWin32>;
28 
RelocUtilsWin32Test()29   RelocUtilsWin32Test() {}
30 
31   // Resets all tester data, calls RelocRvaReaderWin32::FindRelocBlocks(), and
32   // returns its results.
Initialize(const std::vector<uint8_t> & image_raw,BufferRegion reloc_region)33   bool Initialize(const std::vector<uint8_t>& image_raw,
34                   BufferRegion reloc_region) {
35     image_ = BufferSource(image_raw.data(), image_raw.size());
36     reloc_region_ = reloc_region;
37     return RelocRvaReaderWin32::FindRelocBlocks(image_, reloc_region_,
38                                                 &reloc_block_offsets_);
39   }
40 
41   // Uses RelocRvaReaderWin32 to get all relocs, returned as Units.
EmitAll(offset_t lo,offset_t hi)42   Units EmitAll(offset_t lo, offset_t hi) {
43     RelocRvaReaderWin32 reader(image_, reloc_region_, reloc_block_offsets_, lo,
44                                hi);
45     Units units;
46     for (auto unit = reader.GetNext(); unit.has_value();
47          unit = reader.GetNext()) {
48       units.push_back(unit.value());
49     }
50     return units;
51   }
52 
53   ConstBufferView image_;
54   BufferRegion reloc_region_;
55   std::vector<uint32_t> reloc_block_offsets_;
56 };
57 
TEST_F(RelocUtilsWin32Test,RvaReaderEmpty)58 TEST_F(RelocUtilsWin32Test, RvaReaderEmpty) {
59   {
60     std::vector<uint8_t> image_raw = ParseHexString("");
61     EXPECT_TRUE(Initialize(image_raw, {0U, 0U}));
62     EXPECT_EQ(std::vector<uint32_t>(), reloc_block_offsets_);  // Nothing.
63     EXPECT_EQ(Units(), EmitAll(0U, 0U));
64   }
65   {
66     std::vector<uint8_t> image_raw = ParseHexString("AA BB CC DD EE FF");
67     EXPECT_TRUE(Initialize(image_raw, {2U, 0U}));
68     EXPECT_EQ(std::vector<uint32_t>(), reloc_block_offsets_);  // Nothing.
69     EXPECT_EQ(Units(), EmitAll(2U, 2U));
70   }
71   {
72     std::vector<uint8_t> image_raw = ParseHexString("00 C0 00 00 08 00 00 00");
73     EXPECT_TRUE(Initialize(image_raw, {0U, image_raw.size()}));
74     EXPECT_EQ(std::vector<uint32_t>({0U}),
75               reloc_block_offsets_);  // Empty block.
76     EXPECT_EQ(Units(), EmitAll(0U, 8U));
77   }
78 }
79 
TEST_F(RelocUtilsWin32Test,RvaReaderBad)80 TEST_F(RelocUtilsWin32Test, RvaReaderBad) {
81   std::string test_cases[] = {
82       "00 C0 00 00 07 00 00",           // Header too small.
83       "00 C0 00 00 08 00 00",           // Header too small, lies about size.
84       "00 C0 00 00 0A 00 00 00 66 31",  // Odd number of units.
85       "00 C0 00 00 0C 00 00 00 66 31 88 31 FF",  // Trailing data.
86   };
87   for (const std::string& test_case : test_cases) {
88     std::vector<uint8_t> image_raw = ParseHexString(test_case);
89     EXPECT_FALSE(Initialize(image_raw, {0U, image_raw.size()}));
90   }
91 }
92 
TEST_F(RelocUtilsWin32Test,RvaReaderSingle)93 TEST_F(RelocUtilsWin32Test, RvaReaderSingle) {
94   // Block 0: All type 0x3: {0xC166, 0xC288, 0xC342, (padding) 0xCFFF}.
95   std::vector<uint8_t> image_raw = ParseHexString(
96       "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "
97       "00 C0 00 00 10 00 00 00 66 31 88 32 42 33 FF 0F "
98       "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF");
99   constexpr offset_t kBlock0 = 16U;
100   Units exp0 = {{3, kBlock0 + 8U, 0xC166U},
101                 {3, kBlock0 + 10U, 0xC288U},
102                 {3, kBlock0 + 12U, 0xC342U},
103                 {0, kBlock0 + 14U, 0xCFFFU}};
104 
105   EXPECT_TRUE(Initialize(image_raw, {16U, 16U}));
106   EXPECT_EQ(exp0, EmitAll(kBlock0, kBlock0 + 16U));
107   EXPECT_EQ(Units(), EmitAll(kBlock0, kBlock0));
108   EXPECT_EQ(Units(), EmitAll(kBlock0, kBlock0 + 8U));
109   EXPECT_EQ(Units(), EmitAll(kBlock0, kBlock0 + 9U));
110   EXPECT_EQ(Sub(exp0, 0, 1), EmitAll(kBlock0, kBlock0 + 10U));
111   EXPECT_EQ(Sub(exp0, 0, 1), EmitAll(kBlock0 + 8U, kBlock0 + 10U));
112   EXPECT_EQ(Units(), EmitAll(kBlock0 + 9U, kBlock0 + 10U));
113   EXPECT_EQ(Sub(exp0, 0, 3), EmitAll(kBlock0, kBlock0 + 15U));
114   EXPECT_EQ(Sub(exp0, 2, 3), EmitAll(kBlock0 + 11U, kBlock0 + 15U));
115 }
116 
TEST_F(RelocUtilsWin32Test,RvaReaderMulti)117 TEST_F(RelocUtilsWin32Test, RvaReaderMulti) {
118   // The sample image encodes 3 reloc blocks:
119   // Block 0: All type 0x3: {0xC166, 0xC288, 0xC344, (padding) 0xCFFF}.
120   // Block 1: All type 0x3: {0x12166, 0x12288}.
121   // Block 2: All type 0xA: {0x24000, 0x24010, 0x24020, 0x24028, 0x24A3C,
122   //                         0x24170}.
123   std::vector<uint8_t> image_raw = ParseHexString(
124       "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF "
125       "00 C0 00 00 10 00 00 00 66 31 88 32 42 33 FF 0F "
126       "00 20 01 00 0C 00 00 00 66 31 88 32 "
127       "00 40 02 00 14 00 00 00 00 A0 10 A0 20 A0 28 A0 3C A0 70 A1 "
128       "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF");
129   offset_t image_size = base::checked_cast<offset_t>(image_raw.size());
130   constexpr offset_t kBlock0 = 16U;
131   constexpr offset_t kBlock1 = kBlock0 + 16U;
132   constexpr offset_t kBlock2 = kBlock1 + 12U;
133   constexpr offset_t kBlockEnd = kBlock2 + 20U;
134   Units exp0 = {{3, kBlock0 + 8U, 0xC166U},
135                 {3, kBlock0 + 10U, 0xC288U},
136                 {3, kBlock0 + 12U, 0xC342U},
137                 {0, kBlock0 + 14U, 0xCFFFU}};
138   Units exp1 = {{3, kBlock0 + 24U, 0x12166U}, {3, kBlock0 + 26U, 0x12288U}};
139   Units exp2 = {{10, kBlock0 + 36U, 0x24000U}, {10, kBlock0 + 38U, 0x24010U},
140                 {10, kBlock0 + 40U, 0x24020U}, {10, kBlock0 + 42U, 0x24028U},
141                 {10, kBlock0 + 44U, 0x2403CU}, {10, kBlock0 + 46U, 0x24170U}};
142 
143   EXPECT_TRUE(Initialize(image_raw, {kBlock0, kBlockEnd - kBlock0}));
144   EXPECT_EQ(std::vector<uint32_t>({kBlock0, kBlock1, kBlock2}),
145             reloc_block_offsets_);
146 
147   // Everything.
148   EXPECT_EQ(Cat(Cat(exp0, exp1), exp2), EmitAll(kBlock0, kBlockEnd));
149   EXPECT_EQ(Cat(Cat(exp0, exp1), exp2), EmitAll(0, image_size));
150   // Entire blocks.
151   EXPECT_EQ(exp0, EmitAll(kBlock0, kBlock1));
152   EXPECT_EQ(exp1, EmitAll(kBlock1, kBlock2));
153   EXPECT_EQ(exp2, EmitAll(kBlock2, kBlockEnd));
154   EXPECT_EQ(Units(), EmitAll(0, kBlock0));
155   EXPECT_EQ(Units(), EmitAll(kBlockEnd, image_size));
156   // Within blocks, clipped at boundaries.
157   EXPECT_EQ(exp0, EmitAll(kBlock0 + 5U, kBlock1));
158   EXPECT_EQ(exp0, EmitAll(kBlock0 + 8U, kBlock1));
159   EXPECT_EQ(Sub(exp0, 1, 4), EmitAll(kBlock0 + 9U, kBlock1));
160   EXPECT_EQ(Sub(exp0, 0, 3), EmitAll(kBlock0, kBlock0 + 15U));
161   EXPECT_EQ(Sub(exp0, 0, 3), EmitAll(kBlock0, kBlock0 + 14U));
162   EXPECT_EQ(Sub(exp0, 0, 1), EmitAll(kBlock0 + 8U, kBlock0 + 10U));
163   EXPECT_EQ(Sub(exp1, 1, 2), EmitAll(kBlock1 + 10U, kBlock1 + 12U));
164   EXPECT_EQ(Sub(exp2, 2, 4), EmitAll(kBlock2 + 12U, kBlock2 + 16U));
165   EXPECT_EQ(Units(), EmitAll(kBlock0, kBlock0));
166   EXPECT_EQ(Units(), EmitAll(kBlock0, kBlock0 + 8U));
167   EXPECT_EQ(Units(), EmitAll(kBlock2 + 10U, kBlock2 + 11U));
168   EXPECT_EQ(Units(), EmitAll(kBlock2 + 11U, kBlock2 + 12U));
169   // Across blocks.
170   EXPECT_EQ(Cat(Cat(exp0, exp1), exp2), EmitAll(kBlock0 - 5U, kBlockEnd));
171   EXPECT_EQ(Cat(Cat(exp0, exp1), exp2), EmitAll(kBlock0 + 6U, kBlockEnd));
172   EXPECT_EQ(Cat(Cat(exp0, exp1), Sub(exp2, 0, 5)),
173             EmitAll(kBlock0 + 6U, kBlock2 + 18U));
174   EXPECT_EQ(Cat(Sub(exp0, 2, 4), Sub(exp1, 0, 1)),
175             EmitAll(kBlock0 + 12U, kBlock1 + 10U));
176   EXPECT_EQ(Cat(Sub(exp0, 2, 4), Sub(exp1, 0, 1)),
177             EmitAll(kBlock0 + 11U, kBlock1 + 10U));
178   EXPECT_EQ(Cat(Sub(exp0, 2, 4), Sub(exp1, 0, 1)),
179             EmitAll(kBlock0 + 12U, kBlock1 + 11U));
180   EXPECT_EQ(Sub(exp1, 1, 2), EmitAll(kBlock1 + 10U, kBlock2 + 5U));
181   EXPECT_EQ(Cat(Sub(exp1, 1, 2), exp2), EmitAll(kBlock1 + 10U, kBlockEnd + 5));
182   EXPECT_EQ(Units(), EmitAll(kBlock0 + 15, kBlock1 + 9));
183 }
184 
TEST_F(RelocUtilsWin32Test,ReadWrite)185 TEST_F(RelocUtilsWin32Test, ReadWrite) {
186   // Set up mock image: Size = 0x3000, .reloc at 0x600. RVA is 0x40000 + offset.
187   constexpr rva_t kBaseRva = 0x40000;
188   std::vector<uint8_t> image_data(0x3000, 0xFF);
189   // 4 x86 relocs (xx 3x), 3 x64 relocs (xx Ax), 1 padding (xx 0X).
190   std::vector<uint8_t> reloc_data = ParseHexString(
191       "00 10 04 00 10 00 00 00 C0 32 18 A3 F8 A7 FF 0F "
192       "00 20 04 00 10 00 00 00 80 A0 65 31 F8 37 BC 3A");
193   reloc_region_ = {0x600, reloc_data.size()};
194   std::copy(reloc_data.begin(), reloc_data.end(),
195             image_data.begin() + reloc_region_.lo());
196   image_ = {image_data.data(), image_data.size()};
197   offset_t image_size = base::checked_cast<offset_t>(image_.size());
198 
199   AddressTranslator translator;
200   translator.Initialize({{0, image_size, kBaseRva, image_size}});
201 
202   // Precompute |reloc_block_offsets_|.
203   EXPECT_TRUE(RelocRvaReaderWin32::FindRelocBlocks(image_, reloc_region_,
204                                                    &reloc_block_offsets_));
205   EXPECT_EQ(std::vector<uint32_t>({0x600U, 0x610U}), reloc_block_offsets_);
206 
207   // Focus on x86.
208   constexpr uint16_t kRelocTypeX86 = 3;
209   constexpr offset_t kVAWidthX86 = 4;
210 
211   // Make RelocRvaReaderWin32.
212   RelocRvaReaderWin32 reloc_rva_reader(image_, reloc_region_,
213                                        reloc_block_offsets_, 0, image_size);
214   offset_t offset_bound = image_size - kVAWidthX86 + 1;
215 
216   // Make RelocReaderWin32 that wraps |reloc_rva_reader|.
217   auto reader = std::make_unique<RelocReaderWin32>(
218       std::move(reloc_rva_reader), kRelocTypeX86, offset_bound, translator);
219 
220   // Read all references and check.
221   std::vector<Reference> refs;
222   for (std::optional<Reference> ref = reader->GetNext(); ref.has_value();
223        ref = reader->GetNext()) {
224     refs.push_back(ref.value());
225   }
226   std::vector<Reference> exp_refs{
227       {0x608, 0x12C0}, {0x61A, 0x2165}, {0x61C, 0x27F8}, {0x61E, 0x2ABC}};
228   EXPECT_EQ(exp_refs, refs);
229 
230   // Write reference, extract bytes and check.
231   MutableBufferView mutable_image(&image_data[0], image_data.size());
232   auto writer = std::make_unique<RelocWriterWin32>(
233       kRelocTypeX86, mutable_image, reloc_region_, reloc_block_offsets_,
234       translator);
235 
236   writer->PutNext({0x608, 0x1F83});
237   std::vector<uint8_t> exp_reloc_data1 = ParseHexString(
238       "00 10 04 00 10 00 00 00 83 3F 18 A3 F8 A7 FF 0F "
239       "00 20 04 00 10 00 00 00 80 A0 65 31 F8 37 BC 3A");
240   EXPECT_EQ(exp_reloc_data1,
241             Sub(image_data, reloc_region_.lo(), reloc_region_.hi()));
242 
243   writer->PutNext({0x61C, 0x2950});
244   std::vector<uint8_t> exp_reloc_data2 = ParseHexString(
245       "00 10 04 00 10 00 00 00 83 3F 18 A3 F8 A7 FF 0F "
246       "00 20 04 00 10 00 00 00 80 A0 65 31 50 39 BC 3A");
247   EXPECT_EQ(exp_reloc_data2,
248             Sub(image_data, reloc_region_.lo(), reloc_region_.hi()));
249 }
250 
251 }  // namespace zucchini
252