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