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