Building asan as a shared library

1,495 views
Skip to first unread message

Marshall Greenblatt

unread,
Nov 26, 2014, 7:44:41 PM11/26/14
to chromium-dev, gli...@chromium.org
Hi All,

I would like to use AddressSanitizer (asan) with my Chromium-based application on Linux (Ubuntu 14.04 64-bit) which is built as a shared library and loaded into Java via JNI. This doesn't work with Chromium's static asan build due to java being a separate executable. It seems that I need to build asan as a shared library and specify it via LD_PRELOAD [1]. Asan binaries are currently checked out as static libraries in third_party/llvm-build/Release+Asserts/lib/clang/3.6.0/lib/linux. How should I go about creating a shared asan build and configuring Chromium to link against it?

Thanks,
Marshall

Alexander Potapenko

unread,
Nov 27, 2014, 3:08:34 AM11/27/14
to Marshall Greenblatt, chromium-dev
Hi Marshall,

First, you need to build Clang yourself, because the Chromium-shipped
one doesn't contain the shared runtime. Please refer to
https://code.google.com/p/address-sanitizer/wiki/HowToBuild for the
instructions. You may want to use the Clang revision listed in
Chromium's tools/clang/scripts/update.sh, which is considered stable.
Next, you'll need to add the "-shared-libasan" flag to
build/common.gypi in the places where "-fsanitize=address" is passed
to the linker (keep "-fsanitize=address").
I think this should be enough to build Chrome with asan=1.

Let me know if you have other questions,
Alex
--
Alexander Potapenko
Software Engineer
Google Moscow

Marshall Greenblatt

unread,
Nov 27, 2014, 10:35:53 PM11/27/14
to Alexander Potapenko, chromium-dev
On Thu, Nov 27, 2014 at 3:07 AM, Alexander Potapenko <gli...@chromium.org> wrote:
Hi Marshall,

First, you need to build Clang yourself, because the Chromium-shipped
one doesn't contain the shared runtime. Please refer to
https://code.google.com/p/address-sanitizer/wiki/HowToBuild for the
instructions. You may want to use the Clang revision listed in
Chromium's tools/clang/scripts/update.sh, which is considered stable.
Next, you'll need to add the "-shared-libasan" flag to
build/common.gypi in the places where "-fsanitize=address" is passed
to the linker (keep "-fsanitize=address").
I think this should be enough to build Chrome with asan=1.

Thanks! I added "-DCOMPILER_RT_BUILD_SHARED_ASAN=ON" to the cmake command lines in tools/clang/scripts/update.sh and built clang locally as follows:

$ ./update.sh --force-local-build --without-android 

This generated libclang_rt.asan-x86_64.so in the third_party/llvm-build/Release+Asserts/lib/clang/3.6.0/lib/linux directory.

I then needed to do the following for Chromium to build with asan=1:

1. Copy libclang_rt.asan-x86_64.so to the out/Release/lib directory.

2. Add '-shared-libasan' (or modify exclusions) in build/common.gypi, build/sanitizers/sanitizers.gyp, sandbox/linux/sandbox_linux.gypi and third_party/libvpx/libvpx.gyp.

Marshall Greenblatt

unread,
Nov 27, 2014, 10:51:58 PM11/27/14
to Alexander Potapenko, chromium-dev
On Thu, Nov 27, 2014 at 10:35 PM, Marshall Greenblatt <magree...@gmail.com> wrote:
On Thu, Nov 27, 2014 at 3:07 AM, Alexander Potapenko <gli...@chromium.org> wrote:
Hi Marshall,

First, you need to build Clang yourself, because the Chromium-shipped
one doesn't contain the shared runtime. Please refer to
https://code.google.com/p/address-sanitizer/wiki/HowToBuild for the
instructions. You may want to use the Clang revision listed in
Chromium's tools/clang/scripts/update.sh, which is considered stable.
Next, you'll need to add the "-shared-libasan" flag to
build/common.gypi in the places where "-fsanitize=address" is passed
to the linker (keep "-fsanitize=address").
I think this should be enough to build Chrome with asan=1.

Thanks! I added "-DCOMPILER_RT_BUILD_SHARED_ASAN=ON" to the cmake command lines in tools/clang/scripts/update.sh and built clang locally as follows:

$ ./update.sh --force-local-build --without-android 

This generated libclang_rt.asan-x86_64.so in the third_party/llvm-build/Release+Asserts/lib/clang/3.6.0/lib/linux directory.

I then needed to do the following for Chromium to build with asan=1:

1. Copy libclang_rt.asan-x86_64.so to the out/Release/lib directory.

2. Add '-shared-libasan' (or modify exclusions) in build/common.gypi, build/sanitizers/sanitizers.gyp, sandbox/linux/sandbox_linux.gypi and third_party/libvpx/libvpx.gyp.

Using this build with Java shows some assertions in asan like:

AddressSanitizer CHECK failed: /home/marshall/code/chromium_git/chromium/src/third_party/llvm/compiler-rt/lib/asan/asan_globals.cc:117 "((AddrIsAlignedByGranularity(g->beg))) != (0)" (0x0, 0x0)
    #0 0x7fc5ed8ac3de in __asan::AsanCheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) _asan_rtl_:0
    #1 0x7fc5ed8b19c3 in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) /home/marshall/code/chromium_git/chromium/src/third_party/llvm/compiler-rt/lib/sanitizer_common/sanitizer_common.cc:74:0
    #2 0x7fc5ed835a40 in RegisterGlobal _asan_rtl_:0
    #3 0x7fc5ed835a40 in __asan_register_globals _asan_rtl_:0
    #4 0x7fc5d8865186 in asan.module_ctor ??:0:0

It's likely necessary to also disable globals instrumentation with this build by specifying '-mllvm -asan-globals=0' as described here: https://code.google.com/p/address-sanitizer/issues/detail?id=82#c18.
Reply all
Reply to author
Forward
0 new messages