Add a Notification Settings Button to all web notifications behind the web platform...
[chromium-blink-merge.git] / courgette / typedrva_unittest.cc
blobdc30fa9b222cb76ca39d30eeb167f375c288e95d
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
3 // found in the LICENSE file.
5 #include "courgette/base_test_unittest.h"
6 #include "courgette/disassembler_elf_32_arm.h"
7 #include "courgette/disassembler_elf_32_x86.h"
8 #include "courgette/image_utils.h"
10 class TypedRVATest : public BaseTest {
11 public:
12 void TestRelativeTargetX86(courgette::RVA word, courgette::RVA expected)
13 const;
15 void TestRelativeTargetARM(courgette::ARM_RVA arm_rva,
16 courgette::RVA rva,
17 uint32 op,
18 courgette::RVA expected) const;
20 void TestARMOPEncode(courgette::ARM_RVA arm_rva,
21 courgette::RVA rva,
22 uint32 op,
23 courgette::RVA expected) const;
26 void TypedRVATest::TestRelativeTargetX86(courgette::RVA word,
27 courgette::RVA expected) const {
28 courgette::DisassemblerElf32X86::TypedRVAX86* typed_rva
29 = new courgette::DisassemblerElf32X86::TypedRVAX86(0);
30 const uint8* op_pointer = reinterpret_cast<const uint8*>(&word);
32 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer));
33 EXPECT_EQ(typed_rva->relative_target(), expected);
35 delete typed_rva;
38 uint32 Read32LittleEndian(const void* address) {
39 return *reinterpret_cast<const uint32*>(address);
42 void TypedRVATest::TestRelativeTargetARM(courgette::ARM_RVA arm_rva,
43 courgette::RVA rva,
44 uint32 op,
45 courgette::RVA expected) const {
46 courgette::DisassemblerElf32ARM::TypedRVAARM* typed_rva
47 = new courgette::DisassemblerElf32ARM::TypedRVAARM(arm_rva, rva);
48 uint8* op_pointer = reinterpret_cast<uint8*>(&op);
50 EXPECT_TRUE(typed_rva->ComputeRelativeTarget(op_pointer));
51 EXPECT_EQ(rva + typed_rva->relative_target(), expected);
53 delete typed_rva;
56 void TypedRVATest::TestARMOPEncode(courgette::ARM_RVA arm_rva,
57 courgette::RVA rva,
58 uint32 op,
59 courgette::RVA expected) const {
60 uint16 c_op;
61 uint32 addr;
62 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Compress(arm_rva, op, rva,
63 &c_op, &addr));
64 EXPECT_EQ(rva + addr, expected);
66 uint32 new_op;
67 EXPECT_TRUE(courgette::DisassemblerElf32ARM::Decompress(arm_rva, c_op, addr,
68 &new_op));
69 EXPECT_EQ(new_op, op);
72 TEST_F(TypedRVATest, TestX86) {
73 TestRelativeTargetX86(0x0, 0x4);
76 // ARM opcodes taken from and tested against the output of
77 // "arm-linux-gnueabi-objdump -d daisy_3701.98.0/bin/ls"
79 TEST_F(TypedRVATest, TestARM_OFF8_PREFETCH) {
80 TestRelativeTargetARM(courgette::ARM_OFF8, 0x0, 0x0, 0x4);
83 TEST_F(TypedRVATest, TestARM_OFF8_FORWARDS) {
84 TestRelativeTargetARM(courgette::ARM_OFF8, 0x2bcc, 0xd00e, 0x2bec);
85 TestRelativeTargetARM(courgette::ARM_OFF8, 0x3752, 0xd910, 0x3776);
88 TEST_F(TypedRVATest, TestARM_OFF8_BACKWARDS) {
89 TestRelativeTargetARM(courgette::ARM_OFF8, 0x3774, 0xd1f6, 0x3764);
92 TEST_F(TypedRVATest, TestARM_OFF11_PREFETCH) {
93 TestRelativeTargetARM(courgette::ARM_OFF11, 0x0, 0x0, 0x4);
96 TEST_F(TypedRVATest, TestARM_OFF11_FORWARDS) {
97 TestRelativeTargetARM(courgette::ARM_OFF11, 0x2bea, 0xe005, 0x2bf8);
100 TEST_F(TypedRVATest, TestARM_OFF11_BACKWARDS) {
101 TestRelativeTargetARM(courgette::ARM_OFF11, 0x2f80, 0xe6cd, 0x2d1e);
102 TestRelativeTargetARM(courgette::ARM_OFF11, 0x3610, 0xe56a, 0x30e8);
105 TEST_F(TypedRVATest, TestARM_OFF24_PREFETCH) {
106 TestRelativeTargetARM(courgette::ARM_OFF24, 0x0, 0x0, 0x8);
109 TEST_F(TypedRVATest, TestARM_OFF24_FORWARDS) {
110 TestRelativeTargetARM(courgette::ARM_OFF24, 0x2384, 0x4af3613a, 0xffcda874);
111 TestRelativeTargetARM(courgette::ARM_OFF24, 0x23bc, 0x6af961b9, 0xffe5aaa8);
112 TestRelativeTargetARM(courgette::ARM_OFF24, 0x23d4, 0x2b006823, 0x1c468);
115 TEST_F(TypedRVATest, TestARM_OFF24_BACKWARDS) {
116 // TODO(paulgazz): find a real-world example of an non-thumb ARM
117 // branch op that jumps backwards.
120 TEST_F(TypedRVATest, TestARM_OFF25_FORWARDS) {
121 TestRelativeTargetARM(courgette::ARM_OFF25, 0x2bf4, 0xfe06f008, 0xb804);
122 TestRelativeTargetARM(courgette::ARM_OFF25, 0x2c58, 0xfeacf005, 0x89b4);
125 TEST_F(TypedRVATest, TestARM_OFF25_BACKWARDS) {
126 TestRelativeTargetARM(courgette::ARM_OFF25, 0x2bd2, 0xeb9ef7ff, 0x2310);
127 TestRelativeTargetARM(courgette::ARM_OFF25, 0x2bd8, 0xeb8ef7ff, 0x22f8);
128 TestRelativeTargetARM(courgette::ARM_OFF25, 0x2c3e, 0xea2ef7ff, 0x209c);
131 TEST_F(TypedRVATest, TestARM_OFF21_FORWARDS) {
132 TestRelativeTargetARM(courgette::ARM_OFF21, 0x2bc6, 0x84c7f000, 0x3558);
133 TestRelativeTargetARM(courgette::ARM_OFF21, 0x2bde, 0x871df000, 0x3a1c);
134 TestRelativeTargetARM(courgette::ARM_OFF21, 0x2c5e, 0x86c1f2c0, 0x39e4);
137 TEST_F(TypedRVATest, TestARM_OFF21_BACKWARDS) {
138 TestRelativeTargetARM(courgette::ARM_OFF21, 0x67e4, 0xaee9f43f, 0x65ba);
139 TestRelativeTargetARM(courgette::ARM_OFF21, 0x67ee, 0xaee4f47f, 0x65ba);
142 TEST_F(TypedRVATest, TestARMOPEncode) {
143 TestARMOPEncode(courgette::ARM_OFF8, 0x2bcc, 0xd00e, 0x2bec);
144 TestARMOPEncode(courgette::ARM_OFF8, 0x3752, 0xd910, 0x3776);
145 TestARMOPEncode(courgette::ARM_OFF8, 0x3774, 0xd1f6, 0x3764);
146 TestARMOPEncode(courgette::ARM_OFF11, 0x0, 0x0, 0x4);
147 TestARMOPEncode(courgette::ARM_OFF11, 0x2bea, 0xe005, 0x2bf8);
148 TestARMOPEncode(courgette::ARM_OFF11, 0x2f80, 0xe6cd, 0x2d1e);
149 TestARMOPEncode(courgette::ARM_OFF11, 0x3610, 0xe56a, 0x30e8);
150 TestARMOPEncode(courgette::ARM_OFF24, 0x0, 0x0, 0x8);
151 TestARMOPEncode(courgette::ARM_OFF24, 0x2384, 0x4af3613a, 0xffcda874);
152 TestARMOPEncode(courgette::ARM_OFF24, 0x23bc, 0x6af961b9, 0xffe5aaa8);
153 TestARMOPEncode(courgette::ARM_OFF24, 0x23d4, 0x2b006823, 0x1c468);
154 TestARMOPEncode(courgette::ARM_OFF25, 0x2bf4, 0xf008fe06, 0xb804);
155 TestARMOPEncode(courgette::ARM_OFF25, 0x2c58, 0xf005feac, 0x89b4);
156 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd2, 0xf7ffeb9e, 0x2310);
157 TestARMOPEncode(courgette::ARM_OFF25, 0x2bd8, 0xf7ffeb8e, 0x22f8);
158 TestARMOPEncode(courgette::ARM_OFF25, 0x2c3e, 0xf7ffea2e, 0x209c);
159 TestARMOPEncode(courgette::ARM_OFF21, 0x2bc6, 0xf00084c7, 0x3558);
160 TestARMOPEncode(courgette::ARM_OFF21, 0x2bde, 0xf000871d, 0x3a1c);
161 TestARMOPEncode(courgette::ARM_OFF21, 0x2c5e, 0xf2c086c1, 0x39e4);
162 TestARMOPEncode(courgette::ARM_OFF21, 0x67e4, 0xf43faee9, 0x65ba);
163 TestARMOPEncode(courgette::ARM_OFF21, 0x67ee, 0xf47faee4, 0x65ba);