Merge branch 'fuzzer_updates' of https://github.com/randy408/stb into working
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parents f308577, 3b14b5a · Browse files · 6 files·+110 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+110 added−22 removed
- tests:+110 added−22 removed
Provenance
- Commit
3152efa- Tree
e551f73- Author
Sean Barrett <seanb@radgametools.com>- Committer
Sean Barrett <seanb@radgametools.com>- Parent 1
f308577- Parent 2
3b14b5a
6 files changed+110 added−22 removed
added:tests/fuzz_main.c+54 added
1+#include <stdint.h>2+#include <stdio.h>3+#include <stdlib.h>4+5+/* fuzz target entry point, works without libFuzzer */6+7+int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);8+9+int main(int argc, char **argv)10+{11+ FILE *f;12+ char *buf = NULL;13+ long siz_buf;14+15+ if(argc < 2)16+ {17+ fprintf(stderr, "no input file\n");18+ goto err;19+ }20+21+ f = fopen(argv[1], "rb");22+ if(f == NULL)23+ {24+ fprintf(stderr, "error opening input file %s\n", argv[1]);25+ goto err;26+ }27+28+ fseek(f, 0, SEEK_END);29+30+ siz_buf = ftell(f);31+ rewind(f);32+33+ if(siz_buf < 1) goto err;34+35+ buf = (char*)malloc((size_t)siz_buf);36+ if(buf == NULL)37+ {38+ fprintf(stderr, "malloc() failed\n");39+ goto err;40+ }41+42+ if(fread(buf, (size_t)siz_buf, 1, f) != 1)43+ {44+ fprintf(stderr, "fread() failed\n");45+ goto err;46+ }47+48+ (void)LLVMFuzzerTestOneInput((uint8_t*)buf, (size_t)siz_buf);49+50+err:51+ free(buf);52+53+ return 0;54+}modified:tests/Makefile+3 added-1 removed
1 INCLUDES = -I..2 CFLAGS = -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -DSTB_DIVIDE_TEST3 CPPFLAGS = -Wno-write-strings -DSTB_DIVIDE_TEST4 5+# Uncomment this line for reproducing OSS-Fuzz bugs with image_fuzzer5-#CFLAGS += -O -fsanitize=address6+#CFLAGS += -O -fsanitize=address 7 8 all:9 $(CC) $(INCLUDES) $(CFLAGS) ../stb_vorbis.c test_c_compilation.c test_c_lexer.c test_dxt.c test_easyfont.c test_image.c test_image_write.c test_perlin.c test_sprintf.c test_truetype.c test_voxel.c -lm10 $(CC) $(INCLUDES) $(CPPFLAGS) -std=c++0x test_cpp_compilation.cpp -lm -lstdc++11 $(CC) $(INCLUDES) $(CFLAGS) -DIWT_TEST image_write_test.c -lm -o image_write_test12+ $(CC) $(INCLUDES) $(CFLAGS) fuzz_main.c stbi_read_fuzzer.c -lm -o image_fuzzeradded:tests/ossfuzz.sh+25 added
12345678910111213141516171819202122232425#!/bin/bash -eu# This script is meant to be run by# https://github.com/google/oss-fuzz/blob/master/projects/stb/Dockerfile
$CXX $CXXFLAGS -std=c++11 -I. -DSTBI_ONLY_PNG \ $SRC/stb/tests/stbi_read_fuzzer.c \ -o $OUT/stb_png_read_fuzzer $LIB_FUZZING_ENGINE
$CXX $CXXFLAGS -std=c++11 -I. \ $SRC/stb/tests/stbi_read_fuzzer.c \ -o $OUT/stbi_read_fuzzer $LIB_FUZZING_ENGINE
find $SRC/stb/tests/pngsuite -name "*.png" | \ xargs zip $OUT/stb_png_read_fuzzer_seed_corpus.zip
cp $SRC/stb/tests/stb_png.dict $OUT/stb_png_read_fuzzer.dict
tar xvzf $SRC/stb/jpg.tar.gz --directory $SRC/stb/teststar xvzf $SRC/stb/gif.tar.gz --directory $SRC/stb/tests
find $SRC/stb/tests -name "*.png" -o -name "*.jpg" -o -name ".gif" | \ xargs zip $OUT/stbi_read_fuzzer_seed_corpus.zip
echo "" >> $SRC/stb/tests/gif.dictcat $SRC/stb/tests/gif.dict $SRC/stb/tests/stb_png.dict > $OUT/stbi_read_fuzzer.dictdeleted:tests/stb_png_read_fuzzer.cpp-19 removed
1-#define STB_IMAGE_IMPLEMENTATION2-#define STBI_ONLY_PNG3-#include "../stb_image.h"4-5-extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)6-{7- int x, y, channels;8-9- if(!stbi_info_from_memory(data, size, &x, &y, &channels)) return 0;10-11- /* exit if the image is larger than ~80MB */12- if(y && x > (80000000 / 4) / y) return 0;13-14- unsigned char *img = stbi_load_from_memory(data, size, &x, &y, &channels, 4);15-16- free(img);17-18- return 0;19-}deleted:tests/stb_png_read_fuzzer.options-2 removed
1-[libfuzzer]2-dict = stb_png.dictadded:tests/stbi_read_fuzzer.c+28 added
1+#ifdef __cplusplus2+extern "C" {3+#endif4+5+#define STB_IMAGE_IMPLEMENTATION6+7+#include "../stb_image.h"8+9+10+int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)11+{12+ int x, y, channels;13+14+ if(!stbi_info_from_memory(data, size, &x, &y, &channels)) return 0;15+16+ /* exit if the image is larger than ~80MB */17+ if(y && x > (80000000 / 4) / y) return 0;18+19+ unsigned char *img = stbi_load_from_memory(data, size, &x, &y, &channels, 4);20+21+ free(img);22+23+ return 0;24+}25+26+#ifdef __cplusplus27+}28+#endif