| 1 |
/** @file |
| 2 |
* @brief Generates a valid input for e2e_fuzz_test. |
| 3 |
* |
| 4 |
* This bootstraps 2 toxes tox1 and tox2, adds tox1 as tox2's friend, waits for |
| 5 |
* the friend request, then tox1 adds tox2 in response, waits for the friend to |
| 6 |
* come online, sends a 2-message exchange, and waits for the read receipt. |
| 7 |
* |
| 8 |
* All random inputs and network traffic is recorded and dumped to files at the |
| 9 |
* end. This can then be fed to e2e_fuzz_test for replay (only of tox1) and |
| 10 |
* further code path exploration using fuzzer mutations. |
| 11 |
* |
| 12 |
* We write 2 files: an init file that contains all the inputs needed to reach |
| 13 |
* the "friend online" state, and a smaller file containing things to mutate |
| 14 |
* once the tox instance is in that state. This allows for more specific |
| 15 |
* exploration of code paths starting from "friend is online". DHT and onion |
| 16 |
* packet parsing is explored more in bootstrap_fuzz_test. |
| 17 |
* |
| 18 |
* Usage: |
| 19 |
* |
| 20 |
* bazel build //c-toxcore/testing/fuzzing:protodump && \ |
| 21 |
* bazel-bin/c-toxcore/testing/fuzzing/protodump |
| 22 |
*/ |
| 23 |
#include <array> |
| 24 |
#include <cassert> |
| 25 |
#include <cstdio> |
| 26 |
#include <cstring> |
| 27 |
#include <fstream> |
| 28 |
#include <memory> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "../../toxcore/tox.h" |
| 32 |
#include "../../toxcore/tox_dispatch.h" |
| 33 |
#include "../../toxcore/tox_events.h" |
| 34 |
#include "../../toxcore/tox_private.h" |
| 35 |
#include "../support/public/simulated_environment.hh" |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
using tox::test::FakeClock; |
| 40 |
using tox::test::Packet; |
| 41 |
using tox::test::ScopedToxSystem; |
| 42 |
using tox::test::SimulatedEnvironment; |
| 43 |
|
| 44 |
constexpr uint32_t MESSAGE_COUNT = 5; |
| 45 |
|
| 46 |
class Recorder { |
| 47 |
public: |
| 48 |
std::vector<uint8_t> data; |
| 49 |
|
| 50 |
void push(bool val) { data.push_back(val); } |
| 51 |
void push(uint8_t val) { data.push_back(val); } |
| 52 |
void push(const uint8_t *bytes, size_t size) { data.insert(data.end(), bytes, bytes + size); } |
| 53 |
|
| 54 |
// Format: 2 bytes length (big-endian), then data. |
| 55 |
void push_packet(const uint8_t *bytes, size_t size) |
| 56 |
{ |
| 57 |
assert(size <= 65535); |
| 58 |
push(static_cast<uint8_t>(size >> 8)); |
| 59 |
push(static_cast<uint8_t>(size & 0xFF)); |
| 60 |
push(bytes, size); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
struct State { |
| 65 |
Tox *tox; |
| 66 |
uint32_t done; |
| 67 |
}; |
| 68 |
|
| 69 |
void setup_callbacks(Tox_Dispatch *dispatch) |
| 70 |
{ |
| 71 |
tox_events_callback_conference_connected( |
| 72 |
dispatch, [](const Tox_Event_Conference_Connected *event, void *user_data) { |
| 73 |
assert(event == nullptr); |
| 74 |
}); |
| 75 |
tox_events_callback_conference_connected( |
| 76 |
dispatch, [](const Tox_Event_Conference_Connected *event, void *user_data) { |
| 77 |
assert(event == nullptr); |
| 78 |
}); |
| 79 |
tox_events_callback_conference_invite( |
| 80 |
dispatch, [](const Tox_Event_Conference_Invite *event, void *user_data) { |
| 81 |
assert(event == nullptr); |
| 82 |
}); |
| 83 |
tox_events_callback_conference_message( |
| 84 |
dispatch, [](const Tox_Event_Conference_Message *event, void *user_data) { |
| 85 |
assert(event == nullptr); |
| 86 |
}); |
| 87 |
tox_events_callback_conference_peer_list_changed( |
| 88 |
dispatch, [](const Tox_Event_Conference_Peer_List_Changed *event, void *user_data) { |
| 89 |
assert(event == nullptr); |
| 90 |
}); |
| 91 |
tox_events_callback_conference_peer_name( |
| 92 |
dispatch, [](const Tox_Event_Conference_Peer_Name *event, void *user_data) { |
| 93 |
assert(event == nullptr); |
| 94 |
}); |
| 95 |
tox_events_callback_conference_title(dispatch, |
| 96 |
[](const Tox_Event_Conference_Title *event, void *user_data) { assert(event == nullptr); }); |
| 97 |
tox_events_callback_file_chunk_request( |
| 98 |
dispatch, [](const Tox_Event_File_Chunk_Request *event, void *user_data) { |
| 99 |
assert(event == nullptr); |
| 100 |
}); |
| 101 |
tox_events_callback_file_recv(dispatch, |
| 102 |
[](const Tox_Event_File_Recv *event, void *user_data) { assert(event == nullptr); }); |
| 103 |
tox_events_callback_file_recv_chunk(dispatch, |
| 104 |
[](const Tox_Event_File_Recv_Chunk *event, void *user_data) { assert(event == nullptr); }); |
| 105 |
tox_events_callback_file_recv_control( |
| 106 |
dispatch, [](const Tox_Event_File_Recv_Control *event, void *user_data) { |
| 107 |
assert(event == nullptr); |
| 108 |
}); |
| 109 |
tox_events_callback_friend_connection_status( |
| 110 |
dispatch, [](const Tox_Event_Friend_Connection_Status *event, void *user_data) { |
| 111 |
State *state = static_cast<State *>(user_data); |
| 112 |
const uint32_t friend_number |
| 113 |
= tox_event_friend_connection_status_get_friend_number(event); |
| 114 |
assert(friend_number == 0); |
| 115 |
const uint8_t message = 'A'; |
| 116 |
Tox_Err_Friend_Send_Message err; |
| 117 |
tox_friend_send_message( |
| 118 |
state->tox, friend_number, TOX_MESSAGE_TYPE_NORMAL, &message, 1, &err); |
| 119 |
assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK); |
| 120 |
}); |
| 121 |
tox_events_callback_friend_lossless_packet( |
| 122 |
dispatch, [](const Tox_Event_Friend_Lossless_Packet *event, void *user_data) { |
| 123 |
assert(event == nullptr); |
| 124 |
}); |
| 125 |
tox_events_callback_friend_lossy_packet( |
| 126 |
dispatch, [](const Tox_Event_Friend_Lossy_Packet *event, void *user_data) { |
| 127 |
assert(event == nullptr); |
| 128 |
}); |
| 129 |
tox_events_callback_friend_message( |
| 130 |
dispatch, [](const Tox_Event_Friend_Message *event, void *user_data) { |
| 131 |
State *state = static_cast<State *>(user_data); |
| 132 |
const uint32_t friend_number = tox_event_friend_message_get_friend_number(event); |
| 133 |
assert(friend_number == 0); |
| 134 |
const uint32_t message_length = tox_event_friend_message_get_message_length(event); |
| 135 |
assert(message_length == 1); |
| 136 |
const uint8_t *message = tox_event_friend_message_get_message(event); |
| 137 |
const uint8_t reply = message[0] + 1; |
| 138 |
Tox_Err_Friend_Send_Message err; |
| 139 |
tox_friend_send_message( |
| 140 |
state->tox, friend_number, TOX_MESSAGE_TYPE_NORMAL, &reply, 1, &err); |
| 141 |
assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK); |
| 142 |
}); |
| 143 |
tox_events_callback_friend_name( |
| 144 |
dispatch, [](const Tox_Event_Friend_Name *event, void *user_data) { |
| 145 |
const uint32_t friend_number = tox_event_friend_name_get_friend_number(event); |
| 146 |
assert(friend_number == 0); |
| 147 |
}); |
| 148 |
tox_events_callback_friend_read_receipt( |
| 149 |
dispatch, [](const Tox_Event_Friend_Read_Receipt *event, void *user_data) { |
| 150 |
State *state = static_cast<State *>(user_data); |
| 151 |
const uint32_t friend_number = tox_event_friend_read_receipt_get_friend_number(event); |
| 152 |
assert(friend_number == 0); |
| 153 |
const uint32_t message_id = tox_event_friend_read_receipt_get_message_id(event); |
| 154 |
state->done = std::max(state->done, message_id); |
| 155 |
}); |
| 156 |
tox_events_callback_friend_request( |
| 157 |
dispatch, [](const Tox_Event_Friend_Request *event, void *user_data) { |
| 158 |
State *state = static_cast<State *>(user_data); |
| 159 |
Tox_Err_Friend_Add err; |
| 160 |
tox_friend_add_norequest( |
| 161 |
state->tox, tox_event_friend_request_get_public_key(event), &err); |
| 162 |
assert(err == TOX_ERR_FRIEND_ADD_OK || err == TOX_ERR_FRIEND_ADD_OWN_KEY |
| 163 |
|| err == TOX_ERR_FRIEND_ADD_ALREADY_SENT |
| 164 |
|| err == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM); |
| 165 |
}); |
| 166 |
tox_events_callback_friend_status( |
| 167 |
dispatch, [](const Tox_Event_Friend_Status *event, void *user_data) { |
| 168 |
const uint32_t friend_number = tox_event_friend_status_get_friend_number(event); |
| 169 |
assert(friend_number == 0); |
| 170 |
}); |
| 171 |
tox_events_callback_friend_status_message( |
| 172 |
dispatch, [](const Tox_Event_Friend_Status_Message *event, void *user_data) { |
| 173 |
const uint32_t friend_number = tox_event_friend_status_message_get_friend_number(event); |
| 174 |
assert(friend_number == 0); |
| 175 |
}); |
| 176 |
tox_events_callback_friend_typing( |
| 177 |
dispatch, [](const Tox_Event_Friend_Typing *event, void *user_data) { |
| 178 |
const uint32_t friend_number = tox_event_friend_typing_get_friend_number(event); |
| 179 |
assert(friend_number == 0); |
| 180 |
assert(!tox_event_friend_typing_get_typing(event)); |
| 181 |
}); |
| 182 |
tox_events_callback_self_connection_status( |
| 183 |
dispatch, [](const Tox_Event_Self_Connection_Status *event, void *user_data) {}); |
| 184 |
} |
| 185 |
|
| 186 |
void dump(std::vector<uint8_t> recording, const char *filename) |
| 187 |
{ |
| 188 |
std::printf("%zu bytes: %s\n", recording.size(), filename); |
| 189 |
std::ofstream(filename, std::ios::binary) |
| 190 |
.write(reinterpret_cast<const char *>(recording.data()), recording.size()); |
| 191 |
} |
| 192 |
|
| 193 |
void RecordBootstrap(const char *init, const char *bootstrap) |
| 194 |
{ |
| 195 |
SimulatedEnvironment env1; |
| 196 |
SimulatedEnvironment env2; |
| 197 |
|
| 198 |
// Set deterministic seeds. |
| 199 |
std::minstd_rand rng1(4); |
| 200 |
env1.fake_random().set_entropy_source([&](uint8_t *out, size_t count) { |
| 201 |
std::uniform_int_distribution<uint16_t> dist(0, 255); |
| 202 |
for (size_t i = 0; i < count; ++i) |
| 203 |
out[i] = static_cast<uint8_t>(dist(rng1)); |
| 204 |
}); |
| 205 |
|
| 206 |
std::minstd_rand rng2(5); |
| 207 |
env2.fake_random().set_entropy_source([&](uint8_t *out, size_t count) { |
| 208 |
std::uniform_int_distribution<uint16_t> dist(0, 255); |
| 209 |
for (size_t i = 0; i < count; ++i) |
| 210 |
out[i] = static_cast<uint8_t>(dist(rng2)); |
| 211 |
}); |
| 212 |
|
| 213 |
Recorder recorder1; |
| 214 |
Recorder recorder2; |
| 215 |
|
| 216 |
env1.fake_memory().set_observer([&](bool success) { recorder1.push(success); }); |
| 217 |
|
| 218 |
env1.fake_random().set_observer( |
| 219 |
[&](const uint8_t *data, size_t count) { recorder1.push(data, count); }); |
| 220 |
|
| 221 |
auto node1 = env1.create_node(33445); |
| 222 |
auto node2 = env2.create_node(33446); |
| 223 |
|
| 224 |
// Record received packets. |
| 225 |
node1->endpoint->set_recv_observer([&](const std::vector<uint8_t> &data, const IP_Port &from) { |
| 226 |
recorder1.push_packet(data.data(), data.size()); |
| 227 |
}); |
| 228 |
|
| 229 |
// Bridge the two simulated networks. |
| 230 |
env1.simulation().net().add_observer( |
| 231 |
[&](const Packet &p) { env2.simulation().net().send_packet(p); }); |
| 232 |
|
| 233 |
env2.simulation().net().add_observer( |
| 234 |
[&](const Packet &p) { env1.simulation().net().send_packet(p); }); |
| 235 |
|
| 236 |
Tox_Options *opts = tox_options_new(nullptr); |
| 237 |
tox_options_set_local_discovery_enabled(opts, false); |
| 238 |
|
| 239 |
Tox_Options_Testing opts_test1; |
| 240 |
opts_test1.operating_system = &node1->system; |
| 241 |
|
| 242 |
Tox_Err_New error_new; |
| 243 |
Tox_Err_New_Testing error_new_testing; |
| 244 |
|
| 245 |
Tox *tox1 = tox_new_testing(opts, &error_new, &opts_test1, &error_new_testing); |
| 246 |
assert(tox1 != nullptr); |
| 247 |
|
| 248 |
Tox_Options_Testing opts_test2; |
| 249 |
opts_test2.operating_system = &node2->system; |
| 250 |
|
| 251 |
Tox *tox2 = tox_new_testing(opts, &error_new, &opts_test2, &error_new_testing); |
| 252 |
assert(tox2 != nullptr); |
| 253 |
|
| 254 |
tox_options_free(opts); |
| 255 |
|
| 256 |
std::array<uint8_t, TOX_ADDRESS_SIZE> address1; |
| 257 |
tox_self_get_address(tox1, address1.data()); |
| 258 |
std::array<uint8_t, TOX_PUBLIC_KEY_SIZE> dht_key1; |
| 259 |
tox_self_get_dht_id(tox1, dht_key1.data()); |
| 260 |
|
| 261 |
// Bootstrap tox2 to tox1. |
| 262 |
const bool udp_success = tox_bootstrap(tox2, "127.0.0.1", 33445, dht_key1.data(), nullptr); |
| 263 |
assert(udp_success); |
| 264 |
|
| 265 |
tox_events_init(tox1); |
| 266 |
tox_events_init(tox2); |
| 267 |
|
| 268 |
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); |
| 269 |
setup_callbacks(dispatch); |
| 270 |
|
| 271 |
State state1 = {tox1, 0}; |
| 272 |
State state2 = {tox2, 0}; |
| 273 |
|
| 274 |
const auto iterate = [&](uint8_t clock_increment) { |
| 275 |
Tox_Err_Events_Iterate error_iterate; |
| 276 |
Tox_Events *events; |
| 277 |
|
| 278 |
events = tox_events_iterate(tox1, true, &error_iterate); |
| 279 |
tox_dispatch_invoke(dispatch, events, &state1); |
| 280 |
tox_events_free(events); |
| 281 |
|
| 282 |
events = tox_events_iterate(tox2, true, &error_iterate); |
| 283 |
tox_dispatch_invoke(dispatch, events, &state2); |
| 284 |
tox_events_free(events); |
| 285 |
|
| 286 |
// Record the clock increment. |
| 287 |
env1.fake_clock().advance(clock_increment); |
| 288 |
recorder1.push(clock_increment); |
| 289 |
|
| 290 |
env2.fake_clock().advance(clock_increment); |
| 291 |
|
| 292 |
env1.simulation().net().process_events(env1.fake_clock().current_time_ms()); |
| 293 |
env2.simulation().net().process_events(env2.fake_clock().current_time_ms()); |
| 294 |
}; |
| 295 |
|
| 296 |
while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE |
| 297 |
|| tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) { |
| 298 |
iterate(200); |
| 299 |
} |
| 300 |
|
| 301 |
std::printf("toxes are online\n"); |
| 302 |
|
| 303 |
const uint8_t msg = 'A'; |
| 304 |
const uint32_t friend_number = tox_friend_add(tox2, address1.data(), &msg, 1, nullptr); |
| 305 |
assert(friend_number == 0); |
| 306 |
|
| 307 |
while (tox_friend_get_connection_status(tox2, friend_number, nullptr) == TOX_CONNECTION_NONE |
| 308 |
|| tox_friend_get_connection_status(tox1, 0, nullptr) == TOX_CONNECTION_NONE) { |
| 309 |
iterate(200); |
| 310 |
} |
| 311 |
|
| 312 |
std::printf("tox clients connected\n"); |
| 313 |
|
| 314 |
dump(recorder1.data, init); |
| 315 |
|
| 316 |
// Clear the recorder. |
| 317 |
recorder1.data.clear(); |
| 318 |
|
| 319 |
while (state1.done < MESSAGE_COUNT && state2.done < MESSAGE_COUNT) { |
| 320 |
iterate(20); |
| 321 |
} |
| 322 |
|
| 323 |
std::printf("test complete\n"); |
| 324 |
|
| 325 |
tox_dispatch_free(dispatch); |
| 326 |
tox_kill(tox2); |
| 327 |
tox_kill(tox1); |
| 328 |
|
| 329 |
dump(recorder1.data, bootstrap); |
| 330 |
} |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
int main(int argc, char *argv[]) |
| 335 |
{ |
| 336 |
const char *init = "tools/toktok-fuzzer/init/e2e_fuzz_test.dat"; |
| 337 |
const char *bootstrap = "tools/toktok-fuzzer/corpus/e2e_fuzz_test/bootstrap.dat"; |
| 338 |
if (argc == 3) { |
| 339 |
init = argv[1]; |
| 340 |
bootstrap = argv[2]; |
| 341 |
} |
| 342 |
RecordBootstrap(init, bootstrap); |
| 343 |
} |