Skip to content

Commit a8eb12c

Browse files
authored
[compiler-rt] Reapply freadlink interception for macOs. (#110917)
Fixed test, needed explicit O_SYMLINK on symbolic link opening.
1 parent e5fae76 commit a8eb12c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10350,6 +10350,23 @@ INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
1035010350
#define INIT_PWRITEV2
1035110351
#endif
1035210352

10353+
#if SANITIZER_INTERCEPT_FREADLINK
10354+
INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
10355+
void *ctx;
10356+
COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
10357+
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
10358+
SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
10359+
if (res > 0)
10360+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
10361+
if (res >= 0 && fd > 0)
10362+
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
10363+
return res;
10364+
}
10365+
# define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
10366+
#else
10367+
# define INIT_FREADLINK
10368+
#endif
10369+
1035310370
#include "sanitizer_common_interceptors_netbsd_compat.inc"
1035410371

1035510372
namespace __sanitizer {
@@ -10671,6 +10688,7 @@ static void InitializeCommonInterceptors() {
1067110688
INIT_CPUSET_GETAFFINITY;
1067210689
INIT_PREADV2;
1067310690
INIT_PWRITEV2;
10691+
INIT_FREADLINK;
1067410692

1067510693
INIT___PRINTF_CHK;
1067610694
}

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,13 @@
607607
// FIXME: also available from musl 1.2.5
608608
#define SANITIZER_INTERCEPT_PREADV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
609609
#define SANITIZER_INTERCEPT_PWRITEV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
610-
610+
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
611+
__MAC_OS_X_VERSION_MIN_REQUIRED >= 130000
612+
# define SI_MAC_OS_DEPLOYMENT_MIN_13_00 1
613+
#else
614+
# define SI_MAC_OS_DEPLOYMENT_MIN_13_00 0
615+
#endif
616+
#define SANITIZER_INTERCEPT_FREADLINK (SI_MAC && SI_MAC_OS_DEPLOYMENT_MIN_13_00)
611617
// This macro gives a way for downstream users to override the above
612618
// interceptor macros irrespective of the platform they are on. They have
613619
// to do two things:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// RUN: %clang -O0 %s -o %t && %run %t
3+
4+
#include <assert.h>
5+
#include <fcntl.h>
6+
#include <limits.h>
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <sys/types.h>
10+
#include <unistd.h>
11+
12+
int main(int argc, char **argv) {
13+
char symlink_path[PATH_MAX];
14+
snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0],
15+
getpid());
16+
remove(symlink_path);
17+
int res = symlink(argv[0], symlink_path);
18+
assert(!res);
19+
20+
int fd;
21+
char readlink_path[PATH_MAX];
22+
fd = open(symlink_path, O_RDONLY | O_SYMLINK);
23+
assert(fd > 0);
24+
ssize_t res2 = freadlink(fd, readlink_path, sizeof(readlink_path));
25+
assert(res2 >= 0);
26+
readlink_path[res2] = '\0';
27+
assert(!strcmp(readlink_path, argv[0]));
28+
close(fd);
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)