[compiler-rt] r242535 - [msan] Fix open_memstream handling.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Fri Jul 17 09:10:38 PDT 2015


Author: eugenis
Date: Fri Jul 17 11:10:37 2015
New Revision: 242535

URL: http://llvm.org/viewvc/llvm-project?rev=242535&view=rev
Log:
[msan] Fix open_memstream handling.

For open_memstream() files, buffer pointer is only valid immediately after
fflush() or fclose(). Fix the fclose() interceptor to unpoison after the
REAL(fclose) call, not before it.

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/open_memstream.cc

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=242535&r1=242534&r2=242535&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Fri Jul 17 11:10:37 2015
@@ -4831,15 +4831,14 @@ INTERCEPTOR(int, fflush, __sanitizer_FIL
 INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
-  if (fp) {
-    COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
-    const FileMetadata *m = GetInterceptorMetadata(fp);
-    if (m) {
-      COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
-      DeleteInterceptorMetadata(fp);
-    }
+  COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
+  const FileMetadata *m = GetInterceptorMetadata(fp);
+  int res = REAL(fclose)(fp);
+  if (m) {
+    COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
+    DeleteInterceptorMetadata(fp);
   }
-  return REAL(fclose)(fp);
+  return res;
 }
 #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
 #else

Modified: compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/open_memstream.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/open_memstream.cc?rev=242535&r1=242534&r2=242535&view=diff
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/open_memstream.cc (original)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Linux/open_memstream.cc Fri Jul 17 11:10:37 2015
@@ -25,16 +25,18 @@ static void check_mem_is_good(void *p, s
 static void check_mem_is_good(void *p, size_t s) {}
 #endif
 
-static void run(void) {
+static void run(bool flush) {
   char *buf;
   size_t buf_len;
   fprintf(stderr, " &buf %p, &buf_len %p\n", &buf, &buf_len);
   FILE *fp = open_memstream(&buf, &buf_len);
   fprintf(fp, "hello");
-  fflush(fp);
-  check_mem_is_good(&buf, sizeof(buf));
-  check_mem_is_good(&buf_len, sizeof(buf_len));
-  check_mem_is_good(buf, buf_len);
+  if (flush) {
+    fflush(fp);
+    check_mem_is_good(&buf, sizeof(buf));
+    check_mem_is_good(&buf_len, sizeof(buf_len));
+    check_mem_is_good(buf, buf_len);
+  }
 
   char *p = new char[1024];
   memset(p, 'a', 1023);
@@ -42,17 +44,27 @@ static void run(void) {
   for (int i = 0; i < 100; ++i)
     fprintf(fp, "%s", p);
   delete[] p;
-  fflush(fp);
-  fprintf(stderr, " %p addr %p, len %zu\n", &buf, buf, buf_len);
+
+  if (flush) {
+    fflush(fp);
+    fprintf(stderr, " %p addr %p, len %zu\n", &buf, buf, buf_len);
+    check_mem_is_good(&buf, sizeof(buf));
+    check_mem_is_good(&buf_len, sizeof(buf_len));
+    check_mem_is_good(buf, buf_len);\
+  }
+
+  fclose(fp);
   check_mem_is_good(&buf, sizeof(buf));
   check_mem_is_good(&buf_len, sizeof(buf_len));
   check_mem_is_good(buf, buf_len);
-  fclose(fp);
+
   free(buf);
 }
 
 int main(void) {
   for (int i = 0; i < 100; ++i)
-    run();
+    run(false);
+  for (int i = 0; i < 100; ++i)
+    run(true);
   return 0;
 }





More information about the llvm-commits mailing list