From 07454dc5d945a7ffb05ac2b829e210b839fb5a92 Mon Sep 17 00:00:00 2001 From: DavidKorczynski Date: Wed, 18 Nov 2020 23:21:23 +0000 Subject: [PATCH] [leveldb] initial integration (#4636) * initial integration of leveldb * leveldb: fix build. * leveldb: Added a remaining on API call. * leveldb: update project to fit review. --- projects/leveldb/Dockerfile | 26 +++++++++ projects/leveldb/build.sh | 34 +++++++++++ projects/leveldb/fuzz_db.cc | 99 ++++++++++++++++++++++++++++++++ projects/leveldb/fuzz_db.options | 2 + projects/leveldb/project.yaml | 9 +++ 5 files changed, 170 insertions(+) create mode 100644 projects/leveldb/Dockerfile create mode 100755 projects/leveldb/build.sh create mode 100644 projects/leveldb/fuzz_db.cc create mode 100644 projects/leveldb/fuzz_db.options create mode 100644 projects/leveldb/project.yaml diff --git a/projects/leveldb/Dockerfile b/projects/leveldb/Dockerfile new file mode 100644 index 000000000..72342031f --- /dev/null +++ b/projects/leveldb/Dockerfile @@ -0,0 +1,26 @@ +# Copyright 2020 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. +# +################################################################################ + +FROM gcr.io/oss-fuzz-base/base-builder +RUN apt-get update && apt-get install -y make autoconf automake libtool gettext pkg-config build-essential +RUN git clone --recurse-submodules https://github.com/google/leveldb.git + +WORKDIR $SRC/ + +# Copy in our files +COPY build.sh $SRC/ +COPY *.cc $SRC/leveldb/ +COPY *.options $SRC/ diff --git a/projects/leveldb/build.sh b/projects/leveldb/build.sh new file mode 100755 index 000000000..2375288fe --- /dev/null +++ b/projects/leveldb/build.sh @@ -0,0 +1,34 @@ +#!/bin/bash -eu +# Copyright 2020 Google Inc. +# Copyright 2020 Luca Boccassi +# +# 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. +# +################################################################################ + +cd $SRC/leveldb +mkdir -p build && cd build +cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build . + +for fuzzer in fuzz_db; do + # Compile + $CXX $CXXFLAGS -c ../${fuzzer}.cc -o ${fuzzer}.o \ + -DLEVELDB_PLATFORM_POSIX=1 -std=c++11 \ + -I$SRC/leveldb/build/include -I$SRC/leveldb/ -I$SRC/leveldb/include + + # Link + $CXX $LIB_FUZZING_ENGINE $CXXFLAGS ${fuzzer}.o -o $OUT/${fuzzer} libleveldb.a +done + +# Copy options to out +cp $SRC/*options $OUT/ diff --git a/projects/leveldb/fuzz_db.cc b/projects/leveldb/fuzz_db.cc new file mode 100644 index 000000000..7263881e6 --- /dev/null +++ b/projects/leveldb/fuzz_db.cc @@ -0,0 +1,99 @@ +/* Copyright 2020 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. +*/ + +#include "leveldb/db.h" +#include +#include +#include +#include +#include + +#include + + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + // We need at least one byte + if (size == 0) { + return 0; + } + + FuzzedDataProvider fuzzed_data(data, size); + + leveldb::DB* db; + leveldb::Options options; + options.create_if_missing = true; + leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); + + // for random string generation + const uint8_t *curr_offset = data; + size_t curr_size = size; + + std::string value; + + // perform a sequence of calls on our db instance + int max_iter = (int)data[0]; + for(int i=0; i < max_iter && i < size; i++) { + #define SIZE_OF_FUNCS 8 + size_t c = fuzzed_data.ConsumeIntegral() % SIZE_OF_FUNCS; + + if(c == 0) { // PUT + std::string tmp1 = fuzzed_data.ConsumeRandomLengthString(); + std::string tmp2 = fuzzed_data.ConsumeRandomLengthString(); + db->Put(leveldb::WriteOptions(), tmp1, tmp2); + } + else if(c == 1) { // Get + std::string tmp3 = fuzzed_data.ConsumeRandomLengthString(); + db->Get(leveldb::ReadOptions(), tmp3, &value); + } + else if (c == 2) { // Delete + std::string tmp4 = fuzzed_data.ConsumeRandomLengthString(); + db->Delete(leveldb::WriteOptions(), tmp4); + } + else if (c == 3) { // GetProperty + std::string prop; + std::string tmp = fuzzed_data.ConsumeRandomLengthString(); + db->GetProperty(tmp, &prop); + } + else if(c == 4) { // Iterator + leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions()); + for (it->SeekToFirst(); it->Valid(); it->Next()) { + continue; + } + delete it; + } + else if(c == 5) { // GetSnapshot and Release Snapshot + leveldb::ReadOptions snapshot_options; + snapshot_options.snapshot = db->GetSnapshot(); + leveldb::Iterator* it = db->NewIterator(snapshot_options); + db->ReleaseSnapshot(snapshot_options.snapshot); + } + else if(c == 6) { // Open and close DB + delete db; + status = leveldb::DB::Open(options, "/tmp/testdb", &db); + } + else if (c == 7) { + std::string tmp1 = fuzzed_data.ConsumeRandomLengthString(); + std::string tmp2 = fuzzed_data.ConsumeRandomLengthString(); + leveldb::Slice s1 =tmp1; + leveldb::Slice s2 = tmp2; + db->CompactRange(&s1, &s2); + } + } + + // Cleanup DB + delete db; + std::__fs::filesystem::remove_all("/tmp/testdb"); + return 0; +} diff --git a/projects/leveldb/fuzz_db.options b/projects/leveldb/fuzz_db.options new file mode 100644 index 000000000..f9d09656c --- /dev/null +++ b/projects/leveldb/fuzz_db.options @@ -0,0 +1,2 @@ +[libfuzzer] +detect_leaks=0 diff --git a/projects/leveldb/project.yaml b/projects/leveldb/project.yaml new file mode 100644 index 000000000..a6d8b457a --- /dev/null +++ b/projects/leveldb/project.yaml @@ -0,0 +1,9 @@ +homepage: "https://github.com/google/leveldb" +language: c++ +primary_contact: "costan@google.com" +auto_ccs : + - "cmumford@google.com" + - "david@adalogics.com" +sanitizers: + - address + - memory