Skip to content

Commit 17d7c86

Browse files
authored
Merge pull request #141 from yeshan333/master
ci: utilizing AddressSanitizer to automate the detection of memory issues.
2 parents a4065d9 + e4379db commit 17d7c86

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

.github/asan_assets/dlclose.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// This file is used to resolve an issue where the dynamic library is closed prematurely, causing asan to report that it cannot retrieve symbols. <unknown module>
2+
// NOTICE: Do not use in a production environment
3+
#include <stdio.h>
4+
5+
#if defined(__APPLE__)
6+
#include <dlfcn.h>
7+
8+
// from: https://github.com/apple-oss-distributions/dyld/blob/c8a445f88f9fc1713db34674e79b00e30723e79d/include/mach-o/dyld-interposing.h#L43
9+
#define DYLD_INTERPOSE(_replacement,_replacee) \
10+
__attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee \
11+
__attribute__ ((section ("__DATA,__interpose,interposing"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee };
12+
13+
int mydlclose(void * __handle) {
14+
(void)__handle; // [[maybe_unused]] is not in C standard yet
15+
return 0;
16+
}
17+
18+
// dyld-interposing
19+
DYLD_INTERPOSE(mydlclose, dlclose);
20+
#else
21+
// This function prevents unloading of shared libraries, thus the sanitizers【BUG】
22+
// can always find the address that belonged to a shared library.
23+
// See https://github.com/google/sanitizers/issues/89#issuecomment-484435084
24+
// test pass: https://github.com/KatanaGraph/katana/pull/402/files#diff-46402e2a4674871c253dd33ff206252619d7d8890d477f76100f8eecf9071d3a
25+
int dlclose(void* ptr) {
26+
(void)ptr; // [[maybe_unused]] is not in C standard yet
27+
return 0;
28+
}
29+
#endif
30+

.github/asan_assets/test_mem.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
make clean
4+
5+
if [[ "$OSTYPE" == "darwin"* ]]; then
6+
# The version of Clang that comes pre-installed with MacOS is outdated.
7+
brew install llvm
8+
LLVM_SDK_PATH=$(brew --prefix llvm)
9+
if [ -z "$LLVM_SDK_PATH" ]; then
10+
echo "Error: please run command: brew install llvm."
11+
exit 1
12+
fi
13+
14+
make LUAINC="-I/usr/local/include/" CC="$LLVM_SDK_PATH/bin/clang" CFLAGS="-fsanitize=address -g -Wall"
15+
export ASAN_OPTIONS=detect_leaks=1:fast_unwind_on_malloc=false
16+
ASAN_LIB_ABS_PATH=$("$LLVM_SDK_PATH"/bin/clang -print-file-name=libclang_rt.asan_osx_dynamic.dylib)
17+
"$LLVM_SDK_PATH"/bin/clang -dynamiclib .github/asan_assets/dlclose.c -o ./.github/asan_assets/libdlclose.dylib -install_name libdlclose.dylib
18+
# export DYLD_PRINT_LIBRARIES=1 X=1
19+
export DYLD_INSERT_LIBRARIES="./.github/asan_assets/libdlclose.dylib:$ASAN_LIB_ABS_PATH"
20+
21+
lua test.lua
22+
else
23+
make LUAINC="-I/usr/local/include/" CFLAGS="-fsanitize=address -g -Wall"
24+
export ASAN_OPTIONS=fast_unwind_on_malloc=false
25+
ASAN_LIB_ABS_PATH=$(gcc -print-file-name=libasan.so)
26+
gcc -Wl,-undefined,dynamic_lookup --shared .github/asan_assets/dlclose.c -o .github/asan_assets/libdlclose.so
27+
export LD_PRELOAD="$ASAN_LIB_ABS_PATH:./.github/asan_assets/libdlclose.so"
28+
29+
lua test.lua
30+
fi

.github/workflows/asan.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Asan Check
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
mem_test:
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
# ref: https://github.com/actions/runner-images
14+
os: [ubuntu-20.04, macos-13]
15+
luaVersion: ["5.4.7", "5.4.6", "5.4.5"]
16+
runs-on: ${{ matrix.os }}
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: install lua
20+
run: |
21+
wget http://www.lua.org/ftp/lua-${{ matrix.luaVersion }}.tar.gz
22+
tar -xzvf lua-${{ matrix.luaVersion }}.tar.gz
23+
cd lua-${{ matrix.luaVersion }}
24+
sudo make
25+
sudo make install
26+
sudo cp src/*.h /usr/local/include/
27+
- name: run asan check
28+
run: |
29+
bash .github/asan_assets/test_mem.sh

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build/*
33
*.so
44
*.dSYM
55
*.dll
6+
*.dylib

0 commit comments

Comments
 (0)