Skip to content

Commit

Permalink
scripts: convert fuzzing scripts from afl-fuzz to libFuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
ebiggers committed Jan 15, 2023
1 parent ea2c695 commit d6bbdb2
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 294 deletions.
56 changes: 0 additions & 56 deletions scripts/afl-fuzz/deflate_compress/fuzz.c

This file was deleted.

28 changes: 0 additions & 28 deletions scripts/afl-fuzz/deflate_decompress/fuzz.c

This file was deleted.

154 changes: 0 additions & 154 deletions scripts/afl-fuzz/fuzz.sh

This file was deleted.

28 changes: 0 additions & 28 deletions scripts/afl-fuzz/gzip_decompress/fuzz.c

This file was deleted.

28 changes: 0 additions & 28 deletions scripts/afl-fuzz/zlib_decompress/fuzz.c

This file was deleted.

1 change: 1 addition & 0 deletions scripts/libFuzzer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*/fuzz
File renamed without changes.
52 changes: 52 additions & 0 deletions scripts/libFuzzer/deflate_compress/fuzz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <assert.h>
#include <libdeflate.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* Fuzz the DEFLATE compression and decompression round trip. */
int LLVMFuzzerTestOneInput(const uint8_t *in, size_t insize)
{
int level;
bool use_bound;
struct libdeflate_compressor *c;
struct libdeflate_decompressor *d;
size_t csize_avail;
uint8_t *cbuf;
uint8_t *decompressed;
size_t csize;
enum libdeflate_result res;

if (insize < 2)
return 0;

level = in[0] % 13;
use_bound = in[1] % 2;
in += 2;
insize -= 2;

c = libdeflate_alloc_compressor(level);
d = libdeflate_alloc_decompressor();

csize_avail = use_bound ? libdeflate_deflate_compress_bound(c, insize) :
insize;
cbuf = malloc(csize_avail);
decompressed = malloc(insize);

csize = libdeflate_deflate_compress(c, in, insize, cbuf, csize_avail);
if (csize != 0) {
res = libdeflate_deflate_decompress(d, cbuf, csize, decompressed,
insize, NULL);
assert(res == LIBDEFLATE_SUCCESS);
assert(memcmp(in, decompressed, insize) == 0);
} else {
assert(!use_bound);
}

libdeflate_free_compressor(c);
libdeflate_free_decompressor(d);
free(cbuf);
free(decompressed);
return 0;
}
19 changes: 19 additions & 0 deletions scripts/libFuzzer/deflate_decompress/fuzz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <libdeflate.h>
#include <stdint.h>
#include <stdlib.h>

/* Fuzz DEFLATE decompression. */
int LLVMFuzzerTestOneInput(const uint8_t *in, size_t insize)
{
size_t outsize_avail = 3 * insize;
uint8_t *out;
struct libdeflate_decompressor *d;

out = malloc(outsize_avail);

d = libdeflate_alloc_decompressor();
libdeflate_deflate_decompress(d, in, insize, out, outsize_avail, NULL);
libdeflate_free_decompressor(d);
free(out);
return 0;
}
Loading

0 comments on commit d6bbdb2

Please sign in to comment.