• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // Unittests for OMAP related functions.
30 
31 #include "common/windows/omap.h"
32 
33 #include "breakpad_googletest_includes.h"
34 
35 namespace google_breakpad {
36 
37 // Equality operators for ContainerEq. These must be outside of the anonymous
38 // namespace in order for them to be found.
operator ==(const MappedRange & mr1,const MappedRange & mr2)39 bool operator==(const MappedRange& mr1, const MappedRange& mr2) {
40   return mr1.rva_original == mr2.rva_original &&
41       mr1.rva_transformed == mr2.rva_transformed &&
42       mr1.length == mr2.length &&
43       mr1.injected == mr2.injected &&
44       mr1.removed == mr2.removed;
45 }
operator ==(const EndpointIndex & ei1,const EndpointIndex & ei2)46 bool operator==(const EndpointIndex& ei1, const EndpointIndex& ei2) {
47   return ei1.endpoint == ei2.endpoint && ei1.index == ei2.index;
48 }
49 
50 // Pretty printers for more meaningful error messages. Also need to be outside
51 // the anonymous namespace.
operator <<(std::ostream & os,const MappedRange & mr)52 std::ostream& operator<<(std::ostream& os, const MappedRange& mr) {
53   os << "MappedRange(rva_original=" << mr.rva_original
54      << ", rva_transformed=" << mr.rva_transformed
55      << ", length=" << mr.length
56      << ", injected=" << mr.injected
57      << ", removed=" << mr.removed << ")";
58   return os;
59 }
operator <<(std::ostream & os,const EndpointIndex & ei)60 std::ostream& operator<<(std::ostream& os, const EndpointIndex& ei) {
61   os << "EndpointIndex(endpoint=" << ei.endpoint
62      << ", index=" << ei.index << ")";
63   return os;
64 }
operator <<(std::ostream & os,const AddressRange & ar)65 std::ostream& operator<<(std::ostream& os, const AddressRange& ar) {
66   os << "AddressRange(rva=" << ar.rva << ", length=" << ar.length << ")";
67   return os;
68 }
69 
70 namespace {
71 
CreateOmap(DWORD rva,DWORD rvaTo)72 OMAP CreateOmap(DWORD rva, DWORD rvaTo) {
73   OMAP o = { rva, rvaTo };
74   return o;
75 }
76 
CreateMappedRange(DWORD rva_original,DWORD rva_transformed,DWORD length,DWORD injected,DWORD removed)77 MappedRange CreateMappedRange(DWORD rva_original,
78                               DWORD rva_transformed,
79                               DWORD length,
80                               DWORD injected,
81                               DWORD removed) {
82   MappedRange mr = { rva_original, rva_transformed, length, injected, removed };
83   return mr;
84 }
85 
CreateEndpointIndex(DWORD endpoint,size_t index)86 EndpointIndex CreateEndpointIndex(DWORD endpoint, size_t index) {
87   EndpointIndex ei = { endpoint, index };
88   return ei;
89 }
90 
91 //              (C is removed)
92 // Original   :  A B C D E F G H
93 // Transformed:  A B D F E * H1 G1 G2 H2
94 //              (* is injected, G is copied, H is split)
95 // A is implied.
96 
97 // Layout of the original image.
98 const AddressRange B(100, 15);
99 const AddressRange C(B.end(), 10);
100 const AddressRange D(C.end(), 25);
101 const AddressRange E(D.end(), 10);
102 const AddressRange F(E.end(), 40);
103 const AddressRange G(F.end(), 3);
104 const AddressRange H(G.end(), 7);
105 
106 // Layout of the transformed image.
107 const AddressRange Bt(100, 15);
108 const AddressRange Dt(Bt.end(), 20);  // D is shortened.
109 const AddressRange Ft(Dt.end(), F.length);
110 const AddressRange Et(Ft.end(), E.length);
111 const AddressRange injected(Et.end(), 5);
112 const AddressRange H1t(injected.end(), 4);  // H is split.
113 const AddressRange G1t(H1t.end(), G.length);  // G is copied.
114 const AddressRange G2t(G1t.end(), G.length);  // G is copied.
115 const AddressRange H2t(G2t.end(), 3);  // H is split.
116 
117 class BuildImageMapTest : public testing::Test {
118  public:
119   static const DWORD kInvalidAddress = 0xFFFFFFFF;
120 
InitOmapData()121   void InitOmapData() {
122     omap_data.length_original = H.end();
123 
124     // Build the OMAPTO vector (from transformed to original).
125     omap_data.omap_to.push_back(CreateOmap(Bt.rva, B.rva));
126     omap_data.omap_to.push_back(CreateOmap(Dt.rva, D.rva));
127     omap_data.omap_to.push_back(CreateOmap(Ft.rva, F.rva));
128     omap_data.omap_to.push_back(CreateOmap(Et.rva, E.rva));
129     omap_data.omap_to.push_back(CreateOmap(injected.rva, kInvalidAddress));
130     omap_data.omap_to.push_back(CreateOmap(H1t.rva, H.rva));
131     omap_data.omap_to.push_back(CreateOmap(G1t.rva, G.rva));
132     omap_data.omap_to.push_back(CreateOmap(G2t.rva, G.rva));
133     omap_data.omap_to.push_back(CreateOmap(H2t.rva, H.rva + H1t.length));
134     omap_data.omap_to.push_back(CreateOmap(H2t.end(), kInvalidAddress));
135 
136     // Build the OMAPFROM vector (from original to transformed).
137     omap_data.omap_from.push_back(CreateOmap(B.rva, Bt.rva));
138     omap_data.omap_from.push_back(CreateOmap(C.rva, kInvalidAddress));
139     omap_data.omap_from.push_back(CreateOmap(D.rva, Dt.rva));
140     omap_data.omap_from.push_back(CreateOmap(E.rva, Et.rva));
141     omap_data.omap_from.push_back(CreateOmap(F.rva, Ft.rva));
142     omap_data.omap_from.push_back(CreateOmap(G.rva, G1t.rva));
143     omap_data.omap_from.push_back(CreateOmap(H.rva, H1t.rva));
144     omap_data.omap_from.push_back(CreateOmap(H.rva + H1t.length, H2t.rva));
145     omap_data.omap_from.push_back(CreateOmap(H.end(), kInvalidAddress));
146   }
147 
148   OmapData omap_data;
149 };
150 
151 }  // namespace
152 
TEST_F(BuildImageMapTest,EmptyImageMapOnEmptyOmapData)153 TEST_F(BuildImageMapTest, EmptyImageMapOnEmptyOmapData) {
154   ASSERT_EQ(0u, omap_data.omap_from.size());
155   ASSERT_EQ(0u, omap_data.omap_to.size());
156   ASSERT_EQ(0u, omap_data.length_original);
157 
158   ImageMap image_map;
159   BuildImageMap(omap_data, &image_map);
160   EXPECT_EQ(0u, image_map.mapping.size());
161   EXPECT_EQ(0u, image_map.endpoint_index_map.size());
162 }
163 
TEST_F(BuildImageMapTest,ImageMapIsCorrect)164 TEST_F(BuildImageMapTest, ImageMapIsCorrect) {
165   InitOmapData();
166   ASSERT_LE(0u, omap_data.omap_from.size());
167   ASSERT_LE(0u, omap_data.omap_to.size());
168   ASSERT_LE(0u, omap_data.length_original);
169 
170   ImageMap image_map;
171   BuildImageMap(omap_data, &image_map);
172   EXPECT_LE(9u, image_map.mapping.size());
173   EXPECT_LE(9u, image_map.endpoint_index_map.size());
174 
175   Mapping mapping;
176   mapping.push_back(CreateMappedRange(0, 0, B.rva, 0, 0));
177   // C is removed, and it originally comes immediately after B.
178   mapping.push_back(CreateMappedRange(B.rva, Bt.rva, B.length, 0, C.length));
179   // D is shortened by a length of 5.
180   mapping.push_back(CreateMappedRange(D.rva, Dt.rva, Dt.length, 0, 5));
181   // The injected content comes immediately after E in the transformed image.
182   mapping.push_back(CreateMappedRange(E.rva, Et.rva, E.length, injected.length,
183                                       0));
184   mapping.push_back(CreateMappedRange(F.rva, Ft.rva, F.length, 0, 0));
185   // G is copied so creates two entries.
186   mapping.push_back(CreateMappedRange(G.rva, G1t.rva, G.length, 0, 0));
187   mapping.push_back(CreateMappedRange(G.rva, G2t.rva, G.length, 0, 0));
188   // H is split, so create two entries.
189   mapping.push_back(CreateMappedRange(H.rva, H1t.rva, H1t.length, 0, 0));
190   mapping.push_back(CreateMappedRange(H.rva + H1t.length, H2t.rva, H2t.length,
191                                       0, 0));
192   EXPECT_THAT(mapping,
193               testing::ContainerEq(image_map.mapping));
194 
195   EndpointIndexMap endpoint_index_map;
196   endpoint_index_map.push_back(CreateEndpointIndex(0, 0));
197   endpoint_index_map.push_back(CreateEndpointIndex(B.rva, 1));
198   endpoint_index_map.push_back(CreateEndpointIndex(D.rva, 2));
199   endpoint_index_map.push_back(CreateEndpointIndex(E.rva, 3));
200   endpoint_index_map.push_back(CreateEndpointIndex(F.rva, 4));
201   // G is duplicated so 2 ranges map back to it, hence the skip from 5 to 7.
202   endpoint_index_map.push_back(CreateEndpointIndex(G.rva, 5));
203   // H is split so we expect 2 endpoints to show up attributed to it.
204   endpoint_index_map.push_back(CreateEndpointIndex(H.rva, 7));
205   endpoint_index_map.push_back(CreateEndpointIndex(H.rva + H1t.length, 8));
206   endpoint_index_map.push_back(CreateEndpointIndex(H.end(), 9));
207   EXPECT_THAT(endpoint_index_map,
208               testing::ContainerEq(image_map.endpoint_index_map));
209 }
210 
211 namespace {
212 
213 class MapAddressRangeTest : public BuildImageMapTest {
214  public:
215   typedef BuildImageMapTest Super;
SetUp()216   virtual void SetUp() {
217     Super::SetUp();
218     InitOmapData();
219     BuildImageMap(omap_data, &image_map);
220   }
221 
222   ImageMap image_map;
223 
224  private:
225   using BuildImageMapTest::InitOmapData;
226   using BuildImageMapTest::omap_data;
227 };
228 
229 }  // namespace
230 
TEST_F(MapAddressRangeTest,EmptyImageMapReturnsIdentity)231 TEST_F(MapAddressRangeTest, EmptyImageMapReturnsIdentity) {
232   ImageMap im;
233   AddressRangeVector mapped_ranges;
234   AddressRange ar(0, 1024);
235   MapAddressRange(im, ar, &mapped_ranges);
236   EXPECT_EQ(1u, mapped_ranges.size());
237   EXPECT_EQ(ar, mapped_ranges[0]);
238 }
239 
TEST_F(MapAddressRangeTest,MapOutOfImage)240 TEST_F(MapAddressRangeTest, MapOutOfImage) {
241   AddressRangeVector mapped_ranges;
242   MapAddressRange(image_map, AddressRange(H.end() + 10, 10), &mapped_ranges);
243   EXPECT_EQ(0u, mapped_ranges.size());
244 }
245 
TEST_F(MapAddressRangeTest,MapIdentity)246 TEST_F(MapAddressRangeTest, MapIdentity) {
247   AddressRangeVector mapped_ranges;
248   MapAddressRange(image_map, B, &mapped_ranges);
249   EXPECT_EQ(1u, mapped_ranges.size());
250   EXPECT_THAT(mapped_ranges, testing::ElementsAre(B));
251 }
252 
TEST_F(MapAddressRangeTest,MapReorderedContiguous)253 TEST_F(MapAddressRangeTest, MapReorderedContiguous) {
254   AddressRangeVector mapped_ranges;
255 
256   AddressRange DEF(D.rva, F.end() - D.rva);
257   MapAddressRange(image_map, DEF, &mapped_ranges);
258   EXPECT_EQ(1u, mapped_ranges.size());
259 
260   AddressRange DFEt(Dt.rva, Et.end() - Dt.rva);
261   EXPECT_THAT(mapped_ranges, testing::ElementsAre(DFEt));
262 }
263 
TEST_F(MapAddressRangeTest,MapEmptySingle)264 TEST_F(MapAddressRangeTest, MapEmptySingle) {
265   AddressRangeVector mapped_ranges;
266   MapAddressRange(image_map, AddressRange(D.rva, 0), &mapped_ranges);
267   EXPECT_EQ(1u, mapped_ranges.size());
268   EXPECT_THAT(mapped_ranges, testing::ElementsAre(AddressRange(Dt.rva, 0)));
269 }
270 
TEST_F(MapAddressRangeTest,MapEmptyCopied)271 TEST_F(MapAddressRangeTest, MapEmptyCopied) {
272   AddressRangeVector mapped_ranges;
273   MapAddressRange(image_map, AddressRange(G.rva, 0), &mapped_ranges);
274   EXPECT_EQ(2u, mapped_ranges.size());
275   EXPECT_THAT(mapped_ranges, testing::ElementsAre(AddressRange(G1t.rva, 0),
276                                                   AddressRange(G2t.rva, 0)));
277 }
278 
TEST_F(MapAddressRangeTest,MapCopiedContiguous)279 TEST_F(MapAddressRangeTest, MapCopiedContiguous) {
280   AddressRangeVector mapped_ranges;
281   MapAddressRange(image_map, G, &mapped_ranges);
282   EXPECT_EQ(1u, mapped_ranges.size());
283   EXPECT_THAT(mapped_ranges, testing::ElementsAre(
284       AddressRange(G1t.rva, G2t.end() - G1t.rva)));
285 }
286 
TEST_F(MapAddressRangeTest,MapSplitDiscontiguous)287 TEST_F(MapAddressRangeTest, MapSplitDiscontiguous) {
288   AddressRangeVector mapped_ranges;
289   MapAddressRange(image_map, H, &mapped_ranges);
290   EXPECT_EQ(2u, mapped_ranges.size());
291   EXPECT_THAT(mapped_ranges, testing::ElementsAre(H1t, H2t));
292 }
293 
TEST_F(MapAddressRangeTest,MapInjected)294 TEST_F(MapAddressRangeTest, MapInjected) {
295   AddressRangeVector mapped_ranges;
296 
297   AddressRange EFGH(E.rva, H.end() - E.rva);
298   MapAddressRange(image_map, EFGH, &mapped_ranges);
299   EXPECT_EQ(1u, mapped_ranges.size());
300 
301   AddressRange FEHGGHt(Ft.rva, H2t.end() - Ft.rva);
302   EXPECT_THAT(mapped_ranges, testing::ElementsAre(FEHGGHt));
303 }
304 
TEST_F(MapAddressRangeTest,MapRemovedEntirely)305 TEST_F(MapAddressRangeTest, MapRemovedEntirely) {
306   AddressRangeVector mapped_ranges;
307   MapAddressRange(image_map, C, &mapped_ranges);
308   EXPECT_EQ(0u, mapped_ranges.size());
309 }
310 
TEST_F(MapAddressRangeTest,MapRemovedPartly)311 TEST_F(MapAddressRangeTest, MapRemovedPartly) {
312   AddressRangeVector mapped_ranges;
313   MapAddressRange(image_map, D, &mapped_ranges);
314   EXPECT_EQ(1u, mapped_ranges.size());
315   EXPECT_THAT(mapped_ranges, testing::ElementsAre(Dt));
316 }
317 
TEST_F(MapAddressRangeTest,MapFull)318 TEST_F(MapAddressRangeTest, MapFull) {
319   AddressRangeVector mapped_ranges;
320 
321   AddressRange AH(0, H.end());
322   MapAddressRange(image_map, AH, &mapped_ranges);
323   EXPECT_EQ(1u, mapped_ranges.size());
324 
325   AddressRange AHt(0, H2t.end());
326   EXPECT_THAT(mapped_ranges, testing::ElementsAre(AHt));
327 }
328 
329 }  // namespace google_breakpad
330