Examples › Database Examples
database_fuzz_fuzz_db
Legacy: examples_database/fuzz/fuzz_db.cpp · Modern: examples_modern/database/fuzz/fuzz_db.cpp
Modern equivalent of the libFuzzer targets for the object database (the .gxd data file target; the legacy file also has .btx and journal targets built the same way). The input bytes come in as a C++20 std::span<const std::uint8_t>, are written to a file with <fstream>, and the file is opened and walked like a damaged file: every object is read into a std::vector sized from ObjectLength() and capped, checksums are read, and the walk is bounded. Each run must end with an error code or good data.
Build as a libFuzzer target (clang, with the 6.4.3 library): clang++ -std=c++20 -fsanitize=fuzzer,address,undefined \ -DMODERN_LIBFUZZER fuzz_db.cpp ... -ldatareel Without -DMODERN_LIBFUZZER it builds a main() that makes a good database file and then feeds the target copies of it with random bytes changed (std::mt19937), so it runs as an ordinary test.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| Find(), IFind(), FindLast() | find(), a std::search with a case-folding predicate, rfind() | C++98 | Watch out: rfind("") returns size(), where FindLast("") returns -1. |
| new / delete | automatic objects, containers, std::unique_ptr / std::make_unique | C++11 / C++14 | Ownership is in the type, and release() shows a hand-over. |
| strcpy(), strncpy(), memmove() into char arrays | std::string; std::array<char, N> + string_view::copy() | C++11 / C++17 | |
| gxDatabase | keep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>) | C++17 | The standard library has no database file; the file format is DataReel's. |
| gxBtree, DatabaseKeyB key classes | keep gxBtree; key classes with std::array storage and correct copies (rule of three); std::set for in-memory indexes | C++11 / C++17 | DatabaseKeyB's copy keeps the other object's db_key pointer: define the copies. |
| BtreeCache | keep BtreeCache; flush it with an RAII guard | C++11 | |
| gxdJournal Begin()/Commit()/Rollback() | keep gxdJournal; an RAII Transaction that rolls back unless commit() was called | C++11 |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_database/fuzz/fuzz_db.cpp 102 lines
#include "gxdlcode.h" #include <cstdio> #include <cstdint> #include <cstring> #include <string> #include "gxbtree.h" #include "btcache.h" #include "gxdjrnl.h" static const size_t KSIZE = 16; struct FzKeyData { char k[KSIZE]; FAU addr; }; class FzKey : public DatabaseKeyB { public: FzKey() : DatabaseKeyB((char *)&d) { memset(&d, 0, sizeof(d)); } size_t KeySize() { return sizeof(d); } int CompareKey(const DatabaseKeyB &o) const { return memcmp(d.k, ((const FzKeyData *)o.db_key)->k, KSIZE); } int operator==(const DatabaseKeyB &o) const { return CompareKey(o) == 0; } int operator>(const DatabaseKeyB &o) const { return CompareKey(o) > 0; } FzKeyData d; }; static void write_file(const char *name, const uint8_t *data, size_t size) { FILE *f = fopen(name, "wb"); if(!f) return; if(size) fwrite(data, 1, size, f); fclose(f); } #if defined (FUZZ_GXD) static void walk_gxd(int cache) { gxDatabase db; if(db.Open("fz.gxd", gxDBASE_READONLY) != gxDBASE_NO_ERROR) return; if(cache) db.EnablePageCache(64 * 1024); char buf[4096]; int n = 0; FAU_t a = db.FindFirstObject(); while(a && n++ < 2000) { __ULWORD__ len = db.ObjectLength(a); if(len > 0 && len <= sizeof(buf)) db.Read(buf, len, a); db.ReadObjectChecksum(a); a = db.FindNextObject(a); } db.TotalBlocks(); db.Close(); } #endif #if defined (FUZZ_BTX) static void walk_btx(int cached) { FzKey type, key, cmp; gxBtree btx(type, 16); if(btx.Open("fz.btx", gxDBASE_READONLY) != gxDBASE_NO_ERROR) return; BtreeCache *cache = cached ? new BtreeCache(8, &btx) : 0; int n = 0; int ok = cached ? btx.FindFirst(key, cache) : btx.FindFirst(key); while(ok && n++ < 2000) { FzKey f = key, c; memcpy(&f.d, &key.d, sizeof(f.d)); f.db_key = &f.d; if(cached) btx.Find(f, c, cache); else btx.Find(f, c); ok = cached ? btx.FindNext(key, cmp, cache) : btx.FindNext(key, cmp); } FzKey probe; memcpy(probe.d.k, "0000000000000500", KSIZE); if(cached) btx.Find(probe, cmp, cache); else btx.Find(probe, cmp); delete cache; btx.Close(); } #endif extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { #if defined (FUZZ_GXD) write_file("fz.gxd", data, size); walk_gxd(0); walk_gxd(1); #elif defined (FUZZ_BTX) write_file("fz.btx", data, size); walk_btx(0); walk_btx(1); #elif defined (FUZZ_JNL) write_file("fz.jnl", data, size); unsigned long recovered = 0; gxdJournal::Recover("fz.jnl", &recovered); #else #error Define FUZZ_GXD, FUZZ_BTX or FUZZ_JNL #endif return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++20)
examples_modern/database/fuzz/fuzz_db.cpp 86 lines
#include <cstdint> #include <cstdlib> #include <filesystem> #include <fstream> #include <iostream> #include <iterator> #include <random> #include <span> #include <string> #include <vector> #include "gxdbase.h" static const char *const fuzz_file = "modern_fz.gxd"; static void walk_gxd(bool cache) { gxDatabase db; if(db.Open(fuzz_file, gxDBASE_READONLY) != gxDBASE_NO_ERROR) return; if(cache) db.EnablePageCache(64 * 1024); std::vector<unsigned char> buf; int n = 0; for(FAU_t a = db.FindFirstObject(); a && n < 2000; a = db.FindNextObject(a), n++) { __ULWORD__ len = db.ObjectLength(a); if(len > 0 && len <= 4096) { // Never trust a length buf.resize(len); db.Read(buf.data(), len, a); } db.ReadObjectChecksum(a); } db.TotalBlocks(); } static void fuzz_gxd(std::span<const std::uint8_t> in) { { std::ofstream out(fuzz_file, std::ios::binary | std::ios::trunc); out.write(reinterpret_cast<const char *>(in.data()), std::streamsize(in.size())); } walk_gxd(false); walk_gxd(true); } extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { fuzz_gxd({data, size}); return 0; } #if !defined(MODERN_LIBFUZZER) int main(int argc, char **argv) { { gxDatabase db; // A good seed file db.Create(fuzz_file, FAU_t(0), 'C'); for(int i = 0; i < 50; i++) { std::string rec(32 + i, char('a' + i % 26)); FAU_t a = db.Alloc(__ULWORD__(rec.size())); db.Write(rec.data(), __ULWORD__(rec.size()), a); db.WriteObjectChecksum(a); } } std::vector<std::uint8_t> seed; { std::ifstream in(fuzz_file, std::ios::binary); seed.assign(std::istreambuf_iterator<char>(in), {}); } int runs = argc > 1 ? std::atoi(argv[1]) : 300; std::mt19937 gen(2026); std::uniform_int_distribution<std::size_t> pos(0, seed.size() - 1); std::uniform_int_distribution<int> byte(0, 255), count(1, 8); for(int r = 0; r < runs; r++) { std::vector<std::uint8_t> input = seed; for(int c = count(gen); c > 0; c--) input[pos(gen)] = std::uint8_t(byte(gen)); if(r % 10 == 0) input.resize(pos(gen)); // Cut short too LLVMFuzzerTestOneInput(input.data(), input.size()); } std::filesystem::remove(fuzz_file); std::cout << "fuzz_db: " << runs << " damaged copies opened and walked\n"; return 0; } #endif // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=20 cmake --build build-modern --target database_fuzz_fuzz_db ./build-modern/database_fuzz_fuzz_db
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
fuzz_db: 300 damaged copies opened and walked