Skip to content

[compiler-rt] intercept macOs's freadlink call. #83679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10299,6 +10299,24 @@ INTERCEPTOR(SSIZE_T, pwritev2, int fd, __sanitizer_iovec *iov, int iovcnt,
#define INIT_PWRITEV2
#endif

#if SANITIZER_INTERCEPT_FREADLINK
INTERCEPTOR(SSIZE_T, freadlink, int fd, char *buf, SIZE_T bufsiz) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, freadlink, fd, buf, bufsiz);
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
SSIZE_T res = REAL(freadlink)(fd, buf, bufsiz);
if (res > 0)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
if (res >= 0 && fd > 0)
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
return res;
}

# define INIT_FREADLINK COMMON_INTERCEPT_FUNCTION(freadlink)
#else
# define INIT_FREADLINK
#endif

#include "sanitizer_common_interceptors_netbsd_compat.inc"

namespace __sanitizer {
Expand Down Expand Up @@ -10620,6 +10638,7 @@ static void InitializeCommonInterceptors() {
INIT_CPUSET_GETAFFINITY;
INIT_PREADV2;
INIT_PWRITEV2;
INIT_FREADLINK;

INIT___PRINTF_CHK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@
// FIXME: also available from musl 1.2.5
#define SANITIZER_INTERCEPT_PREADV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
#define SANITIZER_INTERCEPT_PWRITEV2 (SI_LINUX && __GLIBC_PREREQ(2, 26))
#define SANITIZER_INTERCEPT_FREADLINK SI_MAC

// This macro gives a way for downstream users to override the above
// interceptor macros irrespective of the platform they are on. They have
Expand Down
29 changes: 29 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/Darwin/freadlink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %clang -O0 %s -o %t && %run %t

#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv) {
char symlink_path[PATH_MAX];
snprintf(symlink_path, sizeof(symlink_path), "%s_%d.symlink", argv[0],
getpid());
remove(symlink_path);
int res = symlink(argv[0], symlink_path);
assert(!res);

int fd;
char readlink_path[PATH_MAX];
fd = open(symlink_path, O_RDONLY);
ssize_t res2 = freadlink(fd, readlink_path, sizeof(readlink_path));
assert(res2 >= 0);
readlink_path[res2] = '\0';
assert(!strcmp(readlink_path, argv[0]));
close(fd);

return 0;
}
Loading