Compiling with address sanitizer

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
danielfa
Posts: 1
Joined: Thu May 21, 2020 10:21 am

Compiling with address sanitizer

Post by danielfa » Thu May 21, 2020 10:31 am

Hello!

I am working on a c module and would like to use GCC's address sanitizer and undefined behaviour sanitizer to verify the quality of my code. I tried to add it in the flags for my module:

Code: Select all

// File: micropython.mk

// [...]

CFLAGS_USERMOD += -fsanitize=address -fsanitize=undefined
LDFLAGS_USERMOD += -fsanitize=address -fsanitize=undefined
This builds fine but when I run the interpreter (built for host) I get the following message printed when exiting the interpreter:
==12703==LeakSanitizer has encountered a fatal error.
==12703==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==12703==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)
Or with more verbose logging:
==12705==AddressSanitizer: failed to intercept 'printf'
==12705==AddressSanitizer: failed to intercept 'snprintf'
==12705==AddressSanitizer: failed to intercept 'vprintf'
==12705==AddressSanitizer: failed to intercept 'vsnprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_printf'
==12705==AddressSanitizer: failed to intercept '__isoc99_sprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_snprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_fprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_vprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_vsprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_vsnprintf'
==12705==AddressSanitizer: failed to intercept '__isoc99_vfprintf'
==12705==AddressSanitizer: libc interceptors initialized
|| `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
|| `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
|| `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
|| `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
|| `[0x000000000000, 0x00007fff7fff]` || LowMem ||
MemToShadow(shadow): 0x00008fff7000 0x000091ff6dff 0x004091ff6e00 0x02008fff6fff
redzone=16
max_redzone=2048
quarantine_size_mb=256M
malloc_context_size=30
SHADOW_SCALE: 3
SHADOW_GRANULARITY: 8
SHADOW_OFFSET: 0x7fff8000
==12705==Installed the sigaction for signal 11
==12705==Installed the sigaction for signal 7
==12705==Installed the sigaction for signal 8
==12705==T0: stack [0x7fffb91d5000,0x7fffb99d5000) size 0x800000; local=0x7fffb99d3304
==12705==LeakSanitizer: Dynamic linker not found. TLS will not be handled correctly.
==12705==AddressSanitizer Init done
<class 'ValueError'> Failed to decode file: Failed to stat file
<class 'ValueError'> Failed to write file: Is a directory
Not a JPEG file: starts with 0x50 0x36
==12706==Could not attach to thread 12705 (errno 1).
==12706==Failed suspending threads.
==12705==LeakSanitizer has encountered a fatal error.
==12705==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==12705==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)
Are we using ptrace somehow in micropython? Has anyone else managed to build micropython with address sanitizer, or how do you usually look for memory leaks?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Compiling with address sanitizer

Post by dhylands » Thu May 21, 2020 7:31 pm

Micropython doesn't have a traditional heap, like most C programs would.

It's heap is designed to be garbage collected, so the definiition of a leak will be a bit different.

The way the heap works is that you allocate something and then there is no longer a pointer to that object then it will get freed the next time the heap is collected.

So even if you could get it to link, I'm not sure it would give you the results you're looking for.

Post Reply