Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MacOS - clang address sanitizer fails to link

I have been trying to use Clang's address code sanitizer, but the linker will not have it. The linker may be "ld", though my CMAKE settings assure me that clang is the linker.

Added -fsanitize=address to compiler and linker flags.

Error:

Undefined symbols for architecture x86_64:
___asan_after_dynamic_init
...
___asan_before_dynamic_init
...
etc.
ld: symbol(s) not found for architecture x86_64    <<<< **suspicious**
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • Environment: MacOS
  • clang: Apple LLVM version 8.0.0 (clang-800.0.38)
  • cmake: 3.7.1

  • CMAKE_CXX_COMPILER = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (redirects to clang)

  • CMAKE_CXX_COMPILER_ID = Clang
  • Compiler Flags: -O0 -g -fsanitize=address
  • CMAKE_LINKER = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ (redirects to clang)
  • CMAKE_CXX_LINK_EXECUTABLE = CMAKE_CXX_COMPILER FLAGS CMAKE_CXX_LINK_FLAGS LINK_FLAGS OBJECTS -o TARGET LINK_LIBRARIES
  • CMAKE_CXX_LINK_FLAGS = -Wl,-search_paths_first -Wl,-headerpad_max_install_names -fsanitize=address -v
like image 616
iseale Avatar asked Oct 24 '17 00:10

iseale


People also ask

What is AddressSanitizer error?

Introduction. AddressSanitizer is a fast memory error detector. It consists of a compiler instrumentation module and a run-time library. The tool can detect the following types of bugs: Out-of-bounds accesses to heap, stack and globals.

How does the AddressSanitizer work?

AddressSanitizer dedicates one-eighth of the virtual address space to its shadow memory and uses a direct mapping with a scale and offset to translate an applica- tion address to its corresponding shadow address.

What is AddressSanitizer in GCC?

Address Sanitizer is a tool developed by Google detect memory access error such as use-after-free and memory leaks. It is built into GCC versions >= 4.8 and can be used on both C and C++ codes.

How do I turn off AddressSanitizer?

AddressSanitizer can optionally detect stack use after return problems. This is available by default, or explicitly ( -fsanitize-address-use-after-return=runtime ). To disable this check at runtime, set the environment variable ASAN_OPTIONS=detect_stack_use_after_return=0 .


1 Answers

Solution from here

Pass the -fsanitize=address flag to the linker as well as the compiler.

-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address"

like image 152
Yunkang YU Avatar answered Sep 24 '22 13:09

Yunkang YU