Skip to content

Commit a6ea0b0

Browse files
authored
[compiler-rt] intercept macOs's freadlink call. (#83679)
available since macOs Ventura.
1 parent c08a437 commit a6ea0b0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10342,6 +10342,24 @@ INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
1034210342
#define INIT_PWRITEV2
1034310343
#endif
1034410344

10345+
#if SANITIZER_INTERCEPT_FREADLINK
10346+
INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
10347+
void *ctx;
10348+
COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
10349+
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10350+
SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
10351+
if (res > 0)
10352+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
10353+
if (res >= 0 && fd > 0)
10354+
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10355+
return res;
10356+
}
10357+
10358+
# define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
10359+
#else
10360+
# define INIT_FREADLINK
10361+
#endif
10362+
1034510363
#include "sanitizer_common_interceptors_netbsd_compat.inc"
1034610364

1034710365
namespace __sanitizer {
@@ -10663,6 +10681,7 @@ static void InitializeCommonInterceptors() {
1066310681
INIT_CPUSET_GETAFFINITY;
1066410682
INIT_PREADV2;
1066510683
INIT_PWRITEV2;
10684+
INIT_FREADLINK;
1066610685

1066710686
INIT___PRINTF_CHK;
1066810687
}

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@
606606
// FIXME: also available from musl 1.2.5
607607
#define SANITIZER_INTERCEPT_PREADV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
608608
#define SANITIZER_INTERCEPT_PWRITEV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
609+
#define SANITIZER_INTERCEPT_FREADLINK SI_MAC
609610

610611
// This macro gives a way for downstream users to override the above
611612
// interceptor macros irrespective of the platform they are on. They have
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang -O0 %s -o %t && %run %t
2+
3+
#include <assert.h>
4+
#include <fcntl.h>
5+
#include <limits.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <sys/types.h>
9+
#include <unistd.h>
10+
11+
int main(int argc, char **argv) {
12+
char symlink_path[PATH_MAX];
13+
snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0],
14+
getpid());
15+
remove(symlink_path);
16+
int res = symlink(argv[0], symlink_path);
17+
assert(!res);
18+
19+
int fd;
20+
char readlink_path[PATH_MAX];
21+
fd = open(symlink_path, O_RDONLY);
22+
ssize_t res2 = freadlink(fd, readlink_path, sizeof(readlink_path));
23+
assert(res2 >= 0);
24+
readlink_path[res2] = '\0';
25+
assert(!strcmp(readlink_path, argv[0]));
26+
close(fd);
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)