This is an archive of the discontinued LLVM Phabricator instance.

tsan: new MemoryAccess interface
ClosedPublic

Authored by dvyukov on Aug 2 2021, 7:33 AM.

Details

Summary

Currently we have MemoryAccess function that accepts
"bool kAccessIsWrite, bool kIsAtomic" and 4 wrappers:
MemoryRead/MemoryWrite/MemoryReadAtomic/MemoryWriteAtomic.

Such scheme with bool flags is not particularly scalable/extendable.
Because of that we did not have Read/Write wrappers for UnalignedMemoryAccess,
and "true, false" or "false, true" at call sites is not very readable.

Moreover, the new tsan runtime will introduce more flags
(e.g. move "freed" and "vptr access" to memory acccess flags).
We can't have 16 wrappers and each flag also takes whole
64-bit register for non-inlined calls.

Introduce AccessType enum that contains bit mask of
read/write, atomic/non-atomic, and later free/non-free,
vptr/non-vptr.
Such scheme is more scalable, more readble, more efficient
(don't consume multiple registers for these flags during calls)
and allows to cover unaligned and range variations of memory
access functions as well.

Also switch from size log to just size.
The new tsan runtime won't have the limitation of supporting
only 1/2/4/8 access sizes, so we don't need the logarithms.

Also add an inline thunk that converts the new interface to the old one.
For inlined calls it should not add any overhead because
all flags/size can be computed as compile time.

Diff Detail

Event Timeline

dvyukov created this revision.Aug 2 2021, 7:33 AM
dvyukov requested review of this revision.Aug 2 2021, 7:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2021, 7:33 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Aug 2 2021, 8:25 AM
melver added inline comments.
compiler-rt/lib/tsan/rtl/tsan_rtl.h
697

I'd explicitly make this match AccessType type:

enum : AccessType {

If we had C++17 I think inline constexpr may be preferred to avoid abusing enum, but I think we're still on C++14 so this is fine. (I recently wondered what the safest way of doing constants esp. in headers is: https://abseil.io/tips/140 .. probably lots of other code still using the non-preferred idioms, but incrementally changing it would be good.)

698

prefix constants with 'k' ?

724

There's Log2() in sanitizer_common. Is it possible to use that? __builtin_ctzll just returns constants for constant expressions: https://godbolt.org/z/9K3T4jhr5

The only problematic thing I see is that it has 2 CHECKs and not DCHECK in it, but maybe that's reasonable to change?

This revision is now accepted and ready to land.Aug 2 2021, 8:25 AM
vitalybuka accepted this revision.Aug 2 2021, 3:54 PM
vitalybuka added inline comments.
compiler-rt/lib/tsan/rtl/tsan_interface.cpp
55

can you swap pc and pc1 to match header and keep better name for interface and not implementation

maybe
uptr pc_no_pac = STRIP_PAC_PC(pc);

also it could be a separate patch

dvyukov updated this revision to Diff 363650.Aug 3 2021, 2:02 AM
dvyukov marked 3 inline comments as done.
  • changed pc to pc_no_pac
  • prefixed MemoryAccess constants with 'k'
  • enum : AccessType
This revision was landed with ongoing or failed builds.Aug 3 2021, 2:03 AM
This revision was automatically updated to reflect the committed changes.