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
16 changes: 12 additions & 4 deletions compiler-rt/lib/radsan/radsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,27 @@ INTERCEPTOR(int, open, const char *path, int oflag, ...) {
// TODO Establish whether we should intercept here if the flag contains
// O_NONBLOCK
radsan::expectNotRealtime("open");

va_list args;
va_start(args, oflag);
auto result = REAL(open)(path, oflag, args);
const mode_t mode = va_arg(args, int);
va_end(args);

const int result = REAL(open)(path, oflag, mode);
return result;
}

INTERCEPTOR(int, openat, int fd, const char *path, int oflag, ...) {
// TODO Establish whether we should intercept here if the flag contains
// O_NONBLOCK
radsan::expectNotRealtime("openat");

va_list args;
va_start(args, oflag);
auto result = REAL(openat)(fd, path, oflag, args);
mode_t mode = va_arg(args, int);
va_end(args);

const int result = REAL(openat)(fd, path, oflag, mode);
return result;
}

Expand All @@ -79,11 +85,13 @@ INTERCEPTOR(int, creat, const char *path, mode_t mode) {

INTERCEPTOR(int, fcntl, int filedes, int cmd, ...) {
radsan::expectNotRealtime("fcntl");

va_list args;
va_start(args, cmd);
auto result = REAL(fcntl)(filedes, cmd, args);
void *arg = va_arg(args, void *);
va_end(args);
return result;

return fcntl(filedes, cmd, arg);
}

INTERCEPTOR(int, close, int filedes) {
Expand Down
38 changes: 38 additions & 0 deletions compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ TEST(TestRadsanInterceptors, openatDiesWhenRealtime) {
std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, openCreatesFileWithProperMode) {
const int mode = S_IRGRP | S_IROTH | S_IRUSR | S_IWUSR;

const int fd = open(temporary_file_path(), O_CREAT | O_WRONLY, mode);
ASSERT_THAT(fd, Ne(-1));
close(fd);

struct stat st;
ASSERT_THAT(stat(temporary_file_path(), &st), Eq(0));

// Mask st_mode to get permission bits only
ASSERT_THAT(st.st_mode & 0777, Eq(mode));

std::remove(temporary_file_path());
}

TEST(TestRadsanInterceptors, creatDiesWhenRealtime) {
auto func = []() { creat(temporary_file_path(), S_IWOTH | S_IROTH); };
expectRealtimeDeath(func, "creat");
Expand All @@ -192,6 +208,28 @@ TEST(TestRadsanInterceptors, fcntlDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}

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

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

ASSERT_THAT(fcntl(fd, F_GETLK, &lock), Eq(0));
ASSERT_THAT(lock.l_type, F_UNLCK);
};
expectRealtimeDeath(func, "fcntl");
expectNonrealtimeSurvival(func);

close(fd);
std::remove(temporary_file_path());
}

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