relative_patcher_test.h revision 63dccbbefef3014c99c22748d18befcc7bcb3b41
1/* 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_LINKER_RELATIVE_PATCHER_TEST_H_ 18#define ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_ 19 20#include "arch/instruction_set.h" 21#include "arch/instruction_set_features.h" 22#include "base/array_ref.h" 23#include "base/macros.h" 24#include "compiled_method.h" 25#include "dex/quick/dex_file_to_method_inliner_map.h" 26#include "dex/verification_results.h" 27#include "driver/compiler_driver.h" 28#include "driver/compiler_options.h" 29#include "globals.h" 30#include "gtest/gtest.h" 31#include "linker/relative_patcher.h" 32#include "method_reference.h" 33#include "oat.h" 34#include "oat_quick_method_header.h" 35#include "vector_output_stream.h" 36 37namespace art { 38namespace linker { 39 40// Base class providing infrastructure for architecture-specific tests. 41class RelativePatcherTest : public testing::Test { 42 protected: 43 RelativePatcherTest(InstructionSet instruction_set, const std::string& variant) 44 : compiler_options_(), 45 verification_results_(&compiler_options_), 46 inliner_map_(), 47 driver_(&compiler_options_, 48 &verification_results_, 49 &inliner_map_, 50 Compiler::kQuick, 51 instruction_set, 52 /* instruction_set_features*/ nullptr, 53 /* image_classes */ nullptr, 54 /* compiled_classes */ nullptr, 55 /* compiled_methods */ nullptr, 56 /* thread_count */ 1u, 57 /* dump_stats */ false, 58 /* dump_passes */ false, 59 /* timer */ nullptr, 60 /* swap_fd */ -1, 61 /* profile_compilation_info */ nullptr), 62 error_msg_(), 63 instruction_set_(instruction_set), 64 features_(InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg_)), 65 method_offset_map_(), 66 patcher_(RelativePatcher::Create(instruction_set, features_.get(), &method_offset_map_)), 67 dex_cache_arrays_begin_(0u), 68 compiled_method_refs_(), 69 compiled_methods_(), 70 patched_code_(), 71 output_(), 72 out_("test output stream", &output_) { 73 CHECK(error_msg_.empty()) << instruction_set << "/" << variant; 74 patched_code_.reserve(16 * KB); 75 } 76 77 MethodReference MethodRef(uint32_t method_idx) { 78 CHECK_NE(method_idx, 0u); 79 return MethodReference(nullptr, method_idx); 80 } 81 82 void AddCompiledMethod(MethodReference method_ref, 83 const ArrayRef<const uint8_t>& code, 84 const ArrayRef<const LinkerPatch>& patches) { 85 compiled_method_refs_.push_back(method_ref); 86 compiled_methods_.emplace_back(new CompiledMethod( 87 &driver_, 88 instruction_set_, 89 code, 90 /* frame_size_in_bytes */ 0u, 91 /* core_spill_mask */ 0u, 92 /* fp_spill_mask */ 0u, 93 /* src_mapping_table */ ArrayRef<const SrcMapElem>(), 94 /* vmap_table */ ArrayRef<const uint8_t>(), 95 /* cfi_info */ ArrayRef<const uint8_t>(), 96 patches)); 97 } 98 99 uint32_t CodeAlignmentSize(uint32_t header_offset_to_align) { 100 // We want to align the code rather than the preheader. 101 uint32_t unaligned_code_offset = header_offset_to_align + sizeof(OatQuickMethodHeader); 102 uint32_t aligned_code_offset = 103 CompiledMethod::AlignCode(unaligned_code_offset, instruction_set_); 104 return aligned_code_offset - unaligned_code_offset; 105 } 106 107 void Link() { 108 // Reserve space. 109 static_assert(kTrampolineOffset == 0u, "Unexpected trampoline offset."); 110 uint32_t offset = kTrampolineSize; 111 size_t idx = 0u; 112 for (auto& compiled_method : compiled_methods_) { 113 offset = patcher_->ReserveSpace(offset, compiled_method.get(), compiled_method_refs_[idx]); 114 115 uint32_t alignment_size = CodeAlignmentSize(offset); 116 offset += alignment_size; 117 118 offset += sizeof(OatQuickMethodHeader); 119 uint32_t quick_code_offset = offset + compiled_method->CodeDelta(); 120 const auto code = compiled_method->GetQuickCode(); 121 offset += code.size(); 122 123 method_offset_map_.map.Put(compiled_method_refs_[idx], quick_code_offset); 124 ++idx; 125 } 126 offset = patcher_->ReserveSpaceEnd(offset); 127 uint32_t output_size = offset; 128 output_.reserve(output_size); 129 130 // Write data. 131 DCHECK(output_.empty()); 132 uint8_t dummy_trampoline[kTrampolineSize]; 133 memset(dummy_trampoline, 0, sizeof(dummy_trampoline)); 134 out_.WriteFully(dummy_trampoline, kTrampolineSize); 135 offset = kTrampolineSize; 136 static const uint8_t kPadding[] = { 137 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u 138 }; 139 uint8_t dummy_header[sizeof(OatQuickMethodHeader)]; 140 memset(dummy_header, 0, sizeof(dummy_header)); 141 for (auto& compiled_method : compiled_methods_) { 142 offset = patcher_->WriteThunks(&out_, offset); 143 144 uint32_t alignment_size = CodeAlignmentSize(offset); 145 CHECK_LE(alignment_size, sizeof(kPadding)); 146 out_.WriteFully(kPadding, alignment_size); 147 offset += alignment_size; 148 149 out_.WriteFully(dummy_header, sizeof(OatQuickMethodHeader)); 150 offset += sizeof(OatQuickMethodHeader); 151 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode(); 152 if (!compiled_method->GetPatches().empty()) { 153 patched_code_.assign(code.begin(), code.end()); 154 code = ArrayRef<const uint8_t>(patched_code_); 155 for (const LinkerPatch& patch : compiled_method->GetPatches()) { 156 if (patch.GetType() == LinkerPatch::Type::kCallRelative) { 157 auto result = method_offset_map_.FindMethodOffset(patch.TargetMethod()); 158 uint32_t target_offset = 159 result.first ? result.second : kTrampolineOffset + compiled_method->CodeDelta(); 160 patcher_->PatchCall(&patched_code_, patch.LiteralOffset(), 161 offset + patch.LiteralOffset(), target_offset); 162 } else if (patch.GetType() == LinkerPatch::Type::kDexCacheArray) { 163 uint32_t target_offset = dex_cache_arrays_begin_ + patch.TargetDexCacheElementOffset(); 164 patcher_->PatchPcRelativeReference(&patched_code_, 165 patch, 166 offset + patch.LiteralOffset(), 167 target_offset); 168 } else if (patch.GetType() == LinkerPatch::Type::kStringRelative) { 169 uint32_t target_offset = string_index_to_offset_map_.Get(patch.TargetStringIndex()); 170 patcher_->PatchPcRelativeReference(&patched_code_, 171 patch, 172 offset + patch.LiteralOffset(), 173 target_offset); 174 } else { 175 LOG(FATAL) << "Bad patch type. " << patch.GetType(); 176 UNREACHABLE(); 177 } 178 } 179 } 180 out_.WriteFully(&code[0], code.size()); 181 offset += code.size(); 182 } 183 offset = patcher_->WriteThunks(&out_, offset); 184 CHECK_EQ(offset, output_size); 185 CHECK_EQ(output_.size(), output_size); 186 } 187 188 bool CheckLinkedMethod(MethodReference method_ref, const ArrayRef<const uint8_t>& expected_code) { 189 // Sanity check: original code size must match linked_code.size(). 190 size_t idx = 0u; 191 for (auto ref : compiled_method_refs_) { 192 if (ref.dex_file == method_ref.dex_file && 193 ref.dex_method_index == method_ref.dex_method_index) { 194 break; 195 } 196 ++idx; 197 } 198 CHECK_NE(idx, compiled_method_refs_.size()); 199 CHECK_EQ(compiled_methods_[idx]->GetQuickCode().size(), expected_code.size()); 200 201 auto result = method_offset_map_.FindMethodOffset(method_ref); 202 CHECK(result.first); // Must have been linked. 203 size_t offset = result.second - compiled_methods_[idx]->CodeDelta(); 204 CHECK_LT(offset, output_.size()); 205 CHECK_LE(offset + expected_code.size(), output_.size()); 206 ArrayRef<const uint8_t> linked_code(&output_[offset], expected_code.size()); 207 if (linked_code == expected_code) { 208 return true; 209 } 210 // Log failure info. 211 DumpDiff(expected_code, linked_code); 212 return false; 213 } 214 215 void DumpDiff(const ArrayRef<const uint8_t>& expected_code, 216 const ArrayRef<const uint8_t>& linked_code) { 217 std::ostringstream expected_hex; 218 std::ostringstream linked_hex; 219 std::ostringstream diff_indicator; 220 static const char digits[] = "0123456789abcdef"; 221 bool found_diff = false; 222 for (size_t i = 0; i != expected_code.size(); ++i) { 223 expected_hex << " " << digits[expected_code[i] >> 4] << digits[expected_code[i] & 0xf]; 224 linked_hex << " " << digits[linked_code[i] >> 4] << digits[linked_code[i] & 0xf]; 225 if (!found_diff) { 226 found_diff = (expected_code[i] != linked_code[i]); 227 diff_indicator << (found_diff ? " ^^" : " "); 228 } 229 } 230 CHECK(found_diff); 231 std::string expected_hex_str = expected_hex.str(); 232 std::string linked_hex_str = linked_hex.str(); 233 std::string diff_indicator_str = diff_indicator.str(); 234 if (diff_indicator_str.length() > 60) { 235 CHECK_EQ(diff_indicator_str.length() % 3u, 0u); 236 size_t remove = diff_indicator_str.length() / 3 - 5; 237 std::ostringstream oss; 238 oss << "[stripped " << remove << "]"; 239 std::string replacement = oss.str(); 240 expected_hex_str.replace(0u, remove * 3u, replacement); 241 linked_hex_str.replace(0u, remove * 3u, replacement); 242 diff_indicator_str.replace(0u, remove * 3u, replacement); 243 } 244 LOG(ERROR) << "diff expected_code linked_code"; 245 LOG(ERROR) << "<" << expected_hex_str; 246 LOG(ERROR) << ">" << linked_hex_str; 247 LOG(ERROR) << " " << diff_indicator_str; 248 } 249 250 // Map method reference to assinged offset. 251 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider. 252 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider { 253 public: 254 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE { 255 auto it = map.find(ref); 256 if (it == map.end()) { 257 return std::pair<bool, uint32_t>(false, 0u); 258 } else { 259 return std::pair<bool, uint32_t>(true, it->second); 260 } 261 } 262 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map; 263 }; 264 265 static const uint32_t kTrampolineSize = 4u; 266 static const uint32_t kTrampolineOffset = 0u; 267 268 CompilerOptions compiler_options_; 269 VerificationResults verification_results_; 270 DexFileToMethodInlinerMap inliner_map_; 271 CompilerDriver driver_; // Needed for constructing CompiledMethod. 272 std::string error_msg_; 273 InstructionSet instruction_set_; 274 std::unique_ptr<const InstructionSetFeatures> features_; 275 MethodOffsetMap method_offset_map_; 276 std::unique_ptr<RelativePatcher> patcher_; 277 uint32_t dex_cache_arrays_begin_; 278 SafeMap<uint32_t, uint32_t> string_index_to_offset_map_; 279 std::vector<MethodReference> compiled_method_refs_; 280 std::vector<std::unique_ptr<CompiledMethod>> compiled_methods_; 281 std::vector<uint8_t> patched_code_; 282 std::vector<uint8_t> output_; 283 VectorOutputStream out_; 284}; 285 286} // namespace linker 287} // namespace art 288 289#endif // ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_ 290