Skip to content

Commit

Permalink
Add a cmake option to build with sanitizers
Browse files Browse the repository at this point in the history
Disabled by default, only for debugging.

When using it it may be required to manually preload:
`LD_PRELOAD=/usr/lib64/libasan.so.8` for the client process.
This is because ASan runtime requires to be first in initial library list.
  • Loading branch information
kontura committed Jul 19, 2024
1 parent 3c85711 commit 9ae7f02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ OPTION (ENABLE_PYTHON "Build Python bindings" ON)
OPTION (USE_GPGME "Use GpgMe (instead of rpm library) for OpenPGP key support" ON)
OPTION (USE_RUN_GNUPG_USER_SOCKET "Create a directory for gpg-agent socket in /run/gnugp/user (instead of /run/user)" OFF)
OPTION (ENABLE_SELINUX "Restore SELinux labels on GnuPG directories" ON)
OPTION (WITH_SANITIZERS "Build with address, leak and undefined sanitizers (DEBUG ONLY)" OFF)

INCLUDE (${CMAKE_SOURCE_DIR}/VERSION.cmake)
SET (VERSION "${LIBREPO_MAJOR}.${LIBREPO_MINOR}.${LIBREPO_PATCH}")
Expand Down Expand Up @@ -95,6 +96,12 @@ IF (NOT CURL_FOUND)
MESSAGE(FATAL_ERROR "No CURL library installed")
ENDIF (NOT CURL_FOUND)

IF (WITH_SANITIZERS)
MESSAGE(WARNING "Building with sanitizers enabled!")
ADD_COMPILE_OPTIONS(-fsanitize=address -fsanitize=undefined -fsanitize=leak)
LINK_LIBRARIES(asan ubsan)
ENDIF()

# Generate header files with the configured ABI (e.g. with/without zchunk).

configure_file("librepo/downloadtarget.h.in" "librepo/downloadtarget.h" @ONLY)
Expand Down
9 changes: 9 additions & 0 deletions librepo.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Needs to match how gnupg2 is compiled
%bcond_with run_gnupg_user_socket

%bcond_without sanitizers

%if %{with use_gpgme} && %{with use_selinux}
%global need_selinux 1
%else
Expand Down Expand Up @@ -59,6 +61,12 @@ BuildRequires: pkgconfig(zck) >= 0.9.11
%endif
Requires: libcurl%{?_isa} >= %{libcurl_version}

%if %{with sanitizers}
BuildRequires: libasan
BuildRequires: liblsan
BuildRequires: libubsan
%endif

%description
A library providing C and Python (libcURL like) API to downloading repository
metadata.
Expand Down Expand Up @@ -97,6 +105,7 @@ Python 3 bindings for the librepo library.
-DWITH_ZCHUNK=%{?with_zchunk:ON}%{!?with_zchunk:OFF} \
-DUSE_GPGME=%{?with_use_gpgme:ON}%{!?with_use_gpgme:OFF} \
-DUSE_RUN_GNUPG_USER_SOCKET=%{?with_run_gnupg_user_socket:ON}%{!?with_run_gnupg_user_socket:OFF} \
-DWITH_SANITIZERS=%{?with_sanitizers:ON}%{!?with_sanitizers:OFF} \
%if %{need_selinux}
-DENABLE_SELINUX=ON
%else
Expand Down

0 comments on commit 9ae7f02

Please sign in to comment.