This is an archive of the discontinued LLVM Phabricator instance.

Add compiler support for dynamic Asan runtime
ClosedPublic

Authored by ygribov on Mar 11 2014, 7:37 AM.

Details

Diff Detail

Event Timeline

ygribov updated this revision to Unknown Object (????).Mar 19 2014, 12:27 AM

Now link shared libs against dynamic runtime.

samsonov added inline comments.Mar 19 2014, 8:19 AM
lib/Driver/SanitizerArgs.cpp
176

Why not hasArg()?

lib/Driver/Tools.cpp
1844

The logic is too obscure here. --whole-archive makes no sense for dynamic library.

If we're linking executable, add "--whole-archive X --no-whole-archive", where X is either full asan, or preinit-asan.
Otherwise add libasan.so.

Maybe it would be even simpler to pull this logic to addAsanRT (we handle dynamic runtime for Android there, for instance),
and call addSanitizerRTLinkFlags with either "asan", or "asan-preinit".

Please add tests for this

Please add tests for this

Done as part of D3043. Or do you want Clang tests? But I don't think we have any to date, everything is tested in compiler-rt...

lib/Driver/SanitizerArgs.cpp
176

Will fix.

lib/Driver/Tools.cpp
1844

Ok, agreed. I'll see what I can do.

Done as part of D3043

Sorry, D3042

We have tests for Clang driver behavior. It makes sense to verify that driver is doing the right thing w/o actually checking out / building compiler-rt. You should probably extend test/Driver/sanitizer-ld.c

ygribov updated this revision to Unknown Object (????).Mar 20 2014, 9:41 AM

Based on Alexey's comments. Tests still tbd.

Ok, this isn't right. I'll fix everything tomorrow.

ygribov updated this revision to Unknown Object (????).Mar 21 2014, 1:11 AM

Fixed, now passes check-all.

Please rebase the patch before uploading the next diff.

lib/Driver/Tools.cpp
1886–1903

The logic is still non-understandable. Please find a better way to express it. As a suggestion:

if (NeedsSharedRT) {
  // add asan-dynamic.so to the link line.
}
if (Args.hasArg(options::OPT_shared)) {
  return;  // don't link static parts into shared objects.
}
StringRef AsanStaticPart = NeedsSharedRT ? "asan-preinit" : "asan";
bool BeforeLibStdCXX = true;
bool ExportSymbols = !NeedsSharedRT;
addSanitizerRTLinkFlags(TC, Args, CmdArgs, AsanStaticPart, BeforeLibStdCXX, ExportSymbols);

now, if you don't want do add "-ldl lpthread -lrt -lm" to the link line for executable and -shared-libasan mode, you may add one more
flag to control whether you add these libs or not. Or, as an alternative, factor adding "-ldl ..." to a separate function and call it when necessary.

ygribov updated this revision to Unknown Object (????).Mar 24 2014, 4:22 AM

Second try to improve readability.

ygribov updated this revision to Unknown Object (????).Mar 24 2014, 7:30 AM

Fixed (I hate CMake...).

General comments:

  1. please don't modify the behavior of Clang driver for static runtime. I think that some users may depend on the fact that there's a single ASan .a runtime, and wouldn't like to break them without strong reasons.
  2. nit: please decrease the amount of added vertical whitespaces.
lib/Driver/Tools.cpp
1833–1834

Update the comment.

1835

just "bool Shared". Also, I think you can avoid using the default value this argument.

1860

Add the comment specifying that we need to explicitly link with system libraries static ASan runtime depends on.

1885

use an early return here to decrease identation:

if (Android) {
  // code
  return;
}
// more code
ygribov updated this revision to Unknown Object (????).Mar 25 2014, 7:26 AM

Fixed based on Alexey's comments.

I think this change will be fine to go in after the following fixes. I'll take a look at compiler-rt part tomorrow.

lib/Driver/SanitizerArgs.cpp
174

AsanSharedRuntime should be true on Android.

lib/Driver/Tools.cpp
1866

Delete this. You already have LibSanitizer defined above.

1882

Please put this Android-specific logic to getSanitizerRTLibName. Then you'll be able to merge this block with the following one (and quit early on Android - there's no asan-preinit there.

1899

Oh, let's switch back to a single

addSanitizerRTLinkFlags(TC, Args, CmdArgs, LibAsanStaticPart,
  /* BeforeLibStdCXX */ true, /* Export Symbols */ !Shared, /* Link Deps */ !Shared);

Sorry for changing my mind.

ygribov added inline comments.Mar 25 2014, 9:56 PM
lib/Driver/Tools.cpp
1882

But this would change behavior for other runtimes (Ubsan, etc.).

1899

Are you sure about those intra-comment whites? We do not use them anywhere else.

samsonov added inline comments.Mar 26 2014, 6:57 AM
lib/Driver/Tools.cpp
1882

That's ok, we have only ASan for Android at the moment.

1899

Um, yes, there shouldn't be whitespaces.

ygribov updated this revision to Unknown Object (????).Mar 26 2014, 8:42 AM

More fixes.

samsonov added inline comments.Mar 26 2014, 11:41 AM
lib/Driver/SanitizerArgs.cpp
174

Why not define AsanSharedRuntime to true for llvm::Triple::Android here?

lib/Driver/Tools.cpp
1822

clang-format-diff this diff as well.

ygribov added inline comments.Mar 26 2014, 11:47 AM
lib/Driver/SanitizerArgs.cpp
174

Well, I thought about this. But my understanding was that SanitizerArgs is really about cmdline args, not about making control decisions.

samsonov added inline comments.Mar 26 2014, 12:00 PM
lib/Driver/SanitizerArgs.cpp
174

Kind of... But we may think of it this way: "-shared-libasan argument is implied for Android". Otherwise, logic for dynamic runtime on Android shoudln't be different from dynamic runtime on Linux, there's no need to special-case it in control decisions.

ygribov updated this revision to Unknown Object (????).Mar 28 2014, 3:18 AM

Updated.

LGTM. I can commit this after support for shared runtime in compiler-rt is in.

samsonov accepted this revision.Apr 1 2014, 6:38 AM

Landed as r205310. Thanks!

samsonov closed this revision.Apr 1 2014, 6:39 AM