blob: bb2e8f0395d5d0fd9db0bf5e7d631f7366e792d3 [file] [log] [blame]
Christopher Ferris8642fcb2017-04-24 11:14:39 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18
19#include <memory>
20#include <type_traits>
21#include <unordered_map>
22
23#include <android-base/stringprintf.h>
24#include <gtest/gtest.h>
25
Christopher Ferrisd226a512017-07-14 10:37:19 -070026#include <unwindstack/DwarfLocation.h>
27#include <unwindstack/DwarfMemory.h>
28#include <unwindstack/DwarfStructs.h>
29#include <unwindstack/Log.h>
30
Christopher Ferris8642fcb2017-04-24 11:14:39 -070031#include "DwarfCfa.h"
Christopher Ferris8642fcb2017-04-24 11:14:39 -070032
33#include "LogFake.h"
34#include "MemoryFake.h"
35
Christopher Ferrisd226a512017-07-14 10:37:19 -070036namespace unwindstack {
37
Christopher Ferris8642fcb2017-04-24 11:14:39 -070038template <typename TypeParam>
39class DwarfCfaLogTest : public ::testing::Test {
40 protected:
41 void SetUp() override {
42 ResetLogs();
43 memory_.Clear();
44
45 dmem_.reset(new DwarfMemory(&memory_));
46
47 cie_.cfa_instructions_offset = 0x1000;
48 cie_.cfa_instructions_end = 0x1030;
49 // These two values should be different to distinguish between
50 // operations that deal with code versus data.
51 cie_.code_alignment_factor = 4;
52 cie_.data_alignment_factor = 8;
53
54 fde_.cfa_instructions_offset = 0x2000;
55 fde_.cfa_instructions_end = 0x2030;
56 fde_.pc_start = 0x2000;
57 fde_.pc_end = 0x2000;
58 fde_.pc_end = 0x10000;
59 fde_.cie = &cie_;
60 cfa_.reset(new DwarfCfa<TypeParam>(dmem_.get(), &fde_));
61 }
62
63 MemoryFake memory_;
64 std::unique_ptr<DwarfMemory> dmem_;
65 std::unique_ptr<DwarfCfa<TypeParam>> cfa_;
Christopher Ferris53a3c9b2017-05-10 18:34:15 -070066 DwarfCie cie_;
67 DwarfFde fde_;
Christopher Ferris8642fcb2017-04-24 11:14:39 -070068};
69TYPED_TEST_CASE_P(DwarfCfaLogTest);
70
71// NOTE: All class variable references have to be prefaced with this->.
72
73TYPED_TEST_P(DwarfCfaLogTest, cfa_illegal) {
74 for (uint8_t i = 0x17; i < 0x3f; i++) {
75 if (i == 0x2e || i == 0x2f) {
76 // Skip gnu extension ops.
77 continue;
78 }
79 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{i});
80
81 ResetLogs();
Christopher Ferris4cc36d22018-06-06 14:47:31 -070082 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
Christopher Ferris8642fcb2017-04-24 11:14:39 -070083 std::string expected = "4 unwind Illegal\n";
84 expected += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", i);
85 ASSERT_EQ(expected, GetFakeLogPrint());
86 ASSERT_EQ("", GetFakeLogBuf());
87 }
88}
89
90TYPED_TEST_P(DwarfCfaLogTest, cfa_nop) {
91 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x00});
92
Christopher Ferris4cc36d22018-06-06 14:47:31 -070093 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
Christopher Ferris8642fcb2017-04-24 11:14:39 -070094 std::string expected =
95 "4 unwind DW_CFA_nop\n"
96 "4 unwind Raw Data: 0x00\n";
97 ASSERT_EQ(expected, GetFakeLogPrint());
98 ASSERT_EQ("", GetFakeLogBuf());
99}
100
101TYPED_TEST_P(DwarfCfaLogTest, cfa_offset) {
102 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x83, 0x04});
103
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700104 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700105 std::string expected =
106 "4 unwind DW_CFA_offset register(3) 4\n"
107 "4 unwind Raw Data: 0x83 0x04\n";
108 ASSERT_EQ(expected, GetFakeLogPrint());
109 ASSERT_EQ("", GetFakeLogBuf());
110
111 ResetLogs();
112 this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x83, 0x84, 0x01});
113
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700114 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700115 expected =
116 "4 unwind DW_CFA_offset register(3) 132\n"
117 "4 unwind Raw Data: 0x83 0x84 0x01\n";
118 ASSERT_EQ(expected, GetFakeLogPrint());
119 ASSERT_EQ("", GetFakeLogBuf());
120}
121
122TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended) {
123 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x05, 0x03, 0x02});
124
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700125 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700126 std::string expected =
127 "4 unwind DW_CFA_offset_extended register(3) 2\n"
128 "4 unwind Raw Data: 0x05 0x03 0x02\n";
129 ASSERT_EQ(expected, GetFakeLogPrint());
130 ASSERT_EQ("", GetFakeLogBuf());
131
132 ResetLogs();
133 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x05, 0x81, 0x01, 0x82, 0x12});
134
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700135 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700136 expected =
137 "4 unwind DW_CFA_offset_extended register(129) 2306\n"
138 "4 unwind Raw Data: 0x05 0x81 0x01 0x82 0x12\n";
139 ASSERT_EQ(expected, GetFakeLogPrint());
140 ASSERT_EQ("", GetFakeLogBuf());
141}
142
143TYPED_TEST_P(DwarfCfaLogTest, cfa_offset_extended_sf) {
144 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x11, 0x05, 0x10});
145
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700146 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700147 std::string expected =
148 "4 unwind DW_CFA_offset_extended_sf register(5) 16\n"
149 "4 unwind Raw Data: 0x11 0x05 0x10\n";
150 ASSERT_EQ(expected, GetFakeLogPrint());
151 ASSERT_EQ("", GetFakeLogBuf());
152
153 // Check a negative value for the offset.
154 ResetLogs();
155 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x11, 0x86, 0x01, 0xff, 0x7f});
156
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700157 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700158 expected =
159 "4 unwind DW_CFA_offset_extended_sf register(134) -1\n"
160 "4 unwind Raw Data: 0x11 0x86 0x01 0xff 0x7f\n";
161 ASSERT_EQ(expected, GetFakeLogPrint());
162 ASSERT_EQ("", GetFakeLogBuf());
163}
164
165TYPED_TEST_P(DwarfCfaLogTest, cfa_restore) {
166 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0xc2});
167
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700168 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2001));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700169 std::string expected =
170 "4 unwind DW_CFA_restore register(2)\n"
171 "4 unwind Raw Data: 0xc2\n";
172 ASSERT_EQ(expected, GetFakeLogPrint());
173 ASSERT_EQ("", GetFakeLogBuf());
174
175 ResetLogs();
176 this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x82, 0x04, 0xc2});
177
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700178 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3003));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700179 expected =
180 "4 unwind DW_CFA_offset register(2) 4\n"
181 "4 unwind Raw Data: 0x82 0x04\n"
182 "4 unwind DW_CFA_restore register(2)\n"
183 "4 unwind Raw Data: 0xc2\n";
184 ASSERT_EQ(expected, GetFakeLogPrint());
185 ASSERT_EQ("", GetFakeLogBuf());
186}
187
188TYPED_TEST_P(DwarfCfaLogTest, cfa_restore_extended) {
189 this->memory_.SetMemory(0x4000, std::vector<uint8_t>{0x06, 0x08});
190
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700191 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4000, 0x4002));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700192 std::string expected =
193 "4 unwind DW_CFA_restore_extended register(8)\n"
194 "4 unwind Raw Data: 0x06 0x08\n";
195 ASSERT_EQ(expected, GetFakeLogPrint());
196 ASSERT_EQ("", GetFakeLogBuf());
197
198 ResetLogs();
199 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x05, 0x82, 0x02, 0x04, 0x06, 0x82, 0x02});
200
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700201 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5007));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700202 expected =
203 "4 unwind DW_CFA_offset_extended register(258) 4\n"
204 "4 unwind Raw Data: 0x05 0x82 0x02 0x04\n"
205 "4 unwind DW_CFA_restore_extended register(258)\n"
206 "4 unwind Raw Data: 0x06 0x82 0x02\n";
207 ASSERT_EQ(expected, GetFakeLogPrint());
208 ASSERT_EQ("", GetFakeLogBuf());
209}
210
211TYPED_TEST_P(DwarfCfaLogTest, cfa_set_loc) {
212 uint8_t buffer[1 + sizeof(TypeParam)];
213 buffer[0] = 0x1;
214 TypeParam address;
215 std::string raw_data("Raw Data: 0x01 ");
216 std::string address_str;
217 if (std::is_same<TypeParam, uint32_t>::value) {
218 address = 0x81234578U;
219 address_str = "0x81234578";
220 raw_data += "0x78 0x45 0x23 0x81";
221 } else {
222 address = 0x8123456712345678ULL;
223 address_str = "0x8123456712345678";
224 raw_data += "0x78 0x56 0x34 0x12 0x67 0x45 0x23 0x81";
225 }
226 memcpy(&buffer[1], &address, sizeof(address));
227
228 this->memory_.SetMemory(0x50, buffer, sizeof(buffer));
229 ResetLogs();
230
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700231 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700232 std::string expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
233 expected += "4 unwind " + raw_data + "\n";
234 expected += "4 unwind \n";
235 expected += "4 unwind PC " + address_str + "\n";
236 ASSERT_EQ(expected, GetFakeLogPrint());
237 ASSERT_EQ("", GetFakeLogBuf());
238
239 // Check for a set going back.
240 ResetLogs();
241 this->fde_.pc_start = address + 0x10;
242
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700243 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x50, 0x51 + sizeof(TypeParam)));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700244 expected = "4 unwind DW_CFA_set_loc " + address_str + "\n";
245 expected += "4 unwind " + raw_data + "\n";
246 expected += "4 unwind \n";
247 expected += "4 unwind PC " + address_str + "\n";
248 ASSERT_EQ(expected, GetFakeLogPrint());
249 ASSERT_EQ("", GetFakeLogBuf());
250}
251
252TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc) {
253 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x44});
254
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700255 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x201));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700256 std::string expected =
257 "4 unwind DW_CFA_advance_loc 4\n"
258 "4 unwind Raw Data: 0x44\n"
259 "4 unwind \n"
260 "4 unwind PC 0x2010\n";
261 ASSERT_EQ(expected, GetFakeLogPrint());
262 ASSERT_EQ("", GetFakeLogBuf());
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700263}
264
265TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc1) {
266 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x02, 0x04});
267
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700268 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x202));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700269 std::string expected =
270 "4 unwind DW_CFA_advance_loc1 4\n"
271 "4 unwind Raw Data: 0x02 0x04\n"
272 "4 unwind \n"
273 "4 unwind PC 0x2004\n";
274 ASSERT_EQ(expected, GetFakeLogPrint());
275 ASSERT_EQ("", GetFakeLogBuf());
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700276}
277
278TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc2) {
279 this->memory_.SetMemory(0x600, std::vector<uint8_t>{0x03, 0x04, 0x03});
280
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700281 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x600, 0x603));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700282 std::string expected =
283 "4 unwind DW_CFA_advance_loc2 772\n"
284 "4 unwind Raw Data: 0x03 0x04 0x03\n"
285 "4 unwind \n"
286 "4 unwind PC 0x2304\n";
287 ASSERT_EQ(expected, GetFakeLogPrint());
288 ASSERT_EQ("", GetFakeLogBuf());
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700289}
290
291TYPED_TEST_P(DwarfCfaLogTest, cfa_advance_loc4) {
292 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x04, 0x04, 0x03, 0x02, 0x01});
293
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700294 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x505));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700295 std::string expected =
296 "4 unwind DW_CFA_advance_loc4 16909060\n"
297 "4 unwind Raw Data: 0x04 0x04 0x03 0x02 0x01\n"
298 "4 unwind \n"
299 "4 unwind PC 0x1022304\n";
300 ASSERT_EQ(expected, GetFakeLogPrint());
301 ASSERT_EQ("", GetFakeLogBuf());
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700302}
303
304TYPED_TEST_P(DwarfCfaLogTest, cfa_undefined) {
305 this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x07, 0x09});
306
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700307 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa02));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700308 std::string expected =
309 "4 unwind DW_CFA_undefined register(9)\n"
310 "4 unwind Raw Data: 0x07 0x09\n";
311 ASSERT_EQ(expected, GetFakeLogPrint());
312 ASSERT_EQ("", GetFakeLogBuf());
313
314 ResetLogs();
315 dwarf_loc_regs_t cie_loc_regs;
316 this->memory_.SetMemory(0x1a00, std::vector<uint8_t>{0x07, 0x81, 0x01});
317
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700318 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1a00, 0x1a03));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700319 expected =
320 "4 unwind DW_CFA_undefined register(129)\n"
321 "4 unwind Raw Data: 0x07 0x81 0x01\n";
322 ASSERT_EQ(expected, GetFakeLogPrint());
323 ASSERT_EQ("", GetFakeLogBuf());
324}
325
326TYPED_TEST_P(DwarfCfaLogTest, cfa_same) {
327 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x08, 0x7f});
328
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700329 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700330 std::string expected =
331 "4 unwind DW_CFA_same_value register(127)\n"
332 "4 unwind Raw Data: 0x08 0x7f\n";
333 ASSERT_EQ(expected, GetFakeLogPrint());
334 ASSERT_EQ("", GetFakeLogBuf());
335
336 ResetLogs();
337 this->memory_.SetMemory(0x2100, std::vector<uint8_t>{0x08, 0xff, 0x01});
338
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700339 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2100, 0x2103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700340 expected =
341 "4 unwind DW_CFA_same_value register(255)\n"
342 "4 unwind Raw Data: 0x08 0xff 0x01\n";
343 ASSERT_EQ(expected, GetFakeLogPrint());
344 ASSERT_EQ("", GetFakeLogBuf());
345}
346
347TYPED_TEST_P(DwarfCfaLogTest, cfa_register) {
348 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01});
349
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700350 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x303));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700351 std::string expected =
352 "4 unwind DW_CFA_register register(2) register(1)\n"
353 "4 unwind Raw Data: 0x09 0x02 0x01\n";
354 ASSERT_EQ(expected, GetFakeLogPrint());
355 ASSERT_EQ("", GetFakeLogBuf());
356
357 ResetLogs();
358 this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x09, 0xff, 0x01, 0xff, 0x03});
359
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700360 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4305));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700361 expected =
362 "4 unwind DW_CFA_register register(255) register(511)\n"
363 "4 unwind Raw Data: 0x09 0xff 0x01 0xff 0x03\n";
364 ASSERT_EQ(expected, GetFakeLogPrint());
365 ASSERT_EQ("", GetFakeLogBuf());
366}
367
368TYPED_TEST_P(DwarfCfaLogTest, cfa_state) {
369 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x0a});
370
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700371 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x301));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700372
373 std::string expected =
374 "4 unwind DW_CFA_remember_state\n"
375 "4 unwind Raw Data: 0x0a\n";
376 ASSERT_EQ(expected, GetFakeLogPrint());
377 ASSERT_EQ("", GetFakeLogBuf());
378
379 ResetLogs();
380 this->memory_.SetMemory(0x4300, std::vector<uint8_t>{0x0b});
381
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700382 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x4300, 0x4301));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700383
384 expected =
385 "4 unwind DW_CFA_restore_state\n"
386 "4 unwind Raw Data: 0x0b\n";
387 ASSERT_EQ(expected, GetFakeLogPrint());
388 ASSERT_EQ("", GetFakeLogBuf());
389}
390
391TYPED_TEST_P(DwarfCfaLogTest, cfa_state_cfa_offset_restore) {
392 this->memory_.SetMemory(0x3000, std::vector<uint8_t>{0x0a, 0x0e, 0x40, 0x0b});
393
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700394 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x3000, 0x3004));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700395
396 std::string expected =
397 "4 unwind DW_CFA_remember_state\n"
398 "4 unwind Raw Data: 0x0a\n"
399 "4 unwind DW_CFA_def_cfa_offset 64\n"
400 "4 unwind Raw Data: 0x0e 0x40\n"
401 "4 unwind DW_CFA_restore_state\n"
402 "4 unwind Raw Data: 0x0b\n";
403 ASSERT_EQ(expected, GetFakeLogPrint());
404 ASSERT_EQ("", GetFakeLogBuf());
405}
406
407TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa) {
408 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0c, 0x7f, 0x74});
409
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700410 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700411
412 std::string expected =
413 "4 unwind DW_CFA_def_cfa register(127) 116\n"
414 "4 unwind Raw Data: 0x0c 0x7f 0x74\n";
415 ASSERT_EQ(expected, GetFakeLogPrint());
416 ASSERT_EQ("", GetFakeLogBuf());
417
418 ResetLogs();
419 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0c, 0xff, 0x02, 0xf4, 0x04});
420
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700421 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700422
423 expected =
424 "4 unwind DW_CFA_def_cfa register(383) 628\n"
425 "4 unwind Raw Data: 0x0c 0xff 0x02 0xf4 0x04\n";
426 ASSERT_EQ(expected, GetFakeLogPrint());
427 ASSERT_EQ("", GetFakeLogBuf());
428}
429
430TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_sf) {
431 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x12, 0x30, 0x25});
432
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700433 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700434
435 std::string expected =
436 "4 unwind DW_CFA_def_cfa_sf register(48) 37\n"
437 "4 unwind Raw Data: 0x12 0x30 0x25\n";
438 ASSERT_EQ(expected, GetFakeLogPrint());
439 ASSERT_EQ("", GetFakeLogBuf());
440
441 // Test a negative value.
442 ResetLogs();
443 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x12, 0xa3, 0x01, 0xfa, 0x7f});
444
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700445 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x205));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700446
447 expected =
448 "4 unwind DW_CFA_def_cfa_sf register(163) -6\n"
449 "4 unwind Raw Data: 0x12 0xa3 0x01 0xfa 0x7f\n";
450 ASSERT_EQ(expected, GetFakeLogPrint());
451 ASSERT_EQ("", GetFakeLogBuf());
452}
453
454TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_register) {
455 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0d, 0x72});
456
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700457 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700458
459 std::string expected =
460 "4 unwind DW_CFA_def_cfa_register register(114)\n"
461 "4 unwind Raw Data: 0x0d 0x72\n";
462 ASSERT_EQ(expected, GetFakeLogPrint());
463 ASSERT_EQ("", GetFakeLogBuf());
464
465 ResetLogs();
466 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0d, 0xf9, 0x20});
467
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700468 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700469
470 expected =
471 "4 unwind DW_CFA_def_cfa_register register(4217)\n"
472 "4 unwind Raw Data: 0x0d 0xf9 0x20\n";
473 ASSERT_EQ(expected, GetFakeLogPrint());
474 ASSERT_EQ("", GetFakeLogBuf());
475}
476
477TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset) {
478 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0e, 0x59});
479
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700480 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700481
482 std::string expected =
483 "4 unwind DW_CFA_def_cfa_offset 89\n"
484 "4 unwind Raw Data: 0x0e 0x59\n";
485 ASSERT_EQ(expected, GetFakeLogPrint());
486 ASSERT_EQ("", GetFakeLogBuf());
487
488 ResetLogs();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700489 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700490
491 expected =
492 "4 unwind DW_CFA_def_cfa_offset 89\n"
493 "4 unwind Raw Data: 0x0e 0x59\n";
494 ASSERT_EQ(expected, GetFakeLogPrint());
495 ASSERT_EQ("", GetFakeLogBuf());
496
497 ResetLogs();
498 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x0e, 0xd4, 0x0a});
499
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700500 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700501
502 expected =
503 "4 unwind DW_CFA_def_cfa_offset 1364\n"
504 "4 unwind Raw Data: 0x0e 0xd4 0x0a\n";
505 ASSERT_EQ(expected, GetFakeLogPrint());
506 ASSERT_EQ("", GetFakeLogBuf());
507}
508
509TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_offset_sf) {
510 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x13, 0x23});
511
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700512 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700513
514 std::string expected =
515 "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
516 "4 unwind Raw Data: 0x13 0x23\n";
517 ASSERT_EQ(expected, GetFakeLogPrint());
518 ASSERT_EQ("", GetFakeLogBuf());
519
520 ResetLogs();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700521 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x102));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700522
523 expected =
524 "4 unwind DW_CFA_def_cfa_offset_sf 35\n"
525 "4 unwind Raw Data: 0x13 0x23\n";
526 ASSERT_EQ(expected, GetFakeLogPrint());
527 ASSERT_EQ("", GetFakeLogBuf());
528
529 // Negative offset.
530 ResetLogs();
531 this->memory_.SetMemory(0x200, std::vector<uint8_t>{0x13, 0xf6, 0x7f});
532
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700533 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x203));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700534
535 expected =
536 "4 unwind DW_CFA_def_cfa_offset_sf -10\n"
537 "4 unwind Raw Data: 0x13 0xf6 0x7f\n";
538 ASSERT_EQ(expected, GetFakeLogPrint());
539 ASSERT_EQ("", GetFakeLogBuf());
540}
541
542TYPED_TEST_P(DwarfCfaLogTest, cfa_def_cfa_expression) {
543 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x0f, 0x04, 0x01, 0x02, 0x04, 0x05});
544
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700545 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x106));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700546
547 std::string expected =
548 "4 unwind DW_CFA_def_cfa_expression 4\n"
549 "4 unwind Raw Data: 0x0f 0x04 0x01 0x02 0x04 0x05\n"
550 "4 unwind Illegal\n"
551 "4 unwind Raw Data: 0x01\n"
552 "4 unwind Illegal\n"
553 "4 unwind Raw Data: 0x02\n"
554 "4 unwind Illegal\n"
555 "4 unwind Raw Data: 0x04\n"
556 "4 unwind Illegal\n"
557 "4 unwind Raw Data: 0x05\n";
558 ASSERT_EQ(expected, GetFakeLogPrint());
559 ASSERT_EQ("", GetFakeLogBuf());
560
561 ResetLogs();
562 std::vector<uint8_t> ops{0x0f, 0x81, 0x01};
563 expected = "4 unwind Raw Data: 0x0f 0x81 0x01";
564 std::string op_string;
565 for (uint8_t i = 3; i < 132; i++) {
566 ops.push_back(0x05);
567 op_string +=
568 "4 unwind Illegal\n"
569 "4 unwind Raw Data: 0x05\n";
570 expected += " 0x05";
571 if (((i + 1) % 10) == 0) {
572 expected += "\n4 unwind Raw Data:";
573 }
574 }
575 expected += '\n';
576 this->memory_.SetMemory(0x200, ops);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700577 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x284));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700578
579 expected = "4 unwind DW_CFA_def_cfa_expression 129\n" + expected;
580 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
581 ASSERT_EQ("", GetFakeLogBuf());
582}
583
584TYPED_TEST_P(DwarfCfaLogTest, cfa_expression) {
585 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x10, 0x04, 0x02, 0xc0, 0xc1});
586
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700587 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700588
589 std::string expected =
590 "4 unwind DW_CFA_expression register(4) 2\n"
591 "4 unwind Raw Data: 0x10 0x04 0x02 0xc0 0xc1\n"
592 "4 unwind Illegal\n"
593 "4 unwind Raw Data: 0xc0\n"
594 "4 unwind Illegal\n"
595 "4 unwind Raw Data: 0xc1\n";
596 ASSERT_EQ(expected, GetFakeLogPrint());
597 ASSERT_EQ("", GetFakeLogBuf());
598
599 ResetLogs();
600 std::vector<uint8_t> ops{0x10, 0xff, 0x01, 0x82, 0x01};
601 expected = "4 unwind Raw Data: 0x10 0xff 0x01 0x82 0x01";
602 std::string op_string;
603 for (uint8_t i = 5; i < 135; i++) {
604 ops.push_back(0xa0 + (i - 5) % 96);
605 op_string += "4 unwind Illegal\n";
606 op_string += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", ops.back());
607 expected += android::base::StringPrintf(" 0x%02x", ops.back());
608 if (((i + 1) % 10) == 0) {
609 expected += "\n4 unwind Raw Data:";
610 }
611 }
612 expected = "4 unwind DW_CFA_expression register(255) 130\n" + expected + "\n";
613
614 this->memory_.SetMemory(0x200, ops);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700615 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x200, 0x287));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700616
617 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
618 ASSERT_EQ("", GetFakeLogBuf());
619}
620
621TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset) {
622 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x14, 0x45, 0x54});
623
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700624 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700625
626 std::string expected =
627 "4 unwind DW_CFA_val_offset register(69) 84\n"
628 "4 unwind Raw Data: 0x14 0x45 0x54\n";
629 ASSERT_EQ(expected, GetFakeLogPrint());
630 ASSERT_EQ("", GetFakeLogBuf());
631
632 ResetLogs();
633 this->memory_.SetMemory(0x400, std::vector<uint8_t>{0x14, 0xa2, 0x02, 0xb4, 0x05});
634
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700635 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x400, 0x405));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700636
637 expected =
638 "4 unwind DW_CFA_val_offset register(290) 692\n"
639 "4 unwind Raw Data: 0x14 0xa2 0x02 0xb4 0x05\n";
640 ASSERT_EQ(expected, GetFakeLogPrint());
641 ASSERT_EQ("", GetFakeLogBuf());
642}
643
644TYPED_TEST_P(DwarfCfaLogTest, cfa_val_offset_sf) {
645 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x15, 0x56, 0x12});
646
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700647 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x103));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700648
649 std::string expected =
650 "4 unwind DW_CFA_val_offset_sf register(86) 18\n"
651 "4 unwind Raw Data: 0x15 0x56 0x12\n";
652 ASSERT_EQ(expected, GetFakeLogPrint());
653 ASSERT_EQ("", GetFakeLogBuf());
654
655 // Negative value.
656 ResetLogs();
657 this->memory_.SetMemory(0xa00, std::vector<uint8_t>{0x15, 0xff, 0x01, 0xc0, 0x7f});
658
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700659 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xa05));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700660
661 expected =
662 "4 unwind DW_CFA_val_offset_sf register(255) -64\n"
663 "4 unwind Raw Data: 0x15 0xff 0x01 0xc0 0x7f\n";
664 ASSERT_EQ(expected, GetFakeLogPrint());
665 ASSERT_EQ("", GetFakeLogBuf());
666}
667
668TYPED_TEST_P(DwarfCfaLogTest, cfa_val_expression) {
669 this->memory_.SetMemory(0x100, std::vector<uint8_t>{0x16, 0x05, 0x02, 0xb0, 0xb1});
670
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700671 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x100, 0x105));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700672
673 std::string expected =
674 "4 unwind DW_CFA_val_expression register(5) 2\n"
675 "4 unwind Raw Data: 0x16 0x05 0x02 0xb0 0xb1\n"
676 "4 unwind Illegal\n"
677 "4 unwind Raw Data: 0xb0\n"
678 "4 unwind Illegal\n"
679 "4 unwind Raw Data: 0xb1\n";
680 ASSERT_EQ(expected, GetFakeLogPrint());
681 ASSERT_EQ("", GetFakeLogBuf());
682
683 ResetLogs();
684 std::vector<uint8_t> ops{0x16, 0x83, 0x10, 0xa8, 0x01};
685 expected = "4 unwind Raw Data: 0x16 0x83 0x10 0xa8 0x01";
686 std::string op_string;
687 for (uint8_t i = 0; i < 168; i++) {
688 ops.push_back(0xa0 + (i % 96));
689 op_string += "4 unwind Illegal\n";
690 op_string += android::base::StringPrintf("4 unwind Raw Data: 0x%02x\n", ops.back());
691 expected += android::base::StringPrintf(" 0x%02x", ops.back());
692 if (((i + 6) % 10) == 0) {
693 expected += "\n4 unwind Raw Data:";
694 }
695 }
696 expected = "4 unwind DW_CFA_val_expression register(2051) 168\n" + expected + "\n";
697
698 this->memory_.SetMemory(0xa00, ops);
699
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700700 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0xa00, 0xaad));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700701
702 ASSERT_EQ(expected + op_string, GetFakeLogPrint());
703 ASSERT_EQ("", GetFakeLogBuf());
704}
705
706TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_args_size) {
707 this->memory_.SetMemory(0x2000, std::vector<uint8_t>{0x2e, 0x04});
708
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700709 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x2000, 0x2002));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700710
711 std::string expected =
712 "4 unwind DW_CFA_GNU_args_size 4\n"
713 "4 unwind Raw Data: 0x2e 0x04\n";
714 ASSERT_EQ(expected, GetFakeLogPrint());
715 ASSERT_EQ("", GetFakeLogBuf());
716
717 ResetLogs();
718 this->memory_.SetMemory(0x5000, std::vector<uint8_t>{0x2e, 0xa4, 0x80, 0x04});
719
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700720 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x5000, 0x5004));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700721
722 expected =
723 "4 unwind DW_CFA_GNU_args_size 65572\n"
724 "4 unwind Raw Data: 0x2e 0xa4 0x80 0x04\n";
725 ASSERT_EQ(expected, GetFakeLogPrint());
726 ASSERT_EQ("", GetFakeLogBuf());
727}
728
729TYPED_TEST_P(DwarfCfaLogTest, cfa_gnu_negative_offset_extended) {
730 this->memory_.SetMemory(0x500, std::vector<uint8_t>{0x2f, 0x08, 0x10});
731
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700732 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x500, 0x503));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700733
734 std::string expected =
735 "4 unwind DW_CFA_GNU_negative_offset_extended register(8) 16\n"
736 "4 unwind Raw Data: 0x2f 0x08 0x10\n";
737 ASSERT_EQ(expected, GetFakeLogPrint());
738 ASSERT_EQ("", GetFakeLogBuf());
739
740 ResetLogs();
741 this->memory_.SetMemory(0x1500, std::vector<uint8_t>{0x2f, 0x81, 0x02, 0xff, 0x01});
742
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700743 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x1500, 0x1505));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700744
745 expected =
746 "4 unwind DW_CFA_GNU_negative_offset_extended register(257) 255\n"
747 "4 unwind Raw Data: 0x2f 0x81 0x02 0xff 0x01\n";
748 ASSERT_EQ(expected, GetFakeLogPrint());
749 ASSERT_EQ("", GetFakeLogBuf());
750}
751
752TYPED_TEST_P(DwarfCfaLogTest, cfa_register_override) {
753 this->memory_.SetMemory(0x300, std::vector<uint8_t>{0x09, 0x02, 0x01, 0x09, 0x02, 0x04});
754
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700755 ASSERT_TRUE(this->cfa_->Log(0, this->fde_.pc_start, 0x300, 0x306));
Christopher Ferris8642fcb2017-04-24 11:14:39 -0700756
757 std::string expected =
758 "4 unwind DW_CFA_register register(2) register(1)\n"
759 "4 unwind Raw Data: 0x09 0x02 0x01\n"
760 "4 unwind DW_CFA_register register(2) register(4)\n"
761 "4 unwind Raw Data: 0x09 0x02 0x04\n";
762 ASSERT_EQ(expected, GetFakeLogPrint());
763 ASSERT_EQ("", GetFakeLogBuf());
764}
765
766REGISTER_TYPED_TEST_CASE_P(DwarfCfaLogTest, cfa_illegal, cfa_nop, cfa_offset, cfa_offset_extended,
767 cfa_offset_extended_sf, cfa_restore, cfa_restore_extended, cfa_set_loc,
768 cfa_advance_loc, cfa_advance_loc1, cfa_advance_loc2, cfa_advance_loc4,
769 cfa_undefined, cfa_same, cfa_register, cfa_state,
770 cfa_state_cfa_offset_restore, cfa_def_cfa, cfa_def_cfa_sf,
771 cfa_def_cfa_register, cfa_def_cfa_offset, cfa_def_cfa_offset_sf,
772 cfa_def_cfa_expression, cfa_expression, cfa_val_offset,
773 cfa_val_offset_sf, cfa_val_expression, cfa_gnu_args_size,
774 cfa_gnu_negative_offset_extended, cfa_register_override);
775
776typedef ::testing::Types<uint32_t, uint64_t> DwarfCfaLogTestTypes;
777INSTANTIATE_TYPED_TEST_CASE_P(, DwarfCfaLogTest, DwarfCfaLogTestTypes);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700778
779} // namespace unwindstack