This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer] Move {,__}pthread_mutex_{lock,unlock} interceptors to tsan
ClosedPublic

Authored by MaskRay on Jan 4 2023, 12:56 AM.

Details

Summary

These interceptors are pure forwarders for other sanitizers. Move them beside
tsan-specific pthread_mutex_{trylock,timedlock} interceptors.

While here, guard __pthread_mutex_{lock,unlock} (D46793) under #if !__GLIBC_PREREQ(2, 34).

In glibc>=2.34 [1], __pthread_mutex_{lock,unlock} only have non-default-version definitions
(unversioned __pthread_mutex_lock causes a linker error. Program preloading is not expected).
In glibc>=2.36 [2], dlsym(RTLD_NEXT, "__pthread_mutex_lock") returns nullptr, so the interceptor won't work.

Fix https://github.com/llvm/llvm-project/issues/59820

[1]: https://sourceware.org/git/?p=glibc.git;a=commit;h=99f841c441feeaa9a3d97fd91bb3d6ec8073c982
[2]: https://sourceware.org/git/?p=glibc.git;a=commit;h=efa7936e4c91b1c260d03614bb26858fbb8a0204

Diff Detail

Event Timeline

MaskRay created this revision.Jan 4 2023, 12:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 12:56 AM
MaskRay requested review of this revision.Jan 4 2023, 12:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 12:56 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Jan 4 2023, 1:23 AM
This revision is now accepted and ready to land.Jan 4 2023, 1:23 AM
MaskRay added a comment.EditedJan 4 2023, 10:06 AM

The approach I take here keeps __pthread_mutex_{lock,unlock} interceptors working.
If a program intercepts pthread_mutex_lock and calls __pthread_mutex_lock, then it needs this mechanism to keep tsan working.
Since glibc 2.34 makes __pthread_mutex_lock non-default, dlvsym or .symver __pthread_mutex_lock,__pthread_mutex_lock@GLIBC_2.2.5 is needed to call __pthread_mutex_lock.

@azat if such a __pthread_mutex_lock using program does not need to work with tsan, we can keep simple and use #if SANITIZER_GLIBC\n#if !__GLIBC_PREREQ(2, 34) instead.

The approach I take here keeps __pthread_mutex_{lock,unlock} interceptors working.
If a program intercepts pthread_mutex_lock and calls __pthread_mutex_lock, then it needs this mechanism to keep tsan working.
Since glibc 2.34 makes __pthread_mutex_lock non-default, dlvsym or .symver __pthread_mutex_lock,__pthread_mutex_lock@GLIBC_2.2.5 is needed to call __pthread_mutex_lock.

@azat if such a __pthread_mutex_lock using program does not need to work with tsan, we can keep simple and use #if SANITIZER_GLIBC\n#if !__GLIBC_PREREQ(2, 34) instead.

so I am not sure why this patch does not uses __GLIBC_PREREQ?

compiler-rt/lib/tsan/rtl-old/tsan_interceptors_posix.cpp
1336

@dvyukov Should we delete rtl-old already?

compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
83

it would be nicer to move code common->tsan in a separate patch

vitalybuka accepted this revision.Jan 4 2023, 10:46 AM

lgtm, but please consider extracting move part into a separate patch

MaskRay updated this revision to Diff 486344.Jan 4 2023, 10:51 AM
MaskRay edited the summary of this revision. (Show Details)

simplify __pthread_mutex_{lock,unlock} interceptors

MaskRay marked an inline comment as done.Jan 4 2023, 11:04 AM
MaskRay updated this revision to Diff 486361.Jan 4 2023, 12:03 PM
MaskRay edited the summary of this revision. (Show Details)

remove more unused macros. clarify the description

This revision was landed with ongoing or failed builds.Jan 4 2023, 12:04 PM
This revision was automatically updated to reflect the committed changes.
azat added a comment.Jan 4 2023, 1:06 PM

@azat if such a pthread_mutex_lock using program does not need to work with tsan, we can keep simple and use #if SANITIZER_GLIBC\n#if !GLIBC_PREREQ(2, 34) instead.

@MaskRay No, it should work under TSan, thank you for a more complete fix!

dvyukov added inline comments.Jan 8 2023, 9:59 PM
compiler-rt/lib/tsan/rtl-old/tsan_interceptors_posix.cpp
1336

Yes.