Skip to content

Commit 4e6d4c1

Browse files
committed
luzer: add macOS ARM64 support
The patch adds macOS ARM64 support. Notable changes are: - Fixed building sanitizers libraries. There's no need to strip libraries because they don't have the same object files as Linux. - Added the -Wno-builtin-memcpy-chk-size option to build the test library; otherwise, the build won't work. - Use the DYLD_INSERT_LIBRARIES environment variable to load the ASAN library. - Added an environment variable ASAN_OPTIONS=abort_on_error=0 for regression regression tests. Without option ASAN aborts Lua runtime and CTest fails the test. The behaviour is macOS-specific: if the option `abort_on_error` is set, the AddressSanitizer calls `abort()` instead of `_exit()` after printing the error report [2]. By default, `abort_on_error` is set to 0 on Linux, but on macOS it is set to 1 [3]. - Added a directory with LuaJIT binary executable to HINTS for find_program(). Otherwise, `luajit` is not found automatically. - Added macOS 26 ARM64 [1] to the regression testing on Github Actions. 1. https://docs.github.com/en/actions/reference/runners/github-hosted-runners 2. https://github.com/google/sanitizers/wiki/SanitizerCommonFlags 3. https://reviews.llvm.org/D7203 Closes #59 Co-authored-by: Sergey Bronnikov <estetus@gmail.com>
1 parent e97eba6 commit 4e6d4c1

File tree

9 files changed

+186
-29
lines changed

9 files changed

+186
-29
lines changed

.github/workflows/test.yaml

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@ jobs:
1212
github.event.pull_request.head.repo.full_name != github.repository
1313
strategy:
1414
matrix:
15+
os: [ubuntu-24.04, macos-26]
1516
BUILDTYPE: [Debug, Release]
1617
LIBLUA:
1718
- "5.4"
1819
- "5.3"
1920
- "5.2"
2021
- "5.1"
2122
- "luajit-v2.1"
23+
exclude:
24+
- os: macos-26
25+
LIBLUA: "5.4"
26+
- os: macos-26
27+
LIBLUA: "5.3"
28+
- os: macos-26
29+
LIBLUA: "5.2"
30+
- os: macos-26
31+
LIBLUA: "5.1"
2232
include:
2333
- BUILDTYPE: Debug
2434
CMAKEFLAGS: -DCMAKE_BUILD_TYPE=Debug
@@ -36,24 +46,49 @@ jobs:
3646
PACKAGES: libluajit-5.1-dev libluajit-5.1-2 luajit
3747
FLAVORFLAGS: -DLUAJIT_FRIENDLY_MODE=ON -DENABLE_LUAJIT=ON
3848
fail-fast: false
39-
runs-on: ubuntu-24.04
49+
runs-on: ${{ matrix.os }}
4050
steps:
4151
- uses: actions/checkout@v3
4252

4353
- name: Disable processing triggers for man-db
54+
if: runner.os == 'Linux'
4455
run: sudo apt-get remove --purge man-db
4556

46-
- name: Setup common packages
57+
- name: Setup Linux packages
58+
if: runner.os == 'Linux'
4759
run: sudo apt install -y clang-15 libclang-common-15-dev ${{ matrix.PACKAGES }}
4860

49-
- name: Running CMake
61+
- name: Setup macOS packages
62+
if: runner.os == 'macOS'
63+
run: brew install llvm luajit cmake ninja
64+
65+
- name: Running CMake (Linux)
66+
if: runner.os == 'Linux'
5067
run: >
5168
cmake -S . -B build -G Ninja -DENABLE_TESTING=ON
5269
-DCMAKE_C_COMPILER=clang-15 -DCMAKE_CXX_COMPILER=clang++-15
5370
${{ matrix.CMAKEFLAGS }} ${{ matrix.FLAVORFLAGS }}
5471
72+
- name: Define Brew prefixes (macOS)
73+
if: runner.os == 'macOS'
74+
run: |
75+
echo "BREW_LLVM_PREFIX=`brew --prefix llvm`" >> $GITHUB_ENV
76+
echo "BREW_LUAJIT_PREFIX=`brew --prefix luajit`" >> $GITHUB_ENV
77+
78+
- name: Running CMake (macOS)
79+
if: runner.os == 'macOS'
80+
run: |
81+
cmake -S . -B build -G Ninja \
82+
-DCMAKE_C_COMPILER=$BREW_LLVM_PREFIX/bin/clang \
83+
-DCMAKE_CXX_COMPILER=$BREW_LLVM_PREFIX/bin/clang++ \
84+
-DLUA_INCLUDE_DIR=$BREW_LUAJIT_PREFIX/include/luajit-2.1 \
85+
-DLUA_LIBRARIES=$BREW_LUAJIT_PREFIX/lib/libluajit-5.1.dylib \
86+
-DENABLE_LUAJIT=ON \
87+
-DLUAJIT_FRIENDLY_MODE=ON \
88+
${{ matrix.CMAKEFLAGS }}
89+
5590
- name: Building
56-
run: cmake --build build --parallel $(nproc)
91+
run: cmake --build build --parallel $(getconf _NPROCESSORS_ONLN)
5792

5893
- name: Testing
5994
run: cmake --build build --target test

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Support LuaJIT metrics.
2424
- Support OSS Fuzz environment (#73).
2525
- Support for building on Ubuntu 24.04 AArch64 (ARM64).
26+
- Support for building on macOS ARM64.
2627

2728
### Changed
2829

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ if(LUA_INCLUDE_DIR AND LUA_LIBRARIES)
2727
set(LUA_FOUND TRUE)
2828
elseif(ENABLE_LUAJIT)
2929
find_package(LuaJIT)
30-
set(LUA_HAS_JIT ON CACHE INTERNAL "Use LuaJIT library")
3130
message(STATUS "Found LuaJIT ${LUA_VERSION_STRING}")
3231
message(STATUS "Found LuaJIT interpreter ${LUA_EXECUTABLE}")
3332
else()
@@ -42,6 +41,10 @@ if(NOT LUA_FOUND)
4241
message(FATAL_ERROR "Lua library is not found")
4342
endif()
4443

44+
if(ENABLE_LUAJIT)
45+
set(LUA_HAS_JIT ON CACHE INTERNAL "Use LuaJIT library")
46+
endif()
47+
4548
if(LUAJIT_FRIENDLY_MODE AND NOT LUA_HAS_JIT)
4649
message(FATAL_ERROR "LuaJIT-friendly mode requires option ENABLE_LUAJIT")
4750
endif()

CONTRIBUTING.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
## Hacking
22

3-
For developing `luzer` you need to install required packages.
3+
For developing `luzer` you need to install required packages,
4+
build the project and run the regression tests.
45

5-
On Debian: `apt install -y liblua5.1-0-dev llvm-17-dev libclang-common-17-dev
6-
libclang-rt-17-dev clang-17 cmake`.
6+
### Setup packages
7+
8+
On Debian: `apt install -y lua5.1 liblua5.1-0-dev llvm-17-dev
9+
libclang-common-17-dev libclang-rt-17-dev clang-17 cmake`.
710

811
Note: with Clang >= 18 you should install a package
912
`libclang-rt-XX-dev` and with Clang <= 15 you should install
1013
a package `libclang-common-XX-dev`, where XX is a Clang version.
1114

15+
On macOS: `brew install llvm cmake luajit`.
16+
17+
### Building
18+
19+
On Debian:
20+
1221
```sh
1322
$ cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DENABLE_TESTING=ON -S . -B build
23+
```
24+
25+
On macOS:
26+
27+
You may need to set environment variables for directories containing
28+
LLVM header files and libraries:
29+
30+
```sh
31+
$ export LDFLAGS="-L$(brew --prefix llvm)/lib"
32+
$ export CPPFLAGS="-I$(brew --prefix llvm)/include"
33+
```
34+
35+
```sh
36+
$ cmake -S . -B build -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 -DENABLE_TESTING=ON -DENABLE_LUAJIT=ON -DLUAJIT_FRIENDLY_MODE=ON
37+
```
38+
39+
### Run regression testing
40+
41+
```sh
1442
$ cmake --build build --parallel
1543
$ cmake --build build --target test
1644
```

cmake/BuildLibSanitizers.cmake

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ macro(GEN_BUILD_TARGET name libsanitizer_path libfuzzer_path
2626
set(AR ${CMAKE_AR})
2727
endif()
2828

29-
# Strip preinit object files in static libraries, otherwise a message
30-
# `.preinit_array section is not allowed in DSO` will prevent building DSO.
29+
# Strip preinit object files in static libraries on Linux,
30+
# otherwise a message `.preinit_array section is not allowed in
31+
# DSO` will prevent building DSO.
3132
add_custom_target(strip_lib_${name}
3233
COMMENT "Strip sanitizer library ${name}"
3334
COMMAND ${AR} d ${libsanitizer_name} ${strip}
@@ -36,25 +37,54 @@ macro(GEN_BUILD_TARGET name libsanitizer_path libfuzzer_path
3637
DEPENDS copy_libs_${name}
3738
)
3839

40+
set(LINK_COMMAND
41+
${CMAKE_C_COMPILER}
42+
-Wl,--whole-archive
43+
${libfuzzer_name}
44+
${libsanitizer_name}
45+
-Wl,--no-whole-archive
46+
-lstdc++
47+
-lpthread
48+
-ldl
49+
-shared
50+
-o ${sanitizer_dso_name}
51+
)
52+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
53+
set(LINK_COMMAND
54+
${CMAKE_C_COMPILER}
55+
${libfuzzer_name}
56+
${libsanitizer_name}
57+
-lstdc++
58+
-lpthread
59+
-dynamiclib
60+
-o ${sanitizer_dso_name}
61+
)
62+
endif()
3963
add_custom_target(build_dso_${name} ALL
4064
COMMENT "Build sanitizer library ${name}"
41-
COMMAND ${CMAKE_C_COMPILER} -Wl,--whole-archive ${libfuzzer_name}
42-
${libsanitizer_name} -Wl,--no-whole-archive -lstdc++ -lpthread -ldl
43-
-shared -o ${sanitizer_dso_name}
65+
COMMAND ${LINK_COMMAND}
4466
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
4567
BYPRODUCTS ${sanitizer_dso_name}
46-
DEPENDS strip_lib_${name}
4768
)
69+
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
70+
add_dependencies(build_dso_${name} strip_lib_${name})
71+
else()
72+
add_dependencies(build_dso_${name} copy_libs_${name})
73+
endif()
4874
endmacro()
4975

50-
list(APPEND LIBCLANG_ASAN_STRIP
51-
asan_preinit.cc.o
52-
asan_preinit.cpp.o
53-
)
54-
list(APPEND LIBCLANG_UBSAN_STRIP
55-
ubsan_init_standalone_preinit.cc.o
56-
ubsan_init_standalone_preinit.cpp.o
57-
)
76+
set(LIBCLANG_ASAN_STRIP "")
77+
set(LIBCLANG_UBSAN_STRIP "")
78+
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
79+
list(APPEND LIBCLANG_ASAN_STRIP
80+
asan_preinit.cc.o
81+
asan_preinit.cpp.o
82+
)
83+
list(APPEND LIBCLANG_UBSAN_STRIP
84+
ubsan_init_standalone_preinit.cc.o
85+
ubsan_init_standalone_preinit.cpp.o
86+
)
87+
endif()
5888

5989
set(ASAN_DSO "libfuzzer_with_asan${CMAKE_SHARED_LIBRARY_SUFFIX}")
6090
set(UBSAN_DSO "libfuzzer_with_ubsan${CMAKE_SHARED_LIBRARY_SUFFIX}")

cmake/FindLuaJIT.cmake

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ find_package_handle_standard_args(LuaJIT
4343
FAIL_MESSAGE "${ERROR_MESSAGE}"
4444
)
4545

46-
find_program(LUAJIT_EXECUTABLE luajit)
46+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
47+
string(JOIN " " LUAJIT_BIN_HINTS
48+
/opt/homebrew/bin/
49+
)
50+
endif()
51+
find_program(LUAJIT_EXECUTABLE
52+
NAMES luajit
53+
HINTS ${LUAJIT_BIN_HINTS}
54+
)
4755
if(NOT EXISTS ${LUAJIT_EXECUTABLE})
4856
message(WARNING "`luajit` is not found")
4957
endif()
@@ -58,5 +66,6 @@ unset(LUAJIT_FOUND)
5866
unset(LUAJIT_LIBRARIES)
5967
unset(LUAJIT_INCLUDE_DIR)
6068
unset(LUAJIT_EXECUTABLE)
69+
unset(LUAJIT_BIN_HINTS)
6170

6271
mark_as_advanced(LUA_INCLUDE_DIR LUA_LIBRARIES)

luzer/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ endif()
1212

1313
set(LIBCLANG_ASAN_NAME "libclang_rt.asan-${ARCH}.a")
1414
set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_standalone-${ARCH}.a")
15-
# Sanitizers libraries in the OSS Fuzz environment have different
16-
# names.
15+
# Sanitizers libraries in the OSS Fuzz environment and macOS have
16+
# different names.
1717
if(OSS_FUZZ)
1818
set(LIBCLANG_ASAN_NAME "libclang_rt.asan.a")
1919
set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_standalone.a")
20+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
21+
set(LIBCLANG_ASAN_NAME "libclang_rt.asan_abi_osx.a")
22+
set(LIBCLANG_UBSAN_NAME "libclang_rt.ubsan_minimal_osx.a")
2023
endif()
2124
SetClangLibPath(${LIBCLANG_ASAN_NAME} LIBCLANG_ASAN_LIB)
2225
SetClangLibPath(${LIBCLANG_UBSAN_NAME} LIBCLANG_UBSAN_LIB)
@@ -41,6 +44,10 @@ add_compile_options(
4144
-Wpedantic
4245
)
4346
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
47+
# It turns out that macOS set _FORTIFY_SOURCE internally, so we
48+
# need to undefine it first, otherwise an error "'_FORTIFY_SOURCE'
49+
# macro redefined" breaks a building.
50+
add_compile_options(-U_FORTIFY_SOURCE)
4451
add_compile_options(-D_FORTIFY_SOURCE=2)
4552
endif()
4653

luzer/luzer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
#include <libgen.h>
2121
#include <fcntl.h>
2222
#include <unistd.h>
23+
#ifdef __linux__
2324
#include <linux/limits.h>
25+
#else
26+
#include <limits.h>
27+
#endif
2428

2529
#include "fuzzed_data_provider.h"
2630
#include "counters.h"
@@ -33,7 +37,11 @@
3337

3438
#define TEST_ONE_INPUT_FUNC "luzer_test_one_input"
3539
#define CUSTOM_MUTATOR_FUNC "luzer_custom_mutator"
40+
#ifdef __APPLE__
41+
#define CUSTOM_MUTATOR_LIB "libcustom_mutator.dylib"
42+
#else
3643
#define CUSTOM_MUTATOR_LIB "libcustom_mutator.so"
44+
#endif /* __APPLE__ */
3745
#define DEBUG_HOOK_FUNC "luzer_custom_hook"
3846

3947
static lua_State *LL;

0 commit comments

Comments
 (0)