Skip to content

Commit cfdd7d7

Browse files
authored
[compiler-rt][rtsan] sched cpu affinity for linux interception. (#124194)
1 parent 6087c30 commit cfdd7d7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,26 @@ INTERCEPTOR(int, sched_yield, void) {
740740
return REAL(sched_yield)();
741741
}
742742

743+
#if SANITIZER_LINUX
744+
INTERCEPTOR(int, sched_getaffinity, pid_t pid, size_t len, cpu_set_t *set) {
745+
__rtsan_notify_intercepted_call("sched_getaffinity");
746+
return REAL(sched_getaffinity)(pid, len, set);
747+
}
748+
749+
INTERCEPTOR(int, sched_setaffinity, pid_t pid, size_t len,
750+
const cpu_set_t *set) {
751+
__rtsan_notify_intercepted_call("sched_setaffinity");
752+
return REAL(sched_setaffinity)(pid, len, set);
753+
}
754+
#define RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY \
755+
INTERCEPT_FUNCTION(sched_getaffinity)
756+
#define RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY \
757+
INTERCEPT_FUNCTION(sched_setaffinity)
758+
#else
759+
#define RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY
760+
#define RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY
761+
#endif
762+
743763
// Memory
744764

745765
INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
@@ -1415,6 +1435,8 @@ void __rtsan::InitializeInterceptors() {
14151435
INTERCEPT_FUNCTION(usleep);
14161436
INTERCEPT_FUNCTION(nanosleep);
14171437
INTERCEPT_FUNCTION(sched_yield);
1438+
RTSAN_MAYBE_INTERCEPT_SCHED_GETAFFINITY;
1439+
RTSAN_MAYBE_INTERCEPT_SCHED_SETAFFINITY;
14181440

14191441
INTERCEPT_FUNCTION(accept);
14201442
INTERCEPT_FUNCTION(bind);

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@ TEST(TestRtsanInterceptors, SchedYieldDiesWhenRealtime) {
323323
ExpectNonRealtimeSurvival(Func);
324324
}
325325

326+
#if SANITIZER_LINUX
327+
TEST(TestRtsanInterceptors, SchedGetaffinityDiesWhenRealtime) {
328+
cpu_set_t set{};
329+
auto Func = [&set]() { sched_getaffinity(0, sizeof(set), &set); };
330+
ExpectRealtimeDeath(Func, "sched_getaffinity");
331+
ExpectNonRealtimeSurvival(Func);
332+
}
333+
334+
TEST(TestRtsanInterceptors, SchedSetaffinityDiesWhenRealtime) {
335+
cpu_set_t set{};
336+
auto Func = [&set]() { sched_setaffinity(0, sizeof(set), &set); };
337+
ExpectRealtimeDeath(Func, "sched_setaffinity");
338+
ExpectNonRealtimeSurvival(Func);
339+
}
340+
#endif
341+
326342
/*
327343
Filesystem
328344
*/

0 commit comments

Comments
 (0)