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