Add interceptor for the setvbuf(3) from NetBSD

Summary:
setvbuf(3) is a routine to setup stream buffering.

Enable the interceptor for NetBSD.

Add dedicated tests for setvbuf(3) and functions
on top of this interface: setbuf, setbuffer, setlinebuf.

Based on original work by Yang Zheng.

Reviewers: joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: devnexen, tomsun.0.7, kubamracek, llvm-commits, mgorny, #sanitizers

Tags: #sanitizers

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

llvm-svn: 347270
This commit is contained in:
Kamil Rytarowski 2018-11-19 22:44:26 +00:00
parent 994a8451ba
commit 74abaf8cdc
3 changed files with 86 additions and 0 deletions

View File

@ -7298,6 +7298,21 @@ INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
#define INIT_MI_VECTOR_HASH
#endif
#if SANITIZER_INTERCEPT_SETVBUF
INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
SIZE_T size) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
int ret = REAL(setvbuf)(stream, buf, mode, size);
if (buf)
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
return ret;
}
#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf)
#else
#define INIT_SETVBUF
#endif
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@ -7553,6 +7568,7 @@ static void InitializeCommonInterceptors() {
INIT_NETENT;
INIT_GETMNTINFO;
INIT_MI_VECTOR_HASH;
INIT_SETVBUF;
INIT___PRINTF_CHK;
}

View File

@ -516,6 +516,7 @@
#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_GETMNTINFO SI_NETBSD
#define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD

View File

@ -0,0 +1,69 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
#include <stdio.h>
void print_something() {
for (size_t i = 0; i < 10 * BUFSIZ; i++)
printf("Hello world %zu\n", i);
}
void test_setbuf() {
char buf[BUFSIZ];
setbuf(stdout, NULL);
print_something();
setbuf(stdout, buf);
print_something();
}
void test_setbuffer() {
char buf[BUFSIZ];
setbuffer(stdout, NULL, 0);
print_something();
setbuffer(stdout, buf, BUFSIZ);
print_something();
}
void test_setlinebuf() {
char buf[BUFSIZ];
setlinebuf(stdout);
print_something();
}
void test_setvbuf() {
char buf[BUFSIZ];
setvbuf(stdout, NULL, _IONBF, 0);
print_something();
setvbuf(stdout, buf, _IOLBF, BUFSIZ);
print_something();
setvbuf(stdout, buf, _IOFBF, BUFSIZ);
print_something();
}
int main(void) {
printf("setvbuf\n");
test_setbuf();
test_setbuffer();
test_setlinebuf();
test_setvbuf();
// CHECK: setvbuf
return 0;
}