Skip to content

Commit f20a737

Browse files
mzfrligurio
andcommitted
luzer: add macOS ARM64 support
Co-authored-by: Sergey Bronnikov <estetus@gmail.com>
1 parent 5a80e88 commit f20a737

File tree

9 files changed

+150
-32
lines changed

9 files changed

+150
-32
lines changed

.github/workflows/test.yaml

Lines changed: 33 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-latest]
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-latest
25+
LIBLUA: "5.4"
26+
- os: macos-latest
27+
LIBLUA: "5.3"
28+
- os: macos-latest
29+
LIBLUA: "5.2"
30+
- os: macos-latest
31+
LIBLUA: "5.1"
2232
include:
2333
- BUILDTYPE: Debug
2434
CMAKEFLAGS: -DCMAKE_BUILD_TYPE=Debug
@@ -36,24 +46,43 @@ 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: Running CMake (macOS)
73+
if: runner.os == 'macOS'
74+
run: >
75+
cmake -S . -B build -G Ninja
76+
-DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang
77+
-DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++
78+
-DLUA_INCLUDE_DIR=/opt/homebrew/include/luajit-2.1
79+
-DLUA_LIBRARIES=/opt/homebrew/lib/libluajit-5.1.dylib
80+
-DENABLE_LUAJIT=ON
81+
-DLUAJIT_FRIENDLY_MODE=ON
82+
${{ matrix.CMAKEFLAGS }}
83+
5584
- name: Building
56-
run: cmake --build build --parallel $(nproc)
85+
run: cmake --build build --parallel
5786

5887
- name: Testing
5988
run: cmake --build build --target test

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Support Address and UndefinedBehaviour sanitizers.
2323
- Support LuaJIT metrics.
2424
- Support OSS Fuzz environment (#73).
25+
- Support for building on macOS ARM64.
2526

2627
### Changed
2728

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endif()
1616
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1717
include(SetClangRTLib)
1818

19-
if(LUA_INCLUDE_DIR AND LUA_LIBRARIES)
19+
if(LUA_INCLUDE_DIR AND LUA_LIBRARIES AND NOT ENABLE_LUAJIT)
2020
# When a path to a Lua library is passed outside, we should
2121
# mimic a real CMake library to don't break code that depends on
2222
# LUA_LIBRARIES.
@@ -27,7 +27,10 @@ if(LUA_INCLUDE_DIR AND LUA_LIBRARIES)
2727
elseif(ENABLE_LUAJIT)
2828
include(FindLuaJIT)
2929
set(LUA_NAME "luajit")
30-
find_program(LUA_EXECUTABLE "${LUA_NAME}")
30+
find_program(LUA_EXECUTABLE
31+
NAMES ${LUA_NAME}
32+
HINTS /opt/homebrew/bin/
33+
)
3134
if(NOT EXISTS ${LUA_EXECUTABLE})
3235
message(FATAL_ERROR "`${LUA_NAME}` is required")
3336
endif()

CONTRIBUTING.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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

56
On Debian: `apt install -y liblua5.1-0-dev llvm-17-dev libclang-common-17-dev
67
libclang-rt-17-dev clang-17 cmake`.
@@ -11,6 +12,15 @@ a package `libclang-common-XX-dev`, where XX is a Clang version.
1112

1213
```sh
1314
$ cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DENABLE_TESTING=ON -S . -B build
15+
```
16+
17+
On macOS: `brew install llvm cmake luajit`
18+
19+
```sh
20+
$ 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
21+
```
22+
23+
```sh
1424
$ cmake --build build --parallel
1525
$ cmake --build build --target test
1626
```

cmake/BuildLibSanitizers.cmake

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,57 @@ macro(GEN_BUILD_TARGET name libsanitizer_path libfuzzer_path
3636
DEPENDS copy_libs_${name}
3737
)
3838

39+
set(LINK_COMMAND
40+
${CMAKE_C_COMPILER}
41+
-Wl,--whole-archive
42+
${libfuzzer_name}
43+
${libsanitizer_name}
44+
-Wl,--no-whole-archive
45+
-lstdc++
46+
-lpthread
47+
-ldl
48+
-shared
49+
-o ${sanitizer_dso_name}
50+
)
51+
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
52+
set(LINK_COMMAND
53+
${CMAKE_C_COMPILER}
54+
${libfuzzer_name}
55+
${libsanitizer_name}
56+
-lstdc++
57+
-lpthread
58+
-dynamiclib
59+
-o ${sanitizer_dso_name}
60+
)
61+
endif()
3962
add_custom_target(build_dso_${name} ALL
4063
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}
64+
COMMAND ${LINK_COMMAND}
4465
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
4566
BYPRODUCTS ${sanitizer_dso_name}
46-
DEPENDS strip_lib_${name}
4767
)
68+
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
69+
add_dependencies(build_dso_${name} strip_lib_${name})
70+
else()
71+
add_dependencies(build_dso_${name} copy_libs_${name})
72+
endif()
4873
endmacro()
4974

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-
)
75+
set(LIBCLANG_ASAN_STRIP "")
76+
set(LIBCLANG_UBSAN_STRIP "")
77+
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
78+
list(APPEND LIBCLANG_ASAN_STRIP
79+
asan_preinit.cc.o
80+
asan_preinit.cpp.o
81+
)
82+
list(APPEND LIBCLANG_UBSAN_STRIP
83+
ubsan_init_standalone_preinit.cc.o
84+
ubsan_init_standalone_preinit.cpp.o
85+
)
86+
endif()
5887

59-
set(ASAN_DSO "libfuzzer_with_asan.so")
60-
set(UBSAN_DSO "libfuzzer_with_ubsan.so")
88+
set(ASAN_DSO "libfuzzer_with_asan${CMAKE_SHARED_LIBRARY_SUFFIX}")
89+
set(UBSAN_DSO "libfuzzer_with_ubsan${CMAKE_SHARED_LIBRARY_SUFFIX}")
6190

6291
GEN_BUILD_TARGET("asan"
6392
${LIBCLANG_ASAN_LIB}

cmake/SetClangRTLib.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ function(SetHwArchString outvar)
22
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
33
set(hw_arch "i386")
44
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
5-
set(hw_arch "x86_64")
5+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
6+
set(hw_arch "arm64")
7+
else()
8+
set(hw_arch "x86_64")
9+
endif()
610
else ()
711
message(FATAL_ERROR "Unsupported architecture.")
812
endif ()

luzer/CMakeLists.txt

Lines changed: 11 additions & 6 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.
17-
if(OSS_FUZZ)
15+
# Sanitizers libraries in the OSS Fuzz environment and macOS have
16+
# different names.
17+
if (DEFINED ENV{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)
@@ -39,10 +42,12 @@ add_compile_options(
3942
-Wextra
4043
-Wno-unused-parameter
4144
-Wpedantic
45+
# It turns out that macOS set _FORTIFY_SOURCE internally, so we
46+
# need to undefine it first, otherwise an error "'_FORTIFY_SOURCE'
47+
# macro redefined" breaks a building.
48+
-U_FORTIFY_SOURCE
49+
-D_FORTIFY_SOURCE=2
4250
)
43-
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
44-
add_compile_options(-D_FORTIFY_SOURCE=2)
45-
endif()
4651

4752
set(LUZER_SOURCES luzer.c
4853
compat.c

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)