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