Skip to content

Commit 8699f30

Browse files
authored
[rtsan] Add more socket interceptors (#115020)
Adds getaddrinfo, getnameinfo, bind, listen, accept and connect
1 parent b5d8a03 commit 8699f30

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,42 @@ INTERCEPTOR(int, shm_unlink, const char *name) {
535535
}
536536

537537
// Sockets
538+
INTERCEPTOR(int, getaddrinfo, const char *node, const char *service,
539+
const struct addrinfo *hints, struct addrinfo **res) {
540+
__rtsan_notify_intercepted_call("getaddrinfo");
541+
return REAL(getaddrinfo)(node, service, hints, res);
542+
}
543+
544+
INTERCEPTOR(int, getnameinfo, const struct sockaddr *sa, socklen_t salen,
545+
char *host, socklen_t hostlen, char *serv, socklen_t servlen,
546+
int flags) {
547+
__rtsan_notify_intercepted_call("getnameinfo");
548+
return REAL(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
549+
}
550+
551+
INTERCEPTOR(int, bind, int socket, const struct sockaddr *address,
552+
socklen_t address_len) {
553+
__rtsan_notify_intercepted_call("bind");
554+
return REAL(bind)(socket, address, address_len);
555+
}
556+
557+
INTERCEPTOR(int, listen, int socket, int backlog) {
558+
__rtsan_notify_intercepted_call("listen");
559+
return REAL(listen)(socket, backlog);
560+
}
561+
562+
INTERCEPTOR(int, accept, int socket, struct sockaddr *address,
563+
socklen_t *address_len) {
564+
__rtsan_notify_intercepted_call("accept");
565+
return REAL(accept)(socket, address, address_len);
566+
}
567+
568+
INTERCEPTOR(int, connect, int socket, const struct sockaddr *address,
569+
socklen_t address_len) {
570+
__rtsan_notify_intercepted_call("connect");
571+
return REAL(connect)(socket, address, address_len);
572+
}
573+
538574
INTERCEPTOR(int, socket, int domain, int type, int protocol) {
539575
__rtsan_notify_intercepted_call("socket");
540576
return REAL(socket)(domain, type, protocol);
@@ -648,14 +684,20 @@ void __rtsan::InitializeInterceptors() {
648684
INTERCEPT_FUNCTION(usleep);
649685
INTERCEPT_FUNCTION(nanosleep);
650686

651-
INTERCEPT_FUNCTION(socket);
687+
INTERCEPT_FUNCTION(accept);
688+
INTERCEPT_FUNCTION(bind);
689+
INTERCEPT_FUNCTION(connect);
690+
INTERCEPT_FUNCTION(getaddrinfo);
691+
INTERCEPT_FUNCTION(getnameinfo);
692+
INTERCEPT_FUNCTION(listen);
693+
INTERCEPT_FUNCTION(recv);
694+
INTERCEPT_FUNCTION(recvfrom);
695+
INTERCEPT_FUNCTION(recvmsg);
652696
INTERCEPT_FUNCTION(send);
653697
INTERCEPT_FUNCTION(sendmsg);
654698
INTERCEPT_FUNCTION(sendto);
655-
INTERCEPT_FUNCTION(recv);
656-
INTERCEPT_FUNCTION(recvmsg);
657-
INTERCEPT_FUNCTION(recvfrom);
658699
INTERCEPT_FUNCTION(shutdown);
700+
INTERCEPT_FUNCTION(socket);
659701
}
660702

661703
#endif // SANITIZER_POSIX

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#endif
3030

3131
#include <fcntl.h>
32+
#include <netdb.h>
3233
#include <pthread.h>
3334
#include <stdio.h>
3435
#include <sys/mman.h>
@@ -46,6 +47,11 @@ using namespace testing;
4647
using namespace rtsan_testing;
4748
using namespace std::chrono_literals;
4849

50+
// NOTE: In the socket tests we pass in bad info to the calls to ensure they
51+
// fail which is why we EXPECT_NE 0 for their return codes.
52+
// We just care that the call is intercepted
53+
const int kNotASocketFd = 0;
54+
4955
void *FakeThreadEntryPoint(void *) { return nullptr; }
5056

5157
class RtsanFileTest : public ::testing::Test {
@@ -676,6 +682,49 @@ TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
676682
/*
677683
Sockets
678684
*/
685+
TEST(TestRtsanInterceptors, GetAddrInfoDiesWhenRealtime) {
686+
auto Func = []() {
687+
addrinfo *info{};
688+
getaddrinfo("localhost", "http", nullptr, &info);
689+
};
690+
ExpectRealtimeDeath(Func, "getaddrinfo");
691+
ExpectNonRealtimeSurvival(Func);
692+
}
693+
694+
TEST(TestRtsanInterceptors, GetNameInfoDiesWhenRealtime) {
695+
auto Func = []() {
696+
char host[NI_MAXHOST];
697+
char serv[NI_MAXSERV];
698+
getnameinfo(nullptr, 0, host, NI_MAXHOST, serv, NI_MAXSERV, 0);
699+
};
700+
ExpectRealtimeDeath(Func, "getnameinfo");
701+
ExpectNonRealtimeSurvival(Func);
702+
}
703+
704+
TEST(TestRtsanInterceptors, BindingASocketDiesWhenRealtime) {
705+
auto Func = []() { EXPECT_NE(bind(kNotASocketFd, nullptr, 0), 0); };
706+
ExpectRealtimeDeath(Func, "bind");
707+
ExpectNonRealtimeSurvival(Func);
708+
}
709+
710+
TEST(TestRtsanInterceptors, ListeningOnASocketDiesWhenRealtime) {
711+
auto Func = []() { EXPECT_NE(listen(kNotASocketFd, 0), 0); };
712+
ExpectRealtimeDeath(Func, "listen");
713+
ExpectNonRealtimeSurvival(Func);
714+
}
715+
716+
TEST(TestRtsanInterceptors, AcceptingASocketDiesWhenRealtime) {
717+
auto Func = []() { EXPECT_LT(accept(kNotASocketFd, nullptr, nullptr), 0); };
718+
ExpectRealtimeDeath(Func, "accept");
719+
ExpectNonRealtimeSurvival(Func);
720+
}
721+
722+
TEST(TestRtsanInterceptors, ConnectingASocketDiesWhenRealtime) {
723+
auto Func = []() { EXPECT_NE(connect(kNotASocketFd, nullptr, 0), 0); };
724+
ExpectRealtimeDeath(Func, "connect");
725+
ExpectNonRealtimeSurvival(Func);
726+
}
727+
679728
TEST(TestRtsanInterceptors, OpeningASocketDiesWhenRealtime) {
680729
auto Func = []() { socket(PF_INET, SOCK_STREAM, 0); };
681730
ExpectRealtimeDeath(Func, "socket");

0 commit comments

Comments
 (0)