Linux Memory detection tool :asan

ASAN(Address-Sanitizier) It was earlier LLVM Features in , After being added GCC 4.8, stay GCC 4.9 After adding the ARM Platform support . therefore GCC 4.8
Use above version ASAN You do not need to install a third-party library , By specifying the compilation at compile time CFLAGS You can turn on the switch .

1, Compile options

1.1 Gcc Compile options

# -fsanitize=address: Enable memory out of bounds detection

# -fsanitize-recover=address
: General background program to ensure stability , You can't simply exit if you encounter an error , It just keeps running , Using this option allows the program to continue running after a memory error , Overlay settings are required
ASAN_OPTIONS=halt_on_error=0 Will take effect ; If this option is not set , Then the memory error is reported to exit

ASAN_CFLAGS += -fsanitize=address -fsanitize-recover=address

 

# -fno-stack-protector: Enable stack overflow protection

# -fno-omit-frame-pointer: Enable stack overflow protection

# -fno-var-tracking: The default option is -fvar-tracking, Can cause very slow operation

# -g1: Represents the minimum debug information , usually debug Version with -g Namely -g2

ASAN_CFLAGS += -fno-stack-protector -fno-omit-frame-pointer -fno-var-tracking
-g1

1.2 Ld Link options

ASAN_LDFLAGS += -fsanitize=address -g1

If used gcc link , It can be ignored here .

2,ASAN Run options

2.1 ASAN_OPTIONS set up

ASAN_OPTIONS yes Address-Sanitizier Run options environment variable for .

# halt_on_error=0: Continue after detecting memory errors

# detect_leaks=1: Enable memory leak detection

# malloc_context_size=15: When a memory error occurs , The number of call stack layers displayed is 15

# log_path=/home/xos/asan.log: Memory check problem log storage file path

# suppressions=$SUPP_FILE: Block printing some memory errors

export
ASAN_OPTIONS=halt_on_error=0:use_sigaltstack=0:detect_leaks=1:malloc_context_size=15:log_path=/home/xos/asan.log:suppressions=$SUPP_FILE

 

In addition to the above common options , The following options can be added according to actual needs :

# detect_stack_use_after_return=1: Check access points to stack space that has been freed

# handle_segv=1: Processing segment error ; You can also add handle_sigill=1 handle SIGILL signal

# quarantine_size=4194304: Memory cache Cacheable free Memory size 4M

ASAN_OPTIONS=${ASAN_OPTIONS}:verbosity=0:handle_segv=1:allow_user_segv_handler=1:detect_stack_use_after_return=1:fast_unwind_on_fatal=1:fast_unwind_on_check=1:fast_unwind_on_malloc=1:quarantine_size=4194304

2.2 LSAN_OPTIONS set up

LSAN_OPTIONS yes LeakSanitizier Environment variables for run options , and LeakSanitizier yes ASAN Memory leak detection module based on , Common run options are :

# exitcode=0: Set the memory leak exit code to 0, Default memory leak exit code 0x16

# use_unaligned=4:4 byte alignment

export LSAN_OPTIONS=exitcode=0:use_unaligned=4

3, summary

In the actual development environment , It may exist gcc Low version , use asan When doing a memory check , Link required libasan.so Database . secondly , Platform software usually implements a set of internal memory operation interface , For the use of
asan tool , Need to be replaced with glibc Interface provided . here , It can be done through LD_PRELOAD Environment variables solve such problems .

export LD_PRELOAD= libasan.so.2:libprelib.so   #vos_malloc --> malloc

last ,ASAN There are many options , This paper will not list them one by one & explain , Readers can choose Baidu !

 

Technology
©2019-2020 Toolsou All rights reserved,
use PyMC3 Bayesian statistical analysis was performed ( code + example )el-ui:select Get value The project followed for a year , The customer finally said no experiment 11-1-6 Output string at specified position (20 branch )ajax get Request Chinese parameter garbled solution npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test_vue_0613@1.0.0 dev: Solutions to errors vue obtain el-select Of the selected item label value Summary of artificial intelligence algorithm vue Value transfer between parent-child and non parent-child components [RK3399][Android7.1] Learning notes DRM Driver development ( introduce )