llvm-project
128 строк · 4.1 Кб
1//===-- sanitizer_common_interceptors_netbsd_compat.inc ---------*- C++ -*-===//
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// Common function interceptors for tools like AddressSanitizer,
10// ThreadSanitizer, MemorySanitizer, etc.
11//
12// Interceptors for NetBSD old function calls that have been versioned.
13//
14// NetBSD minimal version supported 9.0.
15// NetBSD current version supported 9.99.26.
16//
17//===----------------------------------------------------------------------===//
18
19#if SANITIZER_NETBSD
20
21// First undef all mangled symbols.
22// Next, define compat interceptors.
23// Finally, undef INIT_ and redefine it.
24// This allows to avoid preprocessor issues.
25
26#undef fstatvfs
27#undef fstatvfs1
28#undef getmntinfo
29#undef getvfsstat
30#undef statvfs
31#undef statvfs1
32
33INTERCEPTOR(int, statvfs, char *path, void *buf) {
34void *ctx;
35COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
36if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
37// FIXME: under ASan the call below may write to freed memory and corrupt
38// its metadata. See
39// https://github.com/google/sanitizers/issues/321.
40int res = REAL(statvfs)(path, buf);
41if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs90_sz);
42return res;
43}
44
45INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
46void *ctx;
47COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
48COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
49// FIXME: under ASan the call below may write to freed memory and corrupt
50// its metadata. See
51// https://github.com/google/sanitizers/issues/321.
52int res = REAL(fstatvfs)(fd, buf);
53if (!res) {
54COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs90_sz);
55if (fd >= 0)
56COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
57}
58return res;
59}
60
61#undef INIT_STATVFS
62#define INIT_STATVFS \
63COMMON_INTERCEPT_FUNCTION(statvfs); \
64COMMON_INTERCEPT_FUNCTION(fstatvfs); \
65COMMON_INTERCEPT_FUNCTION(__statvfs90); \
66COMMON_INTERCEPT_FUNCTION(__fstatvfs90)
67
68INTERCEPTOR(int, __getmntinfo13, void **mntbufp, int flags) {
69void *ctx;
70COMMON_INTERCEPTOR_ENTER(ctx, __getmntinfo13, mntbufp, flags);
71int cnt = REAL(__getmntinfo13)(mntbufp, flags);
72if (cnt > 0 && mntbufp) {
73COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
74if (*mntbufp)
75COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs90_sz);
76}
77return cnt;
78}
79
80#undef INIT_GETMNTINFO
81#define INIT_GETMNTINFO \
82COMMON_INTERCEPT_FUNCTION(__getmntinfo13); \
83COMMON_INTERCEPT_FUNCTION(__getmntinfo90)
84
85INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
86void *ctx;
87COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
88int ret = REAL(getvfsstat)(buf, bufsize, flags);
89if (buf && ret > 0)
90COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs90_sz);
91return ret;
92}
93
94#undef INIT_GETVFSSTAT
95#define INIT_GETVFSSTAT \
96COMMON_INTERCEPT_FUNCTION(getvfsstat); \
97COMMON_INTERCEPT_FUNCTION(__getvfsstat90)
98
99INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
100void *ctx;
101COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
102if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
103int res = REAL(statvfs1)(path, buf, flags);
104if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs90_sz);
105return res;
106}
107
108INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
109void *ctx;
110COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
111COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
112int res = REAL(fstatvfs1)(fd, buf, flags);
113if (!res) {
114COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs90_sz);
115if (fd >= 0)
116COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
117}
118return res;
119}
120
121#undef INIT_STATVFS1
122#define INIT_STATVFS1 \
123COMMON_INTERCEPT_FUNCTION(statvfs1); \
124COMMON_INTERCEPT_FUNCTION(fstatvfs1); \
125COMMON_INTERCEPT_FUNCTION(__statvfs190); \
126COMMON_INTERCEPT_FUNCTION(__fstatvfs190)
127
128#endif
129