brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · f4637a7 Raw
70 lines · c
1// RUN: %clang_scudo %s -o %t2// RUN: %env_scudo_opts="QuarantineSizeKb=0:ThreadLocalQuarantineSizeKb=0"     %run %t 5 1000000 2>&13// RUN: %env_scudo_opts="QuarantineSizeKb=1024:ThreadLocalQuarantineSizeKb=64" %run %t 5 1000000 2>&14 5// Tests parallel allocations and deallocations of memory chunks from a number6// of concurrent threads, with and without quarantine.7// This test passes if everything executes properly without crashing.8 9#include <assert.h>10#include <pthread.h>11#include <stdio.h>12#include <stdlib.h>13 14#include <sanitizer/allocator_interface.h>15 16int num_threads;17int total_num_alloc;18const int kMaxNumThreads = 500;19pthread_t tid[kMaxNumThreads];20 21pthread_cond_t cond = PTHREAD_COND_INITIALIZER;22pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;23char go = 0;24 25void *thread_fun(void *arg) {26  pthread_mutex_lock(&mutex);27  while (!go)28    pthread_cond_wait(&cond, &mutex);29  pthread_mutex_unlock(&mutex);30  for (int i = 0; i < total_num_alloc / num_threads; i++) {31    void *p = malloc(10);32    __asm__ __volatile__(""33                         :34                         : "r"(p)35                         : "memory");36    free(p);37  }38  return 0;39}40 41int main(int argc, char **argv) {42  assert(argc == 3);43  num_threads = atoi(argv[1]);44  assert(num_threads > 0);45  assert(num_threads <= kMaxNumThreads);46  total_num_alloc = atoi(argv[2]);47  assert(total_num_alloc > 0);48 49  printf("%d threads, %d allocations in each\n", num_threads,50         total_num_alloc / num_threads);51  fprintf(stderr, "Heap size before: %zd\n", __sanitizer_get_heap_size());52  fprintf(stderr, "Allocated bytes before: %zd\n",53          __sanitizer_get_current_allocated_bytes());54 55  for (int i = 0; i < num_threads; i++)56    pthread_create(&tid[i], 0, thread_fun, 0);57  pthread_mutex_lock(&mutex);58  go = 1;59  pthread_cond_broadcast(&cond);60  pthread_mutex_unlock(&mutex);61  for (int i = 0; i < num_threads; i++)62    pthread_join(tid[i], 0);63 64  fprintf(stderr, "Heap size after: %zd\n", __sanitizer_get_heap_size());65  fprintf(stderr, "Allocated bytes after: %zd\n",66          __sanitizer_get_current_allocated_bytes());67 68  return 0;69}70