shithub: dav1d

Download patch

ref: 2bc9ba828f5c2d4663478bd94e125101851d1e9c
parent: 50a2da6d6d6ec151a9bd44942095303c6a7cdf7f
author: Janne Grunau <[email protected]>
date: Thu Feb 7 18:43:25 EST 2019

dav1d_fuzzer_mem: test random pthread_* errors

--- a/tests/libfuzzer/alloc_fail.c
+++ b/tests/libfuzzer/alloc_fail.c
@@ -30,6 +30,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <pthread.h>
 
 #include "alloc_fail.h"
 
@@ -63,3 +64,39 @@
 #else
 #error "HAVE_POSIX_MEMALIGN required"
 #endif
+
+int __wrap_pthread_create(pthread_t *, const pthread_attr_t *,
+                          void *(*) (void *), void *);
+
+int __wrap_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
+                          void *(*start_routine) (void *), void *arg)
+{
+    if (rand() < (fail_probability + RAND_MAX/16))
+        return EAGAIN;
+
+    return pthread_create(thread, attr, start_routine, arg);
+}
+
+int __wrap_pthread_mutex_init(pthread_mutex_t *,
+                              const pthread_mutexattr_t *);
+
+int __wrap_pthread_mutex_init(pthread_mutex_t *restrict mutex,
+                              const pthread_mutexattr_t *restrict attr)
+{
+    if (rand() < (fail_probability + RAND_MAX/8))
+        return ENOMEM;
+
+    return pthread_mutex_init(mutex, attr);
+}
+
+int __wrap_pthread_cond_init(pthread_cond_t *,
+                             const pthread_condattr_t *);
+
+int __wrap_pthread_cond_init(pthread_cond_t *restrict cond,
+                             const pthread_condattr_t *restrict attr)
+{
+    if (rand() < (fail_probability + RAND_MAX/16))
+        return ENOMEM;
+
+    return pthread_cond_init(cond, attr);
+}
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -138,6 +138,9 @@
                                 command: [objcopy,
                                           '--redefine-sym', 'malloc=__wrap_malloc',
                                           '--redefine-sym', 'posix_memalign=__wrap_posix_memalign',
+                                          '--redefine-sym', 'pthread_create=__wrap_pthread_create',
+                                          '--redefine-sym', 'pthread_cond_init=__wrap_pthread_cond_init',
+                                          '--redefine-sym', 'pthread_mutex_init=__wrap_pthread_mutex_init',
                                           '@INPUT@', '@OUTPUT@'])
 
     dav1d_fuzzer_mem = executable('dav1d_fuzzer_mem',