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 namespace { 24 25 bool interceptors_initialized; 26 27 } // namespace 28 29 INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags, 30 int fd, OFF_T offset) { 31 void *res; 32 33 // interceptors_initialized is set to true during preinit_array, when we're 34 // single-threaded. So we don't need to worry about accessing it atomically. 35 if (!interceptors_initialized) 36 res = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); 37 else 38 res = REAL(mmap)(addr, length, prot, flags, fd, offset); 39 40 if (res != (void *)-1) 41 dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached())); 42 return res; 43 } 44 45 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags, 46 int fd, OFF64_T offset) { 47 void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset); 48 if (res != (void *)-1) 49 dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached())); 50 return res; 51 } 52 53 INTERCEPTOR(int, munmap, void *addr, SIZE_T length) { 54 int res = REAL(munmap)(addr, length); 55 if (res != -1) 56 dfsan_set_label(0, addr, RoundUpTo(length, GetPageSizeCached())); 57 return res; 58 } 59 60 namespace __dfsan { 61 void InitializeInterceptors() { 62 CHECK(!interceptors_initialized); 63 64 INTERCEPT_FUNCTION(mmap); 65 INTERCEPT_FUNCTION(mmap64); 66 INTERCEPT_FUNCTION(munmap); 67 68 interceptors_initialized = true; 69 } 70 } // namespace __dfsan 71