2trill2spill 2trill2spill - 2 months ago 20
C Question

linking clang address sanitizer on FreeBSD 10.1 Release

I'm trying to get address sanitizer working on FreeBSD 10.1 Release, but whenever I try to compile a program with

-fsanitize=address
I get undefined references to
_asan_stack_malloc_1
etc, etc. I found

https://forums.freebsd.org/threads/gcc-clang-address-sanitizer.47985/ on google but the suggestion of adding
-L/usr/local/lib -I/usr/local/include
didn't resolve the linking issue. I tried the llvm binaries for FreeBSD but when I go to compile with that clang I get
/usr/bin/../lib/clang/3.6.0/lib/freebsd/libclang_rt.asan-x86_64.a , no such file or directory.
. Either way I'm not sure what library I need to link or where it is.

Below is the program I tried compiling and here is the command I used,
clang -fsanitize=address san.c


#include <stdio.h>

int main(void)
{
return 0;
}

Answer

To use asan on FreeBSD you can build llvm with asan support as shown below or you can install from packages/ports like in Kevinoid's answer.

Step one, grab the latest stable llvm source.

fetch http://llvm.org/releases/3.9.0/llvm-3.9.0.src.tar.xz

Now uncompress the llvm source directory.

tar -xvf llvm-3.9.0.src.tar.xz

Next change directory to llvm and grab the clang source files.

cd llvm-3.9.0.src/tools && fetch http://llvm.org/releases/3.9.0/cfe-3.9.0.src.tar.xz

Uncompress clang.

tar -xvf cfe-3.9.0.src.tar.xz

Enter the projects directory and grab compiler-rt.

cd ../projects && fetch http://llvm.org/releases/3.9.0/compiler-rt-3.9.0.src.tar.xz

Uncompress compiler-rt.

tar -xvf compiler-rt-3.9.0.src.tar.xz

Goto the root llvm directory and make a build directory for cmake.

cd ../ && mkdir build && cd build

Use cmake to setup the llvm build.

cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON [-DLLVM_ENABLE_WERROR=ON] [-DLLVM_TARGETS_TO_BUILD=X86] -DBUILD_SHARED_LIBS=ON ../

Build llvm and go grab some tea, it will take a bit.

make -j12

If the build worked you should be left with clang with asan support. Make sure to remove the old compiler, /usr/bin/clang, /usr/bin/clang++ and /usr/bin/cc. Then install the new clang by doing sudo make install. Finally you will probably want to link /usr/bin/cc to /usr/local/bin/clang by running sudo ln /usr/local/bin/clang /usr/bin/cc.

After doing all these steps you should be able to compile code with the -fsanitize=address compile option.