Skip to content

Commit

Permalink
ci: fix RpcDaemon fuzzer after PR 1744
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat committed Jan 13, 2024
1 parent 84983ea commit f57de8b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
29 changes: 15 additions & 14 deletions cmd/test/fuzzer_diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <CLI/CLI.hpp>
#include <gsl/util>

#include <silkworm/rpc/http/channel.hpp>
#include <silkworm/rpc/test/api_test_database.hpp>

#include "address_sanitizer_fix.hpp"
Expand All @@ -32,9 +33,9 @@ void print_stack_trace() {
int trace_size = backtrace(trace, 16);
char** messages = backtrace_symbols(trace, trace_size);
[[maybe_unused]] auto _ = gsl::finally([&messages] { free(messages); });
std::cout << "Stack Trace:" << std::endl;
std::cout << "Stack Trace:\n";
for (int i = 0; i < trace_size; i++) {
std::cout << messages[i] << std::endl;
std::cout << messages[i] << "\n";

// extract the address from the message
char* address = strchr(messages[i], '[');
Expand All @@ -45,9 +46,9 @@ void print_stack_trace() {
*end = '\0';
// use addr2line to get the file name and line number
std::string command = "addr2line -e ./rpcdaemon_fuzzer_diagnostics " + std::string(address);
auto command_result = system(command.c_str());
auto command_result = system(command.c_str()); // NOLINT(cert-*)
if (command_result != 0) {
std::cout << "addr2line failed" << std::endl;
std::cout << "addr2line failed\n";
}
}
}
Expand All @@ -61,17 +62,17 @@ int main(int argc, char* argv[]) {
std::string input_file;

app.add_option("input", input_str, "Input string")
->description("Wrap JSON in '' to avoid shell escaping, e.g. '{\"jsonrpc\":\"2.0\",\"id\":1}'")
->description(R"(Wrap JSON in '' to avoid shell escaping, e.g. '{"jsonrpc":"2.0","id":1}')")
->required(false);

app.add_option("-f", input_file, "Path to the JSON request file")
->check(CLI::ExistingFile)
->required(false);

CLI11_PARSE(app, argc, argv);
CLI11_PARSE(app, argc, argv)

if (input_str.empty() && input_file.empty()) {
std::cerr << "Either input string or input file must be provided" << std::endl;
std::cerr << "Either input string or input file must be provided" << "\n";
return -1;
}

Expand All @@ -81,13 +82,13 @@ int main(int argc, char* argv[]) {
}

if (!nlohmann::json::accept(input_str)) {
std::cout << "Not valid json: " << input_str << std::endl;
std::cout << "Not valid json: " << input_str << "\n";
} else {
auto request_json = nlohmann::json::parse(input_str);
std::cout << "Request: " << request_json.dump(4) << std::endl;
std::cout << "Request: " << request_json.dump(4) << "\n";
}

silkworm::rpc::http::Reply reply;
silkworm::rpc::Channel::Response reply;

try {
auto context = silkworm::rpc::test::TestDatabaseContext();
Expand All @@ -101,17 +102,17 @@ int main(int argc, char* argv[]) {
std::rethrow_exception(eptr);
}
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << std::endl;
std::cout << "Caught exception: " << e.what() << "\n";
print_stack_trace();
}
}

std::cout << "Reply Status: " << static_cast<int>(reply.status) << std::endl;
std::cout << "Reply Status: " << static_cast<int>(reply.status) << "\n";

if (nlohmann::json::accept(reply.content)) {
std::cout << "Reply Content: " << nlohmann::json::parse(reply.content).dump(4) << std::endl;
std::cout << "Reply Content: " << nlohmann::json::parse(reply.content).dump(4) << "\n";
} else {
std::cout << "Reply Content: " << reply.content << std::endl;
std::cout << "Reply Content: " << reply.content << "\n";
}

return 0;
Expand Down
16 changes: 7 additions & 9 deletions cmd/test/fuzzer_investigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
#include <iostream>
#include <string>

#include <boost/asio/async_result.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
Expand All @@ -33,15 +31,15 @@ class RequestHandler_ForTest {
try {
co_await is_valid_json(request_str);
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid argument: " << e.what() << std::endl;
std::cerr << "Invalid argument: " << e.what() << "\n";
} catch (...) {
std::cerr << "Error occurred" << std::endl;
std::cerr << "Error occurred\n";
}
}

boost::asio::awaitable<bool> is_valid_json(const std::string& request_str) {
if (request_str.length() == 20) {
std::cout << "Target length found, terminating, request_str: " << request_str << std::endl;
std::cout << "Target length found, terminating, request_str: " << request_str << "\n";
throw std::invalid_argument("Invalid argument");
}

Expand All @@ -55,14 +53,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
try {
auto io_context = boost::asio::io_context{};
auto result = boost::asio::co_spawn(
io_context, [&request_str]() -> boost::asio::awaitable<void> {
io_context, [](const auto& request_str) -> boost::asio::awaitable<void> {
try {
RequestHandler_ForTest handler{};
co_await handler.handle_request(request_str);
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
},
}(request_str),
boost::asio::use_future);

io_context.run();
Expand All @@ -72,9 +70,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
io_context.restart();

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << "Error: " << e.what() << "\n";
} catch (...) {
std::cout << "Error" << std::endl;
std::cout << "Error\n";
}

return 0;
Expand Down
7 changes: 5 additions & 2 deletions cmd/test/fuzzer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#include <string>

#include <nlohmann/json.hpp>

#include <silkworm/rpc/http/channel.hpp>
#include <silkworm/rpc/test/api_test_database.hpp>

#include "address_sanitizer_fix.hpp"
Expand All @@ -32,10 +35,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {

auto request_handler = RpcApiTestBase<RequestHandler_ForTest>(context.db);
auto request_json = nlohmann::json::parse(request_str);
silkworm::rpc::http::Reply reply;
silkworm::rpc::Channel::Response reply;
request_handler.run<&RequestHandler_ForTest::handle_request>(request_str, reply);

if (reply.status == silkworm::rpc::http::StatusType::ok) {
if (reply.status == silkworm::rpc::Channel::ResponseStatus::ok) {
return 0;
}

Expand Down

0 comments on commit f57de8b

Please sign in to comment.