Skip to content

Commit

Permalink
Merge pull request #141 from yeshan333/master
Browse files Browse the repository at this point in the history
ci: utilizing AddressSanitizer to automate the detection of memory issues.
  • Loading branch information
cloudwu authored Jan 16, 2025
2 parents a4065d9 + e4379db commit 17d7c86
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/asan_assets/dlclose.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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>
// NOTICE: Do not use in a production environment
#include <stdio.h>

#if defined(__APPLE__)
#include <dlfcn.h>

// from: https://github.com/apple-oss-distributions/dyld/blob/c8a445f88f9fc1713db34674e79b00e30723e79d/include/mach-o/dyld-interposing.h#L43
#define DYLD_INTERPOSE(_replacement,_replacee) \
__attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee \
__attribute__ ((section ("__DATA,__interpose,interposing"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee };

int mydlclose(void * __handle) {
(void)__handle; // [[maybe_unused]] is not in C standard yet
return 0;
}

// dyld-interposing
DYLD_INTERPOSE(mydlclose, dlclose);
#else
// This function prevents unloading of shared libraries, thus the sanitizers【BUG】
// can always find the address that belonged to a shared library.
// See https://github.com/google/sanitizers/issues/89#issuecomment-484435084
// test pass: https://github.com/KatanaGraph/katana/pull/402/files#diff-46402e2a4674871c253dd33ff206252619d7d8890d477f76100f8eecf9071d3a
int dlclose(void* ptr) {
(void)ptr; // [[maybe_unused]] is not in C standard yet
return 0;
}
#endif

30 changes: 30 additions & 0 deletions .github/asan_assets/test_mem.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

make clean

if [[ "$OSTYPE" == "darwin"* ]]; then
# The version of Clang that comes pre-installed with MacOS is outdated.
brew install llvm
LLVM_SDK_PATH=$(brew --prefix llvm)
if [ -z "$LLVM_SDK_PATH" ]; then
echo "Error: please run command: brew install llvm."
exit 1
fi

make LUAINC="-I/usr/local/include/" CC="$LLVM_SDK_PATH/bin/clang" CFLAGS="-fsanitize=address -g -Wall"
export ASAN_OPTIONS=detect_leaks=1:fast_unwind_on_malloc=false
ASAN_LIB_ABS_PATH=$("$LLVM_SDK_PATH"/bin/clang -print-file-name=libclang_rt.asan_osx_dynamic.dylib)
"$LLVM_SDK_PATH"/bin/clang -dynamiclib .github/asan_assets/dlclose.c -o ./.github/asan_assets/libdlclose.dylib -install_name libdlclose.dylib
# export DYLD_PRINT_LIBRARIES=1 X=1
export DYLD_INSERT_LIBRARIES="./.github/asan_assets/libdlclose.dylib:$ASAN_LIB_ABS_PATH"

lua test.lua
else
make LUAINC="-I/usr/local/include/" CFLAGS="-fsanitize=address -g -Wall"
export ASAN_OPTIONS=fast_unwind_on_malloc=false
ASAN_LIB_ABS_PATH=$(gcc -print-file-name=libasan.so)
gcc -Wl,-undefined,dynamic_lookup --shared .github/asan_assets/dlclose.c -o .github/asan_assets/libdlclose.so
export LD_PRELOAD="$ASAN_LIB_ABS_PATH:./.github/asan_assets/libdlclose.so"

lua test.lua
fi
29 changes: 29 additions & 0 deletions .github/workflows/asan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Asan Check

on:
push:
pull_request:
workflow_dispatch:

jobs:
mem_test:
strategy:
fail-fast: false
matrix:
# ref: https://github.com/actions/runner-images
os: [ubuntu-20.04, macos-13]
luaVersion: ["5.4.7", "5.4.6", "5.4.5"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: install lua
run: |
wget http://www.lua.org/ftp/lua-${{ matrix.luaVersion }}.tar.gz
tar -xzvf lua-${{ matrix.luaVersion }}.tar.gz
cd lua-${{ matrix.luaVersion }}
sudo make
sudo make install
sudo cp src/*.h /usr/local/include/
- name: run asan check
run: |
bash .github/asan_assets/test_mem.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build/*
*.so
*.dSYM
*.dll
*.dylib

0 comments on commit 17d7c86

Please sign in to comment.