Skip to content

Commit

Permalink
Add TopicTree fuzz target
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 9, 2020
1 parent 199f42e commit e9d8ed9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions fuzzing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 67 additions & 0 deletions fuzzing/TopicTree.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e9d8ed9

Please sign in to comment.