Skip to content

Commit 939f290

Browse files
authored
[compiler-rt][rtsan] getsockopt/setsockopt interception. (#124004)
1 parent 630177c commit 939f290

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,25 @@ INTERCEPTOR(int, accept4, int socket, struct sockaddr *address,
10021002
#define RTSAN_MAYBE_INTERCEPT_ACCEPT4
10031003
#endif
10041004

1005+
#if SANITIZER_INTERCEPT_GETSOCKOPT
1006+
INTERCEPTOR(int, getsockopt, int socket, int level, int option, void *value,
1007+
socklen_t *len) {
1008+
__rtsan_notify_intercepted_call("getsockopt");
1009+
return REAL(getsockopt)(socket, level, option, value, len);
1010+
}
1011+
1012+
INTERCEPTOR(int, setsockopt, int socket, int level, int option,
1013+
const void *value, socklen_t len) {
1014+
__rtsan_notify_intercepted_call("setsockopt");
1015+
return REAL(setsockopt)(socket, level, option, value, len);
1016+
}
1017+
#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT INTERCEPT_FUNCTION(getsockopt)
1018+
#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT INTERCEPT_FUNCTION(setsockopt)
1019+
#else
1020+
#define RTSAN_MAYBE_INTERCEPT_GETSOCKOPT
1021+
#define RTSAN_MAYBE_INTERCEPT_SETSOCKOPT
1022+
#endif
1023+
10051024
// I/O Multiplexing
10061025

10071026
INTERCEPTOR(int, poll, struct pollfd *fds, nfds_t nfds, int timeout) {
@@ -1332,6 +1351,8 @@ void __rtsan::InitializeInterceptors() {
13321351
RTSAN_MAYBE_INTERCEPT_ACCEPT4;
13331352
RTSAN_MAYBE_INTERCEPT_GETSOCKNAME;
13341353
RTSAN_MAYBE_INTERCEPT_GETPEERNAME;
1354+
RTSAN_MAYBE_INTERCEPT_GETSOCKOPT;
1355+
RTSAN_MAYBE_INTERCEPT_SETSOCKOPT;
13351356

13361357
RTSAN_MAYBE_INTERCEPT_SELECT;
13371358
INTERCEPT_FUNCTION(pselect);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,28 @@ TEST(TestRtsanInterceptors, GetpeernameOnASocketDiesWhenRealtime) {
12861286
}
12871287
#endif
12881288

1289+
#if SANITIZER_INTERCEPT_GETSOCKOPT
1290+
TEST(TestRtsanInterceptors, GetsockoptOnASocketDiesWhenRealtime) {
1291+
int val = 0;
1292+
socklen_t len = static_cast<socklen_t>(sizeof(val));
1293+
auto Func = [&val, &len]() {
1294+
getsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, &len);
1295+
};
1296+
ExpectRealtimeDeath(Func, "getsockopt");
1297+
ExpectNonRealtimeSurvival(Func);
1298+
}
1299+
1300+
TEST(TestRtsanInterceptors, SetsockoptOnASocketDiesWhenRealtime) {
1301+
int val = 0;
1302+
socklen_t len = static_cast<socklen_t>(sizeof(val));
1303+
auto Func = [&val, &len]() {
1304+
setsockopt(0, SOL_SOCKET, SO_REUSEADDR, &val, len);
1305+
};
1306+
ExpectRealtimeDeath(Func, "setsockopt");
1307+
ExpectNonRealtimeSurvival(Func);
1308+
}
1309+
#endif
1310+
12891311
/*
12901312
I/O Multiplexing
12911313
*/

0 commit comments

Comments
 (0)