1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <vector>
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include "dwarf_test.h"
22
23 using namespace testing::ext;
24 using namespace std;
25 // using namespace OHOS::HiviewDFX;
26 namespace OHOS {
27 namespace Developtools {
28 namespace HiPerf {
29 namespace {
30 const unsigned char absptr {0xff};
31 constexpr int data2Size {2};
32 const unsigned char data2[data2Size] {0xff, 0xff};
33 const unsigned char data4[sizeof(int32_t)] {0xff, 0xff, 0xff, 0xff};
34 const unsigned char data8[sizeof(int64_t)] {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
35 constexpr int data128Size {128};
36 unsigned char data128[data128Size] {};
37 #ifdef NOT_USE
38 constexpr int num {9};
39 #else
40 constexpr int num {7};
41 #endif
42 const unsigned char *data[num] {};
43 const std::vector<std::string> strs {
44 "DW_EH_PE_empty:DW_EH_PE_absptr format:00 value size:8",
45 "DW_EH_PE_empty:DW_EH_PE_udata2 format:02 value size:2",
46 "DW_EH_PE_empty:DW_EH_PE_udata4 format:03 value size:4",
47 "DW_EH_PE_empty:DW_EH_PE_udata8 format:04 value size:8",
48 "DW_EH_PE_empty:DW_EH_PE_data2 format:0a value size:2",
49 "DW_EH_PE_empty:DW_EH_PE_sdata4 format:0b value size:4",
50 "DW_EH_PE_empty:DW_EH_PE_sdata8 format:0c value size:8",
51 "DW_EH_PE_pcrel:DW_EH_PE_absptr format:10 value size:8",
52 "DW_EH_PE_pcrel:DW_EH_PE_udata2 format:12 value size:2",
53 "DW_EH_PE_pcrel:DW_EH_PE_udata4 format:13 value size:4",
54 "DW_EH_PE_pcrel:DW_EH_PE_udata8 format:14 value size:8",
55 "DW_EH_PE_pcrel:DW_EH_PE_data2 format:1a value size:2",
56 "DW_EH_PE_pcrel:DW_EH_PE_sdata4 format:1b value size:4",
57 "DW_EH_PE_pcrel:DW_EH_PE_sdata8 format:1c value size:8",
58 "DW_EH_PE_textrel:DW_EH_PE_absptr format:20 value size:8",
59 "DW_EH_PE_textrel:DW_EH_PE_udata2 format:22 value size:2",
60 "DW_EH_PE_textrel:DW_EH_PE_udata4 format:23 value size:4",
61 "DW_EH_PE_textrel:DW_EH_PE_udata8 format:24 value size:8",
62 "DW_EH_PE_textrel:DW_EH_PE_data2 format:2a value size:2",
63 "DW_EH_PE_textrel:DW_EH_PE_sdata4 format:2b value size:4",
64 "DW_EH_PE_textrel:DW_EH_PE_sdata8 format:2c value size:8",
65 "DW_EH_PE_datarel:DW_EH_PE_absptr format:30 value size:8",
66 "DW_EH_PE_datarel:DW_EH_PE_udata2 format:32 value size:2",
67 "DW_EH_PE_datarel:DW_EH_PE_udata4 format:33 value size:4",
68 "DW_EH_PE_datarel:DW_EH_PE_udata8 format:34 value size:8",
69 "DW_EH_PE_datarel:DW_EH_PE_data2 format:3a value size:2",
70 "DW_EH_PE_datarel:DW_EH_PE_sdata4 format:3b value size:4",
71 "DW_EH_PE_datarel:DW_EH_PE_sdata8 format:3c value size:8",
72 "DW_EH_PE_funcrel:DW_EH_PE_absptr format:40 value size:8",
73 "DW_EH_PE_funcrel:DW_EH_PE_udata2 format:42 value size:2",
74 "DW_EH_PE_funcrel:DW_EH_PE_udata4 format:43 value size:4",
75 "DW_EH_PE_funcrel:DW_EH_PE_udata8 format:44 value size:8",
76 "DW_EH_PE_funcrel:DW_EH_PE_data2 format:4a value size:2",
77 "DW_EH_PE_funcrel:DW_EH_PE_sdata4 format:4b value size:4",
78 "DW_EH_PE_funcrel:DW_EH_PE_sdata8 format:4c value size:8",
79 "DW_EH_PE_aligned:DW_EH_PE_absptr format:50 value size:8",
80 "DW_EH_PE_aligned:DW_EH_PE_udata2 format:52 value size:2",
81 "DW_EH_PE_aligned:DW_EH_PE_udata4 format:53 value size:4",
82 "DW_EH_PE_aligned:DW_EH_PE_udata8 format:54 value size:8",
83 "DW_EH_PE_aligned:DW_EH_PE_data2 format:5a value size:2",
84 "DW_EH_PE_aligned:DW_EH_PE_sdata4 format:5b value size:4",
85 "DW_EH_PE_aligned:DW_EH_PE_sdata8 format:5c value size:8",
86 };
87 std::vector<uint64_t> values {
88 65535ULL,
89 4294967295ULL,
90 18446744073709551615ULL,
91 18446744073709551615ULL,
92 18446744073709551615ULL,
93 18446744073709551615ULL,
94 65535ULL,
95 4294967295ULL,
96 1921026034567241472ULL,
97 18446744073709551615ULL,
98 18446744073709551615ULL,
99 1921026034567241472ULL,
100 65535ULL,
101 4294967295ULL,
102 1921010641404477184ULL,
103 18446744073709551615ULL,
104 18446744073709551615ULL,
105 1921010641404477184ULL,
106 65535ULL,
107 447241984ULL,
108 1940452206006808832ULL,
109 18446744073709551615ULL,
110 447241984ULL,
111 1940452206006808832ULL,
112 65535ULL,
113 447273728ULL,
114 1940830438011371264ULL,
115 18446744073709551615ULL,
116 447273728ULL,
117 1940830438011371264ULL,
118 65535ULL,
119 447266560ULL,
120 1940830438011385600ULL,
121 18446744073709551615ULL,
122 447266560ULL,
123 1940830438011385600ULL,
124 };
125 std::vector<dw_encode_t> vfs {
126 DW_EH_PE_absptr,
127 #ifdef NOT_USE
128 DW_EH_PE_uleb128,
129 #endif
130 DW_EH_PE_udata2, DW_EH_PE_udata4, DW_EH_PE_udata8,
131 #ifdef NOT_USE
132 DW_EH_PE_sleb128,
133 #endif
134 DW_EH_PE_sdata2, DW_EH_PE_sdata4, DW_EH_PE_sdata8,
135 };
136 vector<dw_encode_t> ehas {
137 DW_EH_PE_nothing, DW_EH_PE_pcrel, DW_EH_PE_textrel, DW_EH_PE_datarel,
138 DW_EH_PE_funcrel, DW_EH_PE_aligned, DW_EH_PE_omit,
139 };
140 } // namespace
141
142 class DwarfTest : public testing::Test {
143 public:
144 static void SetUpTestCase(void);
145 static void TearDownTestCase(void);
146 void SetUp();
147 void TearDown();
148 };
149
SetUpTestCase(void)150 void DwarfTest::SetUpTestCase(void) {}
151
TearDownTestCase(void)152 void DwarfTest::TearDownTestCase(void) {}
153
SetUp()154 void DwarfTest::SetUp()
155 {
156 for (std::size_t index = 0; index < data128Size; ++index) {
157 data128[index] = 0xff;
158 }
159 std::size_t index {0};
160 data[index++] = &absptr;
161 #ifdef NOT_USE
162 data[index++] = data128;
163 #endif
164 data[index++] = data2;
165 data[index++] = data4;
166 data[index++] = data8;
167 #ifdef NOT_USE
168 data[index++] = data128;
169 #endif
170 data[index++] = data2;
171 data[index++] = data4;
172 data[index++] = data8;
173 }
174
TearDown()175 void DwarfTest::TearDown() {}
176
177 HWTEST_F(DwarfTest, GetEnd, TestSize.Level1)
178 {
179 for (std::size_t i = 0; i < ehas.size(); ++i) {
180 for (std::size_t j = 0; j < num; ++j) {
181 {
182 dw_encode_t dwe = ehas[i] | vfs[j];
183 DwarfEncoding dw {dwe, data[j]};
184 if (!dw.IsOmit()) {
185 if (vfs[j] == DW_EH_PE_absptr) {
186 EXPECT_TRUE(data[j] == dw.GetEnd() - dw.GetSize());
187 } else {
188 EXPECT_TRUE(data[j] == dw.GetEnd());
189 }
190 }
191 }
192 }
193 }
194 }
195
196 HWTEST_F(DwarfTest, GetData, TestSize.Level1)
197 {
198 for (std::size_t i = 0; i < ehas.size(); ++i) {
199 for (std::size_t j = 0; j < num; ++j) {
200 {
201 dw_encode_t dwe = ehas[i] | vfs[j];
202 DwarfEncoding dw {dwe, data[j]};
203 if (!dw.IsOmit()) {
204 if (vfs[j] == DW_EH_PE_absptr) {
205 EXPECT_TRUE(data[j] == dw.GetData());
206 } else {
207 EXPECT_TRUE(data[j] == dw.GetData() + dw.GetSize());
208 }
209 }
210 }
211 }
212 }
213 }
214
215 HWTEST_F(DwarfTest, GetSize, TestSize.Level1)
216 {
217 for (std::size_t i = 0; i < ehas.size(); ++i) {
218 for (std::size_t j = 0; j < num; ++j) {
219 {
220 dw_encode_t dwe = ehas[i] | vfs[j];
221 DwarfEncoding dw {dwe, data[j]};
222 if (!dw.IsOmit()) {
223 EXPECT_TRUE(DWFormatSizeMap.at(vfs[j]) == dw.GetSize());
224 }
225 }
226 }
227 }
228 }
229
230 HWTEST_F(DwarfTest, IsOmit, TestSize.Level1)
231 {
232 for (std::size_t i = 0; i < ehas.size(); ++i) {
233 for (std::size_t j = 0; j < num; ++j) {
234 {
235 dw_encode_t dwe = ehas[i] | vfs[j];
236 DwarfEncoding dw {dwe, data[j]};
237 if (ehas[i] == DW_EH_PE_omit) {
238 EXPECT_TRUE(dw.IsOmit());
239 } else {
240 EXPECT_FALSE(dw.IsOmit());
241 }
242 }
243 }
244 }
245 }
246 } // namespace HiPerf
247 } // namespace Developtools
248 } // namespace OHOS
249