diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp index 71938d3edba38..f65696ef4ebaf 100644 --- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp @@ -995,6 +995,25 @@ INTERCEPTOR(int, accept4, int socket, struct sockaddr *address, #define RTSAN_MAYBE_INTERCEPT_ACCEPT4 #endif +#if SANITIZER_INTERCEPT_GETSOCKOPT +INTERCEPTOR(int, getsockopt, int socket, int level, int option, void *value, + socklen_t *len) { + __rtsan_notify_intercepted_call("getsockopt"); + return REAL(getsockopt)(socket, level, option, value, len); +} + +INTERCEPTOR(int, setsockopt, int socket, int level, int option, + const void *value, socklen_t len) { + __rtsan_notify_intercepted_call("setsockopt"); + return REAL(setsockopt)(socket, level, option, value, len); +} +#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT INTERCEPT_FUNCTION(getsockopt) +#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT INTERCEPT_FUNCTION(setsockopt) +#else +#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT +#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT +#endif + // I/O Multiplexing INTERCEPTOR(int, poll, struct pollfd *fds, nfds_t nfds, int timeout) { @@ -1325,6 +1344,8 @@ void __rtsan::InitializeInterceptors() { RTSAN_MAYBE_INTERCEPT_ACCEPT4; RTSAN_MAYBE_INTERCEPT_GETSOCKNAME; RTSAN_MAYBE_INTERCEPT_GETPEERNAME; + RTSAN_MAYBE_INTERCEPT_GETSOCKOPT; + RTSAN_MAYBE_INTERCEPT_SETSOCKOPT; RTSAN_MAYBE_INTERCEPT_SELECT; INTERCEPT_FUNCTION(pselect); diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp index 0a59ae0ea9254..07a2c8897e758 100644 --- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp +++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp @@ -1282,6 +1282,28 @@ TEST(TestRtsanInterceptors, GetpeernameOnASocketDiesWhenRealtime) { } #endif +#if SANITIZER_INTERCEPT_GETSOCKOPT +TEST(TestRtsanInterceptors, GetsockoptOnASocketDiesWhenRealtime) { + int val = 0; + socklen_t len = static_cast(sizeof(val)); + auto Func = [&val, &len]() { + getsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, &len); + }; + ExpectRealtimeDeath(Func, "getsockopt"); + ExpectNonRealtimeSurvival(Func); +} + +TEST(TestRtsanInterceptors, SetsockoptOnASocketDiesWhenRealtime) { + int val = 0; + socklen_t len = static_cast(sizeof(val)); + auto Func = [&val, &len]() { + setsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, len); + }; + ExpectRealtimeDeath(Func, "setsockopt"); + ExpectNonRealtimeSurvival(Func); +} +#endif + /* I/O Multiplexing */