Skip to main content

Extend your list of debug tools with Sanitizers

How many ways to check if code is OK do you know? Probably the one would make a list with debuggers, logging, static/dynamic checkers, profilers and some other tools. The tools the one has in the list are there cause they've been checked with a time. They don't cover all the possible cases of mistakes that could be made during coding phase of a project, but they cover necessary minimum. Today we will try to extend our tool set with a young tools that already got into clang and if you have clang on your machine you can give it a try.




The tools I'm talking about are called sanitizers. The idea of sanitizers was introduced by Google developers. On one of main conference a developer showed sanitizers and fuzz testing solution they were applying to a project they were working on. I was astonished by how well they've decompounded the process of development and analyzed all of it's parts. They developed tools to detect bugs in their code and get rid of it during all phases of development of the project.

In this article I'm describing sanitizers and somewhere in the future I'll write an article on fuzz testing.

So let's see what is sanitizers and how to use them, shall we?

Sanitizers are set of libraries that covers a ranges of bugs and errors that could occur, while developing a piece of code. Sanitizers were developed by google and currently can be obtained from official github repository.

There is AddressSanitizer, LeakSanitizer, ThreadSanitizer and MemorySanitizer.

AddressSanitizer (aka asan) allows to detect memory related issues such as:
  • usage of pointer after being freed
  •  heap buffer overflow
  • stack buffer overflow
  • global buffer overflow
  • use after return
  • use after scope
  • initialization order bugs
  • memory leaks
The tool is really fast so if you have some problems with memory - give it a try. It's a part of LLVM starting with 3.1 and part of GCC starting 4.8.

Usage is pretty simple - apply a -fsanitize=address flag on compilation to turn on the code.

LeakSanitizer (aka lsan) this one became a part of asan but there is a standalone mode which can be turned on by applying -fsanitize=leak.

ThreadSanitizer (aka tsan) checks for race conditions. It's a part of LLVM 3.2 and part of GCC 4.8. Slowdown can be huge - 2-20x, but it varies of project it's applied to. To apply it use -fsanitize=thread with -O1 in order you have reasonable performance.

MemorySanitizer (aka msan) detects uninitialized memory read attempts. For example if value is read before it is written. At the time of the article was created msan is a part of LLVM 3.3. The one works pretty fast - about 1.5-2.5x slowdown. This sanitizer is significantly faster than Memcheck tool. To use it you have to provide -fsanitize=memory -fPIE -pie to a compiler.

Sanitizers library grant a huge support in finding some tricky problems and let your code be a bit more safer and faster.

Popular posts from this blog

Embrace the changes

“Change is the law of life. And those who look only to the past or present are certain to miss the future.” —John F. Kennedy I want to write a couple things on changes that happen all the time and which we don't notice or choose to do so. Not sure what they teach children in your country but in Belarus of 90-00s there was not much interest in a child that had some interests in topic that was out of school programs. And it made me into self-harming youngster who didn't know what to do and where to go. For some times I felt like there were no changes around me, which was faulty way of thinking. So that approach rooted in me until I've got into a University. People there had a lot of interests so different of mine that all first year I've been stunned. It was so hard for me to get it - people, that have will, do embrace changes and adapt to it to go further with their plans.

What was so alien for me back then, became a most important force in my life now. To be able to mo…

OpenGL: how to start studying?

I've been studying OpenGL for about 2 years, but I haven't got far with it and had no idea what is wrong. Is it me unable to understand OpenGL abstractions or is it designed not very well? I was looking for an answer all these years when suddenly I've started getting into math since my job required some math skills and suddenly OpenGL started to make sense for me. I ain't trying to say that now it's fun and games - honestly I find it challenging, but not as bad as before.

Templates and how to fold them

Variadic templates appeared in C++11 to cover such cases when you would have a template functions that could have a numerous members of different types. Doesn't it remind you of variadic functions that uses va_start, va_arg, va_end and so others? Cause it should be.