mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Add fuzz tests
This commit is contained in:
1 parent
446d4662db
commit
b23c235067
5 files changed
+278
-2
No files matched your search
@@ -290,7 +290,8 @@ background.
|
||||
- `gen` holds the transpiled editions of that standard library. The initial
|
||||
focus is generating C code. Later on, the repository might include generated
|
||||
Go and Rust code.
|
||||
- `test` holds the tests for the Wuffs standard library.
|
||||
- `test` holds the regular tests for the Wuffs standard library.
|
||||
- `fuzz` holds the fuzz tests for the Wuffs standard library.
|
||||
- `script` holds miscellaneous utility programs.
|
||||
- `doc` holds documentation.
|
||||
- `example` holds example programs.
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// Copyright 2018 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef WUFFS_BASE_HEADER_H
|
||||
#error "Wuffs' .c or .h files need to be included before this file"
|
||||
#endif
|
||||
|
||||
volatile int* intentional_segfault_ptr = NULL;
|
||||
|
||||
void intentional_segfault() {
|
||||
*intentional_segfault_ptr = 0;
|
||||
}
|
||||
|
||||
void fuzz(wuffs_base__reader1 src_reader, uint32_t hash);
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
#endif
|
||||
int
|
||||
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
||||
// Hash input as per https://en.wikipedia.org/wiki/Jenkins_hash_function
|
||||
size_t i = 0;
|
||||
uint32_t hash = 0;
|
||||
while (i != size) {
|
||||
hash += data[i++];
|
||||
hash += hash << 10;
|
||||
hash ^= hash >> 6;
|
||||
}
|
||||
hash += hash << 3;
|
||||
hash ^= hash >> 11;
|
||||
hash += hash << 15;
|
||||
|
||||
wuffs_base__buf1 src = {
|
||||
.ptr = (uint8_t*)(data), .len = size, .wi = size, .closed = true};
|
||||
fuzz((wuffs_base__reader1){.buf = &src}, hash);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef WUFFS_CONFIG__FUZZLIB_MAIN
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int i;
|
||||
for (i = 1; i < argc; i++) {
|
||||
printf("Processing %s\n", argv[i]);
|
||||
|
||||
struct stat z;
|
||||
int fd = open(argv[i], O_RDONLY, 0);
|
||||
if (fd == -1) {
|
||||
fprintf(stderr, "FAIL: open: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
if (fstat(fd, &z)) {
|
||||
fprintf(stderr, "FAIL: fstat: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
if ((z.st_size < 0) || (0x7FFFFFFF < z.st_size)) {
|
||||
fprintf(stderr, "FAIL: file size out of bounds");
|
||||
return 1;
|
||||
}
|
||||
size_t n = z.st_size;
|
||||
void* data = mmap(NULL, n, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (data == MAP_FAILED) {
|
||||
fprintf(stderr, "FAIL: mmap: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fuzz = LLVMFuzzerTestOneInput(data, n);
|
||||
if (fuzz) {
|
||||
return fuzz;
|
||||
}
|
||||
|
||||
if (munmap(data, n)) {
|
||||
fprintf(stderr, "FAIL: mmap: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
if (close(fd)) {
|
||||
fprintf(stderr, "FAIL: close: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
printf("PASS: %d files processed\n", argc > 1 ? argc - 1 : 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // WUFFS_CONFIG__FUZZLIB_MAIN
|
||||
@@ -0,0 +1,100 @@
|
||||
// Copyright 2018 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Silence the nested slash-star warning for the next comment's command line.
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wcomment"
|
||||
|
||||
/*
|
||||
This fuzzer (the fuzz function) is typically run indirectly, by a framework
|
||||
such as https://github.com/google/oss-fuzz calling LLVMFuzzerTestOneInput.
|
||||
|
||||
When working on the fuzz implementation, or as a sanity check, defining
|
||||
WUFFS_CONFIG__FUZZLIB_MAIN will let you manually run fuzz over a set of files:
|
||||
|
||||
gcc -DWUFFS_CONFIG__FUZZLIB_MAIN gif.c && ./a.out ../../../test/testdata/*.gif
|
||||
rm -f ./a.out
|
||||
|
||||
It should print "PASS", amongst other information, and exit(0).
|
||||
*/
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
// If building this program in an environment that doesn't easily accomodate
|
||||
// relative includes, you can use the script/inline-c-relative-includes.go
|
||||
// program to generate a stand-alone C file.
|
||||
#include "../../../gen/c/std/gif.c"
|
||||
#include "../fuzzlib/fuzzlib.c"
|
||||
|
||||
void fuzz(wuffs_base__reader1 src_reader, uint32_t hash) {
|
||||
void* pixbuf = NULL;
|
||||
|
||||
// Use a {} code block so that "goto exit" doesn't trigger "jump bypasses
|
||||
// variable initialization" warnings.
|
||||
{
|
||||
wuffs_gif__status s;
|
||||
wuffs_gif__decoder dec;
|
||||
wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0);
|
||||
|
||||
wuffs_base__image_config ic = {{0}};
|
||||
s = wuffs_gif__decoder__decode_config(&dec, &ic, src_reader);
|
||||
if (s || !wuffs_base__image_config__valid(&ic)) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
// Intentionally crash on 0.1% of all possible image widths.
|
||||
//
|
||||
// TODO: remove this. This intentional segfault is a temporary measure to
|
||||
// check that oss-fuzz' fuzzing and reporting works as advertised.
|
||||
{
|
||||
uint32_t width = wuffs_base__image_config__width(&ic);
|
||||
if ((width % 1000) == 123) {
|
||||
fprintf(stderr, "intentional segfault for width == %" PRIu32 "\n",
|
||||
width);
|
||||
intentional_segfault();
|
||||
}
|
||||
}
|
||||
|
||||
size_t pixbuf_size = wuffs_base__image_config__pixbuf_size(&ic);
|
||||
// Don't try to allocate more than 64 MiB.
|
||||
if (pixbuf_size > 64 * 1024 * 1024) {
|
||||
goto exit;
|
||||
}
|
||||
pixbuf = malloc(pixbuf_size);
|
||||
if (!pixbuf) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
wuffs_base__buf1 dst = {.ptr = (uint8_t*)(pixbuf), .len = pixbuf_size};
|
||||
wuffs_base__writer1 dst_writer = {.buf = &dst};
|
||||
|
||||
while (true) {
|
||||
// TODO: handle the frame rect being larger than the image rect. The
|
||||
// GIF89a spec doesn't disallow this and the Wuffs code tolerates it, in
|
||||
// that it returns a "short write" (because the dst buffer is too small)
|
||||
// and will not e.g. write past the buffer bounds. But the Wuffs GIF API
|
||||
// should somehow pass a subset of pixbuf to decode_frame.
|
||||
dst.wi = 0;
|
||||
s = wuffs_gif__decoder__decode_frame(&dec, dst_writer, src_reader);
|
||||
if (s) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
if (pixbuf) {
|
||||
free(pixbuf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright 2018 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Silence the nested slash-star warning for the next comment's command line.
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wcomment"
|
||||
|
||||
/*
|
||||
This fuzzer (the fuzz function) is typically run indirectly, by a framework
|
||||
such as https://github.com/google/oss-fuzz calling LLVMFuzzerTestOneInput.
|
||||
|
||||
When working on the fuzz implementation, or as a sanity check, defining
|
||||
WUFFS_CONFIG__FUZZLIB_MAIN will let you manually run fuzz over a set of files:
|
||||
|
||||
gcc -DWUFFS_CONFIG__FUZZLIB_MAIN zlib.c && ./a.out ../../../test/testdata/*.zlib
|
||||
rm -f ./a.out
|
||||
|
||||
It should print "PASS", amongst other information, and exit(0).
|
||||
*/
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
// If building this program in an environment that doesn't easily accomodate
|
||||
// relative includes, you can use the script/inline-c-relative-includes.go
|
||||
// program to generate a stand-alone C file.
|
||||
#include "../../../gen/c/std/deflate.c"
|
||||
#include "../../../gen/c/std/zlib.c"
|
||||
#include "../fuzzlib/fuzzlib.c"
|
||||
|
||||
void fuzz(wuffs_base__reader1 src_reader, uint32_t hash) {
|
||||
wuffs_zlib__status s;
|
||||
wuffs_zlib__decoder dec;
|
||||
wuffs_zlib__decoder__initialize(&dec, WUFFS_VERSION, 0);
|
||||
|
||||
// Ignore the checksum for 99.99%-ish of all input. When fuzzers generate
|
||||
// random input, the checkum is very unlikely to match. Still, it's useful to
|
||||
// verify that checksumming does not lead to e.g. buffer overflows.
|
||||
wuffs_zlib__decoder__set_ignore_checksum(&dec, hash & 0xFFFF);
|
||||
|
||||
const size_t dstbuf_size = 65536;
|
||||
uint8_t dstbuf[dstbuf_size];
|
||||
wuffs_base__buf1 dst = {.ptr = dstbuf, .len = dstbuf_size};
|
||||
wuffs_base__writer1 dst_writer = {.buf = &dst};
|
||||
|
||||
while (true) {
|
||||
dst.wi = 0;
|
||||
s = wuffs_zlib__decoder__decode(&dec, dst_writer, src_reader);
|
||||
if (s != WUFFS_ZLIB__SUSPENSION_SHORT_WRITE) {
|
||||
break;
|
||||
}
|
||||
if (dst.wi == 0) {
|
||||
fprintf(stderr, "wuffs_zlib__decoder__decode made no progress\n");
|
||||
intentional_segfault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,9 @@ func checkPackageName(s string) string {
|
||||
return ""
|
||||
}
|
||||
s = strings.ToLower(s)
|
||||
if s == "base" || s == "base_header" || s == "base_impl" {
|
||||
// Blacklist certain package names.
|
||||
switch s {
|
||||
case "base", "base_header", "base_impl", "config", "version":
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
|
||||
Reference in new issue
Block a user