Skip to content

Commit

Permalink
Proper asan and ubsan support
Browse files Browse the repository at this point in the history
Load supp files with ignores automatically.
Activate clang sanitizer by default and ignore it with a warning if
clang++ is not installed.
Also include timestamp and build commit hash in san logfile.

Closed #61
Closed ddnet-insta/ddnet-insta#86
  • Loading branch information
ChillerDragon committed Jan 14, 2024
1 parent d9f53b1 commit 6be9712
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions gdb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cache_logpath "$logfile"
# and curl throws a sigpipe
# the ddnet code catches that but gdb catches it first and then crashes the server
# TODO: make this a gdb options config or use a gdbrc file and also include that in loop_gdb
export COMMIT_HASH="$(get_commit)"
run_cmd="$CFG_ENV_RUNTIME gdb -ex='handle SIGPIPE nostop noprint pass' -ex=run --args ./\"$CFG_BIN\" \"exec autoexec.cfg;logfile $logfile;#sid:$SERVER_UUID\""
log "$run_cmd"
launch_commit="$(get_commit)"
Expand Down
65 changes: 59 additions & 6 deletions lib/env_san.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
#!/bin/sh
#!/bin/bash

# source lib/env_san.sh
# COMMIT_HASH=yourhash source lib/env_san.sh

# supp file paths are relative to the bin/ directory
lib_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

export UBSAN_OPTIONS=suppressions=../lib/supp/ubsan.supp:log_path=./SAN:print_stacktrace=1:halt_on_errors=0
export ASAN_OPTIONS=log_path=./SAN:print_stacktrace=1:check_initialization_order=1:detect_leaks=1:halt_on_errors=0
export LSAN_OPTIONS=suppressions=../lib/supp/lsan.supp
if [ "$lib_dir" = "" ]
then
echo "Error: failed to get lib dir"
exit 1
fi

if [ ! -d "$lib_dir" ]
then
echo "Error: lib dir '$lib_dir' is not a directory"
exit 1
fi

assert_file() {
[ -f "$1" ] && return
echo "Error: file not found '$1'"
exit 1
}

assert_dir() {
[ -d "$1" ] && return
echo "Error: directory not found '$1'"
exit 1
}

log_dir="$lib_dir"/../logs
logfile="$log_dir"/SAN_"${COMMIT_HASH:-null}"_"$(date '+%F_%H-%M')"

assert_file "$lib_dir"/supp/ubsan.supp
assert_file "$lib_dir"/supp/lsan.supp
assert_dir "$log_dir"

set_vars() {
if [ ! -x "$(command -v clang++)" ]
then
echo "[!] Warning: trying to to use sanitizer env but clang++ is not installed"
echo " check your env_runtime and env_build in your settings.cfg"
echo " make sure clang++ is in PATH"
echo ""
echo " ignoring the current env! The sanitizer will not be activated to avoid build errors"
echo ""
return
fi

# runtime
export UBSAN_OPTIONS=suppressions="$lib_dir"/supp/ubsan.supp:log_path="$logfile":print_stacktrace=1:halt_on_errors=0
export ASAN_OPTIONS=log_path="$logfile":print_stacktrace=1:check_initialization_order=1:detect_leaks=1:halt_on_errors=0
export LSAN_OPTIONS=suppressions="$lib_dir"/supp/lsan.supp

# compile time
export CC=clang
export CXX=clang++
export CXXFLAGS="-fsanitize=address,undefined -fsanitize-recover=address,undefined -fno-omit-frame-pointer"
export CFLAGS="-fsanitize=address,undefined -fsanitize-recover=address,undefined -fno-omit-frame-pointer"
}

set_vars
1 change: 1 addition & 0 deletions lib/include/crashsave_loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ logfile="$LOGS_PATH_FULL_TW/${CFG_SRV_NAME}_$(date +%F_%H-%M-%S)${CFG_LOG_EXT}"
start_ts_slug=$(date '+%Y-%m-%d_%H-%M-%S')
start_ts=$(date '+%Y-%m-%d %H:%M:%S')

export COMMIT_HASH="$(get_commit)"
run_cmd="$CFG_ENV_RUNTIME ./$CFG_BIN 'exec autoexec.cfg;logfile $logfile;#sid:$SERVER_UUID:loop_script'"
log "running:"
tput bold
Expand Down
1 change: 1 addition & 0 deletions lib/include/gdb_loop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ then
log "custom gdb command '$custom_gdb'"
fi

export COMMIT_HASH="$(get_commit)"
read -rd '' GDB_CMD << EOF
$CFG_ENV_RUNTIME gdb -ex='set confirm off' \
-ex='set pagination off' \
Expand Down
4 changes: 2 additions & 2 deletions lib/include/vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def_var 'CFG_GIT_BRANCH' 'git_branch' "" ''
def_var 'CFG_SERVER_TYPE' 'server_type' "teeworlds" '(tem|teeworlds)'
def_var 'CFG_TEM_SETTINGS' 'tem_settings' "tem.settings" ''
def_var 'CFG_TEM_PATH' 'tem_path' "/home/$USER/git/TeeworldsEconMod" ''
def_var 'CFG_ENV_BUILD' 'env_build' "" ''
def_var 'CFG_ENV_RUNTIME' 'env_runtime' "" ''
def_var 'CFG_ENV_BUILD' 'env_build' "source ./lib/env_san.sh;" '.*(=.*|;$)'
def_var 'CFG_ENV_RUNTIME' 'env_runtime' "source ./lib/env_san.sh;" '.*(=.*|;$)'
def_var 'CFG_LOG_EXT' 'logfile_extension' ".log" ''
def_var 'CFG_AUTO_CLEANUP_OLD_LOCAL_DATA' 'auto_cleanup_old_local_data' "0" '(0|no|off|false|1|yes|on|true)'
def_var 'CFG_GITPATH_ANTIBOT' 'gitpath_antibot' "" ''
Expand Down
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ else # teeworlds
logfile="$LOGS_PATH_FULL/${CFG_SRV_NAME}_$(date +%F_%H-%M-%S)${log_ext}"
cache_logpath "$logfile"

export COMMIT_HASH="$(get_commit)"
run_cmd="$CFG_ENV_RUNTIME nohup ./$CFG_BIN \"exec autoexec.cfg;#sid:$SERVER_UUID\" > $logfile 2>&1 &"
if [ "$arg_is_interactive" == "1" ]
then
Expand Down

0 comments on commit 6be9712

Please sign in to comment.