[compiler-rt] r326100 - [scudo] Make some tests less Linux-y

Kostya Kortchinsky via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 26 09:14:44 PST 2018


Author: cryptoad
Date: Mon Feb 26 09:14:44 2018
New Revision: 326100

URL: http://llvm.org/viewvc/llvm-project?rev=326100&view=rev
Log:
[scudo] Make some tests less Linux-y

Summary:
Start making the Scudo tests less Linux-y:
- `malloc_usable_size` doesn't exist everywhere, so replace them with
  `__sanitizer_get_allocated_size` which we provide;
- move all the `memalign` related tests into `memalign.c` since it's also not
  available everywhere.

I also noticed that the `memalign.c` was missing a line in one of the loops.

Reviewers: alekseyshl

Reviewed By: alekseyshl

Subscribers: delcypher, #sanitizers, llvm-commits

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

Modified:
    compiler-rt/trunk/test/scudo/double-free.cpp
    compiler-rt/trunk/test/scudo/memalign.c
    compiler-rt/trunk/test/scudo/mismatch.cpp
    compiler-rt/trunk/test/scudo/realloc.cpp
    compiler-rt/trunk/test/scudo/sizes.cpp

Modified: compiler-rt/trunk/test/scudo/double-free.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/double-free.cpp?rev=326100&r1=326099&r2=326100&view=diff
==============================================================================
--- compiler-rt/trunk/test/scudo/double-free.cpp (original)
+++ compiler-rt/trunk/test/scudo/double-free.cpp Mon Feb 26 09:14:44 2018
@@ -2,7 +2,6 @@
 // RUN: not %run %t malloc   2>&1 | FileCheck %s
 // RUN: not %run %t new      2>&1 | FileCheck %s
 // RUN: not %run %t newarray 2>&1 | FileCheck %s
-// RUN: not %run %t memalign 2>&1 | FileCheck %s
 
 // Tests double-free error on pointers allocated with different allocation
 // functions.
@@ -32,13 +31,6 @@ int main(int argc, char **argv)
     delete[] p;
     delete[] p;
   }
-  if (!strcmp(argv[1], "memalign")) {
-    void *p = nullptr;
-    posix_memalign(&p, 0x100, sizeof(int));
-    assert(p);
-    free(p);
-    free(p);
-  }
   return 0;
 }
 

Modified: compiler-rt/trunk/test/scudo/memalign.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/memalign.c?rev=326100&r1=326099&r2=326100&view=diff
==============================================================================
--- compiler-rt/trunk/test/scudo/memalign.c (original)
+++ compiler-rt/trunk/test/scudo/memalign.c Mon Feb 26 09:14:44 2018
@@ -1,7 +1,10 @@
 // RUN: %clang_scudo %s -o %t
-// RUN:                                                 %run %t valid   2>&1
-// RUN:                                             not %run %t invalid 2>&1
-// RUN: %env_scudo_opts=allocator_may_return_null=1     %run %t invalid 2>&1
+// RUN:                                                 %run %t valid       2>&1
+// RUN:                                             not %run %t invalid     2>&1
+// RUN: %env_scudo_opts=allocator_may_return_null=1     %run %t invalid     2>&1
+// RUN:                                             not %run %t double-free 2>&1 | FileCheck --check-prefix=CHECK-double-free %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1  not %run %t realloc     2>&1 | FileCheck --check-prefix=CHECK-realloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0      %run %t realloc     2>&1
 
 // Tests that the various aligned allocation functions work as intended. Also
 // tests for the condition where the alignment is not a power of 2.
@@ -51,6 +54,7 @@ int main(int argc, char **argv)
     // For larger alignment, reduce the number of allocations to avoid running
     // out of potential addresses (on 32-bit).
     for (int i = 19; i <= 24; i++) {
+      alignment = 1U << i;
       for (int k = 0; k < 3; k++) {
         p = memalign(alignment, 0x1000 - (2 * sizeof(void *) * k));
         assert(p);
@@ -77,5 +81,22 @@ int main(int argc, char **argv)
     assert(p == p_unchanged);
     assert(err == EINVAL);
   }
+  if (!strcmp(argv[1], "double-free")) {
+    void *p = NULL;
+    posix_memalign(&p, 0x100, sizeof(int));
+    assert(p);
+    free(p);
+    free(p);
+  }
+  if (!strcmp(argv[1], "realloc")) {
+    // We cannot reallocate a memalign'd chunk.
+    void *p = memalign(16, 16);
+    assert(p);
+    p = realloc(p, 32);
+    free(p);
+  }
   return 0;
 }
+
+// CHECK-double-free: ERROR: invalid chunk state
+// CHECK-realloc: ERROR: allocation type mismatch when reallocating address

Modified: compiler-rt/trunk/test/scudo/mismatch.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/mismatch.cpp?rev=326100&r1=326099&r2=326100&view=diff
==============================================================================
--- compiler-rt/trunk/test/scudo/mismatch.cpp (original)
+++ compiler-rt/trunk/test/scudo/mismatch.cpp Mon Feb 26 09:14:44 2018
@@ -1,18 +1,13 @@
 // RUN: %clangxx_scudo %s -o %t
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t mallocdel       2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t mallocdel       2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t newfree         2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t newfree         2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memaligndel     2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t memaligndel     2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memalignrealloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t memalignrealloc 2>&1
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t mallocdel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t mallocdel 2>&1
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t newfree   2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t newfree   2>&1
 
 // Tests that type mismatches between allocation and deallocation functions are
 // caught when the related option is set.
 
 #include <assert.h>
-#include <malloc.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -29,17 +24,6 @@ int main(int argc, char **argv)
     assert(p);
     free((void *)p);
   }
-  if (!strcmp(argv[1], "memaligndel")) {
-    int *p = (int *)memalign(16, 16);
-    assert(p);
-    delete p;
-  }
-  if (!strcmp(argv[1], "memalignrealloc")) {
-    void *p = memalign(16, 16);
-    assert(p);
-    p = realloc(p, 32);
-    free(p);
-  }
   return 0;
 }
 

Modified: compiler-rt/trunk/test/scudo/realloc.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/realloc.cpp?rev=326100&r1=326099&r2=326100&view=diff
==============================================================================
--- compiler-rt/trunk/test/scudo/realloc.cpp (original)
+++ compiler-rt/trunk/test/scudo/realloc.cpp Mon Feb 26 09:14:44 2018
@@ -1,6 +1,6 @@
 // RUN: %clangxx_scudo %s -lstdc++ -o %t
-// RUN: %run %t pointers 2>&1
-// RUN: %run %t contents 2>&1
+// RUN: %run %t pointers   2>&1
+// RUN: %run %t contents   2>&1
 // RUN: %run %t usablesize 2>&1
 
 // Tests that our reallocation function returns the same pointer when the
@@ -15,6 +15,8 @@
 
 #include <vector>
 
+#include <sanitizer/allocator_interface.h>
+
 int main(int argc, char **argv)
 {
   void *p, *old_p;
@@ -35,7 +37,7 @@ int main(int argc, char **argv)
       if (p) free(p);
       size += 16;
       p = malloc(size);
-      usable_size = malloc_usable_size(p);
+      usable_size = __sanitizer_get_allocated_size(p);
       assert(usable_size >= size);
     } while (usable_size == size);
     for (int i = 0; i < usable_size; i++)
@@ -56,7 +58,7 @@ int main(int argc, char **argv)
       if (!strcmp(argv[1], "pointers")) {
         old_p = p = realloc(nullptr, size);
         assert(p);
-        size = malloc_usable_size(p);
+        size = __sanitizer_get_allocated_size(p);
         // Our realloc implementation will return the same pointer if the size
         // requested is lower than or equal to the usable size of the associated
         // chunk.

Modified: compiler-rt/trunk/test/scudo/sizes.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/scudo/sizes.cpp?rev=326100&r1=326099&r2=326100&view=diff
==============================================================================
--- compiler-rt/trunk/test/scudo/sizes.cpp (original)
+++ compiler-rt/trunk/test/scudo/sizes.cpp Mon Feb 26 09:14:44 2018
@@ -21,10 +21,10 @@
 #include <limits>
 #include <new>
 
+#include <sanitizer/allocator_interface.h>
+
 int main(int argc, char **argv) {
   assert(argc == 2);
-  const char *action = argv[1];
-  fprintf(stderr, "%s:\n", action);
 
 #if __LP64__ || defined(_WIN64)
   static const size_t kMaxAllowedMallocSize = 1ULL << 40;
@@ -34,32 +34,32 @@ int main(int argc, char **argv) {
   static const size_t kChunkHeaderSize = 8;
 #endif
 
-  if (!strcmp(action, "malloc")) {
+  if (!strcmp(argv[1], "malloc")) {
     void *p = malloc(kMaxAllowedMallocSize);
     assert(!p);
     p = malloc(kMaxAllowedMallocSize - kChunkHeaderSize);
     assert(!p);
-  } else if (!strcmp(action, "calloc")) {
+  } else if (!strcmp(argv[1], "calloc")) {
     // Trigger an overflow in calloc.
     size_t size = std::numeric_limits<size_t>::max();
     void *p = calloc((size / 0x1000) + 1, 0x1000);
     assert(!p);
-  } else if (!strcmp(action, "new")) {
+  } else if (!strcmp(argv[1], "new")) {
     void *p = operator new(kMaxAllowedMallocSize);
     assert(!p);
-  } else if (!strcmp(action, "new-nothrow")) {
+  } else if (!strcmp(argv[1], "new-nothrow")) {
     void *p = operator new(kMaxAllowedMallocSize, std::nothrow);
     assert(!p);
-  } else if (!strcmp(action, "usable")) {
+  } else if (!strcmp(argv[1], "usable")) {
     // Playing with the actual usable size of a chunk.
     void *p = malloc(1007);
     assert(p);
-    size_t size = malloc_usable_size(p);
+    size_t size = __sanitizer_get_allocated_size(p);
     assert(size >= 1007);
     memset(p, 'A', size);
     p = realloc(p, 2014);
     assert(p);
-    size = malloc_usable_size(p);
+    size = __sanitizer_get_allocated_size(p);
     assert(size >= 2014);
     memset(p, 'B', size);
     free(p);




More information about the llvm-commits mailing list