Skip to content

TSan: question regarding early exit in MemoryAccess due to GetIgnoreBit #77521

Open
@ningvin

Description

@ningvin

TSan supports ignoring certain reads/writes by a begin/end style annotation mechanism. This is e.g. used by Archer to filter out provably race free regions in OpenMP code for the sake of performance.

Calling AnnotateIgnoreWritesBegin causes an ignore bit to be set in the current thread's local state, which lies in thread-local storage.

Looking at MemoryAccess in the TSan runtime (which as far as I can tell ends up being called for most instrumented reads/writes), I noticed that the "early" exit due to a set ignore bit happens later than I would have expected:

FastState fast_state = thr->fast_state;
Shadow cur(fast_state, addr, size, typ);
LOAD_CURRENT_SHADOW(cur, shadow_mem);
if (LIKELY(ContainsSameAccess(shadow_mem, cur, shadow, access, typ)))
return;
if (UNLIKELY(fast_state.GetIgnoreBit()))
return;
if (!TryTraceMemoryAccess(thr, pc, addr, size, typ))
return TraceRestartMemoryAccess(thr, pc, addr, size, typ);
CheckRaces(thr, shadow_mem, cur, shadow, access, typ);

The ignore bit is only evaluated after the shadow memory has already been loaded and the access has been deemed to not be already recorded.

In other situations (MemoryAccess16 and UnalignedMemoryAccess), the ignore bit check is performed first thing.

I am curious as to why that is the case. Is access to the thread-local ThreadState/FastState more expensive than accessing and comparing the shadow memory?

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler-rt:tsanThread sanitizerquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions