| 1 |
#include <cassert> |
| 2 |
#include <cstdio> |
| 3 |
#include <functional> |
| 4 |
#include <memory> |
| 5 |
|
| 6 |
#include "../../toxcore/tox.h" |
| 7 |
#include "../../toxcore/tox_dispatch.h" |
| 8 |
#include "../../toxcore/tox_events.h" |
| 9 |
#include "../support/public/fuzz_data.hh" |
| 10 |
#include "../support/public/fuzz_helpers.hh" |
| 11 |
#include "../support/public/simulated_environment.hh" |
| 12 |
|
| 13 |
namespace { |
| 14 |
|
| 15 |
using tox::test::configure_fuzz_memory_source; |
| 16 |
using tox::test::configure_fuzz_packet_source; |
| 17 |
using tox::test::FakeClock; |
| 18 |
using tox::test::Fuzz_Data; |
| 19 |
using tox::test::SimulatedEnvironment; |
| 20 |
|
| 21 |
template <typename T> |
| 22 |
using Ptr = std::unique_ptr<T, void (*)(T *)>; |
| 23 |
|
| 24 |
void setup_callbacks(Tox_Dispatch *dispatch) |
| 25 |
{ |
| 26 |
tox_events_callback_conference_connected( |
| 27 |
dispatch, [](const Tox_Event_Conference_Connected *event, void *user_data) { |
| 28 |
assert(event == nullptr); |
| 29 |
}); |
| 30 |
tox_events_callback_conference_connected( |
| 31 |
dispatch, [](const Tox_Event_Conference_Connected *event, void *user_data) { |
| 32 |
assert(event == nullptr); |
| 33 |
}); |
| 34 |
tox_events_callback_conference_invite( |
| 35 |
dispatch, [](const Tox_Event_Conference_Invite *event, void *user_data) { |
| 36 |
assert(event == nullptr); |
| 37 |
}); |
| 38 |
tox_events_callback_conference_message( |
| 39 |
dispatch, [](const Tox_Event_Conference_Message *event, void *user_data) { |
| 40 |
assert(event == nullptr); |
| 41 |
}); |
| 42 |
tox_events_callback_conference_peer_list_changed( |
| 43 |
dispatch, [](const Tox_Event_Conference_Peer_List_Changed *event, void *user_data) { |
| 44 |
assert(event == nullptr); |
| 45 |
}); |
| 46 |
tox_events_callback_conference_peer_name( |
| 47 |
dispatch, [](const Tox_Event_Conference_Peer_Name *event, void *user_data) { |
| 48 |
assert(event == nullptr); |
| 49 |
}); |
| 50 |
tox_events_callback_conference_title(dispatch, |
| 51 |
[](const Tox_Event_Conference_Title *event, void *user_data) { assert(event == nullptr); }); |
| 52 |
tox_events_callback_file_chunk_request( |
| 53 |
dispatch, [](const Tox_Event_File_Chunk_Request *event, void *user_data) { |
| 54 |
assert(event == nullptr); |
| 55 |
}); |
| 56 |
tox_events_callback_file_recv(dispatch, |
| 57 |
[](const Tox_Event_File_Recv *event, void *user_data) { assert(event == nullptr); }); |
| 58 |
tox_events_callback_file_recv_chunk(dispatch, |
| 59 |
[](const Tox_Event_File_Recv_Chunk *event, void *user_data) { assert(event == nullptr); }); |
| 60 |
tox_events_callback_file_recv_control( |
| 61 |
dispatch, [](const Tox_Event_File_Recv_Control *event, void *user_data) { |
| 62 |
assert(event == nullptr); |
| 63 |
}); |
| 64 |
tox_events_callback_friend_connection_status( |
| 65 |
dispatch, [](const Tox_Event_Friend_Connection_Status *event, void *user_data) { |
| 66 |
assert(event == nullptr); |
| 67 |
}); |
| 68 |
tox_events_callback_friend_lossless_packet( |
| 69 |
dispatch, [](const Tox_Event_Friend_Lossless_Packet *event, void *user_data) { |
| 70 |
assert(event == nullptr); |
| 71 |
}); |
| 72 |
tox_events_callback_friend_lossy_packet( |
| 73 |
dispatch, [](const Tox_Event_Friend_Lossy_Packet *event, void *user_data) { |
| 74 |
assert(event == nullptr); |
| 75 |
}); |
| 76 |
tox_events_callback_friend_message(dispatch, |
| 77 |
[](const Tox_Event_Friend_Message *event, void *user_data) { assert(event == nullptr); }); |
| 78 |
tox_events_callback_friend_name(dispatch, |
| 79 |
[](const Tox_Event_Friend_Name *event, void *user_data) { assert(event == nullptr); }); |
| 80 |
tox_events_callback_friend_read_receipt( |
| 81 |
dispatch, [](const Tox_Event_Friend_Read_Receipt *event, void *user_data) { |
| 82 |
assert(event == nullptr); |
| 83 |
}); |
| 84 |
tox_events_callback_friend_request( |
| 85 |
dispatch, [](const Tox_Event_Friend_Request *event, void *user_data) { |
| 86 |
Tox *tox = static_cast<Tox *>(user_data); |
| 87 |
Tox_Err_Friend_Add err; |
| 88 |
tox_friend_add_norequest(tox, tox_event_friend_request_get_public_key(event), &err); |
| 89 |
if (!(err == TOX_ERR_FRIEND_ADD_OK || err == TOX_ERR_FRIEND_ADD_OWN_KEY |
| 90 |
|| err == TOX_ERR_FRIEND_ADD_ALREADY_SENT |
| 91 |
|| err == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM |
| 92 |
|| err == TOX_ERR_FRIEND_ADD_MALLOC)) { |
| 93 |
printf("unexpected error: %s\n", tox_err_friend_add_to_string(err)); |
| 94 |
} |
| 95 |
}); |
| 96 |
tox_events_callback_friend_status(dispatch, |
| 97 |
[](const Tox_Event_Friend_Status *event, void *user_data) { assert(event == nullptr); }); |
| 98 |
tox_events_callback_friend_status_message( |
| 99 |
dispatch, [](const Tox_Event_Friend_Status_Message *event, void *user_data) { |
| 100 |
assert(event == nullptr); |
| 101 |
}); |
| 102 |
tox_events_callback_friend_typing(dispatch, |
| 103 |
[](const Tox_Event_Friend_Typing *event, void *user_data) { assert(event == nullptr); }); |
| 104 |
tox_events_callback_self_connection_status( |
| 105 |
dispatch, [](const Tox_Event_Self_Connection_Status *event, void *user_data) { |
| 106 |
assert(event == nullptr); |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
void TestBootstrap(Fuzz_Data &input) |
| 111 |
{ |
| 112 |
SimulatedEnvironment env; |
| 113 |
env.fake_clock().advance(1000000000); // Match legacy behavior |
| 114 |
auto node = env.create_node(33445); |
| 115 |
configure_fuzz_memory_source(env.fake_memory(), input); |
| 116 |
configure_fuzz_packet_source(*node->endpoint, input); |
| 117 |
|
| 118 |
// Create a second null system for tox_events_equal check |
| 119 |
SimulatedEnvironment null_env; |
| 120 |
auto null_node = null_env.create_node(0); // Port 0 (unbound/irrelevant) |
| 121 |
|
| 122 |
Ptr<Tox_Options> opts(tox_options_new(nullptr), tox_options_free); |
| 123 |
assert(opts != nullptr); |
| 124 |
|
| 125 |
tox_options_set_log_callback(opts.get(), |
| 126 |
[](Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func, |
| 127 |
const char *message, void *user_data) { |
| 128 |
// Log to stdout. |
| 129 |
if (Fuzz_Data::FUZZ_DEBUG) { |
| 130 |
// Approximate level name mapping |
| 131 |
char level_char = '?'; |
| 132 |
switch (level) { |
| 133 |
case TOX_LOG_LEVEL_TRACE: |
| 134 |
level_char = 'T'; |
| 135 |
break; |
| 136 |
case TOX_LOG_LEVEL_DEBUG: |
| 137 |
level_char = 'D'; |
| 138 |
break; |
| 139 |
case TOX_LOG_LEVEL_INFO: |
| 140 |
level_char = 'I'; |
| 141 |
break; |
| 142 |
case TOX_LOG_LEVEL_WARNING: |
| 143 |
level_char = 'W'; |
| 144 |
break; |
| 145 |
case TOX_LOG_LEVEL_ERROR: |
| 146 |
level_char = 'E'; |
| 147 |
break; |
| 148 |
} |
| 149 |
std::printf("[tox1] %c %s:%u(%s): %s\n", level_char, file, line, func, message); |
| 150 |
} |
| 151 |
}); |
| 152 |
|
| 153 |
CONSUME1_OR_RETURN(const uint8_t, proxy_type, input); |
| 154 |
if (proxy_type == 0) { |
| 155 |
tox_options_set_proxy_type(opts.get(), TOX_PROXY_TYPE_NONE); |
| 156 |
} else if (proxy_type == 1) { |
| 157 |
tox_options_set_proxy_type(opts.get(), TOX_PROXY_TYPE_SOCKS5); |
| 158 |
tox_options_set_proxy_host(opts.get(), "127.0.0.1"); |
| 159 |
tox_options_set_proxy_port(opts.get(), 8080); |
| 160 |
} else if (proxy_type == 2) { |
| 161 |
tox_options_set_proxy_type(opts.get(), TOX_PROXY_TYPE_HTTP); |
| 162 |
tox_options_set_proxy_host(opts.get(), "127.0.0.1"); |
| 163 |
tox_options_set_proxy_port(opts.get(), 8080); |
| 164 |
} |
| 165 |
|
| 166 |
CONSUME1_OR_RETURN(const uint8_t, tcp_relay_enabled, input); |
| 167 |
if (tcp_relay_enabled >= (UINT8_MAX / 2)) { |
| 168 |
tox_options_set_tcp_port(opts.get(), 33445); |
| 169 |
} |
| 170 |
|
| 171 |
Tox_Options_Testing tox_options_testing; |
| 172 |
tox_options_testing.operating_system = &node->system; |
| 173 |
|
| 174 |
Tox_Err_New error_new; |
| 175 |
Tox_Err_New_Testing error_new_testing; |
| 176 |
Tox *tox = tox_new_testing(opts.get(), &error_new, &tox_options_testing, &error_new_testing); |
| 177 |
|
| 178 |
if (tox == nullptr) { |
| 179 |
// It might fail, because some I/O happens in tox_new, and the fuzzer |
| 180 |
// might do things that make that I/O fail. |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
assert(error_new == TOX_ERR_NEW_OK); |
| 185 |
assert(error_new_testing == TOX_ERR_NEW_TESTING_OK); |
| 186 |
|
| 187 |
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0}; |
| 188 |
|
| 189 |
// These may fail, but that's ok. We ignore their return values. |
| 190 |
tox_bootstrap(tox, "127.0.0.2", 33446, pub_key, nullptr); |
| 191 |
tox_add_tcp_relay(tox, "127.0.0.2", 33446, pub_key, nullptr); |
| 192 |
|
| 193 |
tox_events_init(tox); |
| 194 |
|
| 195 |
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); |
| 196 |
assert(dispatch != nullptr); |
| 197 |
setup_callbacks(dispatch); |
| 198 |
|
| 199 |
size_t input_size = input.size(); |
| 200 |
while (!input.empty()) { |
| 201 |
Tox_Err_Events_Iterate error_iterate; |
| 202 |
Tox_Events *events = tox_events_iterate(tox, true, &error_iterate); |
| 203 |
assert(tox_events_equal(&null_node->system, events, events)); |
| 204 |
|
| 205 |
tox_dispatch_invoke(dispatch, events, tox); |
| 206 |
tox_events_free(events); |
| 207 |
|
| 208 |
env.advance_time(200); |
| 209 |
|
| 210 |
// If no input was consumed, stop. |
| 211 |
if (input_size == input.size()) { |
| 212 |
break; |
| 213 |
} |
| 214 |
|
| 215 |
input_size = input.size(); |
| 216 |
} |
| 217 |
|
| 218 |
tox_dispatch_free(dispatch); |
| 219 |
tox_kill(tox); |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
| 225 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
| 226 |
{ |
| 227 |
Fuzz_Data input{data, size}; |
| 228 |
TestBootstrap(input); |
| 229 |
return 0; // Non-zero return values are reserved for future use. |
| 230 |
} |