Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // Regression test for a leak in tsd: |
| 2 | // https://code.google.com/p/address-sanitizer/issues/detail?id=233 |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 3 | // RUN: %clangxx_asan -O1 %s -pthread -o %t |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 4 | // RUN: env ASAN_OPTIONS=$ASAN_OPTIONS:quarantine_size_mb=0 %run %t |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 5 | #include <pthread.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <assert.h> |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 9 | #include <sanitizer/allocator_interface.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 10 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 11 | static pthread_key_t tsd_key; |
| 12 | |
| 13 | void *Thread(void *) { |
| 14 | pthread_setspecific(tsd_key, malloc(10)); |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | static volatile void *v; |
| 19 | |
| 20 | void Dtor(void *tsd) { |
| 21 | v = malloc(10000); |
| 22 | free(tsd); |
| 23 | free((void*)v); // The bug was that this was leaking. |
| 24 | } |
| 25 | |
| 26 | int main() { |
| 27 | assert(0 == pthread_key_create(&tsd_key, Dtor)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 28 | pthread_t t; |
| 29 | for (int i = 0; i < 3; i++) { |
| 30 | pthread_create(&t, 0, Thread, 0); |
| 31 | pthread_join(t, 0); |
| 32 | } |
| 33 | size_t old_heap_size = __sanitizer_get_heap_size(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 34 | for (int i = 0; i < 10; i++) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 35 | pthread_create(&t, 0, Thread, 0); |
| 36 | pthread_join(t, 0); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 37 | size_t new_heap_size = __sanitizer_get_heap_size(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 38 | fprintf(stderr, "heap size: new: %zd old: %zd\n", new_heap_size, old_heap_size); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 39 | assert(old_heap_size == new_heap_size); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 40 | } |
| 41 | } |