# Testing libc++ with -fsanitize=undefined

DevFeed: [Testing libc++ with -fsanitize=undefined](<https://devfeed.tech/articles/testing-libc-with-fsanitize-undefined-42881.md>)

Original publisher: [Read original article](<https://blog.llvm.org/2013/04/testing-libc-with-fsanitizeundefined.html>)

Author: Marshall Clow

Published: 2013-04-01T10:12:00Z

Content type: tutorial

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [clang](<https://devfeed.tech/topics/clang.md>), [undefined\_behavior](<https://devfeed.tech/topics/undefined-behavior.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [LLVM](<https://devfeed.tech/topics/llvm.md>)

Tags: [built-from-source](<https://devfeed.tech/tags/built-from-source.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [clang](<https://devfeed.tech/tags/clang.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [integer-overflow](<https://devfeed.tech/tags/integer-overflow.md>), [sanitizer](<https://devfeed.tech/tags/sanitizer.md>), [testing](<https://devfeed.tech/tags/testing.md>), [undefined-behavior](<https://devfeed.tech/tags/undefined-behavior.md>)

## AI overview

This article explains testing libc++ with Clang's Undefined Behavior Sanitizer (UBSan). It describes enabling UBSan in the compiler and linker, building compatible libc++ and libc++abi libraries from source, and the test results, including runtime errors and failing tests.

## Source excerpt

[This article is re-posted in a slightly expanded form from Marshall's blog] After my last article, Testing libc++ with Address Sanitizer, I thought "what other tests can I run?" Address Sanitizer (ASan) is not the only "sanitizer" that clang offers. There are "Thread Sanitizer" (TSan), "Undefined Behavior Sanitizer" (UBSan), and others. There's an integer overflow sanitizer which is called IOC coming in the 3.3 release of clang. The documentation for UBSan can be found on the LLVM site. I have been looking at the results of running the libc++ test suite with UBSan enabled. Even if you're not interested in libc++ specifically, this post can be a useful introduction to useful Clang bug detectors, and shows several classes of problems they can find. The mechanicsLike ASan, UBSan is a compiler pass and a custom runtime library. You enable this by passing -fsanitize=undefined to the compiler and linker. I ran the libc++ test suite like this: cd $LLVM/libcxx/test CC=/path/to/tot/clang OPTIONS="--std=c++11 -stdlib=libc++ -fsanitize=undefined" ./testit Unfortunately, this failed; working with unreleased compilers and libraries, I needed updated versions of both libc++.dylib and libc++abi.dylib. So I built those from sources, and then used DYLD_LIBRARY_PATH to make sure that the test program used the libraries that I'd just built. (I didn't want to replace the ones in /usr/lib, because lots of things in the system depend on them) cd $LLVM/libcxx/test DYLD_LIBRARY_PATH=$LLVM/libcxx/lib:$LLVM/libcxxabi/lib CC=/path/to/tot/clang OPTIONS="-std=c++11 -stdlib=libc++ -fsanitize=undefined -L $LLVM/libcxxabi/lib -lc++abi" ./testit where, as before "/path/to/tot/clang" is the clang that I just built from source, and $LLVM is where I've checked out the various parts of LLVM from Subversion. The resultsAnd the tests were off and running. In the last article, I noted that these tests take about 30 minutes to run on my MacBook Pro. The ASan tests took about 90 minutes. I was pleasantly s