This is an archive of the discontinued LLVM Phabricator instance.

tsan: fix XMM register corruption in hacky call
ClosedPublic

Authored by dvyukov on Nov 12 2021, 1:22 AM.

Details

Summary

The compiler does not recognize HACKY_CALL as a call
(we intentionally hide it from the compiler so that it can
compile non-leaf functions as leaf functions).
To compensate for that hacky call thunk saves and restores
all caller-saved registers. However, it saves only
general-purposes registers and does not save XMM registers.
This is a latent bug that was masked up until a recent "NFC" commit
d736002e90 ("tsan: move memory access functions to a separate file"),
which allowed more inlining and exposed the 10-year bug.
Save and restore caller-saved XMM registers (all) as well.

Currently the bug manifests as e.g. frexp interceptor messes the
return value and the added test fails with:

i=8177 y=0.000000 exp=4

Diff Detail

Event Timeline

dvyukov requested review of this revision.Nov 12 2021, 1:22 AM
dvyukov created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 12 2021, 1:22 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Nov 12 2021, 2:41 AM

Does this trick still buy significant performance gains? LLVM/Clang has changed a lot in 10 years, and modern CPUs also have gotten faster. If it doesn't buy more than 1% or such, the additional risk of doing this might not be worth it anymore.

This revision is now accepted and ready to land.Nov 12 2021, 2:41 AM

Does this trick still buy significant performance gains? LLVM/Clang has changed a lot in 10 years, and modern CPUs also have gotten faster. If it doesn't buy more than 1% or such, the additional risk of doing this might not be worth it anymore.

I am sure it's still worth it because non-leaf functions will contain stack frame and a bunch of spills.
But to make this 10-year old latent bug double-fun: https://reviews.llvm.org/D112604

This revision was automatically updated to reflect the committed changes.
apinski-cavium added a subscriber: apinski-cavium.EditedNov 29 2021, 5:27 PM

This breaks TSAN on processors which don't have AVX support because vmovdqu is not valid for them.
Reported as https://github.com/google/sanitizers/issues/1472

This breaks TSAN on processors which don't have AVX support because vmovdqu is not valid for them.
Reported as https://github.com/google/sanitizers/issues/1472

I guess I needed to use MOVDQU, but this code is unused since https://github.com/llvm/llvm-project/commit/66d4ce7e26a5 and will be removed in https://reviews.llvm.org/D112604