Skip to content

[compiler-rt][rtsan] getsockopt/setsockopt interception. #124004

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
Jan 22, 2025
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
21 changes: 21 additions & 0 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,28 @@ TEST(TestRtsanInterceptors, GetpeernameOnASocketDiesWhenRealtime) {
}
#endif

#if SANITIZER_INTERCEPT_GETSOCKOPT
TEST(TestRtsanInterceptors, GetsockoptOnASocketDiesWhenRealtime) {
int val = 0;
socklen_t len = static_cast<socklen_t>(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<socklen_t>(sizeof(val));
auto Func = [&val, &len]() {
setsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, len);
};
ExpectRealtimeDeath(Func, "setsockopt");
ExpectNonRealtimeSurvival(Func);
}
#endif

/*
I/O Multiplexing
*/
Expand Down
Loading