Skip to content
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
14 changes: 12 additions & 2 deletions compiler-rt/lib/radsan/radsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,20 @@ INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {

va_list args;
va_start(args, cmd);
void *arg = va_arg(args, void *);

// Following precedent here. The linux source (fcntl.c, do_fcntl) accepts the
// final argument in a variable that will hold the largest of the possible
// argument types (pointers and ints are typical in fcntl) It is then assumed
// that the implementation of fcntl will cast it properly depending on cmd.
//
// This is also similar to what is done in
// sanitizer_common/sanitizer_common_syscalls.inc
const unsigned long arg = va_arg(args, unsigned long);
int result = REAL(fcntl)(filedes, cmd, arg);

va_end(args);

return fcntl(filedes, cmd, arg);
return result;
}

INTERCEPTOR(int, close, int filedes) {
Expand Down
26 changes: 24 additions & 2 deletions compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) {
ASSERT_THAT(fd, Ne(-1));

auto func = [fd]() {
struct flock lock{};
struct flock lock {};
lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_len = 0;
lock.l_pid = ::getpid();

ASSERT_THAT(fcntl(fd, F_GETLK, &lock), Eq(0));
Expand All @@ -230,6 +230,28 @@ TEST(TestRadsanInterceptors, fcntlFlockDiesWhenRealtime) {
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, fcntlSetFdDiesWhenRealtime) {
int fd = creat(temporary_file_path(), S_IRUSR | S_IWUSR);
ASSERT_THAT(fd, Ne(-1));

auto func = [fd]() {
int old_flags = fcntl(fd, F_GETFD);
ASSERT_THAT(fcntl(fd, F_SETFD, FD_CLOEXEC), Eq(0));

int flags = fcntl(fd, F_GETFD);
ASSERT_THAT(flags, Ne(-1));
ASSERT_THAT(flags & FD_CLOEXEC, Eq(FD_CLOEXEC));

ASSERT_THAT(fcntl(fd, F_SETFD, old_flags), Eq(0));
ASSERT_THAT(fcntl(fd, F_GETFD), Eq(old_flags));
};

expectRealtimeDeath(func, "fcntl");
expectNonrealtimeSurvival(func);

close(fd);
}

TEST(TestRadsanInterceptors, closeDiesWhenRealtime) {
auto func = []() { close(0); };
expectRealtimeDeath(func, "close");
Expand Down