2021-12-05 20:07:27 +08:00
|
|
|
#include <cassert>
|
2021-12-05 20:10:03 +08:00
|
|
|
#include <cstring>
|
2022-03-29 06:42:42 +08:00
|
|
|
#include <memory>
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2021-12-05 20:10:03 +08:00
|
|
|
#include "../../toxcore/tox.h"
|
2022-04-03 01:10:09 +08:00
|
|
|
#include "../../toxcore/tox_dispatch.h"
|
|
|
|
#include "../../toxcore/tox_events.h"
|
2022-03-29 06:42:42 +08:00
|
|
|
#include "../../toxcore/tox_private.h"
|
2022-04-03 01:10:09 +08:00
|
|
|
#include "../../toxcore/tox_struct.h"
|
|
|
|
#include "../../toxcore/util.h"
|
2022-03-29 06:42:42 +08:00
|
|
|
#include "fuzz_support.h"
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-04-03 01:10:09 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void setup_callbacks(Tox_Dispatch *dispatch)
|
|
|
|
{
|
|
|
|
tox_events_callback_conference_connected(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Connected *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_connected(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Connected *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_invite(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Invite *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_message(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Message *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_peer_list_changed(dispatch,
|
|
|
|
[](Tox *tox, const Tox_Event_Conference_Peer_List_Changed *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_peer_name(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Peer_Name *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_conference_title(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Conference_Title *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_file_chunk_request(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_File_Chunk_Request *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_file_recv(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_File_Recv *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_file_recv_chunk(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_File_Recv_Chunk *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_file_recv_control(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_File_Recv_Control *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_connection_status(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Connection_Status *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_lossless_packet(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Lossless_Packet *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_lossy_packet(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Lossy_Packet *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_message(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Message *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_name(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Name *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_read_receipt(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Read_Receipt *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_request(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Request *event, void *user_data) {
|
|
|
|
Tox_Err_Friend_Add err;
|
|
|
|
tox_friend_add_norequest(tox, tox_event_friend_request_get_public_key(event), &err);
|
|
|
|
assert(err == TOX_ERR_FRIEND_ADD_OK || err == TOX_ERR_FRIEND_ADD_OWN_KEY
|
|
|
|
|| err == TOX_ERR_FRIEND_ADD_ALREADY_SENT
|
|
|
|
|| err == TOX_ERR_FRIEND_ADD_BAD_CHECKSUM);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_status(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Status *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_status_message(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Status_Message *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_friend_typing(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Friend_Typing *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
tox_events_callback_self_connection_status(
|
|
|
|
dispatch, [](Tox *tox, const Tox_Event_Self_Connection_Status *event, void *user_data) {
|
|
|
|
assert(event == nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-23 08:46:10 +08:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
2022-02-19 11:24:01 +08:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
|
|
|
|
{
|
2022-04-03 01:10:09 +08:00
|
|
|
Fuzz_Data input{data, size};
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-04-03 01:10:09 +08:00
|
|
|
Fuzz_System sys(input);
|
|
|
|
assert(sys.rng != nullptr);
|
2022-03-29 06:42:42 +08:00
|
|
|
|
|
|
|
Tox_Options *opts = tox_options_new(nullptr);
|
|
|
|
assert(opts != nullptr);
|
2022-04-03 01:10:09 +08:00
|
|
|
tox_options_set_operating_system(opts, sys.sys.get());
|
2022-03-29 06:42:42 +08:00
|
|
|
|
|
|
|
Tox_Err_New error_new;
|
|
|
|
Tox *tox = tox_new(opts, &error_new);
|
2022-03-28 05:45:04 +08:00
|
|
|
|
2022-04-11 06:25:28 +08:00
|
|
|
if (tox == nullptr) {
|
|
|
|
// It might fail, because some I/O happens in tox_new, and the fuzzer
|
|
|
|
// might do things that make that I/O fail.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-19 11:24:01 +08:00
|
|
|
assert(error_new == TOX_ERR_NEW_OK);
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-03-29 06:42:42 +08:00
|
|
|
tox_options_free(opts);
|
|
|
|
|
2022-02-19 11:24:01 +08:00
|
|
|
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-03-28 05:45:04 +08:00
|
|
|
const bool success = tox_bootstrap(tox, "127.0.0.1", 12345, pub_key, nullptr);
|
2022-02-19 11:24:01 +08:00
|
|
|
assert(success);
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-04-03 01:10:09 +08:00
|
|
|
tox_events_init(tox);
|
|
|
|
|
|
|
|
Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
|
|
|
|
assert(dispatch != nullptr);
|
|
|
|
setup_callbacks(dispatch);
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-04-03 01:10:09 +08:00
|
|
|
while (input.size > 0) {
|
|
|
|
Tox_Err_Events_Iterate error_iterate;
|
|
|
|
Tox_Events *events = tox_events_iterate(tox, true, &error_iterate);
|
|
|
|
assert(tox_events_equal(events, events));
|
|
|
|
tox_dispatch_invoke(dispatch, events, tox, nullptr);
|
|
|
|
tox_events_free(events);
|
2022-03-28 05:45:04 +08:00
|
|
|
// Move the clock forward a decent amount so all the time-based checks
|
|
|
|
// trigger more quickly.
|
2022-04-03 01:10:09 +08:00
|
|
|
sys.clock += 200;
|
2022-02-19 11:24:01 +08:00
|
|
|
}
|
2021-12-05 20:07:27 +08:00
|
|
|
|
2022-04-03 01:10:09 +08:00
|
|
|
tox_dispatch_free(dispatch);
|
2022-02-19 11:24:01 +08:00
|
|
|
tox_kill(tox);
|
|
|
|
return 0; // Non-zero return values are reserved for future use.
|
2021-12-05 20:07:27 +08:00
|
|
|
}
|