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 
20 #include <sys/stat.h>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <fstream>
24 #include <iterator>
25 
26 #include "android-base/strings.h"
27 
28 #include "base/os.h"
29 #include "base/utils.h"
30 #include "common_runtime_test.h"  // For ScratchDir.
31 #include "elf/elf_builder.h"
32 #include "elf/elf_debug_reader.h"
33 #include "exec_utils.h"
34 #include "stream/file_output_stream.h"
35 
36 namespace art {
37 
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.
40 static constexpr bool kKeepDisassembledFiles = false;
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 AssemblerTestBase : public testing::Test {
44  public:
AssemblerTestBase()45   AssemblerTestBase() {}
46 
SetUp()47   void SetUp() override {
48     // Fake a runtime test for ScratchDir.
49     CommonArtTest::SetUpAndroidRootEnvVars();
50     CommonRuntimeTest::SetUpAndroidDataDir(android_data_);
51     scratch_dir_.emplace(/*keep_files=*/ kKeepDisassembledFiles);
52   }
53 
TearDown()54   void TearDown() override {
55     // We leave temporaries in case this failed so we can debug issues.
56     CommonRuntimeTest::TearDownAndroidDataDir(android_data_, false);
57   }
58 
59   // This is intended to be run as a test.
CheckTools()60   bool CheckTools() {
61     for (auto cmd : { GetAssemblerCommand()[0], GetDisassemblerCommand()[0] }) {
62       if (!OS::FileExists(cmd.c_str())) {
63         LOG(ERROR) << "Could not find " << cmd;
64         return false;
65       }
66     }
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).
Driver(const std::vector<uint8_t> & art_code,const std::string & assembly_text,const std::string & test_name)73   void Driver(const std::vector<uint8_t>& art_code,
74               const std::string& assembly_text,
75               const std::string& test_name) {
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; };
79 
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());
83 
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);
94     }
95 
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);
105     } else {
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)";
127     }
128   }
129 
130  protected:
131   virtual InstructionSet GetIsa() = 0;
132 
FindTool(const std::string & tool_name)133   std::string FindTool(const std::string& tool_name) {
134     return CommonArtTest::GetAndroidTool(tool_name.c_str(), GetIsa());
135   }
136 
GetAssemblerCommand()137   virtual std::vector<std::string> GetAssemblerCommand() {
138     InstructionSet isa = GetIsa();
139     switch (isa) {
140       case InstructionSet::kX86:
141         return {FindTool("clang"), "--compile", "-target", "i386-linux-gnu"};
142       case InstructionSet::kX86_64:
143         return {FindTool("clang"), "--compile", "-target", "x86_64-linux-gnu"};
144       default:
145         LOG(FATAL) << "Unknown instruction set: " << isa;
146         UNREACHABLE();
147     }
148   }
149 
GetDisassemblerCommand()150   virtual std::vector<std::string> GetDisassemblerCommand() {
151     switch (GetIsa()) {
152       case InstructionSet::kThumb2:
153         return {FindTool("llvm-objdump"), "--disassemble", "-triple", "thumbv7a-linux-gnueabi"};
154       default:
155         return {FindTool("llvm-objdump"), "--disassemble", "--no-show-raw-insn"};
156     }
157   }
158 
Assemble(const std::string & asm_file,const std::string & obj_file)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;
163     bool ok = CommonArtTestImpl::ForkAndExec(args, [](){ return true; }, &output).StandardSuccess();
164     if (!ok) {
165       LOG(ERROR) << "Assembler error:\n" << output;
166     }
167     return ok;
168   }
169 
Disassemble(const std::string & obj_file,std::string * output)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});
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;
179   }
180 
ReadFile(const std::string & filename)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;
188   }
189 
WriteFile(const std::string & filename,const void * data,size_t size)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);
196   }
197 
198   // Helper method which reads the content of .text section from ELF file.
199   template<bool IsElf64>
ReadElf(const std::string & filename,std::vector<uint8_t> * code)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]);
207   }
208 
209   // Helper method to create an ELF file containing only the given code in the .text section.
210   template<bool IsElf64>
WriteElf(const std::string & filename,InstructionSet isa,const std::vector<uint8_t> & code)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);
224   }
225 
GetRootPath()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 
Replace(const std::string & str,const std::string & from,const std::string & to)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;
248   }
249 
250   std::optional<ScratchDir> scratch_dir_;
251   std::string android_data_;
252   DISALLOW_COPY_AND_ASSIGN(AssemblerTestBase);
253 };
254 
255 }  // namespace art
256 
257 #endif  // ART_COMPILER_UTILS_ASSEMBLER_TEST_BASE_H_
258