diff options
author | DavidKorczynski <david@adalogics.com> | 2020-11-18 23:21:23 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-18 15:21:23 -0800 |
commit | 07454dc5d945a7ffb05ac2b829e210b839fb5a92 (patch) | |
tree | ffca1baf990ad929b24c1dfaaa251b61f3cb2f8a /projects/leveldb/fuzz_db.cc | |
parent | 3f782ade9ab219fd62918fedc1881e553dd48047 (diff) |
[leveldb] initial integration (#4636)
* initial integration of leveldb
* leveldb: fix build.
* leveldb: Added a remaining on API call.
* leveldb: update project to fit review.
Diffstat (limited to 'projects/leveldb/fuzz_db.cc')
-rw-r--r-- | projects/leveldb/fuzz_db.cc | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/projects/leveldb/fuzz_db.cc b/projects/leveldb/fuzz_db.cc new file mode 100644 index 00000000..7263881e --- /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 <stdio.h> +#include <stdlib.h> +#include <string> +#include <string.h> +#include <filesystem> + +#include <fuzzer/FuzzedDataProvider.h> + + +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<uint8_t>() % 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; +} |