Răsfoiți Sursa

Sanitizer build: -DLYNX_SANITIZE=address,undefined (scripts/build-sanitize.sh, tests/sanitizers/ suppressions); its crash handlers leave the fault signals to ASan / UBSan

Ka1-S 1 săptămână în urmă
părinte
comite
d85ffeda9e

+ 14 - 0
CMakeLists.txt

@@ -14,6 +14,20 @@ if(NOT MSVC)
     add_compile_options(-ffp-contract=off)
 endif()
 
+# Sanitizer builds (round 190rb, the 0.1.32 quality release): -DLYNX_SANITIZE=address,undefined (any -fsanitize= list:
+# address, undefined, thread, leak ...) for every target, app, tests and tools alike. GCC / Clang only. Use with
+# -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O1 -g -DNDEBUG" -DLYNX_LTO=OFF (scripts/build-sanitize.sh).
+# UBSan reports and goes on (the run lists every report); ASan stops at its first error, as always.
+set(LYNX_SANITIZE "" CACHE STRING "Build everything with -fsanitize=<list> (e.g. address,undefined); empty = off")
+if(LYNX_SANITIZE AND NOT MSVC)
+    add_compile_options(-fsanitize=${LYNX_SANITIZE} -fno-omit-frame-pointer -fno-optimize-sibling-calls)
+    add_link_options(-fsanitize=${LYNX_SANITIZE})
+    add_compile_definitions(LYNX_SANITIZER_BUILD=1)
+    # tests/tests.cpp is one huge translation unit: at -O1 under ASan + UBSan g++ 15 passed 20 GB for it alone (round
+    # 190rb), so it is compiled without optimisation there (the code it tests, lynx_core, keeps -O1)
+    set_source_files_properties(tests/tests.cpp PROPERTIES COMPILE_OPTIONS "-O0")
+endif()
+
 # Linux on ARM makes a plain `char` unsigned (x86 and Apple arm64: signed). The readers of FL's, Live's and the other
 # formats were written and tested with a signed char: Linux arm64 builds keep it (round 190oy)
 if(NOT MSVC AND NOT APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|ARM64")

+ 32 - 0
scripts/build-sanitize.sh

@@ -0,0 +1,32 @@
+#!/usr/bin/env bash
+# A sanitizer build of the app, the tests and the tools (round 190rb, the 0.1.32 quality release), Linux / macOS with
+# GCC or Clang:
+#
+#   scripts/build-sanitize.sh [extra cmake arguments...]
+#
+# env:  SAN=address,undefined (default; e.g. thread for TSan, which cannot be combined with address)
+#       BUILD=build-san       the build folder
+#       JOBS=3                parallel compiles (tests.cpp alone needs several GB under ASan)
+#       NOBUILD=1             only configure (e.g. to build through your own job queue)
+#
+# Running (the suppression files list system libraries, third-party code and LYNX's deliberate process-life caches;
+# a real LYNX leak is fixed, never suppressed). The main thread needs a big stack under ASan (its frames grow): ulimit -s 1048576
+#   export ASAN_OPTIONS=detect_leaks=1:abort_on_error=0:halt_on_error=1:detect_stack_use_after_return=0:\
+#          suppressions=$PWD/tests/sanitizers/asan.supp
+#   export LSAN_OPTIONS=suppressions=$PWD/tests/sanitizers/lsan.supp:print_suppressions=0
+#   export UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=0:suppressions=$PWD/tests/sanitizers/ubsan.supp
+#   build-san/lynx_tests "<FL Studio>/Data"        build-san/LYNX_artefacts/RelWithDebInfo/LYNX --selftest out.txt
+#
+# -O1 keeps the run fast enough for the whole suite while every report still has its file:line; NDEBUG as in releases
+# (the same code paths as the shipped app). LTO is off (the sanitizers' instrumentation and LTO do not mix well).
+set -euo pipefail
+cd "$(dirname "$0")/.."
+SAN="${SAN:-address,undefined}"
+BUILD="${BUILD:-build-san}"
+if [[ ! -f "$BUILD/CMakeCache.txt" ]]; then
+    cmake -S . -B "$BUILD" -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo \
+        -DCMAKE_C_FLAGS_RELWITHDEBINFO="-O1 -g -DNDEBUG" -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O1 -g -DNDEBUG" \
+        -DLYNX_SANITIZE="$SAN" -DLYNX_LTO=OFF "$@"
+fi
+[[ "${NOBUILD:-0}" == 1 ]] && exit 0
+cmake --build "$BUILD" -j "${JOBS:-3}" --target LYNX lynx_tests

+ 18 - 3
src/Main.cpp

@@ -436,6 +436,17 @@ struct StartupTrace
 StartupTrace startupTrace;
 void startupMarkHook (const char* step) { startupTrace.mark (step); }
 
+// Round 190rb: a sanitizer build (-DLYNX_SANITIZE) leaves SIGSEGV / SIGBUS / SIGFPE / SIGABRT to ASan and UBSan (their
+// report names the bad access and where the memory came from); LYNX_CRASHGUARD=1 installs LYNX's handlers anyway
+bool sanitizerOwnsSignals()
+{
+#if defined(LYNX_SANITIZER_BUILD)
+    return juce::SystemStats::getEnvironmentVariable ("LYNX_CRASHGUARD", {}) != "1";
+#else
+    return false;
+#endif
+}
+
 juce::File crashFolderForTest; // --faulttest: where the crash file goes instead of the settings' autosave folder
 // Crashed: try to save the project and write the backtrace (like FL's "emergency save").
 void onCrash (void* exceptionPointers)
@@ -790,8 +801,11 @@ public:
             return;
         }
 
-        // a crash in any mode (UI, command line) writes its backtrace to %APPDATA%\LYNX\autosave
-        juce::SystemStats::setApplicationCrashHandler (onCrash);
+        // a crash in any mode (UI, command line) writes its backtrace to %APPDATA%\LYNX\autosave. A sanitizer build
+        // (-DLYNX_SANITIZE, round 190rb) leaves the fault signals to ASan / UBSan, whose reports name the bad access,
+        // unless LYNX_CRASHGUARD=1 asks for LYNX's own handlers (their tests)
+        if (! sanitizerOwnsSignals())
+            juce::SystemStats::setApplicationCrashHandler (onCrash);
         // rollup step 12: the crash handler's watchdog and its allocation-free crash file (platform/CrashGuard.h)
         lynx::crashguard::prepareCrashWatchdog (lynx::platform::settingsFile ("autosave").getFullPathName().toStdString());
         // rollup 0.1.28 (REA 190hb action 4, FL's "Init DWM"): the compositor on MMCSS, steadier frames under CPU load
@@ -839,7 +853,8 @@ public:
             }
             lynx::crashguard::faultForTest (args[i + 1] == "unwalkable");
         }
-        lynx::crashguard::hardenSignals(); // issue #115: stack overflows and SIGTERM / SIGHUP leave a trace (Linux / macOS)
+        if (! sanitizerOwnsSignals())
+            lynx::crashguard::hardenSignals(); // issue #115: stack overflows and SIGTERM / SIGHUP leave a trace (Linux / macOS)
 
         // SFZ instruments (round 167o): their FLAC / OGG / AIFF / MP3 samples through JUCE's readers
         lynx::sfz::setDecoder ([] (const std::string& path, lynx::sfz::Audio& out) {

+ 2 - 0
tests/sanitizers/asan.supp

@@ -0,0 +1,2 @@
+# AddressSanitizer suppressions for the sanitizer build (round 190rb): system libraries only (interceptor reports in
+# code LYNX does not build). Never LYNX or JUCE code.

+ 34 - 0
tests/sanitizers/lsan.supp

@@ -0,0 +1,34 @@
+# LeakSanitizer suppressions for the sanitizer build (scripts/build-sanitize.sh, round 190rb): system libraries and
+# third-party code (each line names one whose own allocations LSan reports at exit), and LYNX's caches that are kept for
+# the process' life on purpose (listed at the end, each with its reason). A leak in LYNX's own code is fixed, not added.
+leak:libfontconfig.so
+leak:libfreetype.so
+leak:libX11.so
+leak:libXext.so
+leak:libXrandr.so
+leak:libXcursor.so
+leak:libxcb.so
+leak:libasound.so
+leak:libglib-2.0.so
+leak:libgobject-2.0.so
+leak:libgio-2.0.so
+leak:libdbus-1.so
+leak:libGLX.so
+leak:libGL.so
+leak:libEGL.so
+leak:libGLdispatch.so
+leak:libgallium
+leak:_dri.so
+leak:libLLVM
+leak:libpipewire-0.3.so
+leak:libjack.so
+leak:libcurl.so
+# lilv (bundled with JUCE's LV2 host): lilv_world_set_option keeps a copy of LV2_PATH that lilv_world_free does not
+# free (39 bytes per LV2PluginFormat, once per scan; seen in every run of the app)
+leak:lilv_world_set_option
+# lilv: lilv_world_load_all keeps the plugin classes of an earlier load (lilv_world_load_plugin_classes replaces the
+# list without freeing it): ~16 KB each time JUCE's LV2 format scans (PluginHost::describeLv2, the background scan)
+leak:lilv_world_load_plugin_classes
+# LYNX's intentional caches (kept for the process' life: the audio thread may hold them; their index is a function-local
+# static vector that is destroyed at exit, after which LSan no longer sees the pointers)
+leak:lynx::wt::table

+ 2 - 0
tests/sanitizers/ubsan.supp

@@ -0,0 +1,2 @@
+# UndefinedBehaviorSanitizer suppressions for the sanitizer build (round 190rb): third-party code only, one line each
+# with the reason. Never LYNX code: fix the UB instead.