Skip to content

Commit

Permalink
Merge pull request #102 from alex/oss-fuzz
Browse files Browse the repository at this point in the history
OSS-Fuzz fuzzer and build script
  • Loading branch information
PJK committed Jun 27, 2019
2 parents cab5dd3 + 82ce5b8 commit 81de07b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
28 changes: 28 additions & 0 deletions oss-fuzz/build.sh
@@ -0,0 +1,28 @@
#!/bin/bash -eu
# Copyright 2019 Google Inc.
#
# 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
#
# http://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.
#
################################################################################

mkdir build
cd build
# We disable libcbor's default sanitizers since we'll be configuring them ourselves via CFLAGS.
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX="$WORK" -D CBOR_CUSTOM_ALLOC=ON -D SANITIZE=OFF ..
make "-j$(nproc)"
make install

$CXX $CXXFLAGS -std=c++11 "-I$WORK/include" \
../oss-fuzz/cbor_load_fuzzer.cc -o "$OUT/cbor_load_fuzzer" \
$LIB_FUZZING_ENGINE src/libcbor.a

35 changes: 35 additions & 0 deletions oss-fuzz/cbor_load_fuzzer.cc
@@ -0,0 +1,35 @@
#include <cstdint>
#include <cstdio>

#include "cbor.h"

void *limited_malloc(size_t size) {
if (size > 1 << 24) {
return nullptr;
}
return malloc(size);
}

struct State {
FILE* fout;

State() : fout(fopen("/dev/null", "r")) {
cbor_set_allocs(limited_malloc, realloc, free);
}
};

static State kState;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
cbor_load_result result;
cbor_item_t *item = cbor_load(Data, Size, &result);
if (result.error.code == CBOR_ERR_NONE) {
cbor_describe(item, kState.fout);
unsigned char *buffer;
size_t buffer_size;
cbor_serialize_alloc(item, &buffer, &buffer_size);
free(buffer);
cbor_decref(&item);
}
return 0;
}

0 comments on commit 81de07b

Please sign in to comment.