OLD | NEW |
1 //===- subzero/runtime/szrt_asan.c - AddressSanitizer Runtime -----*- C -*-===// | 1 //===- subzero/runtime/szrt_asan.c - AddressSanitizer Runtime -----*- C -*-===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
11 /// \brief Provides the AddressSanitizer runtime. | 11 /// \brief Provides the AddressSanitizer runtime. |
12 /// | 12 /// |
13 /// Exposes functions for initializing the shadow memory region and managing it | 13 /// Exposes functions for initializing the shadow memory region and managing it |
14 /// on loads, stores, and allocations. | 14 /// on loads, stores, and allocations. |
15 /// | 15 /// |
16 //===----------------------------------------------------------------------===// | 16 //===----------------------------------------------------------------------===// |
17 | 17 |
| 18 #include <assert.h> |
18 #include <stddef.h> | 19 #include <stddef.h> |
| 20 #include <stdio.h> |
19 #include <stdlib.h> | 21 #include <stdlib.h> |
20 | 22 |
| 23 static __thread int behind_malloc = 0; |
| 24 |
21 // TODO(tlively): Define and implement this library | 25 // TODO(tlively): Define and implement this library |
22 void __asan_init(void) { | 26 void __asan_init(void) { |
23 printf("Set up shadow memory here\n"); | 27 if (behind_malloc == 0) |
24 return; | 28 printf("set up shadow memory here\n"); |
25 } | 29 } |
26 | 30 |
27 void __asan_check(void *addr, int size) { | 31 void __asan_check(void *addr, int size) { |
28 printf("Check %d bytes at %p\n", size, addr); | 32 if (behind_malloc == 0) |
29 return; | 33 printf("check %d bytes at %p\n", size, addr); |
30 } | 34 } |
31 | 35 |
32 void *__asan_malloc(size_t size) { | 36 void *__asan_malloc(size_t size) { |
33 printf("malloc() called with size %d\n", size); | 37 if (behind_malloc == 0) |
34 return malloc(size); | 38 printf("malloc() called with size %d\n", size); |
| 39 ++behind_malloc; |
| 40 void *ret = malloc(size); |
| 41 --behind_malloc; |
| 42 assert(behind_malloc >= 0); |
| 43 return ret; |
35 } | 44 } |
36 | 45 |
37 void __asan_free(void *ptr) { | 46 void __asan_free(void *ptr) { |
38 printf("free() called on %p\n", ptr); | 47 if (behind_malloc == 0) |
| 48 printf("free() called on %p\n", ptr); |
| 49 ++behind_malloc; |
39 free(ptr); | 50 free(ptr); |
| 51 --behind_malloc; |
| 52 assert(behind_malloc >= 0); |
40 } | 53 } |
| 54 |
| 55 void __asan_alloca(void *ptr, int size) { |
| 56 if (behind_malloc == 0) |
| 57 printf("alloca of %d bytes at %p\n", size, ptr); |
| 58 } |
| 59 |
| 60 void __asan_unalloca(void *ptr, int size) { |
| 61 if (behind_malloc == 0) |
| 62 printf("unalloca of %d bytes as %p\n", size, ptr); |
| 63 } |
OLD | NEW |