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