Add FacadeFuzz
This commit is contained in:
@@ -156,6 +156,12 @@ if(BUILD_TESTING)
|
||||
${TEST_FLAGS})
|
||||
target_link_options(facade_test PRIVATE -fsanitize=address,undefined)
|
||||
|
||||
add_executable(facade_fuzz FacadeFuzz.cpp)
|
||||
target_link_libraries(facade_fuzz PRIVATE ${PROJECT_NAME})
|
||||
target_compile_options(facade_fuzz PRIVATE -fsanitize=address,undefined,fuzzer
|
||||
${TEST_FLAGS})
|
||||
target_link_options(facade_fuzz PRIVATE -fsanitize=address,undefined,fuzzer)
|
||||
|
||||
# symbol visibility tests
|
||||
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
|
||||
if(APPLE)
|
||||
|
107
FacadeFuzz.cpp
Normal file
107
FacadeFuzz.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "Facade.h"
|
||||
#include "Internal.h"
|
||||
#include "KeyCompare.h"
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
constexpr int kKeySize = 4;
|
||||
|
||||
weaselab::VersionedMap::Key randomKey(Arena &arena) {
|
||||
auto *result = new (arena) uint8_t[kKeySize];
|
||||
gRandom.randomHex(result, kKeySize);
|
||||
return {result, kKeySize};
|
||||
}
|
||||
|
||||
weaselab::VersionedMap::Key arbitraryKey(Arena &arena) {
|
||||
auto *result = new (arena) uint8_t[kKeySize];
|
||||
gArbitrary.randomHex(result, kKeySize);
|
||||
return {result, kKeySize};
|
||||
}
|
||||
|
||||
struct KeyComp {
|
||||
bool operator()(const weaselab::VersionedMap::Key &lhs,
|
||||
const weaselab::VersionedMap::Key &rhs) const {
|
||||
return lhs < rhs;
|
||||
}
|
||||
};
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
initFuzz(data, size);
|
||||
|
||||
Facade facade{0};
|
||||
Arena arena;
|
||||
|
||||
while (gArbitrary.hasEntropy()) {
|
||||
switch (gArbitrary.bounded(3)) {
|
||||
case 0: {
|
||||
// Add mutations
|
||||
|
||||
const int numKeys = gArbitrary.bounded(10);
|
||||
std::set<weaselab::VersionedMap::Key, KeyComp> keySet;
|
||||
while (int(keySet.size()) < numKeys) {
|
||||
keySet.insert(randomKey(arena));
|
||||
}
|
||||
std::vector<weaselab::VersionedMap::Key> keys;
|
||||
keys.insert(keys.end(), keySet.begin(), keySet.end());
|
||||
std::vector<weaselab::VersionedMap::Mutation> mutations;
|
||||
for (int i = 0; i < int(keys.size());) {
|
||||
switch (gArbitrary.bounded(3)) {
|
||||
case 0: {
|
||||
// Point write
|
||||
auto val = randomKey(arena);
|
||||
mutations.push_back({keys[i].p, val.p, keys[i].len, val.len,
|
||||
weaselab::VersionedMap::Set});
|
||||
++i;
|
||||
} break;
|
||||
case 1: {
|
||||
// Point clear
|
||||
mutations.push_back({keys[i].p, nullptr, keys[i].len, 0,
|
||||
weaselab::VersionedMap::Clear});
|
||||
++i;
|
||||
} break;
|
||||
case 2: {
|
||||
// Range clear
|
||||
if (i + 1 < int(keys.size())) {
|
||||
mutations.push_back({keys[i].p, keys[i + 1].p, keys[i].len,
|
||||
keys[i + 1].len,
|
||||
weaselab::VersionedMap::Clear});
|
||||
i += 2;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
facade.addMutations(mutations.data(), mutations.size(),
|
||||
facade.getVersion() + 1);
|
||||
|
||||
} break;
|
||||
case 1: {
|
||||
// Set oldest version
|
||||
|
||||
facade.setOldestVersion(facade.getOldestVersion() +
|
||||
gArbitrary.bounded(facade.getVersion() -
|
||||
facade.getOldestVersion() +
|
||||
1));
|
||||
} break;
|
||||
case 2: {
|
||||
// Check range read
|
||||
|
||||
const int64_t version = facade.getOldestVersion() +
|
||||
gArbitrary.bounded(facade.getVersion() -
|
||||
facade.getOldestVersion() + 1);
|
||||
auto begin = arbitraryKey(arena);
|
||||
auto end = arbitraryKey(arena);
|
||||
const int limit = gArbitrary.bounded(100000);
|
||||
const bool reverse = gArbitrary.bounded(2);
|
||||
|
||||
facade.viewAt(version).rangeRead(String(begin.p, begin.len),
|
||||
String(end.p, end.len), limit, reverse);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
18
KeyCompare.h
Normal file
18
KeyCompare.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "VersionedMap.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string.h>
|
||||
|
||||
inline auto operator<=>(const weaselab::VersionedMap::Key &lhs,
|
||||
const weaselab::VersionedMap::Key &rhs) {
|
||||
int cl = std::min(lhs.len, rhs.len);
|
||||
if (cl > 0) {
|
||||
int c = memcmp(lhs.p, rhs.p, cl);
|
||||
if (c != 0) {
|
||||
return c <=> 0;
|
||||
}
|
||||
}
|
||||
return lhs.len <=> rhs.len;
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
#include "VersionedMap.h"
|
||||
#include "Internal.h"
|
||||
#include "KeyCompare.h"
|
||||
#include "RootSet.h"
|
||||
|
||||
#include <assert.h>
|
||||
@@ -390,17 +391,6 @@ auto operator<=>(const VersionedMap::Key &lhs, const Node &rhs) {
|
||||
return lhs.len <=> rhs.entry->keyLen;
|
||||
}
|
||||
|
||||
auto operator<=>(const VersionedMap::Key &lhs, const VersionedMap::Key &rhs) {
|
||||
int cl = std::min(lhs.len, rhs.len);
|
||||
if (cl > 0) {
|
||||
int c = memcmp(lhs.p, rhs.p, cl);
|
||||
if (c != 0) {
|
||||
return c <=> 0;
|
||||
}
|
||||
}
|
||||
return lhs.len <=> rhs.len;
|
||||
}
|
||||
|
||||
constexpr int orderToInt(std::strong_ordering o) {
|
||||
return o == std::strong_ordering::less ? -1
|
||||
: o == std::strong_ordering::equal ? 0
|
||||
|
Reference in New Issue
Block a user