OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stdint.h> |
| 6 |
5 #include "courgette/base_test_unittest.h" | 7 #include "courgette/base_test_unittest.h" |
6 #include "courgette/disassembler_elf_32_arm.h" | 8 #include "courgette/disassembler_elf_32_arm.h" |
7 #include "courgette/disassembler_elf_32_x86.h" | 9 #include "courgette/disassembler_elf_32_x86.h" |
8 #include "courgette/image_utils.h" | 10 #include "courgette/image_utils.h" |
9 | 11 |
10 class TypedRVATest : public BaseTest { | 12 class TypedRVATest : public BaseTest { |
11 public: | 13 public: |
12 void TestRelativeTargetX86(courgette::RVA word, courgette::RVA expected) | 14 void TestRelativeTargetX86(courgette::RVA word, courgette::RVA expected) |
13 const; | 15 const; |
14 | 16 |
15 void TestRelativeTargetARM(courgette::ARM_RVA arm_rva, | 17 void TestRelativeTargetARM(courgette::ARM_RVA arm_rva, |
16 courgette::RVA rva, | 18 courgette::RVA rva, |
17 uint32 op, | 19 uint32_t op, |
18 courgette::RVA expected) const; | 20 courgette::RVA expected) const; |
19 | 21 |
20 void TestARMOPEncode(courgette::ARM_RVA arm_rva, | 22 void TestARMOPEncode(courgette::ARM_RVA arm_rva, |
21 courgette::RVA rva, | 23 courgette::RVA rva, |
22 uint32 op, | 24 uint32_t op, |
23 courgette::RVA expected) const; | 25 courgette::RVA expected) const; |
24 }; | 26 }; |
25 | 27 |
26 void TypedRVATest::TestRelativeTargetX86(courgette::RVA word, | 28 void TypedRVATest::TestRelativeTargetX86(courgette::RVA word, |
27 courgette::RVA expected) const { | 29 courgette::RVA expected) const { |
28 courgette::DisassemblerElf32X86::TypedRVAX86* typed_rva | 30 courgette::DisassemblerElf32X86::TypedRVAX86* typed_rva |
29 = new courgette::DisassemblerElf32X86::TypedRVAX86(0); | 31 = new courgette::DisassemblerElf32X86::TypedRVAX86(0); |
30 const uint8* op_pointer = reinterpret_cast<const uint8*>(&word); | 32 const uint8_t* op_pointer = reinterpret_cast<const uint8_t*>(&word); |
31 | 33 |
32 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer)); | 34 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer)); |
33 EXPECT_EQ(typed_rva->relative_target(), expected); | 35 EXPECT_EQ(typed_rva->relative_target(), expected); |
34 | 36 |
35 delete typed_rva; | 37 delete typed_rva; |
36 } | 38 } |
37 | 39 |
38 uint32 Read32LittleEndian(const void* address) { | 40 uint32_t Read32LittleEndian(const void* address) { |
39 return *reinterpret_cast<const uint32*>(address); | 41 return *reinterpret_cast<const uint32_t*>(address); |
40 } | 42 } |
41 | 43 |
42 void TypedRVATest::TestRelativeTargetARM(courgette::ARM_RVA arm_rva, | 44 void TypedRVATest::TestRelativeTargetARM(courgette::ARM_RVA arm_rva, |
43 courgette::RVA rva, | 45 courgette::RVA rva, |
44 uint32 op, | 46 uint32_t op, |
45 courgette::RVA expected) const { | 47 courgette::RVA expected) const { |
46 courgette::DisassemblerElf32ARM::TypedRVAARM* typed_rva | 48 courgette::DisassemblerElf32ARM::TypedRVAARM* typed_rva |
47 = new courgette::DisassemblerElf32ARM::TypedRVAARM(arm_rva, rva); | 49 = new courgette::DisassemblerElf32ARM::TypedRVAARM(arm_rva, rva); |
48 uint8* op_pointer = reinterpret_cast<uint8*>(&op); | 50 uint8_t* op_pointer = reinterpret_cast<uint8_t*>(&op); |
49 | 51 |
50 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer)); | 52 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer)); |
51 EXPECT_EQ(rva + typed_rva->relative_target(), expected); | 53 EXPECT_EQ(rva + typed_rva->relative_target(), expected); |
52 | 54 |
53 delete typed_rva; | 55 delete typed_rva; |
54 } | 56 } |
55 | 57 |
56 void TypedRVATest::TestARMOPEncode(courgette::ARM_RVA arm_rva, | 58 void TypedRVATest::TestARMOPEncode(courgette::ARM_RVA arm_rva, |
57 courgette::RVA rva, | 59 courgette::RVA rva, |
58 uint32 op, | 60 uint32_t op, |
59 courgette::RVA expected) const { | 61 courgette::RVA expected) const { |
60 uint16 c_op; | 62 uint16_t c_op; |
61 uint32 addr; | 63 uint32_t addr; |
62 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Compress(arm_rva, op, rva, | 64 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Compress(arm_rva, op, rva, |
63 &c_op, &addr)); | 65 &c_op, &addr)); |
64 EXPECT_EQ(rva + addr, expected); | 66 EXPECT_EQ(rva + addr, expected); |
65 | 67 |
66 uint32 new_op; | 68 uint32_t new_op; |
67 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Decompress(arm_rva, c_op, addr, | 69 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Decompress(arm_rva, c_op, addr, |
68 &new_op)); | 70 &new_op)); |
69 EXPECT_EQ(new_op, op); | 71 EXPECT_EQ(new_op, op); |
70 } | 72 } |
71 | 73 |
72 TEST_F(TypedRVATest, TestX86) { | 74 TEST_F(TypedRVATest, TestX86) { |
73 TestRelativeTargetX86(0x0, 0x4); | 75 TestRelativeTargetX86(0x0, 0x4); |
74 } | 76 } |
75 | 77 |
76 // ARM opcodes taken from and tested against the output of | 78 // ARM opcodes taken from and tested against the output of |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 TestARMOPEncode(courgette::ARM_OFF25, 0x2c58, 0xf005feac, 0x89b4); | 157 TestARMOPEncode(courgette::ARM_OFF25, 0x2c58, 0xf005feac, 0x89b4); |
156 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd2, 0xf7ffeb9e, 0x2310); | 158 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd2, 0xf7ffeb9e, 0x2310); |
157 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd8, 0xf7ffeb8e, 0x22f8); | 159 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd8, 0xf7ffeb8e, 0x22f8); |
158 TestARMOPEncode(courgette::ARM_OFF25, 0x2c3e, 0xf7ffea2e, 0x209c); | 160 TestARMOPEncode(courgette::ARM_OFF25, 0x2c3e, 0xf7ffea2e, 0x209c); |
159 TestARMOPEncode(courgette::ARM_OFF21, 0x2bc6, 0xf00084c7, 0x3558); | 161 TestARMOPEncode(courgette::ARM_OFF21, 0x2bc6, 0xf00084c7, 0x3558); |
160 TestARMOPEncode(courgette::ARM_OFF21, 0x2bde, 0xf000871d, 0x3a1c); | 162 TestARMOPEncode(courgette::ARM_OFF21, 0x2bde, 0xf000871d, 0x3a1c); |
161 TestARMOPEncode(courgette::ARM_OFF21, 0x2c5e, 0xf2c086c1, 0x39e4); | 163 TestARMOPEncode(courgette::ARM_OFF21, 0x2c5e, 0xf2c086c1, 0x39e4); |
162 TestARMOPEncode(courgette::ARM_OFF21, 0x67e4, 0xf43faee9, 0x65ba); | 164 TestARMOPEncode(courgette::ARM_OFF21, 0x67e4, 0xf43faee9, 0x65ba); |
163 TestARMOPEncode(courgette::ARM_OFF21, 0x67ee, 0xf47faee4, 0x65ba); | 165 TestARMOPEncode(courgette::ARM_OFF21, 0x67ee, 0xf47faee4, 0x65ba); |
164 } | 166 } |
OLD | NEW |