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