Add TopicTree fuzz target

This commit is contained in:
Alex Hultman
2020-01-09 19:32:14 +01:00
parent 199f42e800
commit e9d8ed9197
2 changed files with 68 additions and 0 deletions
+1
View File
@@ -10,6 +10,7 @@ oss-fuzz:
$(CXX) $(CXXFLAGS) -std=c++17 -O3 WebSocket.cpp -o $(OUT)/WebSocket $(LIB_FUZZING_ENGINE)
$(CXX) $(CXXFLAGS) -std=c++17 -O3 Http.cpp -o $(OUT)/Http $(LIB_FUZZING_ENGINE)
$(CXX) $(CXXFLAGS) -std=c++17 -O3 PerMessageDeflate.cpp -o $(OUT)/PerMessageDeflate $(LIB_FUZZING_ENGINE) -lz
$(CXX) $(CXXFLAGS) -std=c++17 -O3 TopicTree.cpp -o $(OUT)/TopicTree $(LIB_FUZZING_ENGINE)
# "Integration tests"
$(CC) $(CFLAGS) -DLIBUS_NO_SSL -c -O3 uSocketsMock.c
$(CXX) $(CXXFLAGS) -std=c++17 -O3 -DLIBUS_NO_SSL -I../src -I../uSockets/src MockedHelloWorld.cpp uSocketsMock.o -lz -o $(OUT)/MockedHelloWorld $(LIB_FUZZING_ENGINE)
+67
View File
@@ -0,0 +1,67 @@
#define WIN32_EXPORT
#include "helpers.h"
/* Test for the topic tree */
#include "../src/TopicTree.h"
#include <memory>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
/* Create topic tree */
uWS::TopicTree topicTree([](uWS::Subscriber *s, std::string_view message) {
/* We assume sane output */
if (!s || !message.length()) {
free((void *) -1);
}
return 0;
});
/* Holder for all manually allocated subscribers */
std::map<uint32_t, std::unique_ptr<uWS::Subscriber>> subscribers;
/* Iterate the padded fuzz as chunks */
makeChunked(makePadded(data, size), size, [&topicTree, &subscribers](const uint8_t *data, size_t size) {
/* We need at least 5 bytes */
if (size > 4) {
/* Last of all is a string */
std::string_view lastString((char *) data + 5, size - 5);
/* First 4 bytes is the subscriber id */
uint32_t id;
memcpy(&id, data, 4);
/* Then one byte action */
if (data[4] == 'S') {
/* Subscribe */
if (subscribers.find(id) != subscribers.end()) {
uWS::Subscriber *subscriber = new uWS::Subscriber(nullptr);
subscribers[id] = std::unique_ptr<uWS::Subscriber>(subscriber);
topicTree.subscribe(lastString, subscriber);
}
} else if (data[4] == 'U') {
/* Unsubscribe */
auto it = subscribers.find(id);
if (it != subscribers.end()) {
topicTree.unsubscribe(lastString, it->second.get());
}
} else if (data[4] == 'A') {
/* Unsubscribe from all */
auto it = subscribers.find(id);
if (it != subscribers.end()) {
topicTree.unsubscribeAll(it->second.get());
}
} else if (data[4] == 'P') {
/* Publish */
topicTree.publish(lastString, lastString);
} else if (data[4] == 'D') {
/* Drain */
topicTree.drain();
}
}
});
return 0;
}