diff --git a/compiler-rt/lib/dfsan/dfsan_custom.cpp b/compiler-rt/lib/dfsan/dfsan_custom.cpp --- a/compiler-rt/lib/dfsan/dfsan_custom.cpp +++ b/compiler-rt/lib/dfsan/dfsan_custom.cpp @@ -11,12 +11,6 @@ // This file defines the custom functions listed in done_abilist.txt. //===----------------------------------------------------------------------===// -#include "sanitizer_common/sanitizer_common.h" -#include "sanitizer_common/sanitizer_internal_defs.h" -#include "sanitizer_common/sanitizer_linux.h" - -#include "dfsan/dfsan.h" - #include #include #include @@ -32,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +35,11 @@ #include #include +#include "dfsan/dfsan.h" +#include "sanitizer_common/sanitizer_common.h" +#include "sanitizer_common/sanitizer_internal_defs.h" +#include "sanitizer_common/sanitizer_linux.h" + using namespace __dfsan; #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) \ @@ -715,6 +715,18 @@ return ret; } +SANITIZER_INTERFACE_ATTRIBUTE +int __dfsw_epoll_wait(int epfd, struct epoll_event *events, int maxevents, + int timeout, dfsan_label epfd_label, + dfsan_label events_label, dfsan_label maxevents_label, + dfsan_label timeout_label, dfsan_label *ret_label) { + int ret = epoll_wait(epfd, events, maxevents, timeout); + if (ret > 0) + dfsan_set_label(0, events, ret * sizeof(*events)); + *ret_label = 0; + return ret; +} + SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_poll(struct pollfd *fds, nfds_t nfds, int timeout, dfsan_label dfs_label, dfsan_label nfds_label, diff --git a/compiler-rt/lib/dfsan/done_abilist.txt b/compiler-rt/lib/dfsan/done_abilist.txt --- a/compiler-rt/lib/dfsan/done_abilist.txt +++ b/compiler-rt/lib/dfsan/done_abilist.txt @@ -184,6 +184,7 @@ fun:calloc=custom fun:clock_gettime=custom fun:dlopen=custom +fun:epoll_wait=custom fun:fgets=custom fun:fstat=custom fun:getcwd=custom diff --git a/compiler-rt/test/dfsan/custom.cpp b/compiler-rt/test/dfsan/custom.cpp --- a/compiler-rt/test/dfsan/custom.cpp +++ b/compiler-rt/test/dfsan/custom.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -638,6 +639,46 @@ ASSERT_READ_ZERO_LABEL(&pwd, 4); } +void test_epoll_wait() { + // Set up a pipe to monitor with epoll. + int pipe_fds[2]; + int ret = pipe(pipe_fds); + assert(ret != -1); + + // Configure epoll to monitor the pipe. + int epfd = epoll_create1(0); + assert(epfd != -1); + struct epoll_event event; + event.events = EPOLLIN; + event.data.fd = pipe_fds[0]; + ret = epoll_ctl(epfd, EPOLL_CTL_ADD, pipe_fds[0], &event); + assert(ret != -1); + + // Test epoll_wait when no events have occurred. + event = {}; + dfsan_set_label(i_label, &event, sizeof(event)); + ret = epoll_wait(epfd, &event, /*maxevents=*/1, /*timeout=*/0); + assert(ret == 0); + assert(event.events == 0); + assert(event.data.fd == 0); + ASSERT_ZERO_LABEL(ret); + ASSERT_READ_LABEL(&event, sizeof(event), i_label); + + // Test epoll_wait when an event occurs. + write(pipe_fds[1], "x", 1); + ret = epoll_wait(epfd, &event, /*maxevents=*/1, /*timeout=*/0); + assert(ret == 1); + assert(event.events == EPOLLIN); + assert(event.data.fd == pipe_fds[0]); + ASSERT_ZERO_LABEL(ret); + ASSERT_READ_ZERO_LABEL(&event, sizeof(event)); + + // Clean up. + close(epfd); + close(pipe_fds[0]); + close(pipe_fds[1]); +} + void test_poll() { struct pollfd fd; fd.fd = 0; @@ -1027,6 +1068,7 @@ test_dfsan_set_write_callback(); test_dl_iterate_phdr(); test_dlopen(); + test_epoll_wait(); test_fgets(); test_fstat(); test_get_current_dir_name();