From: Slava Barinov Date: Wed, 13 Sep 2017 15:13:49 +0000 (+0300) Subject: [TTC-8][Sanitizers] Add tests for allocator_may_return_null=1 flag X-Git-Tag: accepted/tizen/base/20171027.152140^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F38%2F149938%2F2;p=platform%2Fupstream%2Flinaro-gcc.git [TTC-8][Sanitizers] Add tests for allocator_may_return_null=1 flag The LLVM test has been rewritten to be able to run inside GCC test suite. Change-Id: Ifaf9e3681ddacfaad73a314a01ab3c6c7704d40b Signed-off-by: Slava Barinov --- 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 index 0000000..e03c378 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-1.c @@ -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 index 0000000..4c6cb4d --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-2.c @@ -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 index 0000000..c470e47 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-3.c @@ -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 index 0000000..cc55a01 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-4.c @@ -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 index 0000000..3c1788a --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-5.c @@ -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 index 0000000..3676a97 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-6.c @@ -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 index 0000000..23094a1 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-7.c @@ -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 index 0000000..961c628 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test-8.c @@ -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 index 0000000..2a74e65 --- /dev/null +++ b/gcc/testsuite/c-c++-common/asan/allocator_oom_test.inc @@ -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 +#include +#include +#include +#include +#include + +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; +}