Skip to content

Commit

Permalink
[DFSan] Add custom wrapper for epoll_wait.
Browse files Browse the repository at this point in the history
The wrapper clears shadow for any events written.

Reviewed By: stephan.yichao.zhao

Differential Revision: https://reviews.llvm.org/D92891
  • Loading branch information
morehouse committed Dec 9, 2020
1 parent 10edd10 commit 6f13445
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
24 changes: 18 additions & 6 deletions compiler-rt/lib/dfsan/dfsan_custom.cpp
Expand Up @@ -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 <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
Expand All @@ -32,6 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/stat.h>
Expand All @@ -40,6 +35,11 @@
#include <time.h>
#include <unistd.h>

#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, ...) \
Expand Down Expand Up @@ -715,6 +715,18 @@ int __dfsw_getpwuid_r(id_t uid, struct passwd *pwd,
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,
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/dfsan/done_abilist.txt
Expand Up @@ -184,6 +184,7 @@ fun:uselocale=discard
fun:calloc=custom
fun:clock_gettime=custom
fun:dlopen=custom
fun:epoll_wait=custom
fun:fgets=custom
fun:fstat=custom
fun:getcwd=custom
Expand Down
42 changes: 42 additions & 0 deletions compiler-rt/test/dfsan/custom.cpp
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -638,6 +639,46 @@ void test_getpwuid_r() {
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;
Expand Down Expand Up @@ -1027,6 +1068,7 @@ int main(void) {
test_dfsan_set_write_callback();
test_dl_iterate_phdr();
test_dlopen();
test_epoll_wait();
test_fgets();
test_fstat();
test_get_current_dir_name();
Expand Down

0 comments on commit 6f13445

Please sign in to comment.