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 #ifndef ART_COMPILER_CFI_TEST_H_
18 #define ART_COMPILER_CFI_TEST_H_
19 
20 #include <memory>
21 #include <sstream>
22 #include <vector>
23 
24 #include "arch/instruction_set.h"
25 #include "base/enums.h"
26 #include "debug/dwarf/dwarf_test.h"
27 #include "disassembler.h"
28 #include "dwarf/dwarf_constants.h"
29 #include "dwarf/headers.h"
30 #include "gtest/gtest.h"
31 #include "thread.h"
32 
33 namespace art {
34 
35 class CFITest : public dwarf::DwarfTest {
36  public:
GenerateExpected(FILE * f,InstructionSet isa,const char * isa_str,ArrayRef<const uint8_t> actual_asm,ArrayRef<const uint8_t> actual_cfi)37   void GenerateExpected(FILE* f, InstructionSet isa, const char* isa_str,
38                         ArrayRef<const uint8_t> actual_asm,
39                         ArrayRef<const uint8_t> actual_cfi) {
40     std::vector<std::string> lines;
41     // Print the raw bytes.
42     fprintf(f, "static constexpr uint8_t expected_asm_%s[] = {", isa_str);
43     HexDump(f, actual_asm);
44     fprintf(f, "\n};\n");
45     fprintf(f, "static constexpr uint8_t expected_cfi_%s[] = {", isa_str);
46     HexDump(f, actual_cfi);
47     fprintf(f, "\n};\n");
48     // Pretty-print CFI opcodes.
49     constexpr bool is64bit = false;
50     dwarf::DebugFrameOpCodeWriter<> initial_opcodes;
51     dwarf::WriteCIE(is64bit, dwarf::Reg(8), initial_opcodes, &debug_frame_data_);
52     std::vector<uintptr_t> debug_frame_patches;
53     dwarf::WriteFDE(is64bit,
54                     /* cie_pointer= */ 0,
55                     /* code_address= */ 0,
56                     actual_asm.size(),
57                     actual_cfi,
58                     &debug_frame_data_);
59     ReformatCfi(Objdump(false, "-W"), &lines);
60     // Pretty-print assembly.
61     const uint8_t* asm_base = actual_asm.data();
62     const uint8_t* asm_end = asm_base + actual_asm.size();
63     auto* opts = new DisassemblerOptions(false,
64                                          asm_base,
65                                          asm_end,
66                                          true,
67                                          is64bit
68                                              ? &Thread::DumpThreadOffset<PointerSize::k64>
69                                              : &Thread::DumpThreadOffset<PointerSize::k32>);
70     std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts));
71     std::stringstream stream;
72     const uint8_t* base = actual_asm.data() + (isa == InstructionSet::kThumb2 ? 1 : 0);
73     disasm->Dump(stream, base, base + actual_asm.size());
74     ReformatAsm(&stream, &lines);
75     // Print CFI and assembly interleaved.
76     std::stable_sort(lines.begin(), lines.end(), CompareByAddress);
77     for (const std::string& line : lines) {
78       fprintf(f, "// %s\n", line.c_str());
79     }
80     fprintf(f, "\n");
81   }
82 
83  private:
84   // Helper - get offset just past the end of given string.
FindEndOf(const std::string & str,const char * substr)85   static size_t FindEndOf(const std::string& str, const char* substr) {
86     size_t pos = str.find(substr);
87     CHECK_NE(std::string::npos, pos);
88     return pos + strlen(substr);
89   }
90 
91   // Spit to lines and remove raw instruction bytes.
ReformatAsm(std::stringstream * stream,std::vector<std::string> * output)92   static void ReformatAsm(std::stringstream* stream,
93                           std::vector<std::string>* output) {
94     std::string line;
95     while (std::getline(*stream, line)) {
96       line = line.substr(0, FindEndOf(line, ": ")) +
97              line.substr(FindEndOf(line, "\t"));
98       size_t pos;
99       while ((pos = line.find("  ")) != std::string::npos) {
100         line = line.replace(pos, 2, " ");
101       }
102       while (!line.empty() && line.back() == ' ') {
103         line.pop_back();
104       }
105       output->push_back(line);
106     }
107   }
108 
109   // Find interesting parts of objdump output and prefix the lines with address.
ReformatCfi(const std::vector<std::string> & lines,std::vector<std::string> * output)110   static void ReformatCfi(const std::vector<std::string>& lines,
111                           std::vector<std::string>* output) {
112     std::string address;
113     for (const std::string& line : lines) {
114       if (line.find("DW_CFA_nop") != std::string::npos) {
115         // Ignore.
116       } else if (line.find("DW_CFA_advance_loc") != std::string::npos) {
117         // The last 8 characters are the address.
118         address = "0x" + line.substr(line.size() - 8);
119       } else if (line.find("DW_CFA_") != std::string::npos) {
120         std::string new_line(line);
121         // "bad register" warning is caused by always using host (x86) objdump.
122         const char* bad_reg = "bad register: ";
123         size_t pos;
124         if ((pos = new_line.find(bad_reg)) != std::string::npos) {
125           new_line = new_line.replace(pos, strlen(bad_reg), "");
126         }
127         // Remove register names in parentheses since they have x86 names.
128         if ((pos = new_line.find(" (")) != std::string::npos) {
129           new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, "");
130         }
131         // Use the .cfi_ prefix.
132         new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_"));
133         output->push_back(address + ": " + new_line);
134       }
135     }
136   }
137 
138   // Compare strings by the address prefix.
CompareByAddress(const std::string & lhs,const std::string & rhs)139   static bool CompareByAddress(const std::string& lhs, const std::string& rhs) {
140     EXPECT_EQ(lhs[10], ':');
141     EXPECT_EQ(rhs[10], ':');
142     return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0;
143   }
144 
145   // Pretty-print byte array.  12 bytes per line.
HexDump(FILE * f,ArrayRef<const uint8_t> data)146   static void HexDump(FILE* f, ArrayRef<const uint8_t> data) {
147     for (size_t i = 0; i < data.size(); i++) {
148       fprintf(f, i % 12 == 0 ? "\n    " : " ");  // Whitespace.
149       fprintf(f, "0x%02X,", data[i]);
150     }
151   }
152 };
153 
154 }  // namespace art
155 
156 #endif  // ART_COMPILER_CFI_TEST_H_
157