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 Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/utils.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | #include "common_runtime_test.h" // For ScratchFile |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 30 | #include "exec_utils.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 32 | namespace art { |
| 33 | |
| 34 | // If you want to take a look at the differences between the ART assembler and GCC, set this flag |
| 35 | // to true. The disassembled files will then remain in the tmp directory. |
| 36 | static constexpr bool kKeepDisassembledFiles = false; |
| 37 | |
| 38 | // Use a glocal static variable to keep the same name for all test data. Else we'll just spam the |
| 39 | // temp directory. |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 40 | static std::string tmpnam_; // NOLINT [runtime/string] [4] |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 41 | |
| 42 | // We put this into a class as gtests are self-contained, so this helper needs to be in an h-file. |
| 43 | class AssemblerTestInfrastructure { |
| 44 | public: |
| 45 | AssemblerTestInfrastructure(std::string architecture, |
| 46 | std::string as, |
| 47 | std::string as_params, |
| 48 | std::string objdump, |
| 49 | std::string objdump_params, |
| 50 | std::string disasm, |
| 51 | std::string disasm_params, |
| 52 | const char* asm_header) : |
| 53 | architecture_string_(architecture), |
| 54 | asm_header_(asm_header), |
| 55 | assembler_cmd_name_(as), |
| 56 | assembler_parameters_(as_params), |
| 57 | objdump_cmd_name_(objdump), |
| 58 | objdump_parameters_(objdump_params), |
| 59 | disassembler_cmd_name_(disasm), |
| 60 | disassembler_parameters_(disasm_params) { |
| 61 | // Fake a runtime test for ScratchFile |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 62 | CommonRuntimeTest::SetUpAndroidDataDir(android_data_); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | virtual ~AssemblerTestInfrastructure() { |
| 66 | // We leave temporaries in case this failed so we can debug issues. |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 67 | CommonRuntimeTest::TearDownAndroidDataDir(android_data_, false); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 68 | tmpnam_ = ""; |
| 69 | } |
| 70 | |
| 71 | // This is intended to be run as a test. |
| 72 | bool CheckTools() { |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 73 | std::string asm_tool = FindTool(assembler_cmd_name_); |
| 74 | if (!FileExists(asm_tool)) { |
| 75 | LOG(ERROR) << "Could not find assembler from " << assembler_cmd_name_; |
| 76 | LOG(ERROR) << "FindTool returned " << asm_tool; |
| 77 | FindToolDump(assembler_cmd_name_); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | LOG(INFO) << "Chosen assembler command: " << GetAssemblerCommand(); |
| 81 | |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 82 | std::string objdump_tool = FindTool(objdump_cmd_name_); |
| 83 | if (!FileExists(objdump_tool)) { |
| 84 | LOG(ERROR) << "Could not find objdump from " << objdump_cmd_name_; |
| 85 | LOG(ERROR) << "FindTool returned " << objdump_tool; |
| 86 | FindToolDump(objdump_cmd_name_); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | LOG(INFO) << "Chosen objdump command: " << GetObjdumpCommand(); |
| 90 | |
| 91 | // Disassembly is optional. |
| 92 | std::string disassembler = GetDisassembleCommand(); |
| 93 | if (disassembler.length() != 0) { |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 94 | std::string disassembler_tool = FindTool(disassembler_cmd_name_); |
| 95 | if (!FileExists(disassembler_tool)) { |
| 96 | LOG(ERROR) << "Could not find disassembler from " << disassembler_cmd_name_; |
| 97 | LOG(ERROR) << "FindTool returned " << disassembler_tool; |
| 98 | FindToolDump(disassembler_cmd_name_); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | LOG(INFO) << "Chosen disassemble command: " << GetDisassembleCommand(); |
| 102 | } else { |
| 103 | LOG(INFO) << "No disassembler given."; |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Driver() assembles and compares the results. If the results are not equal and we have a |
| 110 | // disassembler, disassemble both and check whether they have the same mnemonics (in which case |
| 111 | // we just warn). |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 112 | void Driver(const std::vector<uint8_t>& data, |
| 113 | const std::string& assembly_text, |
| 114 | const std::string& test_name) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 115 | EXPECT_NE(assembly_text.length(), 0U) << "Empty assembly"; |
| 116 | |
| 117 | NativeAssemblerResult res; |
| 118 | Compile(assembly_text, &res, test_name); |
| 119 | |
| 120 | EXPECT_TRUE(res.ok) << res.error_msg; |
| 121 | if (!res.ok) { |
| 122 | // No way of continuing. |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (data == *res.code) { |
| 127 | Clean(&res); |
| 128 | } else { |
| 129 | if (DisassembleBinaries(data, *res.code, test_name)) { |
| 130 | if (data.size() > res.code->size()) { |
| 131 | // Fail this test with a fancy colored warning being printed. |
| 132 | EXPECT_TRUE(false) << "Assembly code is not identical, but disassembly of machine code " |
| 133 | "is equal: this implies sub-optimal encoding! Our code size=" << data.size() << |
| 134 | ", gcc size=" << res.code->size(); |
| 135 | } else { |
| 136 | // Otherwise just print an info message and clean up. |
| 137 | LOG(INFO) << "GCC chose a different encoding than ours, but the overall length is the " |
| 138 | "same."; |
| 139 | Clean(&res); |
| 140 | } |
| 141 | } else { |
| 142 | // This will output the assembly. |
| 143 | EXPECT_EQ(*res.code, data) << "Outputs (and disassembly) not identical."; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | protected: |
| 149 | // Return the host assembler command for this test. |
| 150 | virtual std::string GetAssemblerCommand() { |
| 151 | // Already resolved it once? |
| 152 | if (resolved_assembler_cmd_.length() != 0) { |
| 153 | return resolved_assembler_cmd_; |
| 154 | } |
| 155 | |
| 156 | std::string line = FindTool(assembler_cmd_name_); |
| 157 | if (line.length() == 0) { |
| 158 | return line; |
| 159 | } |
| 160 | |
| 161 | resolved_assembler_cmd_ = line + assembler_parameters_; |
| 162 | |
| 163 | return resolved_assembler_cmd_; |
| 164 | } |
| 165 | |
| 166 | // Return the host objdump command for this test. |
| 167 | virtual std::string GetObjdumpCommand() { |
| 168 | // Already resolved it once? |
| 169 | if (resolved_objdump_cmd_.length() != 0) { |
| 170 | return resolved_objdump_cmd_; |
| 171 | } |
| 172 | |
| 173 | std::string line = FindTool(objdump_cmd_name_); |
| 174 | if (line.length() == 0) { |
| 175 | return line; |
| 176 | } |
| 177 | |
| 178 | resolved_objdump_cmd_ = line + objdump_parameters_; |
| 179 | |
| 180 | return resolved_objdump_cmd_; |
| 181 | } |
| 182 | |
| 183 | // Return the host disassembler command for this test. |
| 184 | virtual std::string GetDisassembleCommand() { |
| 185 | // Already resolved it once? |
| 186 | if (resolved_disassemble_cmd_.length() != 0) { |
| 187 | return resolved_disassemble_cmd_; |
| 188 | } |
| 189 | |
| 190 | std::string line = FindTool(disassembler_cmd_name_); |
| 191 | if (line.length() == 0) { |
| 192 | return line; |
| 193 | } |
| 194 | |
| 195 | resolved_disassemble_cmd_ = line + disassembler_parameters_; |
| 196 | |
| 197 | return resolved_disassemble_cmd_; |
| 198 | } |
| 199 | |
| 200 | private: |
| 201 | // Structure to store intermediates and results. |
| 202 | struct NativeAssemblerResult { |
| 203 | bool ok; |
| 204 | std::string error_msg; |
| 205 | std::string base_name; |
| 206 | std::unique_ptr<std::vector<uint8_t>> code; |
| 207 | uintptr_t length; |
| 208 | }; |
| 209 | |
| 210 | // Compile the assembly file from_file to a binary file to_file. Returns true on success. |
| 211 | bool Assemble(const char* from_file, const char* to_file, std::string* error_msg) { |
| 212 | bool have_assembler = FileExists(FindTool(assembler_cmd_name_)); |
| 213 | EXPECT_TRUE(have_assembler) << "Cannot find assembler:" << GetAssemblerCommand(); |
| 214 | if (!have_assembler) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | std::vector<std::string> args; |
| 219 | |
| 220 | // Encaspulate the whole command line in a single string passed to |
| 221 | // the shell, so that GetAssemblerCommand() may contain arguments |
| 222 | // in addition to the program name. |
| 223 | args.push_back(GetAssemblerCommand()); |
| 224 | args.push_back("-o"); |
| 225 | args.push_back(to_file); |
| 226 | args.push_back(from_file); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 227 | std::string cmd = android::base::Join(args, ' '); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 228 | |
| 229 | args.clear(); |
| 230 | args.push_back("/bin/sh"); |
| 231 | args.push_back("-c"); |
| 232 | args.push_back(cmd); |
| 233 | |
| 234 | bool success = Exec(args, error_msg); |
| 235 | if (!success) { |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 236 | LOG(ERROR) << "Assembler command line:"; |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 237 | for (const std::string& arg : args) { |
Nicolas Geoffray | d56376c | 2015-05-21 12:32:34 +0000 | [diff] [blame] | 238 | LOG(ERROR) << arg; |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | return success; |
| 242 | } |
| 243 | |
| 244 | // Runs objdump -h on the binary file and extracts the first line with .text. |
| 245 | // Returns "" on failure. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 246 | std::string Objdump(const std::string& file) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 247 | bool have_objdump = FileExists(FindTool(objdump_cmd_name_)); |
| 248 | EXPECT_TRUE(have_objdump) << "Cannot find objdump: " << GetObjdumpCommand(); |
| 249 | if (!have_objdump) { |
| 250 | return ""; |
| 251 | } |
| 252 | |
| 253 | std::string error_msg; |
| 254 | std::vector<std::string> args; |
| 255 | |
| 256 | // Encaspulate the whole command line in a single string passed to |
| 257 | // the shell, so that GetObjdumpCommand() may contain arguments |
| 258 | // in addition to the program name. |
| 259 | args.push_back(GetObjdumpCommand()); |
| 260 | args.push_back(file); |
| 261 | args.push_back(">"); |
| 262 | args.push_back(file+".dump"); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 263 | std::string cmd = android::base::Join(args, ' '); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 264 | |
| 265 | args.clear(); |
| 266 | args.push_back("/bin/sh"); |
| 267 | args.push_back("-c"); |
| 268 | args.push_back(cmd); |
| 269 | |
| 270 | if (!Exec(args, &error_msg)) { |
| 271 | EXPECT_TRUE(false) << error_msg; |
| 272 | } |
| 273 | |
| 274 | std::ifstream dump(file+".dump"); |
| 275 | |
| 276 | std::string line; |
| 277 | bool found = false; |
| 278 | while (std::getline(dump, line)) { |
| 279 | if (line.find(".text") != line.npos) { |
| 280 | found = true; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | dump.close(); |
| 286 | |
| 287 | if (found) { |
| 288 | return line; |
| 289 | } else { |
| 290 | return ""; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Disassemble both binaries and compare the text. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 295 | bool DisassembleBinaries(const std::vector<uint8_t>& data, |
| 296 | const std::vector<uint8_t>& as, |
| 297 | const std::string& test_name) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 298 | std::string disassembler = GetDisassembleCommand(); |
| 299 | if (disassembler.length() == 0) { |
| 300 | LOG(WARNING) << "No dissassembler command."; |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | std::string data_name = WriteToFile(data, test_name + ".ass"); |
| 305 | std::string error_msg; |
| 306 | if (!DisassembleBinary(data_name, &error_msg)) { |
| 307 | LOG(INFO) << "Error disassembling: " << error_msg; |
| 308 | std::remove(data_name.c_str()); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | std::string as_name = WriteToFile(as, test_name + ".gcc"); |
| 313 | if (!DisassembleBinary(as_name, &error_msg)) { |
| 314 | LOG(INFO) << "Error disassembling: " << error_msg; |
| 315 | std::remove(data_name.c_str()); |
| 316 | std::remove((data_name + ".dis").c_str()); |
| 317 | std::remove(as_name.c_str()); |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | bool result = CompareFiles(data_name + ".dis", as_name + ".dis"); |
| 322 | |
| 323 | if (!kKeepDisassembledFiles) { |
| 324 | std::remove(data_name.c_str()); |
| 325 | std::remove(as_name.c_str()); |
| 326 | std::remove((data_name + ".dis").c_str()); |
| 327 | std::remove((as_name + ".dis").c_str()); |
| 328 | } |
| 329 | |
| 330 | return result; |
| 331 | } |
| 332 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 333 | bool DisassembleBinary(const std::string& file, std::string* error_msg) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 334 | std::vector<std::string> args; |
| 335 | |
| 336 | // Encaspulate the whole command line in a single string passed to |
| 337 | // the shell, so that GetDisassembleCommand() may contain arguments |
| 338 | // in addition to the program name. |
| 339 | args.push_back(GetDisassembleCommand()); |
| 340 | args.push_back(file); |
| 341 | args.push_back("| sed -n \'/<.data>/,$p\' | sed -e \'s/.*://\'"); |
| 342 | args.push_back(">"); |
| 343 | args.push_back(file+".dis"); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 344 | std::string cmd = android::base::Join(args, ' '); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 345 | |
| 346 | args.clear(); |
| 347 | args.push_back("/bin/sh"); |
| 348 | args.push_back("-c"); |
| 349 | args.push_back(cmd); |
| 350 | |
| 351 | return Exec(args, error_msg); |
| 352 | } |
| 353 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 354 | std::string WriteToFile(const std::vector<uint8_t>& buffer, const std::string& test_name) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 355 | std::string file_name = GetTmpnam() + std::string("---") + test_name; |
| 356 | const char* data = reinterpret_cast<const char*>(buffer.data()); |
| 357 | std::ofstream s_out(file_name + ".o"); |
| 358 | s_out.write(data, buffer.size()); |
| 359 | s_out.close(); |
| 360 | return file_name + ".o"; |
| 361 | } |
| 362 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 363 | bool CompareFiles(const std::string& f1, const std::string& f2) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 364 | std::ifstream f1_in(f1); |
| 365 | std::ifstream f2_in(f2); |
| 366 | |
Vladimir Marko | 662f12e | 2020-02-26 12:46:09 +0000 | [diff] [blame] | 367 | bool read1_ok, read2_ok; |
| 368 | char c1, c2; |
| 369 | do { |
| 370 | read1_ok = static_cast<bool>(f1_in >> c1); |
| 371 | read2_ok = static_cast<bool>(f2_in >> c2); |
| 372 | } while (read1_ok && read2_ok && c1 == c2); |
| 373 | return !read1_ok && !read2_ok; // Did we reach the end of both streams? |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // Compile the given assembly code and extract the binary, if possible. Put result into res. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 377 | bool Compile(const std::string& assembly_code, |
| 378 | NativeAssemblerResult* res, |
| 379 | const std::string& test_name) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 380 | res->ok = false; |
| 381 | res->code.reset(nullptr); |
| 382 | |
| 383 | res->base_name = GetTmpnam() + std::string("---") + test_name; |
| 384 | |
| 385 | // TODO: Lots of error checking. |
| 386 | |
| 387 | std::ofstream s_out(res->base_name + ".S"); |
| 388 | if (asm_header_ != nullptr) { |
| 389 | s_out << asm_header_; |
| 390 | } |
| 391 | s_out << assembly_code; |
| 392 | s_out.close(); |
| 393 | |
| 394 | if (!Assemble((res->base_name + ".S").c_str(), (res->base_name + ".o").c_str(), |
| 395 | &res->error_msg)) { |
| 396 | res->error_msg = "Could not compile."; |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | std::string odump = Objdump(res->base_name + ".o"); |
| 401 | if (odump.length() == 0) { |
| 402 | res->error_msg = "Objdump failed."; |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | std::istringstream iss(odump); |
| 407 | std::istream_iterator<std::string> start(iss); |
| 408 | std::istream_iterator<std::string> end; |
| 409 | std::vector<std::string> tokens(start, end); |
| 410 | |
| 411 | if (tokens.size() < OBJDUMP_SECTION_LINE_MIN_TOKENS) { |
| 412 | res->error_msg = "Objdump output not recognized: too few tokens."; |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | if (tokens[1] != ".text") { |
| 417 | res->error_msg = "Objdump output not recognized: .text not second token."; |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | std::string lengthToken = "0x" + tokens[2]; |
| 422 | std::istringstream(lengthToken) >> std::hex >> res->length; |
| 423 | |
| 424 | std::string offsetToken = "0x" + tokens[5]; |
| 425 | uintptr_t offset; |
| 426 | std::istringstream(offsetToken) >> std::hex >> offset; |
| 427 | |
| 428 | std::ifstream obj(res->base_name + ".o"); |
| 429 | obj.seekg(offset); |
| 430 | res->code.reset(new std::vector<uint8_t>(res->length)); |
| 431 | obj.read(reinterpret_cast<char*>(&(*res->code)[0]), res->length); |
| 432 | obj.close(); |
| 433 | |
| 434 | res->ok = true; |
| 435 | return true; |
| 436 | } |
| 437 | |
| 438 | // Remove temporary files. |
| 439 | void Clean(const NativeAssemblerResult* res) { |
| 440 | std::remove((res->base_name + ".S").c_str()); |
| 441 | std::remove((res->base_name + ".o").c_str()); |
| 442 | std::remove((res->base_name + ".o.dump").c_str()); |
| 443 | } |
| 444 | |
| 445 | // Check whether file exists. Is used for commands, so strips off any parameters: anything after |
| 446 | // the first space. We skip to the last slash for this, so it should work with directories with |
| 447 | // spaces. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 448 | static bool FileExists(const std::string& file) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 449 | if (file.length() == 0) { |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | // Need to strip any options. |
| 454 | size_t last_slash = file.find_last_of('/'); |
| 455 | if (last_slash == std::string::npos) { |
| 456 | // No slash, start looking at the start. |
| 457 | last_slash = 0; |
| 458 | } |
| 459 | size_t space_index = file.find(' ', last_slash); |
| 460 | |
| 461 | if (space_index == std::string::npos) { |
| 462 | std::ifstream infile(file.c_str()); |
| 463 | return infile.good(); |
| 464 | } else { |
| 465 | std::string copy = file.substr(0, space_index - 1); |
| 466 | |
| 467 | struct stat buf; |
| 468 | return stat(copy.c_str(), &buf) == 0; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | static std::string GetGCCRootPath() { |
| 473 | return "prebuilts/gcc/linux-x86"; |
| 474 | } |
| 475 | |
| 476 | static std::string GetRootPath() { |
| 477 | // 1) Check ANDROID_BUILD_TOP |
| 478 | char* build_top = getenv("ANDROID_BUILD_TOP"); |
| 479 | if (build_top != nullptr) { |
| 480 | return std::string(build_top) + "/"; |
| 481 | } |
| 482 | |
| 483 | // 2) Do cwd |
| 484 | char temp[1024]; |
| 485 | return getcwd(temp, 1024) ? std::string(temp) + "/" : std::string(""); |
| 486 | } |
| 487 | |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 488 | std::string FindTool(const std::string& tool_name) { |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 489 | // Find the current tool. Wild-card pattern is "arch-string*tool-name". |
| 490 | std::string gcc_path = GetRootPath() + GetGCCRootPath(); |
| 491 | std::vector<std::string> args; |
| 492 | args.push_back("find"); |
| 493 | args.push_back(gcc_path); |
| 494 | args.push_back("-name"); |
| 495 | args.push_back(architecture_string_ + "*" + tool_name); |
| 496 | args.push_back("|"); |
| 497 | args.push_back("sort"); |
| 498 | args.push_back("|"); |
| 499 | args.push_back("tail"); |
| 500 | args.push_back("-n"); |
| 501 | args.push_back("1"); |
| 502 | std::string tmp_file = GetTmpnam(); |
| 503 | args.push_back(">"); |
| 504 | args.push_back(tmp_file); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 505 | std::string sh_args = android::base::Join(args, ' '); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 506 | |
| 507 | args.clear(); |
| 508 | args.push_back("/bin/sh"); |
| 509 | args.push_back("-c"); |
| 510 | args.push_back(sh_args); |
| 511 | |
| 512 | std::string error_msg; |
| 513 | if (!Exec(args, &error_msg)) { |
| 514 | EXPECT_TRUE(false) << error_msg; |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 515 | UNREACHABLE(); |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | std::ifstream in(tmp_file.c_str()); |
| 519 | std::string line; |
| 520 | if (!std::getline(in, line)) { |
| 521 | in.close(); |
| 522 | std::remove(tmp_file.c_str()); |
| 523 | return ""; |
| 524 | } |
| 525 | in.close(); |
| 526 | std::remove(tmp_file.c_str()); |
| 527 | return line; |
| 528 | } |
| 529 | |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 530 | // Helper for below. If name_predicate is empty, search for all files, otherwise use it for the |
| 531 | // "-name" option. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 532 | static void FindToolDumpPrintout(const std::string& name_predicate, |
| 533 | const std::string& tmp_file) { |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 534 | std::string gcc_path = GetRootPath() + GetGCCRootPath(); |
| 535 | std::vector<std::string> args; |
| 536 | args.push_back("find"); |
| 537 | args.push_back(gcc_path); |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 538 | if (!name_predicate.empty()) { |
| 539 | args.push_back("-name"); |
| 540 | args.push_back(name_predicate); |
| 541 | } |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 542 | args.push_back("|"); |
| 543 | args.push_back("sort"); |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 544 | args.push_back(">"); |
| 545 | args.push_back(tmp_file); |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 546 | std::string sh_args = android::base::Join(args, ' '); |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 547 | |
| 548 | args.clear(); |
| 549 | args.push_back("/bin/sh"); |
| 550 | args.push_back("-c"); |
| 551 | args.push_back(sh_args); |
| 552 | |
| 553 | std::string error_msg; |
| 554 | if (!Exec(args, &error_msg)) { |
| 555 | EXPECT_TRUE(false) << error_msg; |
| 556 | UNREACHABLE(); |
| 557 | } |
| 558 | |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 559 | LOG(ERROR) << "FindToolDump: gcc_path=" << gcc_path |
| 560 | << " cmd=" << sh_args; |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 561 | std::ifstream in(tmp_file.c_str()); |
| 562 | if (in) { |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 563 | std::string line; |
| 564 | while (std::getline(in, line)) { |
| 565 | LOG(ERROR) << line; |
| 566 | } |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 567 | } |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 568 | in.close(); |
| 569 | std::remove(tmp_file.c_str()); |
| 570 | } |
| 571 | |
| 572 | // For debug purposes. |
Andreas Gampe | 2e965ac | 2016-11-03 17:24:15 -0700 | [diff] [blame] | 573 | void FindToolDump(const std::string& tool_name) { |
Andreas Gampe | 7532401 | 2015-10-06 18:59:08 -0700 | [diff] [blame] | 574 | // Check with the tool name. |
| 575 | FindToolDumpPrintout(architecture_string_ + "*" + tool_name, GetTmpnam()); |
| 576 | FindToolDumpPrintout("", GetTmpnam()); |
Andreas Gampe | f3e0706 | 2015-10-06 09:05:10 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Andreas Gampe | 03b9ee4 | 2015-04-24 21:41:45 -0700 | [diff] [blame] | 579 | // Use a consistent tmpnam, so store it. |
| 580 | std::string GetTmpnam() { |
| 581 | if (tmpnam_.length() == 0) { |
| 582 | ScratchFile tmp; |
| 583 | tmpnam_ = tmp.GetFilename() + "asm"; |
| 584 | } |
| 585 | return tmpnam_; |
| 586 | } |
| 587 | |
| 588 | static constexpr size_t OBJDUMP_SECTION_LINE_MIN_TOKENS = 6; |
| 589 | |
| 590 | std::string architecture_string_; |
| 591 | const char* asm_header_; |
| 592 | |
| 593 | std::string assembler_cmd_name_; |
| 594 | std::string assembler_parameters_; |
| 595 | |
| 596 | std::string objdump_cmd_name_; |
| 597 | std::string objdump_parameters_; |
| 598 | |
| 599 | std::string disassembler_cmd_name_; |
| 600 | std::string disassembler_parameters_; |
| 601 | |
| 602 | std::string resolved_assembler_cmd_; |
| 603 | std::string resolved_objdump_cmd_; |
| 604 | std::string resolved_disassemble_cmd_; |
| 605 | |
| 606 | std::string android_data_; |
| 607 | |
| 608 | DISALLOW_COPY_AND_ASSIGN(AssemblerTestInfrastructure); |
| 609 | }; |
| 610 | |
| 611 | } // namespace art |
| 612 | |
| 613 | #endif // ART_COMPILER_UTILS_ASSEMBLER_TEST_BASE_H_ |