This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Use @rpath as LC_ID_DYLIB for ASan dylib on OS X
ClosedPublic

Authored by kubamracek on Oct 28 2014, 3:42 PM.

Details

Summary

The current LC_ID_DYLIB is hardcoded to be the absolute path to the build result. This means that ASan only works out-of-the-box if you never move the compiler/libraries from the directory you built it in. We currently have an issue with the prebuilt binary releases, because of the hardcoded path: http://llvm.org/bugs/show_bug.cgi?id=21316

This patch proposes to change the LC_ID_DYLIB of ASan's dynamic libraries on OS X to be set to "@rpath/libclang_rt.asan_osx_dynamic.dylib" and similarly for iossim. Clang driver then sets the "-rpath" to be the real path to where clang currently has the dylib (because clang uses the relative path to its current executable). This means if you move the compiler or install the binary release, -fsanitize=address will link to the proper library. The remaining issue is that if you delete or move the dylib, the previously linked executables will still have the old path in them, but this issue already exists with the current state as well.

I also modified one of ASan tests not to depend on the fact that the dylib is referenced with the full path. Instead, it now locates the dylib by issuing "clang -fsanitize=address -###" and finding the dylib path there.

Diff Detail

Event Timeline

kubamracek updated this revision to Diff 15532.Oct 28 2014, 3:42 PM
kubamracek retitled this revision from to [compiler-rt] Use @rpath as LC_ID_DYLIB for ASan dylib on OS X.
kubamracek updated this object.
kubamracek edited the test plan for this revision. (Show Details)
kubamracek added subscribers: Unknown Object (MLST), zaks.anna, samsonov.
kubamracek added a subscriber: glider.
kubamracek updated this revision to Diff 15538.Oct 28 2014, 9:15 PM

Updated the patch to also add @executable_path to rpath to support having the dylib next to the executable. Added comments explaining the use cases. Added a clang test case.

glider accepted this revision.Oct 28 2014, 9:45 PM
glider added a reviewer: glider.

LGTM, but we probably want to hear Nick's opinion on this.

tools/clang/lib/Driver/ToolChains.cpp
402–403

Can you please comment which these bool constants stand for?
E.g. "/*AlwaysLink*/ true" etc.

This revision is now accepted and ready to land.Oct 28 2014, 9:45 PM
kubamracek updated this revision to Diff 15550.Oct 29 2014, 8:50 AM
kubamracek edited edge metadata.

Added the comments for bool constants.

samsonov added inline comments.
tools/clang/lib/Driver/ToolChains.cpp
313

DarwinStaticLib name makes no sense at this point. Consider fixing this in a preliminary commit.

tools/clang/test/Driver/darwin-asan-rpath.c
1

Can you instead fix the existing test for linking ASan runtime on Darwin (if there is any)?

kledzik edited edge metadata.Oct 29 2014, 1:10 PM

I'm not a clang driver guy, but it would probably be cleaner to leave AddLinkRuntimeLib() as is, and instead add the CmdArgs.push_back()s inside the if (Sanitize.needsAsanRt()).

It looks like the clang driver will always add two rpath entries when building for ASan? Is there anyway to suppress that if Xcode knows better? That is, Xcode knows a better location for the runtime lib?

kubamracek updated this revision to Diff 15596.Oct 30 2014, 5:46 PM
kubamracek edited edge metadata.

DarwinStaticLib name makes no sense at this point. Consider fixing this in a preliminary commit.

Fixed in r220939, http://reviews.llvm.org/D6040.

Can you instead fix the existing test for linking ASan runtime on Darwin (if there is any)?

Done.

I'm not a clang driver guy, but it would probably be cleaner to leave AddLinkRuntimeLib() as is, and instead add the CmdArgs.push_back()s inside the if (Sanitize.needsAsanRt()).

You're right that this currently applies only to ASan, but we might want to reuse this rpath logic for other future compiler-rt sanitizer (tsan, ubsan) dylibs. Leaving it for now, but if you strongly feel that we shouldn't touch AddLinkRuntimeLib, let me know and I'll change the patch.

It looks like the clang driver will always add two rpath entries when building for ASan? Is there anyway to suppress that if Xcode knows better? That is, Xcode knows a better location for the runtime lib?

I think Xcode would have to use install_name_tool to change the rpath entries manually. We could add another flag to disable adding these rpaths in the driver. Alexander, what do you think? Either way, if we agree on adding these rpaths by default, let's start with this patch and then consider adding that flag in a separate patch.

I'm OK with the patch as is. I'd like to avoid adding one more flag for controlling linking strategy for as long as possible =)

samsonov accepted this revision.Oct 30 2014, 6:23 PM
samsonov added a reviewer: samsonov.

Landed in r221278 and r221279.

kubamracek closed this revision.Nov 5 2014, 11:20 AM