1 | // Regression test for a leak in tsd: |
2 | // https://code.google.com/p/address-sanitizer/issues/detail?id=233 |
3 | // RUN: %clangxx_asan -O1 %s -pthread -o %t |
4 | // RUN: %env_asan_opts=quarantine_size_mb=0 %run %t |
5 | // XFAIL: x86_64-netbsd |
6 | #include <pthread.h> |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <assert.h> |
10 | #include <sanitizer/allocator_interface.h> |
11 | |
12 | static pthread_key_t tsd_key; |
13 | |
14 | void *Thread(void *) { |
15 | pthread_setspecific(key: tsd_key, pointer: malloc(size: 10)); |
16 | return 0; |
17 | } |
18 | |
19 | static volatile void *v; |
20 | |
21 | void Dtor(void *tsd) { |
22 | v = malloc(size: 10000); |
23 | free(ptr: tsd); |
24 | free(ptr: (void*)v); // The bug was that this was leaking. |
25 | } |
26 | |
27 | int main() { |
28 | assert(0 == pthread_key_create(&tsd_key, Dtor)); |
29 | pthread_t t; |
30 | for (int i = 0; i < 3; i++) { |
31 | pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0); |
32 | pthread_join(th: t, thread_return: 0); |
33 | } |
34 | size_t old_heap_size = __sanitizer_get_heap_size(); |
35 | for (int i = 0; i < 10; i++) { |
36 | pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0); |
37 | pthread_join(th: t, thread_return: 0); |
38 | size_t new_heap_size = __sanitizer_get_heap_size(); |
39 | fprintf(stderr, format: "heap size: new: %zd old: %zd\n" , new_heap_size, old_heap_size); |
40 | assert(old_heap_size == new_heap_size); |
41 | } |
42 | } |
43 | |