[TTC-8][Sanitizers] Add tests for allocator_may_return_null=1 flag 38/149938/2 accepted/tizen/base/20171027.152140 submit/tizen_base/20171024.003248
authorSlava Barinov <v.barinov@samsung.com>
Wed, 13 Sep 2017 15:13:49 +0000 (18:13 +0300)
committerDongkyun Son <dongkyun.s@samsung.com>
Fri, 20 Oct 2017 08:25:47 +0000 (08:25 +0000)
The LLVM test has been rewritten to be able to run inside GCC test suite.

Change-Id: Ifaf9e3681ddacfaad73a314a01ab3c6c7704d40b
Signed-off-by: Slava Barinov <v.barinov@samsung.com>
gcc/testsuite/c-c++-common/asan/allocator_oom_test-1.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-2.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-3.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-4.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-5.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-6.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-7.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test-8.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/asan/allocator_oom_test.inc [new file with mode: 0644]

diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-1.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-1.c
new file mode 100644 (file)
index 0000000..e03c378
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+
+#define ACTION "malloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-shouldfail "asan" } */
+/* { dg-output "malloc:\n==.*==AddressSanitizer's allocator is terminating the process" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-2.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-2.c
new file mode 100644 (file)
index 0000000..4c6cb4d
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+/* { dg-set-target-env-var ASAN_OPTIONS "allocator_may_return_null=1" } */
+
+#define ACTION "malloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-output "malloc:\nx: 0" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-3.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-3.c
new file mode 100644 (file)
index 0000000..c470e47
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+
+#define ACTION "realloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-shouldfail "asan" } */
+/* { dg-output "realloc:\n==.*==AddressSanitizer's allocator is terminating the process" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-4.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-4.c
new file mode 100644 (file)
index 0000000..cc55a01
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+/* { dg-set-target-env-var ASAN_OPTIONS "allocator_may_return_null=1" } */
+
+#define ACTION "realloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-output "realloc:\nx: 0" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-5.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-5.c
new file mode 100644 (file)
index 0000000..3c1788a
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of calloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+
+#define ACTION "calloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-shouldfail "asan" } */
+/* { dg-output "calloc:\n==.*==AddressSanitizer's allocator is terminating the process" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-6.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-6.c
new file mode 100644 (file)
index 0000000..3676a97
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of calloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+/* { dg-set-target-env-var ASAN_OPTIONS "allocator_may_return_null=1" } */
+
+#define ACTION "calloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-output "calloc:\nx: 0" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-7.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-7.c
new file mode 100644 (file)
index 0000000..23094a1
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of realloc-after-malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+
+#define ACTION "realloc-after-malloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-shouldfail "asan" } */
+/* { dg-output "realloc-after-malloc:\n==.*==AddressSanitizer's allocator is terminating the process" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test-8.c b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-8.c
new file mode 100644 (file)
index 0000000..961c628
--- /dev/null
@@ -0,0 +1,16 @@
+// Test the behavior of realloc-after-malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+/* { dg-do run } */
+/* { dg-set-target-env-var ASAN_OPTIONS "allocator_may_return_null=1" } */
+
+#define ACTION "realloc-after-malloc"
+#include "allocator_oom_test.inc"
+
+/* { dg-output "realloc-after-malloc:\nx: 0" } */
diff --git a/gcc/testsuite/c-c++-common/asan/allocator_oom_test.inc b/gcc/testsuite/c-c++-common/asan/allocator_oom_test.inc
new file mode 100644 (file)
index 0000000..2a74e65
--- /dev/null
@@ -0,0 +1,58 @@
+// Test the behavior of malloc/calloc/realloc when the allocation causes OOM
+// in the secondary allocator.
+// By default (allocator_may_return_null=0) the process should crash.
+// With allocator_may_return_null=1 the allocator should return 0.
+// Set the limit to 20.5T on 64 bits to account for ASan shadow memory,
+// allocator buffers etc. so that the test allocation of ~1T will trigger OOM.
+// Limit this test to Linux since we're relying on allocator internal
+// limits (shadow memory size, allocation limits etc.)
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+int main(int argc, char **argv) {
+  struct rlimit new_lim;
+  new_lim.rlim_cur = 22024290304l;
+  new_lim.rlim_max = 4196971l;
+  setrlimit(RLIMIT_AS, &new_lim);
+  const char *action = ACTION;
+  fprintf(stderr, "%s:\n", action);
+
+  // Allocate just a bit less than max allocation size enforced by ASan's
+  // allocator (currently 1T and 3G).
+  const size_t size =
+#if __LP64__
+      (1ULL << 40) - (1ULL << 30);
+#else
+      (3ULL << 30) - (1ULL << 20);
+#endif
+
+  void *x = 0;
+
+  if (!strcmp(action, "malloc")) {
+    x = malloc(size);
+  } else if (!strcmp(action, "calloc")) {
+    x = calloc(size / 4, 4);
+  } else if (!strcmp(action, "realloc")) {
+    x = realloc(0, size);
+  } else if (!strcmp(action, "realloc-after-malloc")) {
+    char *t = (char*)malloc(100);
+    *t = 42;
+    x = realloc(t, size);
+    assert(*t == 42);
+    free(t);
+  } else {
+    assert(0);
+  }
+
+  // The NULL pointer is printed differently on different systems, while (long)0
+  // is always the same.
+  fprintf(stderr, "x: %lx\n", (long)x);
+  free(x);
+
+  return x != 0;
+}