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