xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_interceptors.cpp (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 "dfsan/dfsan.h"
15 #include "interception/interception.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 
18 using namespace __sanitizer;
19 
20 INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
21             int fd, OFF_T offset) {
22   void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
23   if (res != (void*)-1)
24     dfsan_set_label(0, res, RoundUpTo(length, GetPageSize()));
25   return res;
26 }
27 
28 INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
29             int fd, OFF64_T offset) {
30   void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
31   if (res != (void*)-1)
32     dfsan_set_label(0, res, RoundUpTo(length, GetPageSize()));
33   return res;
34 }
35 
36 namespace __dfsan {
37 void InitializeInterceptors() {
38   static int inited = 0;
39   CHECK_EQ(inited, 0);
40 
41   INTERCEPT_FUNCTION(mmap);
42   INTERCEPT_FUNCTION(mmap64);
43   inited = 1;
44 }
45 }  // namespace __dfsan
46