[compiler-rt] r347270 - Add interceptor for the setvbuf(3) from NetBSD

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 19 14:44:26 PST 2018


Author: kamil
Date: Mon Nov 19 14:44:26 2018
New Revision: 347270

URL: http://llvm.org/viewvc/llvm-project?rev=347270&view=rev
Log:
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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=347270&r1=347269&r2=347270&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Mon Nov 19 14:44:26 2018
@@ -7298,6 +7298,21 @@ INTERCEPTOR(void, mi_vector_hash, const
 #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;
 }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=347270&r1=347269&r2=347270&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Mon Nov 19 14:44:26 2018
@@ -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
 

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc?rev=347270&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/setvbuf.cc Mon Nov 19 14:44:26 2018
@@ -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;
+}




More information about the llvm-commits mailing list