Add ALSan(Attachable Leak Sanitizer) feature on libbpf-tools
ALSan feature originally comes from the llvm-project lsan
https://github.com/llvm/llvm-project
This tool detect and report unreachable memory periodically
USAGE:
$ ./alsan -h
Usage: alsan [OPTION...]
Detect memory leak resulting from unreachable pointers.
Either -c or -p is a mandatory option
EXAMPLES:
alsan -p 1234 # Detect leaks on process id 1234
alsan -c a.out # Detect leaks on a.out
alsan -c 'a.out arg' # Detect leaks on a.out with argument
-c, --command=COMMAND Execute and detect memory leak on the specified
command
-i, --interval=INTERVAL Set interval in second to detect leak
-p, --pid=PID Detect memory leak on the specified process
-s, --suppressions=SUPPRESSIONS
Suppressions file name
-T, --top=TOP Report only specified amount of backtraces
-v, --verbose Verbose debug output
-w, --stop-the-world Stop the target process during tracing
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
Report bugs to https://github.com/iovisor/bcc/tree/master/libbpf-tools.
Report example:
$ sudo ./alsan -p 28346
[2024-05-22 14:44:58] Print leaks:
44 bytes direct leak found in 1 allocations from stack id(57214)
iovisor#1 0x00583bca1b2250 baz+0x1c (/home/bojun/alsan/libbpf-tools/a.out+0x1250)
iovisor#2 0x00583bca1b22d7 main+0x73 (/home/bojun/alsan/libbpf-tools/a.out+0x12d7)
iovisor#3 0x007470c7c2a1ca [unknown] (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
iovisor#4 0x007470c7c2a28b __libc_start_main+0x8b (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
iovisor#5 0x00583bca1b2105 _start+0x25 (/home/bojun/alsan/libbpf-tools/a.out+0x1105)
[2024-05-22 14:45:08] Print leaks:
132 bytes direct leak found in 3 allocations from stack id(57214)
iovisor#1 0x00583bca1b2250 baz+0x1c (/home/bojun/alsan/libbpf-tools/a.out+0x1250)
iovisor#2 0x00583bca1b22d7 main+0x73 (/home/bojun/alsan/libbpf-tools/a.out+0x12d7)
iovisor#3 0x007470c7c2a1ca [unknown] (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
iovisor#4 0x007470c7c2a28b __libc_start_main+0x8b (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
iovisor#5 0x00583bca1b2105 _start+0x25 (/home/bojun/alsan/libbpf-tools/a.out+0x1105)
Source code of test program:
$ cat leak_test.c
#include <stdlib.h>
#include <unistd.h>
int *arr[10000];
int *foo(size_t size) {
int *tmp = malloc(size);
*tmp = 99;
return tmp;
}
int *bar(size_t nmemb, size_t size) {
int *tmp = calloc(nmemb, size);
*tmp = 22;
return tmp;
}
int *baz(size_t size) {
int *tmp = valloc(size);
*tmp = 11;
return tmp;
}
int main(int argc, char* argv[]) {
int *a;
int i = 0;
while (1) {
a = foo(4);
arr[i++] = a;
a = bar(4, 4);
free(a);
a = baz(44);
sleep(5);
}
return 0;
}