This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Relax pthread_getaffinity test to account for cgroups/docker
ClosedPublic

Authored by DavidSpickett on Oct 11 2022, 7:45 AM.

Details

Summary

Fixes #58283

When running in a docker container you can have fewer cores assigned
to you than get_nrpoc would suggest.

Since the test just wants to know that interception worked, allow
any result > 0 and <= the global core count.

Diff Detail

Event Timeline

DavidSpickett created this revision.Oct 11 2022, 7:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 11 2022, 7:45 AM
Herald added subscribers: Enna1, dberris. · View Herald Transcript
DavidSpickett requested review of this revision.Oct 11 2022, 7:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 11 2022, 7:45 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript

For https://github.com/llvm/llvm-project/issues/58283

Does this seem reasonable?

Perhaps there is a way to call the real pthread_getaffinity_np in the test instead, but I wasn't sure how that would be done.

DavidSpickett edited the summary of this revision. (Show Details)Oct 11 2022, 7:48 AM
MaskRay added a comment.EditedOct 11 2022, 9:57 PM

I wonder whether we should just relax the test, instead of introducing a lit feature based on if os.cpu_count() != len(os.sched_getaffinity(0)).

I can confirm that the test also fails if I restrict affinity with something like numactl -C 0-3 /tmp/RelA/bin/llvm-lit -a /tmp/RelA/projects/compiler-rt/test/sanitizer_common/asan-x86_64-Linux/Linux/pthread_getaffinity_np.cpp

vitalybuka added inline comments.Oct 12 2022, 1:35 PM
compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
14

Relaxing the test looks better to me

DavidSpickett added inline comments.Oct 13 2022, 2:35 AM
compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
14

What does relaxing it look like?

I admit I don't have the context for all this test is trying to assert. My guess was that it's checking that the interception of pthread_getaffinity_np has been done correctly.

Relaxing it would mean simply calling it and expecting it to return something reasonable? Perhaps >0 and < get_nprocs()?

I wouldn't want to end up with a test that didn't test anything.

vitalybuka added inline comments.Oct 13 2022, 12:56 PM
compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
14

Interceptors do some sanitizer specific checking, then forward the call to the real function, then do more sanitizer specific stuff.
Some interceptors are more complex and replace some logic of the default libc implementation

pthread_getaffinity_np is trivial interceptor, it just makes sanitizer know that cpu_set_t is going to be written. So we wan't to test that it does not crash, return some reasonable result, and, as in this case, output parameter filled with data or accessible.

E.g test will catch if we fail to intercept, the Msan will complain that CPU_COUNT_S is using uninitialized data from cpu_set_t.
We are not trying to validate behavior of pthread_getaffinity_np deep.

So here ">0 && <=nprocs" is reasonable fix.

DavidSpickett added inline comments.Oct 14 2022, 1:38 AM
compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
14

Thanks for the explanation, I'll update this change then.

Relax the test condition instead of disabling it.

DavidSpickett retitled this revision from [compiler-rt] Disable pthread_getaffinity test when running with cgroups to [compiler-rt] Relax pthread_getaffinity test to account for cgroups/docker.Oct 14 2022, 1:59 AM
DavidSpickett edited the summary of this revision. (Show Details)
DavidSpickett marked 2 inline comments as done.
vitalybuka accepted this revision.Oct 14 2022, 1:45 PM
This revision is now accepted and ready to land.Oct 14 2022, 1:45 PM
MaskRay accepted this revision.Oct 14 2022, 6:56 PM

LGTM.

compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_getaffinity_np.cpp
20

delete parentheses

DavidSpickett marked an inline comment as done.Oct 17 2022, 3:58 AM