ClickHouse/programs/server/fuzzers/tcp_protocol_fuzzer.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.0 KiB
C++
Raw Normal View History

2022-10-24 03:43:14 +00:00
#include <cstdint>
2022-10-24 00:51:57 +00:00
#include <future>
#include <thread>
#include <utility>
#include <vector>
#include <iostream>
#include <chrono>
#include <Poco/Net/PollSet.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/StreamSocket.h>
#include <Interpreters/Context.h>
int mainEntryClickHouseServer(int argc, char ** argv);
static std::string clickhouse("clickhouse-server");
static std::vector<char *> args{clickhouse.data()};
static std::future<int> main_app;
2022-10-24 03:43:14 +00:00
static std::string s_host("0.0.0.0");
static char * host = s_host.data();
static int64_t port = 9000;
2022-10-24 00:51:57 +00:00
using namespace std::chrono_literals;
2022-10-24 03:43:14 +00:00
extern "C"
2022-10-24 00:51:57 +00:00
int LLVMFuzzerInitialize(int * argc, char ***argv)
{
for (int i = 1; i < *argc; ++i)
{
if ((*argv)[i][0] == '-')
{
if ((*argv)[i][1] == '-')
args.push_back((*argv)[i]);
else
{
if (strncmp((*argv)[i], "-host=", 6) == 0)
{
host = (*argv)[i] + 6;
}
else if (strncmp((*argv)[i], "-port=", 6) == 0)
{
char * p_end = nullptr;
port = strtol((*argv)[i] + 6, &p_end, 10);
}
}
}
}
args.push_back(nullptr);
main_app = std::async(std::launch::async, mainEntryClickHouseServer, args.size() - 1, args.data());
while (!DB::Context::getGlobalContextInstance() || !DB::Context::getGlobalContextInstance()->isServerCompletelyStarted())
{
std::this_thread::sleep_for(100ms);
if (main_app.wait_for(0s) == std::future_status::ready)
exit(-1);
}
return 0;
}
extern "C"
int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size)
{
try
{
if (main_app.wait_for(0s) == std::future_status::ready)
return -1;
if (size == 0)
return -1;
2022-10-24 00:51:57 +00:00
Poco::Net::SocketAddress address(host, port);
Poco::Net::StreamSocket socket;
socket.connectNB(address);
Poco::Net::PollSet ps;
ps.add(socket, Poco::Net::PollSet::POLL_READ | Poco::Net::PollSet::POLL_WRITE);
std::vector<char> buf(1048576);
size_t sent = 0;
while (true)
{
auto m = ps.poll(Poco::Timespan(1000000));
if (m.empty())
continue;
if (m.begin()->second & Poco::Net::PollSet::POLL_READ)
{
2022-10-24 03:43:14 +00:00
if (int n = socket.receiveBytes(buf.data(), static_cast<int>(buf.size())); n == 0)
2022-10-24 00:51:57 +00:00
{
socket.close();
break;
}
continue;
}
if (sent < size && m.begin()->second & Poco::Net::PollSet::POLL_WRITE)
{
2022-10-24 03:43:14 +00:00
sent += socket.sendBytes(data + sent, static_cast<int>(size - sent));
2022-10-24 00:51:57 +00:00
if (sent == size)
{
socket.shutdownSend();
continue;
}
}
}
}
catch (...)
2022-10-24 00:51:57 +00:00
{
}
return 0;
}