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 namespace __dfsan {
50 void InitializeInterceptors() {
51   CHECK(!interceptors_initialized);
52 
53   INTERCEPT_FUNCTION(mmap);
54   INTERCEPT_FUNCTION(mmap64);
55 
56   interceptors_initialized = true;
57 }
58 }  // namespace __dfsan
59