3 ms·
How do I use the sanitizers with freestanding targets? They seem to require a lot of library support which is unavailable on freestanding programs. The article
by matheusmoreira 4y ago
How do I use the sanitizers with freestanding targets? They seem to require a lot of library support which is unavailable on freestanding programs.
The article mentions the address sanitizer keeps track of allocations and their sizes. I wrote my own allocator, how do I integrate it with the sanitizer? I added malloc attributes to all allocation functions but they don't seem to do anything.
- lionkor 4y agoyeah youd need to write some code to integrate it, i believe. I think Andreas Kling did it for SerenityOS, and has a video on it on his channel
- zyedidia 4y agoYou need to provide implementations of the sanitizer callbacks yourself. For example, to use the address sanitizer you should use `-fsanitize=kernel-address`, and then the compiler will generate calls to `__asan_load8_noabort`, `__asan_load4_noabort`, `__asan_store8_noabort`... when loads/stores happen. In those functions, you need to implement the sanitizer yourself, and have it panic if it detects an invalid access (address sanitizers are usually implemented with shadow memory). You'll have to give your sanitizer enough shadow memory during initialization, and also add your own alloc/free sanitizer callbacks to your allocator (so the sanitizer knows when things are allocated/freed). I have an example [1] that implements a basic ASAN and UBSAN in a kernel (it's written in D, but could be easily adapted to C or C++). Hopefully it's helpful! [1]: https://github.com/zyedidia/multiplix/blob/master/kernel/sanitizer.d https://github.com/zyedidia/multiplix/blob/master/kernel/san...
- ebanorafael 4y agoAdding to other replies https://mcuoneclipse.com/2021/05/31/finding-memory-bugs-with-google-address-sanitizer-asan-on-microcontrollers/ https://mcuoneclipse.com/2021/05/31/finding-memory-bugs-with... https://interrupt.memfault.com/blog/ubsan-trap https://interrupt.memfault.com/blog/ubsan-trap I've tested the UBSan, it worked with some limitations but helped me catch some old hidden bugs.