From 277c9500f91956927e66d1715704aa3baece71cb Mon Sep 17 00:00:00 2001 From: blankie Date: Tue, 17 Jan 2023 23:22:12 +0700 Subject: [PATCH] Use ubsan and tsan --- Makefile | 2 +- logcat_thread.cpp | 12 ++++++++---- logcat_thread.h | 6 ++++-- main.cpp | 3 ++- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6af75d3..97184aa 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES)))) UNAME_S := $(shell uname -s) LINUX_GL_LIBS = -lGL -CXXFLAGS += -std=c++20 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/misc/cpp -I$(IMGUI_DIR)/backends -D'IMGUI_USER_CONFIG="../myimconfig.h"' +CXXFLAGS += -fsanitize=undefined,thread -std=c++20 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/misc/cpp -I$(IMGUI_DIR)/backends -D'IMGUI_USER_CONFIG="../myimconfig.h"' # https://t.me/NightShadowsHangout/670691 CXXFLAGS += -g -Werror -Wall -Wextra -Wshadow -Wpedantic -Wno-gnu-anonymous-struct -fno-rtti -fPIC -Wconversion -Wno-unused-parameter -Wimplicit-fallthrough LIBS = diff --git a/logcat_thread.cpp b/logcat_thread.cpp index f8e906b..120c215 100644 --- a/logcat_thread.cpp +++ b/logcat_thread.cpp @@ -21,7 +21,7 @@ static void mark_nonblock(int fd) { } -LogcatThread::LogcatThread(bool* run_event_loop) { +LogcatThread::LogcatThread() { int fds[2]; struct epoll_event event = {.events = EPOLLIN | EPOLLET}; @@ -72,7 +72,7 @@ LogcatThread::LogcatThread(bool* run_event_loop) { throw_system_error(errsv, "epoll_ctl() for stderr"); } - this->_thread = std::thread(&LogcatThread::_run, this, run_event_loop); + this->_thread = std::thread(&LogcatThread::_run, this, this->_stop_source.get_token()); } LogcatThread::~LogcatThread() { @@ -93,14 +93,18 @@ LogcatThread::~LogcatThread() { } } +void LogcatThread::request_stop() { + this->_stop_source.request_stop(); +} + void LogcatThread::join() { this->_thread.join(); } -void LogcatThread::_run(bool* run_event_loop) { +void LogcatThread::_run(std::stop_token stoken) { struct epoll_event events[EPOLL_MAX_EVENTS]; - while (*run_event_loop) { + while (!stoken.stop_requested()) { printf("(boop)\n"); int ready_fds = epoll_wait(this->_epoll_fd, events, EPOLL_MAX_EVENTS, 1000); if (ready_fds == -1) { diff --git a/logcat_thread.h b/logcat_thread.h index 7765281..d2f2496 100644 --- a/logcat_thread.h +++ b/logcat_thread.h @@ -8,17 +8,19 @@ public: LogcatThread(const LogcatThread&) = delete; LogcatThread& operator=(const LogcatThread&) = delete; - LogcatThread(bool* run_event_loop); + LogcatThread(); ~LogcatThread(); + void request_stop(); void join(); private: - void _run(bool* run_event_loop); + void _run(std::stop_token stoken); int _epoll_fd = -1; int _stdout_read_fd = -1; int _stdout_write_fd = -1; int _stderr_read_fd = -1; int _stderr_write_fd = -1; + std::stop_source _stop_source; std::thread _thread; }; diff --git a/main.cpp b/main.cpp index 88d32d2..f21ac4f 100644 --- a/main.cpp +++ b/main.cpp @@ -133,7 +133,7 @@ int main(int, char**) { float config_write_timer = 0.0f; LogcatThread logcat_thread = [&]() { try { - return LogcatThread(&run_event_loop); + return LogcatThread(); } catch (const std::exception& e) { fprintf(stderr, "Failed to spawn logcat thread: %s\n", e.what()); exit(1); @@ -189,6 +189,7 @@ int main(int, char**) { return 1; } } + logcat_thread.request_stop(); logcat_thread.join(); return 0;