xref: /aosp_15_r20/art/compiler/cfi_test.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_CFI_TEST_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_CFI_TEST_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <memory>
21*795d594fSAndroid Build Coastguard Worker #include <sstream>
22*795d594fSAndroid Build Coastguard Worker #include <vector>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
27*795d594fSAndroid Build Coastguard Worker #include "debug/dwarf/dwarf_test.h"
28*795d594fSAndroid Build Coastguard Worker #include "disassembler.h"
29*795d594fSAndroid Build Coastguard Worker #include "dwarf/dwarf_constants.h"
30*795d594fSAndroid Build Coastguard Worker #include "dwarf/headers.h"
31*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
32*795d594fSAndroid Build Coastguard Worker #include "thread.h"
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker class CFITest : public dwarf::DwarfTest {
37*795d594fSAndroid Build Coastguard Worker  public:
GenerateExpected(FILE * f,InstructionSet isa,const char * isa_str,ArrayRef<const uint8_t> actual_asm,ArrayRef<const uint8_t> actual_cfi)38*795d594fSAndroid Build Coastguard Worker   void GenerateExpected(FILE* f, InstructionSet isa, const char* isa_str,
39*795d594fSAndroid Build Coastguard Worker                         ArrayRef<const uint8_t> actual_asm,
40*795d594fSAndroid Build Coastguard Worker                         ArrayRef<const uint8_t> actual_cfi) {
41*795d594fSAndroid Build Coastguard Worker     std::vector<std::string> lines;
42*795d594fSAndroid Build Coastguard Worker     // Print the raw bytes.
43*795d594fSAndroid Build Coastguard Worker     fprintf(f, "static constexpr uint8_t expected_asm_%s[] = {", isa_str);
44*795d594fSAndroid Build Coastguard Worker     HexDump(f, actual_asm);
45*795d594fSAndroid Build Coastguard Worker     fprintf(f, "\n};\n");
46*795d594fSAndroid Build Coastguard Worker     fprintf(f, "static constexpr uint8_t expected_cfi_%s[] = {", isa_str);
47*795d594fSAndroid Build Coastguard Worker     HexDump(f, actual_cfi);
48*795d594fSAndroid Build Coastguard Worker     fprintf(f, "\n};\n");
49*795d594fSAndroid Build Coastguard Worker     // Pretty-print CFI opcodes.
50*795d594fSAndroid Build Coastguard Worker     constexpr bool is64bit = false;
51*795d594fSAndroid Build Coastguard Worker     dwarf::DebugFrameOpCodeWriter<> initial_opcodes;
52*795d594fSAndroid Build Coastguard Worker     dwarf::WriteCIE(is64bit, dwarf::Reg(8), initial_opcodes, &debug_frame_data_);
53*795d594fSAndroid Build Coastguard Worker     std::vector<uintptr_t> debug_frame_patches;
54*795d594fSAndroid Build Coastguard Worker     dwarf::WriteFDE(is64bit,
55*795d594fSAndroid Build Coastguard Worker                     /* cie_pointer= */ 0,
56*795d594fSAndroid Build Coastguard Worker                     /* code_address= */ 0,
57*795d594fSAndroid Build Coastguard Worker                     actual_asm.size(),
58*795d594fSAndroid Build Coastguard Worker                     actual_cfi,
59*795d594fSAndroid Build Coastguard Worker                     &debug_frame_data_);
60*795d594fSAndroid Build Coastguard Worker     ReformatCfi(Objdump(false, "-W"), &lines);
61*795d594fSAndroid Build Coastguard Worker     // Pretty-print assembly.
62*795d594fSAndroid Build Coastguard Worker     const uint8_t* asm_base = actual_asm.data();
63*795d594fSAndroid Build Coastguard Worker     const uint8_t* asm_end = asm_base + actual_asm.size();
64*795d594fSAndroid Build Coastguard Worker     auto* opts = new DisassemblerOptions(false,
65*795d594fSAndroid Build Coastguard Worker                                          asm_base,
66*795d594fSAndroid Build Coastguard Worker                                          asm_end,
67*795d594fSAndroid Build Coastguard Worker                                          true,
68*795d594fSAndroid Build Coastguard Worker                                          is64bit
69*795d594fSAndroid Build Coastguard Worker                                              ? &Thread::DumpThreadOffset<PointerSize::k64>
70*795d594fSAndroid Build Coastguard Worker                                              : &Thread::DumpThreadOffset<PointerSize::k32>);
71*795d594fSAndroid Build Coastguard Worker     std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts));
72*795d594fSAndroid Build Coastguard Worker     std::stringstream stream;
73*795d594fSAndroid Build Coastguard Worker     const uint8_t* base = actual_asm.data() + (isa == InstructionSet::kThumb2 ? 1 : 0);
74*795d594fSAndroid Build Coastguard Worker     disasm->Dump(stream, base, base + actual_asm.size());
75*795d594fSAndroid Build Coastguard Worker     ReformatAsm(&stream, &lines);
76*795d594fSAndroid Build Coastguard Worker     // Print CFI and assembly interleaved.
77*795d594fSAndroid Build Coastguard Worker     std::stable_sort(lines.begin(), lines.end(), CompareByAddress);
78*795d594fSAndroid Build Coastguard Worker     for (const std::string& line : lines) {
79*795d594fSAndroid Build Coastguard Worker       fprintf(f, "// %s\n", line.c_str());
80*795d594fSAndroid Build Coastguard Worker     }
81*795d594fSAndroid Build Coastguard Worker     fprintf(f, "\n");
82*795d594fSAndroid Build Coastguard Worker   }
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker  private:
85*795d594fSAndroid Build Coastguard Worker   // Helper - get offset just past the end of given string.
FindEndOf(const std::string & str,const char * substr)86*795d594fSAndroid Build Coastguard Worker   static size_t FindEndOf(const std::string& str, const char* substr) {
87*795d594fSAndroid Build Coastguard Worker     size_t pos = str.find(substr);
88*795d594fSAndroid Build Coastguard Worker     CHECK_NE(std::string::npos, pos);
89*795d594fSAndroid Build Coastguard Worker     return pos + strlen(substr);
90*795d594fSAndroid Build Coastguard Worker   }
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker   // Spit to lines and remove raw instruction bytes.
ReformatAsm(std::stringstream * stream,std::vector<std::string> * output)93*795d594fSAndroid Build Coastguard Worker   static void ReformatAsm(std::stringstream* stream,
94*795d594fSAndroid Build Coastguard Worker                           std::vector<std::string>* output) {
95*795d594fSAndroid Build Coastguard Worker     std::string line;
96*795d594fSAndroid Build Coastguard Worker     while (std::getline(*stream, line)) {
97*795d594fSAndroid Build Coastguard Worker       line = line.substr(0, FindEndOf(line, ": ")) +
98*795d594fSAndroid Build Coastguard Worker              line.substr(FindEndOf(line, "\t"));
99*795d594fSAndroid Build Coastguard Worker       size_t pos;
100*795d594fSAndroid Build Coastguard Worker       while ((pos = line.find("  ")) != std::string::npos) {
101*795d594fSAndroid Build Coastguard Worker         line = line.replace(pos, 2, " ");
102*795d594fSAndroid Build Coastguard Worker       }
103*795d594fSAndroid Build Coastguard Worker       while (!line.empty() && line.back() == ' ') {
104*795d594fSAndroid Build Coastguard Worker         line.pop_back();
105*795d594fSAndroid Build Coastguard Worker       }
106*795d594fSAndroid Build Coastguard Worker       output->push_back(line);
107*795d594fSAndroid Build Coastguard Worker     }
108*795d594fSAndroid Build Coastguard Worker   }
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker   // Find interesting parts of objdump output and prefix the lines with address.
ReformatCfi(const std::vector<std::string> & lines,std::vector<std::string> * output)111*795d594fSAndroid Build Coastguard Worker   static void ReformatCfi(const std::vector<std::string>& lines,
112*795d594fSAndroid Build Coastguard Worker                           std::vector<std::string>* output) {
113*795d594fSAndroid Build Coastguard Worker     std::string address;
114*795d594fSAndroid Build Coastguard Worker     for (const std::string& line : lines) {
115*795d594fSAndroid Build Coastguard Worker       if (line.find("DW_CFA_nop") != std::string::npos) {
116*795d594fSAndroid Build Coastguard Worker         // Ignore.
117*795d594fSAndroid Build Coastguard Worker       } else if (line.find("DW_CFA_advance_loc") != std::string::npos) {
118*795d594fSAndroid Build Coastguard Worker         // The last 8 characters are the address.
119*795d594fSAndroid Build Coastguard Worker         address = "0x" + line.substr(line.size() - 8);
120*795d594fSAndroid Build Coastguard Worker       } else if (line.find("DW_CFA_") != std::string::npos) {
121*795d594fSAndroid Build Coastguard Worker         std::string new_line(line);
122*795d594fSAndroid Build Coastguard Worker         // "bad register" warning is caused by always using host (x86) objdump.
123*795d594fSAndroid Build Coastguard Worker         const char* bad_reg = "bad register: ";
124*795d594fSAndroid Build Coastguard Worker         size_t pos;
125*795d594fSAndroid Build Coastguard Worker         if ((pos = new_line.find(bad_reg)) != std::string::npos) {
126*795d594fSAndroid Build Coastguard Worker           new_line = new_line.replace(pos, strlen(bad_reg), "");
127*795d594fSAndroid Build Coastguard Worker         }
128*795d594fSAndroid Build Coastguard Worker         // Remove register names in parentheses since they have x86 names.
129*795d594fSAndroid Build Coastguard Worker         if ((pos = new_line.find(" (")) != std::string::npos) {
130*795d594fSAndroid Build Coastguard Worker           new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, "");
131*795d594fSAndroid Build Coastguard Worker         }
132*795d594fSAndroid Build Coastguard Worker         // Use the .cfi_ prefix.
133*795d594fSAndroid Build Coastguard Worker         new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_"));
134*795d594fSAndroid Build Coastguard Worker         output->push_back(ART_FORMAT("{}: {}", address, new_line));
135*795d594fSAndroid Build Coastguard Worker       }
136*795d594fSAndroid Build Coastguard Worker     }
137*795d594fSAndroid Build Coastguard Worker   }
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker   // Compare strings by the address prefix.
CompareByAddress(const std::string & lhs,const std::string & rhs)140*795d594fSAndroid Build Coastguard Worker   static bool CompareByAddress(const std::string& lhs, const std::string& rhs) {
141*795d594fSAndroid Build Coastguard Worker     EXPECT_EQ(lhs[10], ':');
142*795d594fSAndroid Build Coastguard Worker     EXPECT_EQ(rhs[10], ':');
143*795d594fSAndroid Build Coastguard Worker     return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0;
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   // Pretty-print byte array.  12 bytes per line.
HexDump(FILE * f,ArrayRef<const uint8_t> data)147*795d594fSAndroid Build Coastguard Worker   static void HexDump(FILE* f, ArrayRef<const uint8_t> data) {
148*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i < data.size(); i++) {
149*795d594fSAndroid Build Coastguard Worker       fprintf(f, i % 12 == 0 ? "\n    " : " ");  // Whitespace.
150*795d594fSAndroid Build Coastguard Worker       fprintf(f, "0x%02X,", data[i]);
151*795d594fSAndroid Build Coastguard Worker     }
152*795d594fSAndroid Build Coastguard Worker   }
153*795d594fSAndroid Build Coastguard Worker };
154*795d594fSAndroid Build Coastguard Worker 
155*795d594fSAndroid Build Coastguard Worker }  // namespace art
156*795d594fSAndroid Build Coastguard Worker 
157*795d594fSAndroid Build Coastguard Worker #endif  // ART_COMPILER_CFI_TEST_H_
158