Skip to content

Commit 427b24a

Browse files
authored
[compiler-rt][rtsan] adding readlink(at) interception (#126262)
1 parent 8b12acd commit 427b24a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,27 @@ INTERCEPTOR(int, fchdir, int fd) {
254254
return REAL(fchdir)(fd);
255255
}
256256

257+
#if SANITIZER_INTERCEPT_READLINK
258+
INTERCEPTOR(ssize_t, readlink, const char *pathname, char *buf, size_t size) {
259+
__rtsan_notify_intercepted_call("readlink");
260+
return REAL(readlink)(pathname, buf, size);
261+
}
262+
#define RTSAN_MAYBE_INTERCEPT_READLINK INTERCEPT_FUNCTION(readlink)
263+
#else
264+
#define RTSAN_MAYBE_INTERCEPT_READLINK
265+
#endif
266+
267+
#if SANITIZER_INTERCEPT_READLINKAT
268+
INTERCEPTOR(ssize_t, readlinkat, int dirfd, const char *pathname, char *buf,
269+
size_t size) {
270+
__rtsan_notify_intercepted_call("readlinkat");
271+
return REAL(readlinkat)(dirfd, pathname, buf, size);
272+
}
273+
#define RTSAN_MAYBE_INTERCEPT_READLINKAT INTERCEPT_FUNCTION(readlinkat)
274+
#else
275+
#define RTSAN_MAYBE_INTERCEPT_READLINKAT
276+
#endif
277+
257278
// Streams
258279

259280
INTERCEPTOR(FILE *, fopen, const char *path, const char *mode) {
@@ -1402,6 +1423,8 @@ void __rtsan::InitializeInterceptors() {
14021423
INTERCEPT_FUNCTION(close);
14031424
INTERCEPT_FUNCTION(chdir);
14041425
INTERCEPT_FUNCTION(fchdir);
1426+
RTSAN_MAYBE_INTERCEPT_READLINK;
1427+
RTSAN_MAYBE_INTERCEPT_READLINKAT;
14051428
INTERCEPT_FUNCTION(fopen);
14061429
RTSAN_MAYBE_INTERCEPT_FOPEN64;
14071430
RTSAN_MAYBE_INTERCEPT_FREOPEN64;

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,24 @@ TEST(TestRtsanInterceptors, FchdirDiesWhenRealtime) {
457457
ExpectNonRealtimeSurvival(Func);
458458
}
459459

460+
#if SANITIZER_INTERCEPT_READLINK
461+
TEST(TestRtsanInterceptors, ReadlinkDiesWhenRealtime) {
462+
char buf[1024];
463+
auto Func = [&buf]() { readlink("/proc/self", buf, sizeof(buf)); };
464+
ExpectRealtimeDeath(Func, "readlink");
465+
ExpectNonRealtimeSurvival(Func);
466+
}
467+
#endif
468+
469+
#if SANITIZER_INTERCEPT_READLINKAT
470+
TEST(TestRtsanInterceptors, ReadlinkatDiesWhenRealtime) {
471+
char buf[1024];
472+
auto Func = [&buf]() { readlinkat(0, "/proc/self", buf, sizeof(buf)); };
473+
ExpectRealtimeDeath(Func, "readlinkat");
474+
ExpectNonRealtimeSurvival(Func);
475+
}
476+
#endif
477+
460478
TEST_F(RtsanFileTest, FopenDiesWhenRealtime) {
461479
auto Func = [this]() {
462480
FILE *f = fopen(GetTemporaryFilePath(), "w");

0 commit comments

Comments
 (0)