2013-01-18 21:01:18 +08:00
|
|
|
//===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
|
2012-12-12 17:54:35 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Common function interceptors for tools like AddressSanitizer,
|
|
|
|
// ThreadSanitizer, MemorySanitizer, etc.
|
|
|
|
//
|
|
|
|
// This file should be included into the tool's interceptor file,
|
|
|
|
// which has to define it's own macros:
|
|
|
|
// COMMON_INTERCEPTOR_ENTER
|
|
|
|
// COMMON_INTERCEPTOR_READ_RANGE
|
|
|
|
// COMMON_INTERCEPTOR_WRITE_RANGE
|
2013-01-17 21:09:00 +08:00
|
|
|
// COMMON_INTERCEPTOR_FD_ACQUIRE
|
|
|
|
// COMMON_INTERCEPTOR_FD_RELEASE
|
2013-01-18 14:43:13 +08:00
|
|
|
// COMMON_INTERCEPTOR_SET_THREAD_NAME
|
2012-12-12 17:54:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2012-12-13 14:31:40 +08:00
|
|
|
#include "interception/interception.h"
|
2012-12-13 16:50:16 +08:00
|
|
|
#include "sanitizer_platform_interceptors.h"
|
2012-12-13 13:27:08 +08:00
|
|
|
|
2013-01-18 19:17:23 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-12-13 16:36:13 +08:00
|
|
|
#if SANITIZER_INTERCEPT_READ
|
2012-12-13 14:31:40 +08:00
|
|
|
INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
|
2012-12-13 14:31:40 +08:00
|
|
|
SSIZE_T res = REAL(read)(fd, ptr, count);
|
2012-12-12 17:54:35 +08:00
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
|
2013-01-17 21:09:00 +08:00
|
|
|
if (res >= 0 && fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
|
2012-12-12 17:54:35 +08:00
|
|
|
return res;
|
|
|
|
}
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_READ INTERCEPT_FUNCTION(read)
|
|
|
|
#else
|
|
|
|
# define INIT_READ
|
2012-12-13 16:36:13 +08:00
|
|
|
#endif
|
2012-12-12 17:54:35 +08:00
|
|
|
|
2012-12-13 16:10:23 +08:00
|
|
|
#if SANITIZER_INTERCEPT_PREAD
|
2012-12-13 14:31:40 +08:00
|
|
|
INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
|
2012-12-13 14:31:40 +08:00
|
|
|
SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
|
2012-12-12 17:54:35 +08:00
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
|
2013-01-17 21:09:00 +08:00
|
|
|
if (res >= 0 && fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
|
2012-12-12 17:54:35 +08:00
|
|
|
return res;
|
|
|
|
}
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_PREAD INTERCEPT_FUNCTION(pread)
|
|
|
|
#else
|
|
|
|
# define INIT_PREAD
|
2012-12-13 16:10:23 +08:00
|
|
|
#endif
|
2012-12-12 17:54:35 +08:00
|
|
|
|
2012-12-13 13:27:08 +08:00
|
|
|
#if SANITIZER_INTERCEPT_PREAD64
|
2012-12-13 14:31:40 +08:00
|
|
|
INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
|
2012-12-13 14:31:40 +08:00
|
|
|
SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
|
2012-12-12 17:54:35 +08:00
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
|
2013-01-17 21:09:00 +08:00
|
|
|
if (res >= 0 && fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
|
2012-12-12 17:54:35 +08:00
|
|
|
return res;
|
|
|
|
}
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_PREAD64 INTERCEPT_FUNCTION(pread64)
|
|
|
|
#else
|
|
|
|
# define INIT_PREAD64
|
2012-12-12 19:52:26 +08:00
|
|
|
#endif
|
2012-12-12 17:54:35 +08:00
|
|
|
|
2013-01-18 14:43:13 +08:00
|
|
|
#if SANITIZER_INTERCEPT_WRITE
|
|
|
|
INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
|
2013-01-18 14:43:13 +08:00
|
|
|
if (fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
|
2013-01-18 14:43:13 +08:00
|
|
|
SSIZE_T res = REAL(write)(fd, ptr, count);
|
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
|
2013-01-18 14:43:13 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
# define INIT_WRITE INTERCEPT_FUNCTION(write)
|
2013-01-17 22:48:03 +08:00
|
|
|
#else
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_WRITE
|
2013-01-17 22:48:03 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-18 14:43:13 +08:00
|
|
|
#if SANITIZER_INTERCEPT_PWRITE
|
|
|
|
INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count);
|
2013-01-18 14:43:13 +08:00
|
|
|
if (fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
|
2013-01-18 14:43:13 +08:00
|
|
|
SSIZE_T res = REAL(pwrite)(fd, ptr, count);
|
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
|
2013-01-18 14:43:13 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
# define INIT_PWRITE INTERCEPT_FUNCTION(pwrite)
|
2013-01-17 22:48:03 +08:00
|
|
|
#else
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_PWRITE
|
2013-01-17 22:48:03 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-18 14:43:13 +08:00
|
|
|
#if SANITIZER_INTERCEPT_PWRITE64
|
|
|
|
INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count) {
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count);
|
2013-01-18 14:43:13 +08:00
|
|
|
if (fd >= 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
|
2013-01-18 14:43:13 +08:00
|
|
|
SSIZE_T res = REAL(pwrite64)(fd, ptr, count);
|
|
|
|
if (res > 0)
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
|
2013-01-18 14:43:13 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
# define INIT_PWRITE64 INTERCEPT_FUNCTION(pwrite64)
|
2013-01-17 22:48:03 +08:00
|
|
|
#else
|
2013-01-18 14:43:13 +08:00
|
|
|
# define INIT_PWRITE64
|
2013-01-17 22:48:03 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-18 14:43:13 +08:00
|
|
|
#if SANITIZER_INTERCEPT_PRCTL
|
|
|
|
INTERCEPTOR(int, prctl, int option,
|
|
|
|
unsigned long arg2, unsigned long arg3, // NOLINT
|
|
|
|
unsigned long arg4, unsigned long arg5) { // NOLINT
|
2013-01-18 19:17:23 +08:00
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
|
2013-01-18 14:43:13 +08:00
|
|
|
static const int PR_SET_NAME = 15;
|
|
|
|
int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
|
|
|
|
if (option == PR_SET_NAME) {
|
|
|
|
char buff[16];
|
|
|
|
internal_strncpy(buff, (char*)arg2, 15);
|
|
|
|
buff[15] = 0;
|
2013-01-18 19:17:23 +08:00
|
|
|
COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
|
2013-01-18 14:43:13 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
# define INIT_PRCTL INTERCEPT_FUNCTION(prctl)
|
|
|
|
#else
|
|
|
|
# define INIT_PRCTL
|
|
|
|
#endif // SANITIZER_INTERCEPT_PRCTL
|
|
|
|
|
2013-01-18 19:17:23 +08:00
|
|
|
|
|
|
|
#if SANITIZER_INTERCEPT_SCANF
|
|
|
|
|
2013-01-18 21:01:18 +08:00
|
|
|
#include "sanitizer_common_interceptors_scanf.inc"
|
2013-01-18 19:17:23 +08:00
|
|
|
|
|
|
|
INTERCEPTOR(int, vscanf, const char *format, va_list ap) { // NOLINT
|
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, vscanf, format, ap);
|
|
|
|
scanf_common(ctx, format, ap);
|
|
|
|
int res = REAL(vscanf)(format, ap); // NOLINT
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, vsscanf, const char *str, const char *format, // NOLINT
|
|
|
|
va_list ap) {
|
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, vsscanf, str, format, ap);
|
|
|
|
scanf_common(ctx, format, ap);
|
|
|
|
int res = REAL(vsscanf)(str, format, ap); // NOLINT
|
|
|
|
// FIXME: read of str
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, vfscanf, void *stream, const char *format, // NOLINT
|
|
|
|
va_list ap) {
|
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, vfscanf, stream, format, ap);
|
|
|
|
scanf_common(ctx, format, ap);
|
|
|
|
int res = REAL(vfscanf)(stream, format, ap); // NOLINT
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, scanf, const char *format, ...) { // NOLINT
|
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, scanf, format);
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int res = vscanf(format, ap); // NOLINT
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, fscanf, void* stream, const char *format, ...) { // NOLINT
|
|
|
|
void* ctx;
|
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, fscanf, stream, format);
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int res = vfscanf(stream, format, ap); // NOLINT
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERCEPTOR(int, sscanf, const char *str, const char *format, ...) { // NOLINT
|
|
|
|
void* ctx;
|
2013-01-18 23:53:43 +08:00
|
|
|
COMMON_INTERCEPTOR_ENTER(ctx, sscanf, str, format); // NOLINT
|
2013-01-18 19:17:23 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
int res = vsscanf(str, format, ap); // NOLINT
|
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INIT_SCANF \
|
2013-01-18 23:53:43 +08:00
|
|
|
INTERCEPT_FUNCTION(scanf); \
|
|
|
|
INTERCEPT_FUNCTION(sscanf); /* NOLINT */ \
|
|
|
|
INTERCEPT_FUNCTION(fscanf); \
|
|
|
|
INTERCEPT_FUNCTION(vscanf); \
|
|
|
|
INTERCEPT_FUNCTION(vsscanf); \
|
2013-01-18 19:17:23 +08:00
|
|
|
INTERCEPT_FUNCTION(vfscanf)
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define INIT_SCANF
|
|
|
|
#endif
|
|
|
|
|
2012-12-12 17:54:35 +08:00
|
|
|
#define SANITIZER_COMMON_INTERCEPTORS_INIT \
|
2012-12-13 16:36:13 +08:00
|
|
|
INIT_READ; \
|
2012-12-13 16:10:23 +08:00
|
|
|
INIT_PREAD; \
|
2012-12-12 19:52:26 +08:00
|
|
|
INIT_PREAD64; \
|
2013-01-18 14:43:13 +08:00
|
|
|
INIT_PRCTL; \
|
|
|
|
INIT_WRITE; \
|
2013-01-23 19:52:19 +08:00
|
|
|
INIT_PWRITE; \
|
|
|
|
INIT_PWRITE64; \
|
2013-01-18 21:01:18 +08:00
|
|
|
INIT_SCANF;
|