1 /*
2  * Copyright (C) 2015 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 "dwarf_test.h"
18 
19 #include "dwarf/debug_frame_opcode_writer.h"
20 #include "dwarf/debug_info_entry_writer.h"
21 #include "dwarf/debug_line_opcode_writer.h"
22 #include "dwarf/dwarf_constants.h"
23 #include "dwarf/headers.h"
24 #include "gtest/gtest.h"
25 
26 namespace art HIDDEN {
27 namespace dwarf {
28 
29 // Run the tests only on host since we need objdump.
30 #ifndef ART_TARGET_ANDROID
31 
TEST_F(DwarfTest,DebugFrame)32 TEST_F(DwarfTest, DebugFrame) {
33   const bool is64bit = false;
34 
35   // Pick offset value which would catch Uleb vs Sleb errors.
36   const int offset = 40000;
37   ASSERT_EQ(UnsignedLeb128Size(offset / 4), 2u);
38   ASSERT_EQ(SignedLeb128Size(offset / 4), 3u);
39   const Reg reg(6);
40 
41   // Test the opcodes in the order mentioned in the spec.
42   // There are usually several encoding variations of each opcode.
43   DebugFrameOpCodeWriter<> opcodes;
44   DW_CHECK(".debug_frame contents:");
45   DW_CHECK("FDE");
46   DW_CHECK_NEXT("DWARF32");
47   DW_CHECK_NEXT("DW_CFA_nop:");  // TODO: Why is a nop here.
48   int pc = 0;
49   for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
50     pc += i;
51     opcodes.AdvancePC(pc);
52   }
53   DW_CHECK_NEXT("DW_CFA_advance_loc: 1");
54   DW_CHECK_NEXT("DW_CFA_advance_loc: 63");
55   DW_CHECK_NEXT("DW_CFA_advance_loc1: 64");
56   DW_CHECK_NEXT("DW_CFA_advance_loc1: 255");
57   DW_CHECK_NEXT("DW_CFA_advance_loc2: 256");
58   DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535");
59   DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536");
60   opcodes.DefCFA(reg, offset);
61   DW_CHECK_NEXT("DW_CFA_def_cfa: ESI +40000");
62   opcodes.DefCFA(reg, -offset);
63   DW_CHECK_NEXT("DW_CFA_def_cfa_sf: ESI -40000");
64   opcodes.DefCFARegister(reg);
65   DW_CHECK_NEXT("DW_CFA_def_cfa_register: ESI");
66   opcodes.DefCFAOffset(offset);
67   DW_CHECK_NEXT("DW_CFA_def_cfa_offset: +40000");
68   opcodes.DefCFAOffset(-offset);
69   DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
70   uint8_t expr[] = { /*nop*/ 0x96 };
71   opcodes.DefCFAExpression(expr, arraysize(expr));
72   DW_CHECK_NEXT("DW_CFA_def_cfa_expression: DW_OP_nop");
73   opcodes.Undefined(reg);
74   DW_CHECK_NEXT("DW_CFA_undefined: ESI");
75   opcodes.SameValue(reg);
76   DW_CHECK_NEXT("DW_CFA_same_value: ESI");
77   opcodes.Offset(Reg(0x3F), -offset);
78   DW_CHECK_NEXT("DW_CFA_offset: reg63 -40000");
79   opcodes.Offset(Reg(0x40), -offset);
80   DW_CHECK_NEXT("DW_CFA_offset_extended: reg64 -40000");
81   opcodes.Offset(Reg(0x40), offset);
82   DW_CHECK_NEXT("DW_CFA_offset_extended_sf: reg64 40000");
83   opcodes.ValOffset(reg, -offset);
84   DW_CHECK_NEXT("DW_CFA_val_offset: ESI -40000");
85   opcodes.ValOffset(reg, offset);
86   DW_CHECK_NEXT("DW_CFA_val_offset_sf: ESI 40000");
87   opcodes.Register(reg, Reg(1));
88   DW_CHECK_NEXT("DW_CFA_register: ESI ECX");
89   opcodes.Expression(reg, expr, arraysize(expr));
90   DW_CHECK_NEXT("DW_CFA_expression: ESI DW_OP_nop");
91   opcodes.ValExpression(reg, expr, arraysize(expr));
92   DW_CHECK_NEXT("DW_CFA_val_expression: ESI DW_OP_nop");
93   opcodes.Restore(Reg(0x3F));
94   DW_CHECK_NEXT("DW_CFA_restore: reg63");
95   opcodes.Restore(Reg(0x40));
96   DW_CHECK_NEXT("DW_CFA_restore_extended: reg64");
97   opcodes.Restore(reg);
98   DW_CHECK_NEXT("DW_CFA_restore: ESI");
99   opcodes.RememberState();
100   DW_CHECK_NEXT("DW_CFA_remember_state:");
101   opcodes.RestoreState();
102   DW_CHECK_NEXT("DW_CFA_restore_state:");
103   opcodes.Nop();
104   DW_CHECK_NEXT("DW_CFA_nop:");
105 
106   // Also test helpers.
107   opcodes.DefCFA(Reg(4), 100);  // ESP
108   DW_CHECK_NEXT("DW_CFA_def_cfa: ESP +100");
109   opcodes.AdjustCFAOffset(8);
110   DW_CHECK_NEXT("DW_CFA_def_cfa_offset: +108");
111   opcodes.RelOffset(Reg(0), 0);  // push R0
112   DW_CHECK_NEXT("DW_CFA_offset: EAX -108");
113   opcodes.RelOffset(Reg(1), 4);  // push R1
114   DW_CHECK_NEXT("DW_CFA_offset: ECX -104");
115   opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4);  // push R2 and R5
116   DW_CHECK_NEXT("DW_CFA_offset: EDX -100");
117   DW_CHECK_NEXT("DW_CFA_offset: EBP -96");
118   opcodes.RestoreMany(Reg(2), 1 | (1 << 3));  // pop R2 and R5
119   DW_CHECK_NEXT("DW_CFA_restore: EDX");
120   DW_CHECK_NEXT("DW_CFA_restore: EBP");
121 
122   DebugFrameOpCodeWriter<> initial_opcodes;
123   WriteCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &debug_frame_data_);
124   WriteFDE(is64bit,
125            /* cie_pointer= */ 0,
126            0x01000000,
127            0x01000000,
128            ArrayRef<const uint8_t>(*opcodes.data()),
129            &debug_frame_data_);
130 
131   CheckObjdumpOutput(is64bit, "-debug-frame");
132 }
133 
TEST_F(DwarfTest,DISABLED_DebugFrame64)134 TEST_F(DwarfTest, DISABLED_DebugFrame64) {
135   constexpr bool is64bit = true;
136   DebugFrameOpCodeWriter<> initial_opcodes;
137   WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
138   DebugFrameOpCodeWriter<> opcodes;
139   DW_CHECK(".debug_frame contents:");
140   WriteFDE(is64bit,
141            /* cie_pointer= */ 0,
142            0x0100000000000000,
143            0x0200000000000000,
144            ArrayRef<const uint8_t>(*opcodes.data()),
145            &debug_frame_data_);
146   DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
147 
148   CheckObjdumpOutput(is64bit, "-debug-frame");
149 }
150 
151 // Test x86_64 register mapping. It is the only non-trivial architecture.
152 // ARM and X86 have: dwarf_reg = art_reg + constant.
TEST_F(DwarfTest,x86_64_RegisterMapping)153 TEST_F(DwarfTest, x86_64_RegisterMapping) {
154   constexpr bool is64bit = true;
155   DebugFrameOpCodeWriter<> opcodes;
156   DW_CHECK(".debug_frame contents:");
157   for (int i = 0; i < 16; i++) {
158     opcodes.RelOffset(Reg::X86_64Core(i), 0);
159   }
160   DW_CHECK("FDE");
161   DW_CHECK_NEXT("DWARF32");
162   DW_CHECK_NEXT("DW_CFA_nop:");  // TODO: Why is a nop here.
163   DW_CHECK_NEXT("DW_CFA_offset: RAX 0");
164   DW_CHECK_NEXT("DW_CFA_offset: RCX 0");
165   DW_CHECK_NEXT("DW_CFA_offset: RDX 0");
166   DW_CHECK_NEXT("DW_CFA_offset: RBX 0");
167   DW_CHECK_NEXT("DW_CFA_offset: RSP 0");
168   DW_CHECK_NEXT("DW_CFA_offset: RBP 0");
169   DW_CHECK_NEXT("DW_CFA_offset: RSI 0");
170   DW_CHECK_NEXT("DW_CFA_offset: RDI 0");
171   DW_CHECK_NEXT("DW_CFA_offset: R8 0");
172   DW_CHECK_NEXT("DW_CFA_offset: R9 0");
173   DW_CHECK_NEXT("DW_CFA_offset: R10 0");
174   DW_CHECK_NEXT("DW_CFA_offset: R11 0");
175   DW_CHECK_NEXT("DW_CFA_offset: R12 0");
176   DW_CHECK_NEXT("DW_CFA_offset: R13 0");
177   DW_CHECK_NEXT("DW_CFA_offset: R14 0");
178   DW_CHECK_NEXT("DW_CFA_offset: R15 0");
179 
180   DebugFrameOpCodeWriter<> initial_opcodes;
181   WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
182   WriteFDE(is64bit,
183            /* cie_pointer= */ 0,
184            0x0100000000000000,
185            0x0200000000000000,
186            ArrayRef<const uint8_t>(*opcodes.data()),
187            &debug_frame_data_);
188 
189   CheckObjdumpOutput(is64bit, "-debug-frame");
190 }
191 
TEST_F(DwarfTest,DebugLine)192 TEST_F(DwarfTest, DebugLine) {
193   const bool is64bit = false;
194   const int code_factor_bits = 1;
195   DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
196   DW_CHECK(".debug_line contents:");
197 
198   std::vector<std::string> include_directories;
199   include_directories.push_back("/path/to/source");
200   DW_CHECK("include_directories[  1] = \"/path/to/source\"");
201 
202   std::vector<FileEntry> files {
203     { "file0.c", 0, 1000, 2000 },
204     { "file1.c", 1, 1000, 2000 },
205     { "file2.c", 1, 1000, 2000 },
206   };
207   DW_CHECK_NEXT("file_names[  1]:");
208   DW_CHECK_NEXT("           name: \"file0.c\"");
209   DW_CHECK_NEXT("      dir_index: 0");
210   DW_CHECK_NEXT("       mod_time: 0x000003e8");
211   DW_CHECK_NEXT("         length: 0x000007d0");
212   DW_CHECK_NEXT("file_names[  2]:");
213   DW_CHECK_NEXT("           name: \"file1.c\"");
214   DW_CHECK_NEXT("      dir_index: 1");
215   DW_CHECK_NEXT("       mod_time: 0x000003e8");
216   DW_CHECK_NEXT("         length: 0x000007d0");
217   DW_CHECK_NEXT("file_names[  3]:");
218   DW_CHECK_NEXT("           name: \"file2.c\"");
219   DW_CHECK_NEXT("      dir_index: 1");
220   DW_CHECK_NEXT("       mod_time: 0x000003e8");
221   DW_CHECK_NEXT("         length: 0x000007d0");
222 
223   opcodes.SetAddress(0x01000000);
224   opcodes.SetIsStmt(true);
225   opcodes.AddRow();
226   opcodes.AdvancePC(0x01000100);
227   opcodes.SetFile(2);
228   opcodes.AdvanceLine(3);
229   opcodes.SetColumn(4);
230   opcodes.SetIsStmt(false);
231   opcodes.SetBasicBlock();
232   opcodes.SetPrologueEnd();
233   opcodes.SetEpilogueBegin();
234   opcodes.SetISA(5);
235   opcodes.EndSequence();
236   opcodes.DefineFile("file.c", 0, 1000, 2000);
237   DW_CHECK_NEXT("Address            Line   Column File   ISA Discriminator OpIndex Flags");
238   DW_CHECK_NEXT("------------------ ------ ------ ------ --- ------------- ------- -------------");
239   DW_CHECK_NEXT("0x0000000001000000      1      0      1   0             0       0  is_stmt");
240   DW_CHECK_NEXT("0x0000000001000100      3      4      2   5             0       0  basic_block prologue_end epilogue_begin end_sequence");
241 
242   WriteDebugLineTable(include_directories, files, opcodes, &debug_line_data_);
243 
244   CheckObjdumpOutput(is64bit, "-debug-line");
245 }
246 
247 // DWARF has special one byte codes which advance PC and line at the same time.
TEST_F(DwarfTest,DebugLineSpecialOpcodes)248 TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
249   const bool is64bit = false;
250   const int code_factor_bits = 1;
251   uint32_t pc = 0x01000000;
252   int line = 1;
253   DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
254   opcodes.SetAddress(pc);
255   size_t num_rows = 0;
256   DW_CHECK(".debug_line contents:");
257   DW_CHECK("file_names[  1]:");
258   DW_CHECK("           name: \"file.c\"");
259   DW_CHECK("Address            Line   Column File   ISA Discriminator OpIndex Flags");
260   DW_CHECK("------------------ ------ ------ ------ --- ------------- ------- -------------");
261   for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
262     for (int line_delta = 16; line_delta >= -16; --line_delta) {
263       pc += addr_delta;
264       line += line_delta;
265       opcodes.AddRow(pc, line);
266       num_rows++;
267       ASSERT_EQ(opcodes.CurrentAddress(), pc);
268       ASSERT_EQ(opcodes.CurrentLine(), line);
269       char expected[1024];
270       sprintf(expected, "0x%016x %6i      0      1   0             0       0", pc, line);
271       DW_CHECK_NEXT(expected);
272     }
273   }
274   opcodes.EndSequence();
275   EXPECT_LT(opcodes.data()->size(), num_rows * 3);
276 
277   std::vector<std::string> directories;
278   std::vector<FileEntry> files = { { "file.c", 0, 1000, 2000 } };
279   WriteDebugLineTable(directories, files, opcodes, &debug_line_data_);
280 
281   CheckObjdumpOutput(is64bit, "-debug-line");
282 }
283 
TEST_F(DwarfTest,DebugInfo)284 TEST_F(DwarfTest, DebugInfo) {
285   constexpr bool is64bit = false;
286 
287   DebugAbbrevWriter<> debug_abbrev(&debug_abbrev_data_);
288   DW_CHECK(".debug_abbrev contents:");
289   DW_CHECK_NEXT("Abbrev table for offset: 0x00000000");
290   DW_CHECK_NEXT("[1] DW_TAG_compile_unit DW_CHILDREN_yes");
291   DW_CHECK_NEXT(" DW_AT_producer DW_FORM_strp");
292   DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
293   DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
294   DW_CHECK_NEXT("[2] DW_TAG_subprogram DW_CHILDREN_no");
295   DW_CHECK_NEXT(" DW_AT_name DW_FORM_strp");
296   DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
297   DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
298   DW_CHECK_NEXT("[3] DW_TAG_compile_unit DW_CHILDREN_no");
299 
300   DebugInfoEntryWriter<> info(is64bit, &debug_abbrev);
301   DW_CHECK(".debug_info contents:");
302   info.StartTag(dwarf::DW_TAG_compile_unit);
303   DW_CHECK_NEXT("Compile Unit: length = 0x00000030, format = DWARF32, version = 0x0004, abbr_offset = 0x0000, addr_size = 0x04");
304   DW_CHECK_NEXT("DW_TAG_compile_unit");
305   info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
306   DW_CHECK_NEXT("  DW_AT_producer (\"Compiler name\")");
307   info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
308   DW_CHECK_NEXT("  DW_AT_low_pc (0x01000000)");
309   info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
310   DW_CHECK_NEXT("  DW_AT_high_pc (0x02000000)");
311   info.StartTag(dwarf::DW_TAG_subprogram);
312   DW_CHECK_NEXT("  DW_TAG_subprogram");
313   info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
314   DW_CHECK_NEXT("    DW_AT_name (\"Foo\")");
315   info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
316   DW_CHECK_NEXT("    DW_AT_low_pc (0x01010000)");
317   info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
318   DW_CHECK_NEXT("    DW_AT_high_pc (0x01020000)");
319   info.EndTag();  // DW_TAG_subprogram
320   info.StartTag(dwarf::DW_TAG_subprogram);
321   DW_CHECK_NEXT("  DW_TAG_subprogram");
322   info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
323   DW_CHECK_NEXT("    DW_AT_name (\"Bar\")");
324   info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
325   DW_CHECK_NEXT("    DW_AT_low_pc (0x01020000)");
326   info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
327   DW_CHECK_NEXT("    DW_AT_high_pc (0x01030000)");
328   info.EndTag();  // DW_TAG_subprogram
329   info.EndTag();  // DW_TAG_compile_unit
330   DW_CHECK_NEXT("  NULL");
331   // Test that previous list was properly terminated and empty children.
332   info.StartTag(dwarf::DW_TAG_compile_unit);
333   info.EndTag();  // DW_TAG_compile_unit
334 
335   dwarf::WriteDebugInfoCU(/* debug_abbrev_offset= */ 0, info, &debug_info_data_);
336 
337   CheckObjdumpOutput(is64bit, "-debug-info -debug-abbrev");
338 }
339 
340 #endif  // ART_TARGET_ANDROID
341 
342 }  // namespace dwarf
343 }  // namespace art
344