Skip to content

Commit 0c81a62

Browse files
committed
[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
1 parent ceeaa48 commit 0c81a62

7 files changed

+60
-2
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7306,9 +7306,44 @@ INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
73067306
int ret = REAL(setvbuf)(stream, buf, mode, size);
73077307
if (buf)
73087308
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
7309+
if (stream)
7310+
unpoison_file(stream);
73097311
return ret;
73107312
}
7311-
#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf)
7313+
7314+
INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
7315+
void *ctx;
7316+
COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
7317+
REAL(setbuf)(stream, buf);
7318+
if (buf) {
7319+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7320+
}
7321+
if (stream)
7322+
unpoison_file(stream);
7323+
}
7324+
7325+
INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, int mode) {
7326+
void *ctx;
7327+
COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, mode);
7328+
REAL(setbuffer)(stream, buf, mode);
7329+
if (buf) {
7330+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
7331+
}
7332+
if (stream)
7333+
unpoison_file(stream);
7334+
}
7335+
7336+
INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
7337+
void *ctx;
7338+
COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
7339+
REAL(setlinebuf)(stream);
7340+
if (stream)
7341+
unpoison_file(stream);
7342+
}
7343+
#define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
7344+
COMMON_INTERCEPT_FUNCTION(setbuf); \
7345+
COMMON_INTERCEPT_FUNCTION(setbuffer); \
7346+
COMMON_INTERCEPT_FUNCTION(setlinebuf)
73127347
#else
73137348
#define INIT_SETVBUF
73147349
#endif

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@
516516
#define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
517517
#define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
518518
#define SANITIZER_INTERCEPT_NETENT SI_NETBSD
519-
#define SANITIZER_INTERCEPT_SETVBUF SI_NETBSD
519+
#define SANITIZER_INTERCEPT_SETVBUF (SI_NETBSD || SI_FREEBSD || \
520+
SI_LINUX || SI_MAC)
520521
#define SANITIZER_INTERCEPT_GETMNTINFO SI_NETBSD
521522
#define SANITIZER_INTERCEPT_MI_VECTOR_HASH SI_NETBSD
522523

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ const uptr sig_dfl = (uptr)SIG_DFL;
266266
const uptr sig_err = (uptr)SIG_ERR;
267267
const uptr sa_siginfo = (uptr)SA_SIGINFO;
268268

269+
const unsigned long __sanitizer_bufsiz = BUFSIZ;
270+
269271
int ptrace_pt_io = PT_IO;
270272
int ptrace_pt_lwpinfo = PT_LWPINFO;
271273
int ptrace_pt_set_event_mask = PT_SET_EVENT_MASK;

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ struct __sanitizer_ttyent {
460460
char *ty_class;
461461
};
462462

463+
extern const unsigned long __sanitizer_bufsiz;
464+
463465
#define IOC_NRBITS 8
464466
#define IOC_TYPEBITS 8
465467
#define IOC_SIZEBITS 14

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ unsigned struct_ElfW_Phdr_sz = sizeof(Elf_Phdr);
501501
unsigned struct_sioc_vif_req_sz = sizeof(struct sioc_vif_req);
502502
#endif
503503

504+
const unsigned long __sanitizer_bufsiz = BUFSIZ;
505+
504506
const unsigned IOCTL_NOT_PRESENT = 0;
505507

506508
unsigned IOCTL_FIOASYNC = FIOASYNC;

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,8 @@ struct __sanitizer_cookie_io_functions_t {
10731073
extern unsigned struct_unimapinit_sz;
10741074
#endif // SANITIZER_LINUX && !SANITIZER_ANDROID
10751075

1076+
extern const unsigned long __sanitizer_bufsiz;
1077+
10761078
#if (SANITIZER_LINUX || SANITIZER_FREEBSD) && !SANITIZER_ANDROID
10771079
extern unsigned struct_audio_buf_info_sz;
10781080
extern unsigned struct_ppp_stats_sz;

compiler-rt/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc renamed to compiler-rt/test/sanitizer_common/TestCases/Posix/setvbuf.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
22

3+
// UNSUPPORTED: solaris
4+
35
#include <stdio.h>
46

57
void print_something() {
68
for (size_t i = 0; i < 10 * BUFSIZ; i++)
79
printf("Hello world %zu\n", i);
810
}
911

12+
void print_one_byte(char *buf) {
13+
printf("First byte is %c\n", buf[0]);
14+
}
15+
1016
void test_setbuf() {
1117
char buf[BUFSIZ];
1218

@@ -17,6 +23,8 @@ void test_setbuf() {
1723
setbuf(stdout, buf);
1824

1925
print_something();
26+
27+
print_one_byte(buf);
2028
}
2129

2230
void test_setbuffer() {
@@ -29,6 +37,8 @@ void test_setbuffer() {
2937
setbuffer(stdout, buf, BUFSIZ);
3038

3139
print_something();
40+
41+
print_one_byte(buf);
3242
}
3343

3444
void test_setlinebuf() {
@@ -48,9 +58,13 @@ void test_setvbuf() {
4858

4959
print_something();
5060

61+
print_one_byte(buf);
62+
5163
setvbuf(stdout, buf, _IOFBF, BUFSIZ);
5264

5365
print_something();
66+
67+
print_one_byte(buf);
5468
}
5569

5670
int main(void) {

0 commit comments

Comments
 (0)