Skip to content

Commit

Permalink
Move ossfuzz directory and use Makefile.am
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeister2 committed Jul 19, 2019
1 parent 100e554 commit 24cc9dd
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ script:
- if [ "$JANSSON_BUILD_METHOD" = "autotools" ]; then autoreconf -f -i && CFLAGS=-Werror ./configure && make check; fi
- if [ "$JANSSON_BUILD_METHOD" = "cmake" ]; then mkdir build && cd build && cmake $JANSSON_CMAKE_OPTIONS .. && cmake --build . && ctest --output-on-failure; fi
- if [ "$JANSSON_BUILD_METHOD" = "coverage" ]; then mkdir build && cd build && cmake $JANSSON_CMAKE_OPTIONS .. && cmake --build . && cmake --build . --target coveralls; fi
- if [ "$JANSSON_BUILD_METHOD" = "fuzzer" ]; then ./ossfuzz/travisoss.sh; fi
- if [ "$JANSSON_BUILD_METHOD" = "fuzzer" ]; then ./test/ossfuzz/travisoss.sh; fi
12 changes: 0 additions & 12 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ dvi:

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = jansson.pc


# Add fuzzing support
LIB_FUZZING_ENGINE ?= standaloneengine.o

ossfuzz/%.o: ossfuzz/%.cc
$(CXX) -c -Isrc $(CXXFLAGS) $< -o $@

.PHONY: json_load_fuzzer
json_load_fuzzer: ossfuzz/json_load_fuzzer.o src/.libs/libjansson.a
$(CXX) -c $(CXXFLAGS) ossfuzz/standaloneengine.cc -o standaloneengine.o
$(CXX) $(CXXFLAGS) $(LIB_FUZZING_ENGINE) $^ -o $@$(EXT)
15 changes: 15 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AC_CONFIG_HEADERS([jansson_private_config.h])

# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
AC_PROG_LIBTOOL
AM_CONDITIONAL([GCC], [test x$GCC = xyes])

Expand Down Expand Up @@ -136,6 +137,19 @@ fi
AS_IF([test "x$with_Bsymbolic" = "xyes"], [JSON_BSYMBOLIC_LDFLAGS=-Wl[,]-Bsymbolic-functions])
AC_SUBST(JSON_BSYMBOLIC_LDFLAGS)


AC_ARG_ENABLE([ossfuzzers],
[AS_HELP_STRING([--enable-ossfuzzers],
[Whether to generate the fuzzers for OSS-Fuzz])],
[have_ossfuzzers=yes], [have_ossfuzzers=no])
AM_CONDITIONAL([USE_OSSFUZZERS], [test "x$have_ossfuzzers" = "xyes"])


AC_SUBST([LIB_FUZZING_ENGINE])
AM_CONDITIONAL([USE_OSSFUZZ_FLAG], [test "x$LIB_FUZZING_ENGINE" = "x-fsanitize=fuzzer"])
AM_CONDITIONAL([USE_OSSFUZZ_STATIC], [test -f "x$LIB_FUZZING_ENGINE"])


if test x$GCC = xyes; then
AC_MSG_CHECKING(for -Wno-format-truncation)
wnoformat_truncation="-Wno-format-truncation"
Expand All @@ -156,6 +170,7 @@ AC_CONFIG_FILES([
src/jansson_config.h
test/Makefile
test/bin/Makefile
test/ossfuzz/Makefile
test/suites/Makefile
test/suites/api/Makefile
])
Expand Down
11 changes: 0 additions & 11 deletions ossfuzz/json_load_fuzzer.cc

This file was deleted.

2 changes: 1 addition & 1 deletion test/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SUBDIRS = bin suites
SUBDIRS = bin suites ossfuzz
EXTRA_DIST = scripts run-suites

TESTS = run-suites
Expand Down
1 change: 1 addition & 0 deletions test/ossfuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json_load_dump_fuzzer
32 changes: 32 additions & 0 deletions test/ossfuzz/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
AM_CPPFLAGS = -I$(top_builddir)/src -I$(top_srcdir)/src
LDADD = $(top_builddir)/src/libjansson.la

if USE_OSSFUZZ_FLAG
FUZZ_FLAG = $(LIB_FUZZING_ENGINE)
else
if USE_OSSFUZZ_STATIC
LDADD += $(LIB_FUZZING_ENGINE)
FUZZ_FLAG =
else
LDADD += libstandaloneengine.a
FUZZ_FLAG =
endif
endif

noinst_PROGRAMS =
noinst_LIBRARIES =

if USE_OSSFUZZERS
noinst_PROGRAMS += \
json_load_dump_fuzzer

noinst_LIBRARIES += \
libstandaloneengine.a
endif

json_load_dump_fuzzer_SOURCES = json_load_dump_fuzzer.cc testinput.h
json_load_dump_fuzzer_CXXFLAGS = $(AM_CXXFLAGS) $(FUZZ_FLAG)
json_load_dump_fuzzer_LDFLAGS = $(AM_LDFLAGS) -static

libstandaloneengine_a_SOURCES = standaloneengine.cc
libstandaloneengine_a_CXXFLAGS = $(AM_CXXFLAGS)
47 changes: 47 additions & 0 deletions test/ossfuzz/json_load_dump_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdint.h>
#include <sys/types.h>

#include "jansson.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
json_error_t error;

if (size < sizeof(size_t) + sizeof(size_t))
{
return 0;
}

// Use the first sizeof(size_t) bytes as load flags.
size_t load_flags = *(const size_t*)data;
data += sizeof(size_t);
size -= sizeof(size_t);

// Use the next sizeof(size_t) bytes as dump flags.
size_t dump_flags = *(const size_t*)data;
data += sizeof(size_t);
size -= sizeof(size_t);

// Attempt to load the remainder of the data with the given load flags.
const char* text = reinterpret_cast<const char *>(data);
json_t* jobj = json_loadb(text, size, load_flags, &error);

if (jobj == NULL)
{
return 0;
}

// Attempt to dump the loaded json object with the given dump flags.
char* out = json_dumps(jobj, dump_flags);
if (out)
{
free(out);
}

if (jobj)
{
json_decref(jobj);
}

return 0;
}
5 changes: 2 additions & 3 deletions ossfuzz/ossfuzz.sh → test/ossfuzz/ossfuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ apt-get -y install automake libtool

# Compile the fuzzer.
autoreconf -i
./configure
./configure --enable-ossfuzzers
make
make json_load_fuzzer

# Copy the fuzzer to the output directory.
cp -v json_load_fuzzer $OUT/
cp -v test/ossfuzz/json_load_dump_fuzzer $OUT/
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 24cc9dd

Please sign in to comment.