Skip to content

Commit

Permalink
Add a new interceptors for statvfs1(2) and fstatvfs1(2) from NetBSD
Browse files Browse the repository at this point in the history
Summary:
statvfs1, fstatvfs1 - get file system statistics.

While there, use file descriptor related macros in the fstatvfs interceptor.

Add a dedicated test.

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

Subscribers: dvyukov, kubamracek, mgorny, llvm-commits, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D55031

llvm-svn: 348656
  • Loading branch information
krytarowski committed Dec 7, 2018
1 parent b754f7a commit 2f5fd17
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4277,11 +4277,16 @@ INTERCEPTOR(int, statvfs, char *path, void *buf) {
INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
// FIXME: under ASan the call below may write to freed memory and corrupt
// its metadata. See
// https://github.com/google/sanitizers/issues/321.
int res = REAL(fstatvfs)(fd, buf);
if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
if (!res) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
if (fd >= 0)
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
}
return res;
}
#define INIT_STATVFS \
Expand Down Expand Up @@ -7776,6 +7781,34 @@ INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
#define INIT_FPARSELN
#endif

#if SANITIZER_INTERCEPT_STATVFS1
INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
int res = REAL(statvfs1)(path, buf, flags);
if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
return res;
}
INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
int res = REAL(fstatvfs1)(fd, buf, flags);
if (!res) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
if (fd >= 0)
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
}
return res;
}
#define INIT_STATVFS1 \
COMMON_INTERCEPT_FUNCTION(statvfs1); \
COMMON_INTERCEPT_FUNCTION(fstatvfs1);
#else
#define INIT_STATVFS1
#endif

static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
Expand Down Expand Up @@ -8042,6 +8075,7 @@ static void InitializeCommonInterceptors() {
INIT_MODCTL;
INIT_STRTONUM;
INIT_FPARSELN;
INIT_STATVFS1;

INIT___PRINTF_CHK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,6 @@
#define SANITIZER_INTERCEPT_MODCTL SI_NETBSD
#define SANITIZER_INTERCEPT_STRTONUM SI_NETBSD
#define SANITIZER_INTERCEPT_FPARSELN SI_NETBSD
#define SANITIZER_INTERCEPT_STATVFS1 SI_NETBSD

#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
58 changes: 58 additions & 0 deletions compiler-rt/test/sanitizer_common/TestCases/NetBSD/statvfs1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s

#include <sys/param.h>
#include <sys/types.h>

#include <sys/statvfs.h>

#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void test_statvfs1() {
printf("statvfs1\n");

struct statvfs buf;
int rv = statvfs1("/etc/fstab", &buf, ST_WAIT);
assert(rv != -1);

printf("fstypename='%s'\n", buf.f_fstypename);
printf("mntonname='%s'\n", buf.f_mntonname);
printf("mntfromname='%s'\n", buf.f_mntfromname);
}

void test_fstatvfs1() {
printf("fstatvfs1\n");

int fd = open("/etc/fstab", O_RDONLY);
assert(fd > 0);

struct statvfs buf;
int rv = fstatvfs1(fd, &buf, ST_WAIT);
assert(rv != -1);

printf("fstypename='%s'\n", buf.f_fstypename);
printf("mntonname='%s'\n", buf.f_mntonname);
printf("mntfromname='%s'\n", buf.f_mntfromname);

rv = close(fd);
assert(rv != -1);
}

int main(void) {
test_statvfs1();
test_fstatvfs1();

// CHECK: statvfs1
// CHECK: fstypename='{{.*}}'
// CHECK: mntonname='{{.*}}'
// CHECK: mntfromname='{{.*}}'
// CHECK: fstatvfs1
// CHECK: fstypename='{{.*}}'
// CHECK: mntonname='{{.*}}'
// CHECK: mntfromname='{{.*}}'

return 0;
}

0 comments on commit 2f5fd17

Please sign in to comment.