1 //===-- dfsan_interceptors.cpp --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of DataFlowSanitizer. 10 // 11 // Interceptors for standard library functions. 12 //===----------------------------------------------------------------------===// 13 14 #include <sys/syscall.h> 15 #include <unistd.h> 16 17 #include "dfsan/dfsan.h" 18 #include "interception/interception.h" 19 #include "sanitizer_common/sanitizer_common.h" 20 21 using namespace __sanitizer; 22 23 static bool interceptors_initialized; 24 25 INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, 26 int fd, OFF_T offset) { 27 void *res; 28 29 // interceptors_initialized is set to true during preinit_array, when we're 30 // single-threaded. So we don't need to worry about accessing it atomically. 31 if (!interceptors_initialized) 32 res = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); 33 else 34 res = REAL(mmap)(addr, length, prot, flags, fd, offset); 35 36 if (res != (void *)-1) 37 dfsan_set_label(0, res, RoundUpTo(length, GetPageSize())); 38 return res; 39 } 40 41 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, 42 int fd, OFF64_T offset) { 43 void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset); 44 if (res != (void *)-1) 45 dfsan_set_label(0, res, RoundUpTo(length, GetPageSize())); 46 return res; 47 } 48 49 INTERCEPTOR(int, munmap, void *addr, SIZE_T length) { 50 int res = REAL(munmap)(addr, length); 51 if (res != -1) { 52 uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr); 53 void *end_addr = 54 (void *)((uptr)addr + RoundUpTo(length, GetPageSizeCached())); 55 uptr end_shadow_addr = (uptr)__dfsan::shadow_for(end_addr); 56 ReleaseMemoryPagesToOS(beg_shadow_addr, end_shadow_addr); 57 } 58 return res; 59 } 60 61 namespace __dfsan { 62 void InitializeInterceptors() { 63 CHECK(!interceptors_initialized); 64 65 INTERCEPT_FUNCTION(mmap); 66 INTERCEPT_FUNCTION(mmap64); 67 INTERCEPT_FUNCTION(munmap); 68 69 interceptors_initialized = true; 70 } 71 } // namespace __dfsan 72