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