Add fuzzer for termin_input_parser

Current state: the fuzzer find interesting input immediately...
```
terminate called after throwing an instance of 'std::range_error'
  what():  wstring_convert::from_bytes
```

See: https://github.com/ArthurSonzogni/FTXUI/issues/118
This commit is contained in:
ArthurSonzogni 2021-06-17 23:07:26 +02:00 committed by Arthur Sonzogni
parent 3d5e4eb6ca
commit 986ea2b37e
3 changed files with 46 additions and 6 deletions

View File

@ -18,12 +18,11 @@ project(ftxui
VERSION 0.6.${git_version} VERSION 0.6.${git_version}
) )
option(FTXUI_BUILD_EXAMPLES "Set to ON to build examples" ON)
option(FTXUI_ENABLE_INSTALL "Generate the install target" ON)
option(FTXUI_BUILD_TESTS "Set to ON to build tests" OFF)
option(FTXUI_BUILD_DOCS "Set to ON to build tests" ON) option(FTXUI_BUILD_DOCS "Set to ON to build tests" ON)
option(FTXUI_BUILD_EXAMPLES "Set to ON to build examples" ON)
enable_testing() option(FTXUI_BUILD_TESTS "Set to ON to build tests" OFF)
option(FTXUI_BUILD_TESTS_FUZZER "Set to ON to enable fuzzing" OFF)
option(FTXUI_ENABLE_INSTALL "Generate the install target" ON)
find_package(Threads) find_package(Threads)
@ -209,6 +208,7 @@ export(TARGETS screen dom component NAMESPACE ftxui::
if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4") if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4")
enable_testing()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE)
@ -254,6 +254,26 @@ if (FTXUI_BUILD_TESTS AND ${CMAKE_VERSION} VERSION_GREATER "3.11.4")
) )
set_property(TARGET tests PROPERTY CXX_STANDARD 17) set_property(TARGET tests PROPERTY CXX_STANDARD 17)
if (FTXUI_BUILD_TESTS_FUZZER)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
add_executable(terminal_input_parser_test_fuzz
src/ftxui/component/terminal_input_parser_test_fuzz.cpp
)
target_include_directories(terminal_input_parser_test_fuzz
PRIVATE src
)
target_link_libraries(terminal_input_parser_test_fuzz
PRIVATE component
)
target_compile_options(terminal_input_parser_test_fuzz
PRIVATE -fsanitize=fuzzer,address
)
target_link_libraries(terminal_input_parser_test_fuzz
PRIVATE -fsanitize=fuzzer,address
)
endif()
endif() endif()
if(FTXUI_BUILD_EXAMPLES) if(FTXUI_BUILD_EXAMPLES)

View File

@ -70,6 +70,7 @@ class ReceiverImpl {
senders_++; senders_++;
return std::unique_ptr<SenderImpl<T>>(new SenderImpl<T>(this)); return std::unique_ptr<SenderImpl<T>>(new SenderImpl<T>(this));
} }
ReceiverImpl() { senders_ = 0; }
bool Receive(T* t) { bool Receive(T* t) {
while (senders_ || !queue_.empty()) { while (senders_ || !queue_.empty()) {
@ -109,7 +110,7 @@ class ReceiverImpl {
std::mutex mutex_; std::mutex mutex_;
std::queue<T> queue_; std::queue<T> queue_;
std::condition_variable notifier_; std::condition_variable notifier_;
std::atomic<int> senders_ = 0; std::atomic<int> senders_;
}; };
template <class T> template <class T>

View File

@ -0,0 +1,19 @@
//#include "ftxui/component/event.hpp"
//#include "ftxui/component/receiver.hpp"
#include "ftxui/component/terminal_input_parser.hpp"
#include <vector>
extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
using namespace ftxui;
auto event_receiver = MakeReceiver<Event>();
{
auto parser = TerminalInputParser(event_receiver->MakeSender());
for (size_t i = 0; i < size; ++i)
parser.Add(data[i]);
}
Event received;
while (event_receiver->Receive(&received))
;
return 0; // Non-zero return values are reserved for future use.
}