Path: blob/main/contrib/libder/tests/fuzz_parallel.c
39536 views
/*-1* Copyright (c) 2024 Kyle Evans <[email protected]>2*3* SPDX-License-Identifier: BSD-2-Clause4*/56#include <sys/param.h>7#include <sys/socket.h>89#include <assert.h>10#include <pthread.h>11#include <signal.h>12#include <stdbool.h>13#include <stdint.h>14#include <stdio.h>15#include <stdlib.h>16#include <unistd.h>1718#include <libder.h>1920#include "fuzzers.h"2122struct fuzz_frame {23uint8_t frame_threads;24};2526struct thread_input {27const uint8_t *data;28size_t datasz;29};3031static void *32thread_main(void *cookie)33{34const struct thread_input *input = cookie;35struct libder_ctx *ctx;36struct libder_object *obj;37const uint8_t *data = input->data;38size_t readsz, sz = input->datasz;3940ctx = libder_open();41readsz = sz;42obj = libder_read(ctx, data, &readsz);43if (obj == NULL || readsz != sz)44goto out;4546if (obj != NULL) {47uint8_t *buf = NULL;48size_t bufsz = 0;4950/*51* If we successfully read it, then it shouldn't52* overflow. We're letting libder allocate the buffer,53* so we shouldn't be able to hit the 'too small' bit.54*55* I can't imagine what other errors might happen, so56* we'll just assert on it.57*/58buf = libder_write(ctx, obj, buf, &bufsz);59if (buf == NULL)60goto out;6162assert(bufsz != 0);6364free(buf);65}6667out:68libder_obj_free(obj);69libder_close(ctx);70return (NULL);71}7273int74LLVMFuzzerTestOneInput(const uint8_t *data, size_t sz)75{76const struct fuzz_frame *frame;77pthread_t *threads;78struct thread_input inp;79size_t nthreads;8081if (sz <= sizeof(*frame))82return (-1);8384frame = (const void *)data;85data += sizeof(*frame);86sz -= sizeof(*frame);8788if (frame->frame_threads < 2)89return (-1);9091threads = malloc(sizeof(*threads) * frame->frame_threads);92if (threads == NULL)93return (-1);9495inp.data = data;96inp.datasz = sz;9798for (nthreads = 0; nthreads < frame->frame_threads; nthreads++) {99if (pthread_create(&threads[nthreads], NULL, thread_main,100&inp) != 0)101break;102}103104for (uint8_t i = 0; i < nthreads; i++)105pthread_join(threads[i], NULL);106107free(threads);108109return (0);110}111112113