sanitizer_common_interceptors.inc revision c20b321d49f0eff60f1394d56e623d8ca94f24d7
1//===-- sanitizer_common_interceptors.h -------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Common function interceptors for tools like AddressSanitizer,
11// ThreadSanitizer, MemorySanitizer, etc.
12//
13// This file should be included into the tool's interceptor file,
14// which has to define it's own macros:
15//   COMMON_INTERCEPTOR_ENTER
16//   COMMON_INTERCEPTOR_READ_RANGE
17//   COMMON_INTERCEPTOR_WRITE_RANGE
18//   COMMON_INTERCEPTOR_FD_ACQUIRE
19//   COMMON_INTERCEPTOR_FD_RELEASE
20//   COMMON_INTERCEPTOR_SET_THREAD_NAME
21//===----------------------------------------------------------------------===//
22#ifndef SANITIZER_COMMON_INTERCEPTORS_H
23#define SANITIZER_COMMON_INTERCEPTORS_H
24
25#include "interception/interception.h"
26#include "sanitizer_platform_interceptors.h"
27
28#if SANITIZER_INTERCEPT_READ
29INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
30  COMMON_INTERCEPTOR_ENTER(read, fd, ptr, count);
31  SSIZE_T res = REAL(read)(fd, ptr, count);
32  if (res > 0)
33    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
34  if (res >= 0 && fd >= 0)
35    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
36  return res;
37}
38# define INIT_READ INTERCEPT_FUNCTION(read)
39#else
40# define INIT_READ
41#endif
42
43#if SANITIZER_INTERCEPT_PREAD
44INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
45  COMMON_INTERCEPTOR_ENTER(pread, fd, ptr, count, offset);
46  SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
47  if (res > 0)
48    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
49  if (res >= 0 && fd >= 0)
50    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
51  return res;
52}
53# define INIT_PREAD INTERCEPT_FUNCTION(pread)
54#else
55# define INIT_PREAD
56#endif
57
58#if SANITIZER_INTERCEPT_PREAD64
59INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
60  COMMON_INTERCEPTOR_ENTER(pread64, fd, ptr, count, offset);
61  SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
62  if (res > 0)
63    COMMON_INTERCEPTOR_WRITE_RANGE(ptr, res);
64  if (res >= 0 && fd >= 0)
65    COMMON_INTERCEPTOR_FD_ACQUIRE(fd);
66  return res;
67}
68# define INIT_PREAD64 INTERCEPT_FUNCTION(pread64)
69#else
70# define INIT_PREAD64
71#endif
72
73#if SANITIZER_INTERCEPT_WRITE
74INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
75  COMMON_INTERCEPTOR_ENTER(write, fd, ptr, count);
76  if (fd >= 0)
77    COMMON_INTERCEPTOR_FD_RELEASE(fd);
78  SSIZE_T res = REAL(write)(fd, ptr, count);
79  if (res > 0)
80    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
81  return res;
82}
83# define INIT_WRITE INTERCEPT_FUNCTION(write)
84#else
85# define INIT_WRITE
86#endif
87
88#if SANITIZER_INTERCEPT_PWRITE
89INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count) {
90  COMMON_INTERCEPTOR_ENTER(pwrite, fd, ptr, count);
91  if (fd >= 0)
92    COMMON_INTERCEPTOR_FD_RELEASE(fd);
93  SSIZE_T res = REAL(pwrite)(fd, ptr, count);
94  if (res > 0)
95    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
96  return res;
97}
98# define INIT_PWRITE INTERCEPT_FUNCTION(pwrite)
99#else
100# define INIT_PWRITE
101#endif
102
103#if SANITIZER_INTERCEPT_PWRITE64
104INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count) {
105  COMMON_INTERCEPTOR_ENTER(pwrite64, fd, ptr, count);
106  if (fd >= 0)
107    COMMON_INTERCEPTOR_FD_RELEASE(fd);
108  SSIZE_T res = REAL(pwrite64)(fd, ptr, count);
109  if (res > 0)
110    COMMON_INTERCEPTOR_READ_RANGE(ptr, res);
111  return res;
112}
113# define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
114#else
115# define INIT_PWRITE64
116#endif
117
118#if SANITIZER_INTERCEPT_PRCTL
119INTERCEPTOR(int, prctl, int option,
120            unsigned long arg2, unsigned long arg3,  // NOLINT
121            unsigned long arg4, unsigned long arg5) {  // NOLINT
122  COMMON_INTERCEPTOR_ENTER(prctl, option, arg2, arg3, arg4, arg5);
123  static const int PR_SET_NAME = 15;
124  int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
125  if (option == PR_SET_NAME) {
126    char buff[16];
127    internal_strncpy(buff, (char*)arg2, 15);
128    buff[15] = 0;
129    COMMON_INTERCEPTOR_SET_THREAD_NAME(buff);
130  }
131  return res;
132}
133# define INIT_PRCTL INTERCEPT_FUNCTION(prctl)
134#else
135# define INIT_PRCTL
136#endif  // SANITIZER_INTERCEPT_PRCTL
137
138#define SANITIZER_COMMON_INTERCEPTORS_INIT \
139  INIT_READ;                               \
140  INIT_PREAD;                              \
141  INIT_PREAD64;                            \
142  INIT_PRCTL;                              \
143  INIT_WRITE;                              \
144
145#endif  // SANITIZER_COMMON_INTERCEPTORS_H
146