tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

alloc_fail.c (3385B)


      1 /*
      2 * Copyright © 2018, VideoLAN and dav1d authors
      3 * Copyright © 2018, Janne Grunau
      4 * All rights reserved.
      5 *
      6 * Redistribution and use in source and binary forms, with or without
      7 * modification, are permitted provided that the following conditions are met:
      8 *
      9 * 1. Redistributions of source code must retain the above copyright notice, this
     10 *    list of conditions and the following disclaimer.
     11 *
     12 * 2. Redistributions in binary form must reproduce the above copyright notice,
     13 *    this list of conditions and the following disclaimer in the documentation
     14 *    and/or other materials provided with the distribution.
     15 *
     16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
     20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 #include "config.h"
     29 
     30 #include <stddef.h>
     31 #include <stdlib.h>
     32 #include <errno.h>
     33 #include <pthread.h>
     34 
     35 #include "alloc_fail.h"
     36 
     37 static int fail_probability;
     38 
     39 void dav1d_setup_alloc_fail(unsigned seed, unsigned probability) {
     40    srand(seed);
     41 
     42    while (probability >= RAND_MAX)
     43        probability >>= 1;
     44 
     45    fail_probability = probability;
     46 }
     47 
     48 void * __wrap_malloc(size_t);
     49 
     50 void * __wrap_malloc(size_t sz) {
     51    if (rand() < fail_probability)
     52        return NULL;
     53    return malloc(sz);
     54 }
     55 
     56 #if defined(HAVE_POSIX_MEMALIGN)
     57 int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size);
     58 
     59 int __wrap_posix_memalign(void **memptr, size_t alignment, size_t size) {
     60    if (rand() < fail_probability)
     61        return ENOMEM;
     62    return posix_memalign(memptr, alignment, size);
     63 }
     64 #else
     65 #error "HAVE_POSIX_MEMALIGN required"
     66 #endif
     67 
     68 int __wrap_pthread_create(pthread_t *, const pthread_attr_t *,
     69                          void *(*) (void *), void *);
     70 
     71 int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
     72                          void *(*start_routine) (void *), void *arg)
     73 {
     74    if (rand() < (fail_probability + RAND_MAX/16))
     75        return EAGAIN;
     76 
     77    return pthread_create(thread, attr, start_routine, arg);
     78 }
     79 
     80 int __wrap_pthread_mutex_init(pthread_mutex_t *,
     81                              const pthread_mutexattr_t *);
     82 
     83 int __wrap_pthread_mutex_init(pthread_mutex_t *restrict mutex,
     84                              const pthread_mutexattr_t *restrict attr)
     85 {
     86    if (rand() < (fail_probability + RAND_MAX/8))
     87        return ENOMEM;
     88 
     89    return pthread_mutex_init(mutex, attr);
     90 }
     91 
     92 int __wrap_pthread_cond_init(pthread_cond_t *,
     93                             const pthread_condattr_t *);
     94 
     95 int __wrap_pthread_cond_init(pthread_cond_t *restrict cond,
     96                             const pthread_condattr_t *restrict attr)
     97 {
     98    if (rand() < (fail_probability + RAND_MAX/16))
     99        return ENOMEM;
    100 
    101    return pthread_cond_init(cond, attr);
    102 }