Skip to content

Commit

Permalink
[msan] Intercept getutent and friends.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D27791

llvm-svn: 289878
  • Loading branch information
eugenis committed Dec 15, 2016
1 parent 327188a commit 40f05dc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
68 changes: 68 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
Expand Up @@ -5890,6 +5890,72 @@ INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {

// FIXME: add other *stat interceptor

#if SANITIZER_INTERCEPT_UTMP
INTERCEPTOR(void *, getutent, int dummy) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
void *res = REAL(getutent)(dummy);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
return res;
}
INTERCEPTOR(void *, getutid, void *ut) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
void *res = REAL(getutid)(ut);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
return res;
}
INTERCEPTOR(void *, getutline, void *ut) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
void *res = REAL(getutline)(ut);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
return res;
}
#define INIT_UTMP \
COMMON_INTERCEPT_FUNCTION(getutent); \
COMMON_INTERCEPT_FUNCTION(getutid); \
COMMON_INTERCEPT_FUNCTION(getutline);
#else
#define INIT_UTMP
#endif

#if SANITIZER_INTERCEPT_UTMPX
INTERCEPTOR(void *, getutxent, int dummy) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
void *res = REAL(getutxent)(dummy);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
return res;
}
INTERCEPTOR(void *, getutxid, void *ut) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
void *res = REAL(getutxid)(ut);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
return res;
}
INTERCEPTOR(void *, getutxline, void *ut) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
void *res = REAL(getutxline)(ut);
if (res)
COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
return res;
}
#define INIT_UTMPX \
COMMON_INTERCEPT_FUNCTION(getutxent); \
COMMON_INTERCEPT_FUNCTION(getutxid); \
COMMON_INTERCEPT_FUNCTION(getutxline);
#else
#define INIT_UTMPX
#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 @@ -6086,4 +6152,6 @@ static void InitializeCommonInterceptors() {
INIT___LXSTAT;
INIT___LXSTAT64;
// FIXME: add other *stat interceptors.
INIT_UTMP;
INIT_UTMPX;
}
Expand Up @@ -312,4 +312,8 @@
#define SANITIZER_INTERCEPT___XSTAT64 SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT___LXSTAT SANITIZER_INTERCEPT___XSTAT
#define SANITIZER_INTERCEPT___LXSTAT64 SI_LINUX_NOT_ANDROID

#define SANITIZER_INTERCEPT_UTMP SI_NOT_WINDOWS
#define SANITIZER_INTERCEPT_UTMPX SI_LINUX_NOT_ANDROID || SI_MAC || SI_FREEBSD

#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
Expand Up @@ -51,6 +51,7 @@
#include <termios.h>
#include <time.h>
#include <wchar.h>
#include <utmp.h>

#if !SANITIZER_IOS
#include <net/route.h>
Expand All @@ -59,6 +60,7 @@
#if !SANITIZER_ANDROID
#include <sys/mount.h>
#include <sys/timeb.h>
#include <utmpx.h>
#endif

#if SANITIZER_LINUX
Expand Down Expand Up @@ -284,6 +286,11 @@ namespace __sanitizer {
int shmctl_shm_stat = (int)SHM_STAT;
#endif

unsigned struct_utmp_sz = sizeof(struct utmp);
#if !SANITIZER_ANDROID
unsigned struct_utmpx_sz = sizeof(struct utmpx);
#endif

int map_fixed = MAP_FIXED;

int af_inet = (int)AF_INET;
Expand Down
Expand Up @@ -862,6 +862,11 @@ namespace __sanitizer {
extern int shmctl_shm_stat;
#endif

extern unsigned struct_utmp_sz;
#if !SANITIZER_ANDROID
extern unsigned struct_utmpx_sz;
#endif

extern int map_fixed;

// ioctl arguments
Expand Down
17 changes: 17 additions & 0 deletions compiler-rt/test/msan/getutent.cc
@@ -0,0 +1,17 @@
// RUN: %clangxx_msan -O0 -g %s -o %t && %run %t

#include <utmp.h>
#include <utmpx.h>
#include <sanitizer/msan_interface.h>

int main(void) {
setutent();
while (struct utmp *ut = getutent())
__msan_check_mem_is_initialized(ut, sizeof(*ut));
endutent();

setutxent();
while (struct utmpx *utx = getutxent())
__msan_check_mem_is_initialized(utx, sizeof(*utx));
endutxent();
}

0 comments on commit 40f05dc

Please sign in to comment.