Skip to content

Commit bc1c84a

Browse files
devnexenvitalybuka
andauthored
[compiler-rt] adding preadv2/pwritev2 interceptions. (#97216)
Co-authored-by: Vitaly Buka <[email protected]>
1 parent 1347b9a commit bc1c84a

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10259,6 +10259,38 @@ INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T
1025910259
#define INIT_CPUSET_GETAFFINITY
1026010260
#endif
1026110261

10262+
#if SANITIZER_INTERCEPT_PREADV2
10263+
INTERCEPTOR(SSIZE_T, preadv2, int fd, __sanitizer_iovec *iov, int iovcnt,
10264+
OFF_T offset, int flags) {
10265+
void *ctx;
10266+
COMMON_INTERCEPTOR_ENTER(ctx, preadv2, fd, iov, iovcnt, offset, flags);
10267+
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10268+
SSIZE_T res = REAL(preadv2)(fd, iov, iovcnt, offset, flags);
10269+
if (res > 0) write_iovec(ctx, iov, iovcnt, res);
10270+
if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10271+
return res;
10272+
}
10273+
#define INIT_PREADV2 COMMON_INTERCEPT_FUNCTION(preadv2)
10274+
#else
10275+
#define INIT_PREADV2
10276+
#endif
10277+
10278+
#if SANITIZER_INTERCEPT_PWRITEV2
10279+
INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
10280+
OFF_T offset, int flags) {
10281+
void *ctx;
10282+
COMMON_INTERCEPTOR_ENTER(ctx, pwritev2, fd, iov, iovcnt, offset, flags);
10283+
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10284+
if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
10285+
SSIZE_T res = REAL(pwritev2)(fd, iov, iovcnt, offset, flags);
10286+
if (res > 0) read_iovec(ctx, iov, iovcnt, res);
10287+
return res;
10288+
}
10289+
#define INIT_PWRITEV2 COMMON_INTERCEPT_FUNCTION(pwritev2)
10290+
#else
10291+
#define INIT_PWRITEV2
10292+
#endif
10293+
1026210294
#include "sanitizer_common_interceptors_netbsd_compat.inc"
1026310295

1026410296
namespace __sanitizer {
@@ -10578,6 +10610,8 @@ static void InitializeCommonInterceptors() {
1057810610
INIT___XUNAME;
1057910611
INIT_ARGP_PARSE;
1058010612
INIT_CPUSET_GETAFFINITY;
10613+
INIT_PREADV2;
10614+
INIT_PWRITEV2;
1058110615

1058210616
INIT___PRINTF_CHK;
1058310617
}

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@
598598
#define SANITIZER_INTERCEPT_PROCCTL SI_FREEBSD
599599
#define SANITIZER_INTERCEPT_ARGP_PARSE SI_GLIBC
600600
#define SANITIZER_INTERCEPT_CPUSET_GETAFFINITY SI_FREEBSD
601+
// FIXME: also available from musl 1.2.5
602+
#define SANITIZER_INTERCEPT_PREADV2 SI_GLIBC
603+
#define SANITIZER_INTERCEPT_PWRITEV2 SI_GLIBC
601604

602605
// This macro gives a way for downstream users to override the above
603606
// interceptor macros irrespective of the platform they are on. They have
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %clangxx -O0 %s -o %t
2+
3+
// REQUIRES: glibc
4+
5+
#include <assert.h>
6+
#include <fcntl.h>
7+
#include <sys/uio.h>
8+
#include <unistd.h>
9+
10+
int main(void) {
11+
int fd = open("/proc/self/stat", O_RDONLY);
12+
char bufa[7];
13+
char bufb[7];
14+
struct iovec vec[2];
15+
vec[0].iov_base = bufa + 4;
16+
vec[0].iov_len = 1;
17+
vec[1].iov_base = bufb;
18+
vec[1].iov_len = sizeof(bufb);
19+
ssize_t rd = preadv2(fd, vec, 2, 0, 0);
20+
assert(rd > 0);
21+
vec[0].iov_base = bufa;
22+
rd = preadv2(fd, vec, 2, 0, 0);
23+
assert(rd > 0);
24+
rd = preadv2(fd, vec, 5, -25, 0);
25+
assert(rd < 0);
26+
close(fd);
27+
return 0;
28+
}

0 commit comments

Comments
 (0)