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

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



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


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 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=   #vos_malloc --> malloc

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


©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library )Unity3D of UGUI Basics -- Three modes of canvas os Simple use of module computer network --- Basic concepts of computer network ( agreement , system )html Writing about cherry trees , Writing about cherry trees Some East 14 Pay change 16 salary , Sincerity or routine ?Unity-Demo Examples ✨ realization UI- Backpack equipment drag function 06【 Interpretation according to the frame 】 Data range filtering -- awesome java Four functional interfaces ( a key , simple )