1 | // Tests __hwasan_print_memory_usage. |
2 | // RUN: %clang_hwasan %s -o %t |
3 | // RUN: ulimit -s 1000 |
4 | // RUN: %run %t 2>&1 | FileCheck %s |
5 | |
6 | #include <pthread.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
9 | #include <string.h> |
10 | |
11 | #include <sanitizer/hwasan_interface.h> |
12 | |
13 | int state; |
14 | __thread volatile char *sink; |
15 | |
16 | __attribute__((noinline)) |
17 | void *malloc_and_use(int size) { |
18 | char *x = (char*)malloc(size: size); |
19 | for (int i = 0; i < size; i++) |
20 | x[i] = 42; // make this memory used. |
21 | return x; |
22 | } |
23 | |
24 | void *T1(void *arg) { |
25 | for (int i = 1; i <= (1 << 20); i *= 2) { |
26 | sink = malloc_and_use(size: i); |
27 | free(sink); |
28 | } |
29 | |
30 | __sync_fetch_and_add(&state, 1); |
31 | while (__sync_fetch_and_add(&state, 0) != 4) {} |
32 | return NULL; |
33 | } |
34 | |
35 | void *T4(void *arg) { return NULL; } |
36 | |
37 | int main() { |
38 | __hwasan_enable_allocator_tagging(); |
39 | sink = malloc_and_use(size: 10); |
40 | |
41 | __hwasan_print_memory_usage(); |
42 | // CHECK: HWASAN pid: [[PID:[0-9]*]] rss: {{.*}} threads: 1 stacks: [[STACKS:[0-9]*]] thr_aux: {{.*}} stack_depot: {{.*}} uniq_stacks: [[UNIQ_STACKS:[0-9]*]] heap: [[HEAP:[0-9]*]] |
43 | |
44 | void *one_meg = malloc_and_use(size: 1 << 20); |
45 | |
46 | __hwasan_print_memory_usage(); |
47 | // CHECK: HWASAN pid: [[PID]] rss: {{.*}} threads: 1 stacks: [[STACKS]] thr_aux: {{.*}} stack_depot: {{.*}} |
48 | |
49 | free(ptr: one_meg); |
50 | |
51 | __hwasan_print_memory_usage(); |
52 | // CHECK: HWASAN pid: [[PID]] rss: {{.*}} threads: 1 stacks: [[STACKS]] thr_aux: {{.*}} stack_depot: {{.*}} uniq_stacks: {{.*}} heap: [[HEAP]] |
53 | |
54 | pthread_t t1, t2, t3, t4; |
55 | |
56 | pthread_create(newthread: &t1, NULL, start_routine: T1, NULL); |
57 | pthread_create(newthread: &t2, NULL, start_routine: T1, NULL); |
58 | pthread_create(newthread: &t3, NULL, start_routine: T1, NULL); |
59 | pthread_create(newthread: &t4, NULL, start_routine: T4, NULL); |
60 | while (__sync_fetch_and_add(&state, 0) != 3) {} |
61 | pthread_join(th: t4, NULL); |
62 | |
63 | __hwasan_print_memory_usage(); |
64 | // CHECK: HWASAN pid: [[PID]] rss: {{.*}} threads: 4 stacks: |
65 | |
66 | __sync_fetch_and_add(&state, 1); |
67 | pthread_join(th: t1, NULL); |
68 | pthread_join(th: t2, NULL); |
69 | pthread_join(th: t3, NULL); |
70 | __hwasan_print_memory_usage(); |
71 | // CHECK: HWASAN pid: [[PID]] rss: {{.*}} threads: 1 stacks: [[STACKS]] |
72 | } |
73 | |