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