[Sanitizer] Adding setvbuf in supported platforms and other stream buffer functions

- Enabling setvbuf interceptions for non NetBSD platforms.
- setbuf, setbuffer, setlinebuf as well.

Reviewers: vitalybuka, krytarowski	

Reviewed By: vitalybuka

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

llvm-svn: 347426
This commit is contained in:
David Carlier
2018-11-21 21:17:46 +00:00
parent ceeaa48052
commit 0c81a62d9d
7 changed files with 60 additions and 2 deletions

View File

@@ -7306,9 +7306,44 @@ INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
int ret = REAL(setvbuf)(stream, buf, mode, size);
if (buf)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
if (stream)
unpoison_file(stream);
return ret;
}
#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf)
INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
REAL(setbuf)(stream, buf);
if (buf) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
}
if (stream)
unpoison_file(stream);
}
INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
REAL(setbuffer)(stream, buf, mode);
if (buf) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
}
if (stream)
unpoison_file(stream);
}
INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
REAL(setlinebuf)(stream);
if (stream)
unpoison_file(stream);
}
#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
COMMON_INTERCEPT_FUNCTION(setbuf); \
COMMON_INTERCEPT_FUNCTION(setbuffer); \
COMMON_INTERCEPT_FUNCTION(setlinebuf)
#else
#define INIT_SETVBUF
#endif

View File

@@ -516,7 +516,8 @@
#define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
#define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
#define SANITIZER_INTERCEPT_NETENT SI_NETBSD
#define SANITIZER_INTERCEPT_SETVBUF SI_NETBSD
#define SANITIZER_INTERCEPT_SETVBUF (SI_NETBSD || SI_FREEBSD || \
SI_LINUX || SI_MAC)
#define SANITIZER_INTERCEPT_GETMNTINFO SI_NETBSD
#define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD

View File

@@ -266,6 +266,8 @@ const uptr sig_dfl = (uptr)SIG_DFL;
const uptr sig_err = (uptr)SIG_ERR;
const uptr sa_siginfo = (uptr)SA_SIGINFO;
const unsigned long __sanitizer_bufsiz = BUFSIZ;
int ptrace_pt_io = PT_IO;
int ptrace_pt_lwpinfo = PT_LWPINFO;
int ptrace_pt_set_event_mask = PT_SET_EVENT_MASK;

View File

@@ -460,6 +460,8 @@ struct __sanitizer_ttyent {
char *ty_class;
};
extern const unsigned long __sanitizer_bufsiz;
#define IOC_NRBITS 8
#define IOC_TYPEBITS 8
#define IOC_SIZEBITS 14

View File

@@ -501,6 +501,8 @@ unsigned struct_ElfW_Phdr_sz = sizeof(Elf_Phdr);
unsigned struct_sioc_vif_req_sz = sizeof(struct sioc_vif_req);
#endif
const unsigned long __sanitizer_bufsiz = BUFSIZ;
const unsigned IOCTL_NOT_PRESENT = 0;
unsigned IOCTL_FIOASYNC = FIOASYNC;

View File

@@ -1073,6 +1073,8 @@ struct __sanitizer_cookie_io_functions_t {
extern unsigned struct_unimapinit_sz;
#endif // SANITIZER_LINUX && !SANITIZER_ANDROID
extern const unsigned long __sanitizer_bufsiz;
#if (SANITIZER_LINUX || SANITIZER_FREEBSD) && !SANITIZER_ANDROID
extern unsigned struct_audio_buf_info_sz;
extern unsigned struct_ppp_stats_sz;

View File

@@ -1,5 +1,7 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
// UNSUPPORTED: solaris
#include <stdio.h>
void print_something() {
@@ -7,6 +9,10 @@ void print_something() {
printf("Hello world %zu\n", i);
}
void print_one_byte(char *buf) {
printf("First byte is %c\n", buf[0]);
}
void test_setbuf() {
char buf[BUFSIZ];
@@ -17,6 +23,8 @@ void test_setbuf() {
setbuf(stdout, buf);
print_something();
print_one_byte(buf);
}
void test_setbuffer() {
@@ -29,6 +37,8 @@ void test_setbuffer() {
setbuffer(stdout, buf, BUFSIZ);
print_something();
print_one_byte(buf);
}
void test_setlinebuf() {
@@ -48,9 +58,13 @@ void test_setvbuf() {
print_something();
print_one_byte(buf);
setvbuf(stdout, buf, _IOFBF, BUFSIZ);
print_something();
print_one_byte(buf);
}
int main(void) {