This is an archive of the discontinued LLVM Phabricator instance.

add supporting of the sanitizer arguments into Clang on FreeBSD platform.
ClosedPublic

Authored by kutuzov.viktor.84 on Jan 28 2014, 3:34 PM.

Details

Reviewers
kcc
samsonov
Summary

This patch provides the following functionality:

  • allows the FreeBSD platform in the sanitizer argument filters;
  • adds the linker cmdline args and RT libraries in according of specified sanitizer arguments (--sanitize=...) for the FreeBSD targets.

Diff Detail

Event Timeline

This patch needs a corresponding set of tests in test/Driver.

Do the asan, msan, tsan, ubsan, ... test suites pass on freebsd with this change?

lib/Driver/Tools.cpp
1963

You mean, "in the freebsd directory"?

5889

Put the & on the right, please, since you're touching this line anyway =)

5896

Local variable names should start with an uppercase letter.

5899–5904

I think you only need a C++ stdlib if you're using certain parts of ubsan (asan, tsan, msan, and lsan shouldn't require it IIUC).

6027–6028

Please give this a more informative name rather than using a comment to describe what it is.

6029

ex |= ... ?

6058–6062

I would expect this either needs -whole-archive or needs to come at the end of the linker arguments? (None of the symbols in this archive will be needed at this point.)

kutuzov.viktor.84 updated this revision to Unknown Object (????).Feb 10 2014, 11:43 AM

Richard, here's updated patch. Thanks a lot for your comments.

Could you please follow advice from http://llvm.org/docs/Phabricator.html and run smth. like "svn diff --diff-cmd=diff -x -U999999" (or use arc command-line interface to upload diffs), so that it's possible to see more context? Thanks.

lib/Driver/SanitizerArgs.cpp
249

IsPosix name is bad, Mac OS X is also Posix. You can just use "IsLinuxOrFreeBSD".
By the way, did you really check that TSan, MSan, DFSan all work on FreeBSD?

lib/Driver/Tools.cpp
1867

Please re-use/generalize addProfileRT() function.

1891

Please re-use/generalize addSanitizerRTFlagsLinux. I really don't like we duplicate the logic for calculating the name/location of sanitizer runtimes, adding --dynamic-list/--export-dynamic etc.

1910

To support previous comment - either comment is now wrong, or you forgot to add --export-dynamic here.

6116

I'd prefer to add -lpthread_p in addSanitizerRTLinkFlagsFreeBSD, as we do on Linux.

kutuzov.viktor.84 updated this revision to Unknown Object (????).Feb 11 2014, 12:40 AM

The same patch with full context.

kcc added a comment.Feb 11 2014, 12:50 AM

tests please

kutuzov.viktor.84 updated this revision to Unknown Object (????).Feb 11 2014, 7:22 AM

Tests added.

Do the asan, msan, tsan, ubsan, ... test suites pass on freebsd with this change?

No, that require further changes in compiler-rt. The test suites are not enabled for FreeBSD yet.

In this case it probably makes sense to keep that sanitizers as "unsupported" on FreeBSD for now.

They are currently not supported so we have no failures.

kutuzov.viktor.84 updated this revision to Unknown Object (????).Feb 17 2014, 11:20 AM

Tests updated so that they pass on both Linux and FreeBSD.

Regarding handling of the "-lpthread*" option: I tried to move the code adding various libraries to addSanitizerRTLinkFlagsLinux[OrFreeBSD]() and that resulted in a somewhat larger function with a lot of conditions for different libraries. No problem with implementing it this way, but it seems the readability suffers significantly.

lib/Driver/SanitizerArgs.cpp
249

Yes, with corresponding changes applied (not submitted for review yet) we do support those on FreeBSD.

lib/Driver/Tools.cpp
1910

I will fix this, but just to make the logic clear: in this case we return true to let the caller know we should append "--export-dynamic".

kutuzov.viktor.84 updated this revision to Unknown Object (????).Feb 18 2014, 8:47 AM

Reworked as suggested.
Somehow missed your comments. Thanks, Alexey.

lib/Driver/Tools.cpp
219

By the way, do we really need the "Triple" parameter? It seems in all cases we get triple as just TC.getTriple().

samsonov added inline comments.Feb 20 2014, 6:27 AM
lib/Driver/SanitizerArgs.cpp
253

You haven't mailed the changes supporting tsan/msan/dfsan on FreeBSD. I suggest to keep filtering them for now, but leaving this up to you.

lib/Driver/Tools.cpp
232

OMG, it turned out there was the correct addProfileRTLinux(), and this function is long obsolete. I've fixed this problem in r201789.

1769

You can use a newly introduced getOSNameForCompilerRTLib() for that.

1799

Why do you need this?

  • If the user specifies -rdynamic, --export-dynamic will be added to the link line anyway, no harm in duplicating this.
  • -static is not supposed to work with ASan anyway.
6046

Why not re-use the whole chunk of code that calls add?sanRTLinux() in gnutools::Link::ConstructJob (factored out into a separate function)?

6058

Why don't you make use of "BeforeLibStdCXX" parameter of addSanitizerRTLinkFlagsLinux(), or even reuse addAsanRTLinux, addTSanRTLinux, and so on?

6072

You add -lrt because of sanitizers in this place, and you add -lpthread(_p)? in another place below. That's why I suggest to link in all the system libraries required by sanitizers in a single place (addSanitizerRTLinkFlags method). By the way, will the sanitizers actually work if the user specifies smth. like --nodefaultlibs? Maybe, it's fine to fail with link error in that case?

6145–6146

Do you have two if(NeedsDefaultLibs) { ... } blocks in a row?

OK, it looks there is a lot of details that require deep and careful analysis against recent revisions--more than I could do with the current level of the sanitizers support on FreeBSD. So, I propose we defer this patch untill we need it to run the tests and then make as small changes as possible and only if necessary. Please let me know if you think that would work. Thanks.

OK, I don't mind it. I will hopefully do some cleanup/generalization for sanitizers support in the Driver code that will help you minimize the changes to enable them on FreeBSD.

For the record: r202148 and r202150 should help in simplifying this patch.

kutuzov.viktor.84 updated this revision to Unknown Object (????).Mar 7 2014, 5:23 AM

Updated to leverage changes by Alexey Samsonov.

The change in getCompilerRTLibDir() is necessary as otherwise it yields "lib/freebsd10.0" on FreeBSD 10.0 whereas "lib/freebsd" (without the version number) is expected.

Thanks, Alexey!

Please add a testcase. It should probably go to test/Driver/sanitizer-ld.c

lib/Driver/Tools.cpp
1741

Please note that we expect "darwin" directory on Mac OS X / iOS. Maybe it makes sense to special-case freebsd for now.

kutuzov.viktor.84 updated this revision to Unknown Object (????).Mar 7 2014, 9:53 AM

Fixed as suggested and supplied with tests.

samsonov added inline comments.Mar 13 2014, 8:05 AM
lib/Driver/Tools.cpp
1741

Please calculate os-specific dir in a separate variable. Also, why not just use "freebsd" constant for llvm::Triple::FreeBSD? Is it going to change?

test/Driver/sanitizer-ld.c
26 ↗(On Diff #7644)

Please how CHECK-NOT lines work in the FileCheck man. Most likely you'll need to add CHECK-ASAN-FREEBSD-NOT: "-ldl" between all CHECK-ASAN-FREEBSD: lines. Or just add a single separate test case that would contain a single CHECK-NOT constraint for ldl.

27 ↗(On Diff #7644)

I wonder if you can somehow match "freebsd" part of a path here.

49 ↗(On Diff #7644)

This and the following test case doesn't seem to add any value. I think it's ok to drop them.

lib/Driver/Tools.cpp
1741

The idea is that given there may be more exceptions for which ToolChain::getOS() doesn't return the right thing, using Triple::getOSTypeName() seems to be the common way to get the dir name for them.

kutuzov.viktor.84 updated this revision to Unknown Object (????).Mar 17 2014, 3:51 AM

Updated.

Alexey, please let me know if you would prefer explicit "freebsd" instead of calling Triple::getOSTypeName(). Thanks.

samsonov accepted this revision.Mar 17 2014, 5:48 AM

LGTM

lib/Driver/Tools.cpp
1742

Yes, please use explicit "freebsd" for now (and leave a comment why you're doing this).

1790

Remove newline here.

Committed as r204129.