Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_UTILS_ASSEMBLER_TEST_BASE_H_ |
| 18 | #define ART_COMPILER_UTILS_ASSEMBLER_TEST_BASE_H_ |
| 19 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 21 | #include <cstdio> |
| 22 | #include <cstdlib> |
| 23 | #include <fstream> |
| 24 | #include <iterator> |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 26 | #include "android-base/strings.h" |
| 27 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 28 | #include "base/os.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/utils.h" |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 30 | #include "common_runtime_test.h" // For ScratchDir. |
| 31 | #include "elf/elf_builder.h" |
| 32 | #include "elf/elf_debug_reader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 33 | #include "exec_utils.h" |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 34 | #include "stream/file_output_stream.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 35 | |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 36 | namespace art { |
| 37 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 38 | // If you want to take a look at the differences between the ART assembler and clang, |
| 39 | // set this flag to true. The disassembled files will then remain in the tmp directory. |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 40 | static constexpr bool kKeepDisassembledFiles = false; |
| 41 | |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 42 | // We put this into a class as gtests are self-contained, so this helper needs to be in an h-file. |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 43 | class AssemblerTestBase : public testing::Test { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 44 | public: |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 45 | AssemblerTestBase() {} |
| 46 | |
| 47 | void SetUp() override { |
| 48 | // Fake a runtime test for ScratchDir. |
| 49 | CommonArtTest::SetUpAndroidRootEnvVars(); |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 50 | CommonRuntimeTest::SetUpAndroidDataDir(android_data_); |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 51 | scratch_dir_.emplace(/*keep_files=*/ kKeepDisassembledFiles); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 52 | } |
| 53 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 54 | void TearDown() override { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 55 | // We leave temporaries in case this failed so we can debug issues. |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 56 | CommonRuntimeTest::TearDownAndroidDataDir(android_data_, false); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // This is intended to be run as a test. |
| 60 | bool CheckTools() { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 61 | for (auto cmd : { GetAssemblerCommand()[0], GetDisassemblerCommand()[0] }) { |
| 62 | if (!OS::FileExists(cmd.c_str())) { |
| 63 | LOG(ERROR) << "Could not find " << cmd; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 66 | } |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | |
| 70 | // Driver() assembles and compares the results. If the results are not equal and we have a |
| 71 | // disassembler, disassemble both and check whether they have the same mnemonics (in which case |
| 72 | // we just warn). |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 73 | void Driver(const std::vector<uint8_t>& art_code, |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 74 | const std::string& assembly_text, |
| 75 | const std::string& test_name) { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 76 | ASSERT_NE(assembly_text.length(), 0U) << "Empty assembly"; |
| 77 | InstructionSet isa = GetIsa(); |
| 78 | auto test_path = [&](const char* ext) { return scratch_dir_->GetPath() + test_name + ext; }; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 79 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 80 | // Create file containing the reference source code. |
| 81 | std::string ref_asm_file = test_path(".ref.S"); |
| 82 | WriteFile(ref_asm_file, assembly_text.data(), assembly_text.size()); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 83 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 84 | // Assemble reference object file. |
| 85 | std::string ref_obj_file = test_path(".ref.o"); |
| 86 | ASSERT_TRUE(Assemble(ref_asm_file.c_str(), ref_obj_file.c_str())); |
| 87 | |
| 88 | // Read the code produced by assembler from the ELF file. |
| 89 | std::vector<uint8_t> ref_code; |
| 90 | if (Is64BitInstructionSet(isa)) { |
| 91 | ReadElf</*IsElf64=*/true>(ref_obj_file, &ref_code); |
| 92 | } else { |
| 93 | ReadElf</*IsElf64=*/false>(ref_obj_file, &ref_code); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 94 | } |
| 95 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 96 | // Compare the ART generated code to the expected reference code. |
| 97 | if (art_code == ref_code) { |
| 98 | return; // Success! |
| 99 | } |
| 100 | |
| 101 | // Create ELF file containing the ART code. |
| 102 | std::string art_obj_file = test_path(".art.o"); |
| 103 | if (Is64BitInstructionSet(isa)) { |
| 104 | WriteElf</*IsElf64=*/true>(art_obj_file, isa, art_code); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 105 | } else { |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 106 | WriteElf</*IsElf64=*/false>(art_obj_file, isa, art_code); |
| 107 | } |
| 108 | |
| 109 | // Disassemble both object files, and check that the outputs match. |
| 110 | std::string art_disassembly; |
| 111 | ASSERT_TRUE(Disassemble(art_obj_file, &art_disassembly)); |
| 112 | art_disassembly = Replace(art_disassembly, art_obj_file, test_path("<extension-redacted>")); |
| 113 | std::string ref_disassembly; |
| 114 | ASSERT_TRUE(Disassemble(ref_obj_file, &ref_disassembly)); |
| 115 | ref_disassembly = Replace(ref_disassembly, ref_obj_file, test_path("<extension-redacted>")); |
| 116 | ASSERT_EQ(art_disassembly, ref_disassembly) << "Outputs (and disassembly) not identical."; |
| 117 | |
| 118 | // ART produced different (but valid) code than the reference assembler, report it. |
| 119 | if (art_code.size() > ref_code.size()) { |
| 120 | EXPECT_TRUE(false) << "ART code is larger then the reference code, but the disassembly" |
| 121 | "of machine code is equal: this means that ART is generating sub-optimal encoding! " |
| 122 | "ART code size=" << art_code.size() << ", reference code size=" << ref_code.size(); |
| 123 | } else if (art_code.size() < ref_code.size()) { |
| 124 | EXPECT_TRUE(false) << "ART code is smaller than the reference code. Too good to be true?"; |
| 125 | } else { |
| 126 | LOG(INFO) << "Reference assembler chose a different encoding than ART (of the same size)"; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | protected: |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 131 | virtual InstructionSet GetIsa() = 0; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 132 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 133 | std::string FindTool(const std::string& tool_name) { |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 134 | return CommonArtTest::GetAndroidTool(tool_name.c_str(), GetIsa()); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 135 | } |
| 136 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 137 | virtual std::vector<std::string> GetAssemblerCommand() { |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 138 | InstructionSet isa = GetIsa(); |
| 139 | switch (isa) { |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 140 | case InstructionSet::kX86: |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 141 | return {FindTool("clang"), "--compile", "-target", "i386-linux-gnu"}; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 142 | case InstructionSet::kX86_64: |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 143 | return {FindTool("clang"), "--compile", "-target", "x86_64-linux-gnu"}; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 144 | default: |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 145 | LOG(FATAL) << "Unknown instruction set: " << isa; |
| 146 | UNREACHABLE(); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 147 | } |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 148 | } |
| 149 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 150 | virtual std::vector<std::string> GetDisassemblerCommand() { |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 151 | switch (GetIsa()) { |
| 152 | case InstructionSet::kThumb2: |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 153 | return {FindTool("llvm-objdump"), "--disassemble", "-triple", "thumbv7a-linux-gnueabi"}; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 154 | default: |
David Srbecky | b461b53 | 2020-07-13 17:45:22 +0000 | [diff] [blame] | 155 | return {FindTool("llvm-objdump"), "--disassemble", "--no-show-raw-insn"}; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 156 | } |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 157 | } |
| 158 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 159 | bool Assemble(const std::string& asm_file, const std::string& obj_file) { |
| 160 | std::vector<std::string> args = GetAssemblerCommand(); |
| 161 | args.insert(args.end(), {"-o", obj_file, asm_file}); |
| 162 | std::string output; |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 163 | bool ok = CommonArtTestImpl::ForkAndExec(args, [](){ return true; }, &output).StandardSuccess(); |
| 164 | if (!ok) { |
| 165 | LOG(ERROR) << "Assembler error:\n" << output; |
| 166 | } |
| 167 | return ok; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 168 | } |
| 169 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 170 | bool Disassemble(const std::string& obj_file, std::string* output) { |
| 171 | std::vector<std::string> args = GetDisassemblerCommand(); |
| 172 | args.insert(args.end(), {obj_file}); |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 173 | bool ok = CommonArtTestImpl::ForkAndExec(args, [](){ return true; }, output).StandardSuccess(); |
| 174 | if (!ok) { |
| 175 | LOG(ERROR) << "Disassembler error:\n" << *output; |
| 176 | } |
| 177 | *output = Replace(*output, "\t", " "); |
| 178 | return ok; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 179 | } |
| 180 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 181 | std::vector<uint8_t> ReadFile(const std::string& filename) { |
| 182 | std::unique_ptr<File> file(OS::OpenFileForReading(filename.c_str())); |
| 183 | CHECK(file.get() != nullptr); |
| 184 | std::vector<uint8_t> data(file->GetLength()); |
| 185 | bool success = file->ReadFully(&data[0], data.size()); |
| 186 | CHECK(success) << filename; |
| 187 | return data; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 188 | } |
| 189 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 190 | void WriteFile(const std::string& filename, const void* data, size_t size) { |
| 191 | std::unique_ptr<File> file(OS::CreateEmptyFile(filename.c_str())); |
| 192 | CHECK(file.get() != nullptr); |
| 193 | bool success = file->WriteFully(data, size); |
| 194 | CHECK(success) << filename; |
| 195 | CHECK_EQ(file->FlushClose(), 0); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 196 | } |
| 197 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 198 | // Helper method which reads the content of .text section from ELF file. |
| 199 | template<bool IsElf64> |
| 200 | void ReadElf(const std::string& filename, /*out*/ std::vector<uint8_t>* code) { |
| 201 | using ElfTypes = typename std::conditional<IsElf64, ElfTypes64, ElfTypes32>::type; |
| 202 | std::vector<uint8_t> data = ReadFile(filename); |
| 203 | ElfDebugReader<ElfTypes> reader((ArrayRef<const uint8_t>(data))); |
| 204 | const typename ElfTypes::Shdr* text = reader.GetSection(".text"); |
| 205 | CHECK(text != nullptr); |
| 206 | *code = std::vector<uint8_t>(&data[text->sh_offset], &data[text->sh_offset + text->sh_size]); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 207 | } |
| 208 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 209 | // Helper method to create an ELF file containing only the given code in the .text section. |
| 210 | template<bool IsElf64> |
| 211 | void WriteElf(const std::string& filename, InstructionSet isa, const std::vector<uint8_t>& code) { |
| 212 | using ElfTypes = typename std::conditional<IsElf64, ElfTypes64, ElfTypes32>::type; |
| 213 | std::unique_ptr<File> file(OS::CreateEmptyFile(filename.c_str())); |
| 214 | CHECK(file.get() != nullptr); |
| 215 | FileOutputStream out(file.get()); |
| 216 | std::unique_ptr<ElfBuilder<ElfTypes>> builder(new ElfBuilder<ElfTypes>(isa, &out)); |
| 217 | builder->Start(/* write_program_headers= */ false); |
| 218 | builder->GetText()->Start(); |
| 219 | builder->GetText()->WriteFully(code.data(), code.size()); |
| 220 | builder->GetText()->End(); |
| 221 | builder->End(); |
| 222 | CHECK(builder->Good()); |
| 223 | CHECK_EQ(file->Close(), 0); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static std::string GetRootPath() { |
| 227 | // 1) Check ANDROID_BUILD_TOP |
| 228 | char* build_top = getenv("ANDROID_BUILD_TOP"); |
| 229 | if (build_top != nullptr) { |
| 230 | return std::string(build_top) + "/"; |
| 231 | } |
| 232 | |
| 233 | // 2) Do cwd |
| 234 | char temp[1024]; |
| 235 | return getcwd(temp, 1024) ? std::string(temp) + "/" : std::string(""); |
| 236 | } |
| 237 | |
David Srbecky | 194f555 | 2020-07-07 01:10:07 +0100 | [diff] [blame] | 238 | std::string Replace(const std::string& str, const std::string& from, const std::string& to) { |
| 239 | std::string output; |
| 240 | size_t pos = 0; |
| 241 | for (auto match = str.find(from); match != str.npos; match = str.find(from, pos)) { |
| 242 | output += str.substr(pos, match - pos); |
| 243 | output += to; |
| 244 | pos = match + from.size(); |
| 245 | } |
| 246 | output += str.substr(pos, str.size() - pos); |
| 247 | return output; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 248 | } |
| 249 | |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 250 | std::optional<ScratchDir> scratch_dir_; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 251 | std::string android_data_; |
David Srbecky | d6e14e0 | 2020-07-01 13:19:17 +0100 | [diff] [blame] | 252 | DISALLOW_COPY_AND_ASSIGN(AssemblerTestBase); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | } // namespace art |
| 256 | |
| 257 | #endif // ART_COMPILER_UTILS_ASSEMBLER_TEST_BASE_H_ |