From a7002d208948eb78699349400440f895ea951c10 Mon Sep 17 00:00:00 2001 From: mxms Date: Thu, 17 Jul 2025 20:15:33 +0000 Subject: [PATCH] Add WEBP_ENABLE_FBOUNDS_SAFETY to CMakeLists.txt Conditionally enable -fbounds-safety for webputils This is the first step towards making -fbounds-safety (https://clang.llvm.org/docs/BoundsSafety.html) available to folks with a supported toolchain. In the next CL, I'll provide a header file which wraps the annotations, keeping the code portable. It will also address the build failures below by marking the webputils files as "default unsafe", which shouldn't change any behavior (build or runtime) when -fbounds-safety is disabled. Example using a toolchain built from github.com/swiftlang/llvm-project/next ``` $ mkdir build && cd build $ cmake .. -DCMAKE_C_COMPILER=$(pwd)/../llvm-build/bin/clang -DCMAKE_CXX_COMPILER=$(pwd)/../llvm-build/bin/clang++ -DWEBP_ENABLE_FBOUNDS_SAFETY=true $ make ... [ 74%] Building C object CMakeFiles/webputils.dir/src/utils/bit_reader_utils.c.o In file included from libwebp/src/utils/bit_reader_utils.c:23: In file included from libwebp/src/utils/bit_reader_inl_utils.h:30: libwebp/src/utils/utils.h:72:14: error: passing 'const uint8_t *__single' (aka 'const unsigned char *__single') with pointee of size 1 to parameter '__src' of type 'const void *__single __sized_by(__n)' (aka 'const void *__single') with size value of 4 always fails 72 | memcpy(&A, ptr, sizeof(A)); | ^ ~~~~~~~~~ ... ``` Bug: webp:432511821 Change-Id: Ie3db8d29445f2cff06e2ee180f5dd454acedbae3 --- CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 39d3d8f6..6a2f473c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,10 @@ option(WEBP_USE_THREAD "Enable threading support" ON) option(WEBP_NEAR_LOSSLESS "Enable near-lossless encoding" ON) option(WEBP_ENABLE_SWAP_16BIT_CSP "Enable byte swap for 16 bit colorspaces." OFF) +option( + WEBP_ENABLE_FBOUNDS_SAFETY + "Enable -fbounds-safety for the part of the codebase which supports it. This expects an experimental toolchain." + OFF) set(WEBP_BITTRACE "0" CACHE STRING "Bit trace mode (0=none, 1=bit, 2=bytes)") set_property(CACHE WEBP_BITTRACE PROPERTY STRINGS 0 1 2) option(WEBP_ENABLE_WUNUSED_RESULT "Add [[nodiscard]] to some functions. \ @@ -408,6 +412,11 @@ target_include_directories(webputils PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) add_library(webp $ $ $ $) +if(WEBP_ENABLE_FBOUNDS_SAFETY) + # Enable -fbounds-safety only for webputils for now. + target_compile_options(webputils PRIVATE -fbounds-safety) +endif() + target_link_libraries(webp sharpyuv) if(XCODE) libwebp_add_stub_file(webp)