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>
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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" } */
--- /dev/null
+// 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;
+}