[compiler-rt] r208881 - [ASan/Win tests] Add tests for malloc/calloc/realloc

Timur Iskhodzhanov timurrrr at google.com
Thu May 15 06:50:12 PDT 2014


Author: timurrrr
Date: Thu May 15 08:50:12 2014
New Revision: 208881

URL: http://llvm.org/viewvc/llvm-project?rev=208881&view=rev
Log:
[ASan/Win tests] Add tests for malloc/calloc/realloc

Added:
    compiler-rt/trunk/test/asan/TestCases/Windows/allocators_sanity.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/calloc_left_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/calloc_right_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/calloc_uaf.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/double_free.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/double_operator_delete.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/malloc_left_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/malloc_right_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/malloc_uaf.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/realloc_left_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/realloc_right_oob.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/realloc_uaf.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/use_after_realloc.cc

Added: compiler-rt/trunk/test/asan/TestCases/Windows/allocators_sanity.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/allocators_sanity.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/allocators_sanity.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/allocators_sanity.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,37 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t | FileCheck %s
+
+#include <malloc.h>
+#include <stdio.h>
+
+int main() {
+  int *p = (int*)malloc(1024 * sizeof(int));
+  p[512] = 0;
+  free(p);
+
+  p = (int*)malloc(128);
+  p = (int*)realloc(p, 2048 * sizeof(int));
+  p[1024] = 0;
+  free(p);
+
+  p = (int*)calloc(16, sizeof(int));
+  if (p[8] != 0)
+    return 1;
+  p[15]++;
+  if (16 * sizeof(int) != _msize(p))
+    return 2;
+  free(p);
+
+  p = new int;
+  *p = 42;
+  delete p;
+
+  p = new int[42];
+  p[15]++;
+  delete [] p;
+
+  printf("All ok\n");
+// CHECK: All ok
+
+  return 0;
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/calloc_left_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/calloc_left_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/calloc_left_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/calloc_left_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  int *buffer = (int*)calloc(42, sizeof(int));
+  buffer[-1] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*calloc_left_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 4 bytes to the left of 168-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* calloc }}
+// CHECK-NEXT: {{#1 .* main .*calloc_left_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/calloc_right_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/calloc_right_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/calloc_right_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/calloc_right_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  int *buffer = (int*)calloc(42, sizeof(int));
+  buffer[42] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*calloc_right_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes to the right of 168-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* calloc }}
+// CHECK-NEXT: {{#1 .* main .*calloc_right_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/calloc_uaf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/calloc_uaf.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/calloc_uaf.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/calloc_uaf.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,21 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  int *buffer = (int*)calloc(42, sizeof(int));
+  free(buffer);
+  buffer[0] = 42;
+// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*calloc_uaf.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
+// CHECK: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* free }}
+// CHECK-NEXT: {{#1 .* main .*calloc_uaf.cc}}:[[@LINE-8]]
+// CHECK: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* calloc }}
+// CHECK-NEXT: {{#1 .* main .*calloc_uaf.cc}}:[[@LINE-12]]
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/double_free.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/double_free.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/double_free.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/double_free.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,22 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  int *x = (int*)malloc(42 * sizeof(int));
+  free(x);
+  free(x);
+// CHECK: AddressSanitizer: attempting double-free on [[ADDR:0x[0-9a-f]+]]
+// CHECK-NEXT: {{#0 .* free }}
+// CHECK-NEXT: {{#1 .* main .*double_free.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
+// CHECK-LABEL: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* free }}
+// CHECK-NEXT: {{#1 .* main .*double_free.cc}}:[[@LINE-8]]
+// CHECK-LABEL: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* malloc }}
+// CHECK-NEXT: {{#1 .* main .*double_free.cc}}:[[@LINE-12]]
+  return 0;
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/double_operator_delete.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/double_operator_delete.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/double_operator_delete.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/double_operator_delete.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,23 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  int *x = new int[42];
+  delete [] x;
+  delete [] x;
+// CHECK: AddressSanitizer: attempting double-free on [[ADDR:0x[0-9a-f]+]]
+// CHECK-NEXT: {{#0 .* operator delete}}[]
+// CHECK-NEXT: {{#1 .* main .*double_operator_delete.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
+// CHECK-LABEL: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* operator delete}}[]
+// CHECK-NEXT: {{#1 .* main .*double_operator_delete.cc}}:[[@LINE-8]]
+// CHECK-LABEL: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* operator new}}[]
+// CHECK-NEXT: {{#1 .* main .*double_operator_delete.cc}}:[[@LINE-12]]
+  return 0;
+}
+

Added: compiler-rt/trunk/test/asan/TestCases/Windows/malloc_left_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/malloc_left_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/malloc_left_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/malloc_left_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)malloc(42);
+  buffer[-1] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*malloc_left_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 1 bytes to the left of 42-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* malloc }}
+// CHECK-NEXT: {{#1 .* main .*malloc_left_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/malloc_right_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/malloc_right_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/malloc_right_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/malloc_right_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)malloc(42);
+  buffer[42] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*malloc_right_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes to the right of 42-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* malloc }}
+// CHECK-NEXT: {{#1 .* main .*malloc_right_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/malloc_uaf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/malloc_uaf.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/malloc_uaf.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/malloc_uaf.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,21 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)malloc(42);
+  free(buffer);
+  buffer[0] = 42;
+// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*malloc_uaf.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
+// CHECK: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* free }}
+// CHECK-NEXT: {{#1 .* main .*malloc_uaf.cc}}:[[@LINE-8]]
+// CHECK: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* malloc }}
+// CHECK-NEXT: {{#1 .* main .*malloc_uaf.cc}}:[[@LINE-12]]
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/realloc_left_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/realloc_left_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/realloc_left_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/realloc_left_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)realloc(0, 42);
+  buffer[-1] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*realloc_left_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 1 bytes to the left of 42-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* realloc }}
+// CHECK-NEXT: {{#1 .* main .*realloc_left_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/realloc_right_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/realloc_right_oob.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/realloc_right_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/realloc_right_oob.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)realloc(0, 42);
+  buffer[42] = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*realloc_right_oob.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes to the right of 42-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* realloc }}
+// CHECK-NEXT: {{#1 .* main .*realloc_right_oob.cc}}:[[@LINE-8]]
+  free(buffer);
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/realloc_uaf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/realloc_uaf.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/realloc_uaf.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/realloc_uaf.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,21 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)realloc(0, 42);
+  free(buffer);
+  buffer[0] = 42;
+// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*realloc_uaf.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
+// CHECK: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* free }}
+// CHECK-NEXT: {{#1 .* main .*realloc_uaf.cc}}:[[@LINE-8]]
+// CHECK: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* realloc }}
+// CHECK-NEXT: {{#1 .* main .*realloc_uaf.cc}}:[[@LINE-12]]
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/use_after_realloc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/use_after_realloc.cc?rev=208881&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/use_after_realloc.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/use_after_realloc.cc Thu May 15 08:50:12 2014
@@ -0,0 +1,24 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <malloc.h>
+
+int main() {
+  char *buffer = (char*)realloc(0, 32),
+       *stale = buffer;
+  buffer = (char*)realloc(buffer, 64);
+  // The 'stale' may now point to a free'd memory.
+  stale[0] = 42;
+// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*use_after_realloc.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes inside of 32-byte region
+// CHECK: freed by thread T0 here:
+// CHECK-NEXT: {{#0 .* realloc }}
+// CHECK-NEXT: {{#1 .* main .*use_after_realloc.cc}}:[[@LINE-9]]
+// CHECK: previously allocated by thread T0 here:
+// CHECK-NEXT: {{#0 .* realloc }}
+// CHECK-NEXT: {{#1 .* main .*use_after_realloc.cc}}:[[@LINE-14]]
+  free(buffer);
+}





More information about the llvm-commits mailing list