Asan: How to use clang’s linking step instead of ld through Xcode

402 views
Skip to first unread message

Amit Kumar

unread,
Mar 22, 2013, 7:38:37 AM3/22/13
to address-...@googlegroups.com

I am trying to build a project with Address sanitizer in Xcode 4.6.1. I have already switched the compiler to “Apple LLVM compiler 4.2” though a setting in Xcode. My problem is that the address sanitizer page on llvm.org (http://clang.llvm.org/docs/AddressSanitizer.html) specifically says “ make sure to use clang (not ld) for the final link step”.

Based on the linker warnings I am getting, I think (but not sure) the project I am building is using ‘ld’ and not ‘clang’ for the linking step. I am unable to figure out how to instruct Xcode 4.6.1 to use clang’s linker. I couldn’t find any setting.

The project I am trying to build is dependent on hundreds of projects. These projects output static libraries (.a), dynamic libraries (.dylib) and frameworks (.framework).

I have changed all these projects to use address sanitizer

Compilation option –fsanitize=address was added for all the projects

Linker option –fsanitize=address was added for all the projects except for static libraries

In case I am unable to use clang’s linker instead of ld through Xcode, is there any way by which I can explicitly enable asan? Perhaps by linking explicitly to libclang_rt.asan_osx_dynamic.dylib?

Is this group is not the correct place to ask such questions then could you please direct me to the correct group?


Thanks,

Amit Kumar



Alexander Potapenko

unread,
Mar 22, 2013, 7:46:05 AM3/22/13
to address-...@googlegroups.com
Hi Amit,

I've little experience with the Xcode IDE, so I'm not sure which
settings you need.
But here's how you can use ASan with xcodebuild from the command line:

$ export CC=.../clang
$ export CXX=.../clang++
$ xcodebuild -project projectname.xcodeproj -target "target_name"
CC=$CC CXX=$CXX LDPLUSPLUS=$CXX

HTH,
Alex
> --
> You received this message because you are subscribed to the Google Groups
> "address-sanitizer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to address-saniti...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Alexander Potapenko
Software Engineer
Google Moscow

Konstantin Serebryany

unread,
Mar 22, 2013, 7:47:35 AM3/22/13
to address-...@googlegroups.com
Hi Amit,

None of us here use Xcode, so we may not be well equipped to answer you.
But we may have more chances to be useful if you paste the error messages here.

--kcc

On Fri, Mar 22, 2013 at 3:38 PM, Amit Kumar <amt.am...@gmail.com> wrote:

Amit Kumar

unread,
Mar 23, 2013, 10:43:33 AM3/23/13
to address-...@googlegroups.com

I investigated further and I think address sanitizer is not currently usable for even simple GUI applications on Mac.

I experimented with a very simple test cocoa application (generated entirely by Xcode). I switched back to older Xcode 4.5.2. In Xcode 4.5.2, the flag for address sanitizer is -faddress-sanitizer. If I use this flag with compiler but not linker then I get a lot of linker errors. If I supply the same flag to the linker then the errors go away. In this application, I found a lot of symbols with ‘asan’ in their names (I used nm for getting symbols). If I launch this instrumented application then I get a crash (EXEC_BAD_ACCESS). I think that the build was properly instrumented by address sanitizer because I do not get a crash if I do not use -faddress-sanitize.

I don’t think that address sanitizer could be useful if even a very simple application (generated by Xcode) instrumented by Asan crashes on launch.

With Xcode 4.6.1, clang gives me an error that -faddress-sanitizer is not a valid flag. Address sanitizer page has the info that the new flag is -fsanitize=address. I think that the new flag -fsanitize=address was ignored since there wasn’t any symbol in the instrumented binary which had asan in its name.

I didn’t expect that Xcode 4.6.1 would ignore-fsanitize=address and I was incorrectly suspecting that the linker used by Xcode was still ld.


Thanks,

Amit Kumar

Message has been deleted

Alexander Potapenko

unread,
Mar 24, 2013, 9:09:42 AM3/24/13
to address-...@googlegroups.com

Um, sorry, are you trying to use the Clang version shipped with Xcode? It's not gonna work.
Instead you need to build Clang yourself. I suppose it should be possible to make Xcode use your own Clang.

Reply all
Reply to author
Forward
0 new messages