Reviving TypeSanitizer - a sanitizer to catch type-based aliasing violations

Hi,

A while ago, @hfinkel proposed a new sanitizer for type-based aliasing violations. He also shared an initial implementation as well:

Recently I spent some time rebasing the patches, ported them to current main and also made them work in macOS.

At the moment, there are a few key limitations on type-based aliasing in LLVM/Clang and one factor blocking progress in that area is concerns for correctness; in particular the fact that we don’t have a way to check a program is free of type-based aliasing violations makes it difficult the determine if a mis-compile is caused by a new bug in the TBAA implementation or UB in the source.

Therefore I propose to revive the type sanitizer patches and try to get an initial version submitted. To do that I would need help, especially on the sanitizer runtime side. At the moment, the all tysan tests added in the runtime patch work on macOS, but it would be great if someone could give it a try on Linux as well.

Please let me know if you are interested in helping out, there certainly are many areas that would need further tuning and testing.

cc’ing some of the people on the original review on discourse: @jdoerfert @kcc

12 Likes

Hi @fhahn, I’m familiar with ASan and TSan but not familiar with TySan. I’m very interested in helping this.

Glad to see this is getting picked up again. We had this on queue as an intern project. What kind of help would you need landing these patches?

Thanks both @Enna1 & @leonardchan!

I think one of the main things is that I am only able to test this on macOS so far. Any help with checking the tests (in particular the end-to-end tests in compiler-rt) on Linux would be great. Then there’s the technical review of the implementation.

It would also be great if we could collect additional test cases for violations we want to detect or should not detect.

Finally, performance is also an area that will likely need improvements. Both that and increasing the test coverage is probably easier done once we have a baseline in tree.

Hi @fhahn, I tried TypeSanitizer on my Linux environment, I think I make TySan build and tests passed on Linux.
Should I update the diff in the initial implementation patch or send a new patch( ⚙ D137414 [TySan] Fix Type Sanitizer build on Linux)?
Thanks!

1 Like

That’s great, thank you very much!

I think I’ll commandeer the reviews and restart the reviews.

3 Likes

Hiya. What’s the current status of this? If there’s any blockers or reviewers are needed, I’d be happy to volunteer.

I recently rebased the patches and addressed the latest comments (thanks everyone!).

Any additional help with the reviews + testing on Linux/X86 and Linux/AArch64 would be very much appreciated

1 Like

Any hope to continue the work in GitHub PR era?

IMO this would be very helpful for “legacy” code like OpenSSL…

2 Likes

Yes hopefully!

Just moved the patches to GH as PRs:

2 Likes

Just rebased the patches again and addressed the current comments.

I also did a quick search of the issue tracker to find strict-alias violations that caused mis-compiles (reported by users) to check if the current implementation catches them.

Caught:
https://github.com/llvm/llvm-project/issues/86685
https://github.com/llvm/llvm-project/issues/68655
https://github.com/llvm/llvm-project/issues/62828
https://github.com/llvm/llvm-project/issues/62544
https://github.com/llvm/llvm-project/issues/47137
https://github.com/llvm/llvm-project/issues/45282

Not caught (Did not check closely yet if they may not be violations)
https://github.com/llvm/llvm-project/issues/78154
https://github.com/llvm/llvm-project/issues/51837

On the SingleSource subset of the test-suite on ARM64 macOS, there currently are the following failures. I had a quick look at some of them (SingleSource/UnitTests/Vectorizer/* and the violations there are in libc++'s random number generator, not yet confirmed if this is a false positive)

Failed Tests (30):
  test-suite :: SingleSource/Benchmarks/BenchmarkGame/fannkuch.test
  test-suite :: SingleSource/Benchmarks/CoyoteBench/fftbench.test
  test-suite :: SingleSource/Benchmarks/Misc-C++/Large/ray.test
  test-suite :: SingleSource/Benchmarks/Misc-C++/stepanov_container.test
  test-suite :: SingleSource/Benchmarks/Misc/flops.test
  test-suite :: SingleSource/Benchmarks/Misc/himenobmtxpa.test
  test-suite :: SingleSource/Benchmarks/Misc/richards_benchmark.test
  test-suite :: SingleSource/Benchmarks/Shootout-C++/Shootout-C++-lists.test
  test-suite :: SingleSource/Benchmarks/Shootout-C++/Shootout-C++-lists1.test
  test-suite :: SingleSource/Benchmarks/Shootout-C++/Shootout-C++-sieve.test
  test-suite :: SingleSource/Benchmarks/Shootout/Shootout-methcall.test
  test-suite :: SingleSource/Regression/C++/EH/Regression-C++-class_hierarchy.test
  test-suite :: SingleSource/Regression/C++/EH/Regression-C++-ctor_dtor_count.test
  test-suite :: SingleSource/Regression/C++/EH/Regression-C++-exception_spec_test.test
  test-suite :: SingleSource/Regression/C++/EH/Regression-C++-inlined_cleanup.test
  test-suite :: SingleSource/Regression/C++/EH/Regression-C++-throw_rethrow_test.test
  test-suite :: SingleSource/Regression/C/Regression-C-2003-05-21-BitfieldHandling.test
  test-suite :: SingleSource/UnitTests/2003-05-02-DependentPHI.test
  test-suite :: SingleSource/UnitTests/2004-11-28-GlobalBoolLayout.test
  test-suite :: SingleSource/UnitTests/2006-01-23-UnionInit.test
  test-suite :: SingleSource/UnitTests/2009-04-16-BitfieldInitialization.test
  test-suite :: SingleSource/UnitTests/2020-01-06-coverage-008.test
  test-suite :: SingleSource/UnitTests/SignlessTypes/Large/cast.test
  test-suite :: SingleSource/UnitTests/Vectorizer/VPlanNativePath/outer-loop-vect.test
  test-suite :: SingleSource/UnitTests/Vectorizer/dependences.test
  test-suite :: SingleSource/UnitTests/Vectorizer/index-select.test
  test-suite :: SingleSource/UnitTests/Vectorizer/recurrences.test
  test-suite :: SingleSource/UnitTests/Vectorizer/runtime-checks.test
  test-suite :: SingleSource/UnitTests/byval-alignment.test
  test-suite :: SingleSource/UnitTests/initp1.test
3 Likes

I’ve tested TySan on Linux/X86 and the tests are passing. I also tested it on some historical internal issues that involved type aliasing and it caught all of them.
The two cases you say aren’t caught both involve unions. In the pull requests you mention that Clang’s TBAA representation currently has issues representing unions. Do we know if that’s still the case?