Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add UBSAN option for cmake. #193

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .ci-scripts/build-qtox-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ usage() {
echo "$0 [--minimal|--full] --build-type [Debug|Release] [--with-gui-tests] [--sanitize] [--tidy]"
echo "Build script to build/test qtox from a CI environment."
echo "--minimal or --full are required, --build-type is required."
echo "UndefinedBehaviorSanitizer is always enabled. In Release builds, it is used without additional runtime dependencies."
}

while (($# > 0)); do
Expand Down Expand Up @@ -84,19 +85,21 @@ export QT_QPA_PLATFORM=offscreen
if [ "$MINIMAL" -eq 1 ]; then
cmake "$SRCDIR" \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
-DSTRICT_OPTIONS=ON \
-DUBSAN=ON \
-GNinja \
-DSMILEYS=DISABLED \
-DUPDATE_CHECK=OFF \
-DSTRICT_OPTIONS=ON \
-DSPELL_CHECK=OFF \
-GNinja \
"${CMAKE_ARGS[@]}"
else
cmake "$SRCDIR" \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
-DUPDATE_CHECK=ON \
-DSTRICT_OPTIONS=ON \
-DCODE_COVERAGE=ON \
-DUBSAN=ON \
-GNinja \
-DCODE_COVERAGE=ON \
-DUPDATE_CHECK=ON \
"${CMAKE_ARGS[@]}"
fi

Expand Down
3 changes: 2 additions & 1 deletion .ci-scripts/build-qtox-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build_qtox() {
# CMake will use -I instead of -isystem, so we need to set it manually.
cmake \
-DCMAKE_CXX_FLAGS="-isystem/usr/local/include" \
-DUBSAN=ON \
-DUPDATE_CHECK=ON \
-DSPELL_CHECK=OFF \
-DSTRICT_OPTIONS=ON \
Expand All @@ -45,7 +46,7 @@ build_qtox() {
.
cmake --build .
ctest --output-on-failure --parallel "$(sysctl -n hw.ncpu)"
cmake --build . --target install
cmake --install .
cp qTox.dmg "$BIN_NAME"
}

Expand Down
81 changes: 57 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#
# ##############################################################################

cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.16)
cmake_policy(VERSION 3.16)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

option(PLATFORM_EXTENSIONS
Expand All @@ -21,15 +23,11 @@ option(SPELL_CHECK "Enable spellcheck support" ON)
option(SVGZ_ICON "Compress the SVG icon of qTox" ON)
option(ASAN "Compile with AddressSanitizer" OFF)
option(TSAN "Compile with ThreadSanitizer" OFF)
option(UBSAN "Compile with UndefinedBehaviorSanitizer" OFF)
option(STRICT_OPTIONS "Error on compile warning, used by CI" OFF)
option(GUI_TESTS
"Enable GUI rendering tests, likely tied to a specific Qt version" OFF)

# process generated files if cmake >= 3.10
if(POLICY CMP0071)
cmake_policy(SET CMP0071 NEW)
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Debug
Expand All @@ -38,20 +36,6 @@ if(NOT CMAKE_BUILD_TYPE)
FORCE)
endif()

if(ASAN)
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG
"${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
endif()

if(TSAN)
set(CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=thread")
set(CMAKE_LINKER_FLAGS_DEBUG
"${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=thread")
endif()

set(ENV{PKG_CONFIG_PATH}
${CMAKE_SOURCE_DIR}/libs/lib/pkgconfig:/opt/ffmpeg/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}
)
Expand All @@ -72,6 +56,58 @@ execute_process(

project(qtox)

# C++ and C standards.
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 11)

if(ASAN
OR TSAN
OR UBSAN)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer")
endif()

if(ASAN)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=address")
endif()

if(TSAN)
if(ASAN)
message(FATAL_ERROR "ASAN and TSAN cannot be enabled at the same time")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=thread")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=thread")
endif()

if(UBSAN)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Add these to common flags (release and debug), because we can use the
# runtime-less version via -fsanitize-trap=all below.
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fsanitize=undefined,nullability,local-bounds,float-divide-by-zero"
)
set(CMAKE_LINKER_FLAGS
"${CMAKE_LINKER_FLAGS} -fsanitize=undefined,nullability,local-bounds,float-divide-by-zero"
)
# In release, reduce attack surface by never calling into a runtime library,
# and instead just aborting the program on UB.
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} -fsanitize-trap=all")
set(CMAKE_LINKER_FLAGS_RELEASE
"${CMAKE_LINKER_FLAGS_RELEASE} -fsanitize-trap=all")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -fsanitize=undefined")
# This is the GCC version of -fsanitize-trap=all.
set(CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} -fno-sanitize-recover=all")
else()
message(
FATAL_ERROR
"UBSAN is not supported by the compiler (${CMAKE_CXX_COMPILER_ID})")
endif()
endif()

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Run resource compilation automatically
Expand All @@ -84,8 +120,7 @@ set(AUTORCC_OPTIONS -compress 9 -threshold 0)
# unwanted side effects.
set(AUTORCC_OPTIONS ${AUTORCC_OPTIONS} -format-version 1)

# Use C++20.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
# Disable exceptions (Qt doesn't use them, we don't need them).
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")

# Hardening flags (ASLR, warnings, etc)
Expand All @@ -104,8 +139,6 @@ add_definitions(-DQT_NO_CAST_FROM_BYTEARRAY)
add_definitions(-DQT_NO_CAST_TO_ASCII)
add_definitions(-DQT_RESTRICTED_CAST_FROM_ASCII)

include(CheckAtomic)

# Use ccache when available to speed up builds.
if(USE_CCACHE)
find_program(CCACHE_FOUND ccache)
Expand Down
14 changes: 14 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
"generator": "Ninja",
"cacheVariables": {
"ASAN": true,
"UBSAN": true,
"SPELL_CHECK": true,
"STRICT_OPTIONS": true,
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_COMPILE_WARNING_AS_ERROR": true,
"CMAKE_EXPORT_COMPILE_COMMANDS": true
}
},
{
"name": "debug-tsan",
"binaryDir": "${sourceDir}/_build-tsan",
"generator": "Ninja",
"cacheVariables": {
"TSAN": true,
"SPELL_CHECK": true,
"STRICT_OPTIONS": true,
"CMAKE_BUILD_TYPE": "Debug",
Expand Down
97 changes: 0 additions & 97 deletions cmake/CheckAtomic.cmake

This file was deleted.

3 changes: 2 additions & 1 deletion src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ void Nexus::updateWindowsArg(QWindow* closedWindow)
QAction* action = windowActions->addAction(windowList[i]->title());
action->setCheckable(true);
action->setChecked(windowList[i] == activeWindow);
connect(action, &QAction::triggered, [=] { onOpenWindow(windowList[i]); });
connect(action, &QAction::triggered, this,
[this, window = windowList[i]] { onOpenWindow(window); });
windowMenu->addAction(action);
dockMenu->insertAction(dockLast, action);
}
Expand Down
2 changes: 1 addition & 1 deletion test/net/bsu_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ void TestBootstrapNodesUpdater::testLocal()
QVERIFY(defaultNodes.size() > 0);
}

QTEST_GUILESS_MAIN(TestBootstrapNodesUpdater)
QTEST_MAIN(TestBootstrapNodesUpdater)
#include "bsu_test.moc"
2 changes: 1 addition & 1 deletion test/persistence/paths_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ void TestPaths::checkPathsPortable()
}
#endif

QTEST_GUILESS_MAIN(TestPaths)
QTEST_MAIN(TestPaths)
#include "paths_test.moc"
Loading