From c26f54a60d1c99c2ea03591373de75cd9f8d91e2 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Fri, 8 Aug 2025 11:22:40 +0300 Subject: [PATCH 1/8] luzer: use CMAKE_SHARED_LIBRARY_SUFFIX The patch replace static suffix ".so" for shared libraries with CMAKE_SHARED_LIBRARY_SUFFIX [1]. Needed for the following commit. 1. https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LIBRARY_SUFFIX.html --- luzer/CMakeLists.txt | 2 +- luzer/tests/CMakeLists.txt | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/luzer/CMakeLists.txt b/luzer/CMakeLists.txt index 5ed2475..3f01afb 100644 --- a/luzer/CMakeLists.txt +++ b/luzer/CMakeLists.txt @@ -90,7 +90,7 @@ install( TARGETS luzer_impl LIBRARY DESTINATION "${CMAKE_LIBDIR}/" - RENAME luzer.so + RENAME luzer${CMAKE_SHARED_LIBRARY_SUFFIX} ) install( diff --git a/luzer/tests/CMakeLists.txt b/luzer/tests/CMakeLists.txt index 3be9519..5f02f65 100644 --- a/luzer/tests/CMakeLists.txt +++ b/luzer/tests/CMakeLists.txt @@ -2,7 +2,7 @@ include(MakeLuaPath) make_lua_path(LUA_CPATH PATHS - ${PROJECT_BINARY_DIR}/luzer/?.so + ${PROJECT_BINARY_DIR}/luzer/?${CMAKE_SHARED_LIBRARY_SUFFIX} ) make_lua_path(LUA_PATH @@ -282,25 +282,25 @@ list(APPEND TEST_ENV ) if (LUA_HAS_JIT) generate_ffi_test(luzer_ffi - "${TEST_ENV};FFI_LIB_NAME=testlib.so" + "${TEST_ENV};FFI_LIB_NAME=testlib${CMAKE_SHARED_LIBRARY_SUFFIX}" "Done 10 runs in 0 second" ) # XXX: Memory leak in FDP is expected, should be fixed in [1]. # 1. https://github.com/ligurio/luzer/issues/52 generate_ffi_test(luzer_ffi_asan - "${TEST_ENV};LD_PRELOAD=${ASAN_DSO_PATH};FFI_LIB_NAME=testlib_asan.so" + "${TEST_ENV};LD_PRELOAD=${ASAN_DSO_PATH};FFI_LIB_NAME=testlib_asan${CMAKE_SHARED_LIBRARY_SUFFIX}" "LeakSanitizer: detected memory leaks" ) generate_ffi_test(luzer_ffi_ubsan - "${TEST_ENV};LD_PRELOAD=${UBSAN_DSO_PATH};FFI_LIB_NAME=testlib_ubsan.so" + "${TEST_ENV};LD_PRELOAD=${UBSAN_DSO_PATH};FFI_LIB_NAME=testlib_ubsan${CMAKE_SHARED_LIBRARY_SUFFIX}" "Done 10 runs in 0 second" ) generate_ffi_test(luzer_ffi_asan_heap_overflow - "${TEST_ENV};LD_PRELOAD=${ASAN_DSO_PATH};FFI_LIB_NAME=testlib_asan.so;ERR_INJECTION=HEAP_BUFFER_OVERFLOW" + "${TEST_ENV};LD_PRELOAD=${ASAN_DSO_PATH};FFI_LIB_NAME=testlib_asan${CMAKE_SHARED_LIBRARY_SUFFIX};ERR_INJECTION=HEAP_BUFFER_OVERFLOW" "AddressSanitizer: heap-buffer-overflow" ) generate_ffi_test(luzer_ffi_ubsan_null_pointer_deref - "${TEST_ENV};LD_PRELOAD=${UBSAN_DSO_PATH};FFI_LIB_NAME=testlib_ubsan.so;ERR_INJECTION=NULL_POINTER_DEREF" + "${TEST_ENV};LD_PRELOAD=${UBSAN_DSO_PATH};FFI_LIB_NAME=testlib_ubsan${CMAKE_SHARED_LIBRARY_SUFFIX};ERR_INJECTION=NULL_POINTER_DEREF" "runtime error: load of null pointer of type" ) endif() From 6e67121ff9030267a24a531353a466962275d699 Mon Sep 17 00:00:00 2001 From: mzfr Date: Tue, 5 Aug 2025 13:05:14 +0800 Subject: [PATCH 2/8] luzer: added support to build on macOS ARM64 Current limitations: - no testing on macOS - commented out `FORTIFY_SOURCE` compilation flag - no build for sanitizer libraries --- .github/workflows/test.yaml | 37 +++++++++++++++++++++++++---- CHANGELOG.md | 1 + cmake/BuildLibSanitizers.cmake | 43 +++++++++++++++++++++++----------- cmake/SetClangRTLib.cmake | 6 ++++- luzer/CMakeLists.txt | 16 +++++++++---- luzer/luzer.c | 4 ++++ 6 files changed, 84 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0eacbbe..761d681 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,6 +12,7 @@ jobs: github.event.pull_request.head.repo.full_name != github.repository strategy: matrix: + os: [ubuntu-24.04, macos-latest] BUILDTYPE: [Debug, Release] LIBLUA: - "5.4" @@ -19,6 +20,15 @@ jobs: - "5.2" - "5.1" - "luajit-v2.1" + exclude: + - os: macos-latest + LIBLUA: "5.4" + - os: macos-latest + LIBLUA: "5.3" + - os: macos-latest + LIBLUA: "5.2" + - os: macos-latest + LIBLUA: "5.1" include: - BUILDTYPE: Debug CMAKEFLAGS: -DCMAKE_BUILD_TYPE=Debug @@ -36,24 +46,43 @@ jobs: PACKAGES: libluajit-5.1-dev libluajit-5.1-2 luajit FLAVORFLAGS: -DLUAJIT_FRIENDLY_MODE=ON -DENABLE_LUAJIT=ON fail-fast: false - runs-on: ubuntu-24.04 + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - name: Disable processing triggers for man-db + if: runner.os == "Linux" run: sudo apt-get remove --purge man-db - - name: Setup common packages + - name: Setup Linux packages + if: runner.os == "Linux" run: sudo apt install -y clang-15 libclang-common-15-dev ${{ matrix.PACKAGES }} - - name: Running CMake + - name: Setup macOS packages + if: runner.os == "macOS" + run: brew install llvm luajit cmake ninja + + - name: Running CMake (Linux) + if: runner.os == "Linux" run: > cmake -S . -B build -G Ninja -DENABLE_TESTING=ON -DCMAKE_C_COMPILER=clang-15 -DCMAKE_CXX_COMPILER=clang++-15 ${{ matrix.CMAKEFLAGS }} ${{ matrix.FLAVORFLAGS }} + - name: Running CMake (macOS) + if: runner.os == "macOS" + run: > + cmake -S . -B build -G Ninja + -DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang + -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ + -DLUA_INCLUDE_DIR=/opt/homebrew/include/luajit-2.1 + -DLUA_LIBRARIES=/opt/homebrew/lib/libluajit-5.1.dylib + -DLLVM_DIR=/opt/homebrew/opt/llvm/ + -DCMAKE_PREFIX_PATH=/opt/homebrew/Cellar/llvm/20.1.8//lib/cmake/llvm/ + ${{ matrix.CMAKEFLAGS }} + - name: Building - run: cmake --build build --parallel $(nproc) + run: cmake --build build --parallel - name: Testing run: cmake --build build --target test diff --git a/CHANGELOG.md b/CHANGELOG.md index 373488b..543f965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support Address and UndefinedBehaviour sanitizers. - Support LuaJIT metrics. - Support OSS Fuzz environment (#73). +- Support for building on macOS ARM64. ### Changed diff --git a/cmake/BuildLibSanitizers.cmake b/cmake/BuildLibSanitizers.cmake index 99baa6c..d4d214a 100644 --- a/cmake/BuildLibSanitizers.cmake +++ b/cmake/BuildLibSanitizers.cmake @@ -31,28 +31,43 @@ macro(GEN_BUILD_TARGET name libsanitizer_path libfuzzer_path DEPENDS copy_libs_${name} ) + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(LINK_COMMAND ${CMAKE_C_COMPILER} -Wl,-force_load,${libfuzzer_name} + -Wl,-force_load,${libsanitizer_name} -lstdc++ -lpthread + -dynamiclib -o ${sanitizer_dso_name}) + else() + set(LINK_COMMAND ${CMAKE_C_COMPILER} -Wl,--whole-archive ${libfuzzer_name} + ${libsanitizer_name} -Wl,--no-whole-archive -lstdc++ -lpthread -ldl + -shared -o ${sanitizer_dso_name}) + endif() add_custom_target(build_dso_${name} ALL COMMENT "Build sanitizer library ${name}" - COMMAND ${CMAKE_C_COMPILER} -Wl,--whole-archive ${libfuzzer_name} - ${libsanitizer_name} -Wl,--no-whole-archive -lstdc++ -lpthread -ldl - -shared -o ${sanitizer_dso_name} + COMMAND ${LINK_COMMAND} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} BYPRODUCTS ${sanitizer_dso_name} - DEPENDS strip_lib_${name} ) + if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + add_dependencies(build_dso_${name} strip_lib_${name}) + else() + add_dependencies(build_dso_${name} copy_libs_${name}) + endif() endmacro() -list(APPEND LIBCLANG_ASAN_STRIP - asan_preinit.cc.o - asan_preinit.cpp.o -) -list(APPEND LIBCLANG_UBSAN_STRIP - ubsan_init_standalone_preinit.cc.o - ubsan_init_standalone_preinit.cpp.o -) +set(LIBCLANG_ASAN_STRIP "") +set(LIBCLANG_UBSAN_STRIP "") +if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + list(APPEND LIBCLANG_ASAN_STRIP + asan_preinit.cc.o + asan_preinit.cpp.o + ) + list(APPEND LIBCLANG_UBSAN_STRIP + ubsan_init_standalone_preinit.cc.o + ubsan_init_standalone_preinit.cpp.o + ) +endif() -set(ASAN_DSO "libfuzzer_with_asan.so") -set(UBSAN_DSO "libfuzzer_with_ubsan.so") +set(ASAN_DSO "libfuzzer_with_asan${CMAKE_SHARED_LIBRARY_SUFFIX}") +set(UBSAN_DSO "libfuzzer_with_ubsan${CMAKE_SHARED_LIBRARY_SUFFIX}") GEN_BUILD_TARGET("asan" ${LIBCLANG_ASAN_LIB} diff --git a/cmake/SetClangRTLib.cmake b/cmake/SetClangRTLib.cmake index 0ab150f..09782e9 100644 --- a/cmake/SetClangRTLib.cmake +++ b/cmake/SetClangRTLib.cmake @@ -2,7 +2,11 @@ function(SetHwArchString outvar) if (CMAKE_SIZEOF_VOID_P EQUAL 4) set(hw_arch "i386") elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) - set(hw_arch "x86_64") + if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)") + set(hw_arch "arm64") + else() + set(hw_arch "x86_64") + endif() else () message(FATAL_ERROR "Unsupported architecture.") endif () diff --git a/luzer/CMakeLists.txt b/luzer/CMakeLists.txt index 3f01afb..451c8ce 100644 --- a/luzer/CMakeLists.txt +++ b/luzer/CMakeLists.txt @@ -12,17 +12,23 @@ endif() set(LIBCLANG_ASAN_NAME "libclang_rt.asan-${ARCH}.a") set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_standalone-${ARCH}.a") -# Sanitizers libraries in the OSS Fuzz environment have different -# names. +# Sanitizers libraries in the OSS Fuzz environment and macOS have +# different names. if (DEFINED ENV{OSS_FUZZ}) set(LIBCLANG_ASAN_NAME "libclang_rt.asan.a") set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_standalone.a") +elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(LIBCLANG_ASAN_NAME "libclang_rt.asan_abi_osx.a") + set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_minimal_osx.a") endif() SetClangLibPath(${LIBCLANG_ASAN_NAME} LIBCLANG_ASAN_LIB) SetClangLibPath(${LIBCLANG_UBSAN_NAME} LIBCLANG_UBSAN_LIB) SetClangLibPath(${LIBFUZZER_NO_MAIN_LIB_NAME} FUZZER_NO_MAIN_LIBRARY) -include(BuildLibSanitizers) +# FIXME +if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + include(BuildLibSanitizers) +endif() configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config.c @@ -40,7 +46,9 @@ add_compile_options( -Wno-unused-parameter -Wpedantic ) -if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") +# FIXME +if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND + NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") add_compile_options(-D_FORTIFY_SOURCE=2) endif() diff --git a/luzer/luzer.c b/luzer/luzer.c index 734e02d..f116b30 100644 --- a/luzer/luzer.c +++ b/luzer/luzer.c @@ -20,7 +20,11 @@ #include #include #include +#ifdef __linux__ #include +#else +#include +#endif #include "fuzzed_data_provider.h" #include "counters.h" From d5b819cb1d900dfe4aacff7fcfb8612641fd6254 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Tue, 20 Jan 2026 09:29:14 +0300 Subject: [PATCH 3/8] build sanitizers --- luzer/CMakeLists.txt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/luzer/CMakeLists.txt b/luzer/CMakeLists.txt index 451c8ce..c88d442 100644 --- a/luzer/CMakeLists.txt +++ b/luzer/CMakeLists.txt @@ -25,10 +25,7 @@ SetClangLibPath(${LIBCLANG_ASAN_NAME} LIBCLANG_ASAN_LIB) SetClangLibPath(${LIBCLANG_UBSAN_NAME} LIBCLANG_UBSAN_LIB) SetClangLibPath(${LIBFUZZER_NO_MAIN_LIB_NAME} FUZZER_NO_MAIN_LIBRARY) -# FIXME -if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") - include(BuildLibSanitizers) -endif() +include(BuildLibSanitizers) configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config.c From b446a56756059615530ce1386844d8a636f31cd0 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Tue, 20 Jan 2026 09:37:13 +0300 Subject: [PATCH 4/8] slash --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 761d681..cfaeed8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -78,7 +78,7 @@ jobs: -DLUA_INCLUDE_DIR=/opt/homebrew/include/luajit-2.1 -DLUA_LIBRARIES=/opt/homebrew/lib/libluajit-5.1.dylib -DLLVM_DIR=/opt/homebrew/opt/llvm/ - -DCMAKE_PREFIX_PATH=/opt/homebrew/Cellar/llvm/20.1.8//lib/cmake/llvm/ + -DCMAKE_PREFIX_PATH=/opt/homebrew/Cellar/llvm/20.1.8/lib/cmake/llvm/ ${{ matrix.CMAKEFLAGS }} - name: Building From 272c77bc5372f9f0bb9c4b6845969af936036819 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Tue, 20 Jan 2026 09:37:19 +0300 Subject: [PATCH 5/8] disable -Werror --- luzer/CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/luzer/CMakeLists.txt b/luzer/CMakeLists.txt index c88d442..ee5fc8d 100644 --- a/luzer/CMakeLists.txt +++ b/luzer/CMakeLists.txt @@ -38,16 +38,17 @@ add_compile_options( -fPIC -pedantic -Wall - -Werror -Wextra -Wno-unused-parameter -Wpedantic ) -# FIXME if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") add_compile_options(-D_FORTIFY_SOURCE=2) endif() +if(ENABLE_TESTING) + # add_compile_options(-Werror) +endif() set(LUZER_SOURCES luzer.c compat.c From f3ceb41cf7d208d5679f5a6d39feafc3cca8ac4c Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Tue, 20 Jan 2026 09:12:41 +0300 Subject: [PATCH 6/8] single quotes --- .github/workflows/test.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cfaeed8..86f2a57 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -51,26 +51,26 @@ jobs: - uses: actions/checkout@v3 - name: Disable processing triggers for man-db - if: runner.os == "Linux" + if: runner.os == 'Linux' run: sudo apt-get remove --purge man-db - name: Setup Linux packages - if: runner.os == "Linux" + if: runner.os == 'Linux' run: sudo apt install -y clang-15 libclang-common-15-dev ${{ matrix.PACKAGES }} - name: Setup macOS packages - if: runner.os == "macOS" + if: runner.os == 'macOS' run: brew install llvm luajit cmake ninja - name: Running CMake (Linux) - if: runner.os == "Linux" + if: runner.os == 'Linux' run: > cmake -S . -B build -G Ninja -DENABLE_TESTING=ON -DCMAKE_C_COMPILER=clang-15 -DCMAKE_CXX_COMPILER=clang++-15 ${{ matrix.CMAKEFLAGS }} ${{ matrix.FLAVORFLAGS }} - name: Running CMake (macOS) - if: runner.os == "macOS" + if: runner.os == 'macOS' run: > cmake -S . -B build -G Ninja -DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang From 6a462f87c32405c3c831e12fbc7669292553f611 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Fri, 8 Aug 2025 11:18:31 +0300 Subject: [PATCH 7/8] ci: enable tmate [TMP] --- .github/workflows/test.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 86f2a57..ea12691 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -81,6 +81,10 @@ jobs: -DCMAKE_PREFIX_PATH=/opt/homebrew/Cellar/llvm/20.1.8/lib/cmake/llvm/ ${{ matrix.CMAKEFLAGS }} + - name: Setup tmate session + if: ${{ failure() }} + uses: mxschmitt/action-tmate@v3 + - name: Building run: cmake --build build --parallel From 9ac267aab03d0d2571d2ab4f99d7cd511c75c5b9 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Tue, 20 Jan 2026 11:48:02 +0300 Subject: [PATCH 8/8] -lc++ --- cmake/BuildLibSanitizers.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/BuildLibSanitizers.cmake b/cmake/BuildLibSanitizers.cmake index d4d214a..50df41e 100644 --- a/cmake/BuildLibSanitizers.cmake +++ b/cmake/BuildLibSanitizers.cmake @@ -33,7 +33,7 @@ macro(GEN_BUILD_TARGET name libsanitizer_path libfuzzer_path if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") set(LINK_COMMAND ${CMAKE_C_COMPILER} -Wl,-force_load,${libfuzzer_name} - -Wl,-force_load,${libsanitizer_name} -lstdc++ -lpthread + -Wl,-force_load,${libsanitizer_name} -lstdc++ -lc++ -lpthread -dynamiclib -o ${sanitizer_dso_name}) else() set(LINK_COMMAND ${CMAKE_C_COMPILER} -Wl,--whole-archive ${libfuzzer_name}