OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 // The C++ implementation of the memory interceptors, intended to work on win64, |
| 16 // unlike the win32 implementation, written (generated) in pure assembly. |
| 17 // This implementation provides less functions than the original win32 one, |
| 18 // as it's intended to be used outside of Syzygy. |
| 19 |
| 20 #include "syzygy/agent/asan/error_info.h" |
| 21 #include "syzygy/agent/asan/rtl_utils.h" |
| 22 #include "syzygy/agent/asan/runtime.h" |
| 23 #include "syzygy/agent/asan/shadow.h" |
| 24 |
| 25 using namespace agent::asan; |
| 26 |
| 27 // The template function that performs the checks. |
| 28 // @tparam access_size Access size in bytes. |
| 29 // @tparam address_space_size The virtual address space size limit in bytes. |
| 30 // It's 8 TB for Win7 and Win8 and 128 TB for Win8.1+. |
| 31 // @tparam access_mode The access mode, which can be any of AccessMode values, |
| 32 // allthough this file only exports the probes for read and write accesses. |
| 33 // @param addr The address being accessed. |
| 34 template<size_t access_size, size_t address_space_size, AccessMode access_mode> |
| 35 void asan_check(const void* addr) { |
| 36 if (reinterpret_cast<uintptr_t>(addr) >= address_space_size || |
| 37 !AsanRuntime::runtime()->shadow()->IsRangeAccessible(addr, access_size)) { |
| 38 CONTEXT ctx = {}; |
| 39 ::RtlCaptureContext(&ctx); |
| 40 AsanContext asan_ctx = {}; |
| 41 ContextToAsanContext(ctx, &asan_ctx); |
| 42 ReportBadMemoryAccess(addr, access_mode, access_size, asan_ctx); |
| 43 } |
| 44 } |
| 45 |
| 46 // A few macros to instantiate 'asan_check' and export the instantiations |
| 47 // with appropriate names. |
| 48 |
| 49 #define EXPORT_INTERCEPTOR_READ(access_size, suffix, address_space_size) \ |
| 50 void asan_check_##access_size##_byte_read_access_no_flags_##suffix( \ |
| 51 const void* addr) { \ |
| 52 return asan_check<access_size, address_space_size, \ |
| 53 agent::asan::ASAN_READ_ACCESS>(addr); \ |
| 54 } \ |
| 55 void asan_check_##access_size##_byte_read_access_##suffix(const void* addr) { \ |
| 56 return asan_check<access_size, address_space_size, \ |
| 57 agent::asan::ASAN_READ_ACCESS>(addr); \ |
| 58 } |
| 59 |
| 60 #define EXPORT_INTERCEPTOR_WRITE(access_size, suffix, address_space_size) \ |
| 61 void asan_check_##access_size##_byte_write_access_no_flags_##suffix( \ |
| 62 const void* addr) { \ |
| 63 return asan_check<access_size, address_space_size, \ |
| 64 agent::asan::ASAN_WRITE_ACCESS>(addr); \ |
| 65 } \ |
| 66 void asan_check_##access_size##_byte_write_access_##suffix(const void* addr) { \ |
| 67 return asan_check<access_size, address_space_size, \ |
| 68 agent::asan::ASAN_WRITE_ACCESS>(addr); \ |
| 69 } |
| 70 |
| 71 #define EXPORT_INTERCEPTOR(access_size, suffix, address_space_size) \ |
| 72 EXPORT_INTERCEPTOR_READ(access_size, suffix, address_space_size) \ |
| 73 EXPORT_INTERCEPTOR_WRITE(access_size, suffix, address_space_size) |
| 74 |
| 75 #define EXPORT_INTERCEPTORS_ALL_SIZES(suffix, address_space_size) \ |
| 76 EXPORT_INTERCEPTOR(1, suffix, address_space_size) \ |
| 77 EXPORT_INTERCEPTOR(2, suffix, address_space_size) \ |
| 78 EXPORT_INTERCEPTOR(4, suffix, address_space_size) \ |
| 79 EXPORT_INTERCEPTOR(8, suffix, address_space_size) \ |
| 80 EXPORT_INTERCEPTOR(10, suffix, address_space_size) \ |
| 81 EXPORT_INTERCEPTOR(16, suffix, address_space_size) \ |
| 82 EXPORT_INTERCEPTOR(32, suffix, address_space_size) |
| 83 |
| 84 namespace { |
| 85 const size_t ONE_TB = static_cast<size_t>(1) << 40; |
| 86 } |
| 87 |
| 88 extern "C" { |
| 89 void asan_no_check() { return; } |
| 90 void asan_string_no_check() { return; } |
| 91 void* asan_shadow_references[] = { nullptr }; |
| 92 EXPORT_INTERCEPTORS_ALL_SIZES(8tb, 8 * ONE_TB) |
| 93 EXPORT_INTERCEPTORS_ALL_SIZES(128tb, 128 * ONE_TB) |
| 94 } |
OLD | NEW |