Skip to content

Commit 963b8e3

Browse files
authored
[rtsan] Add sched_yield interceptor (#117084)
This calls the system calls switch_pri and sys_ulock_wait. It also is one of the more straightforwardly rt-unsafe, in that it gives up this thread's timeslice.
1 parent 27923f7 commit 963b8e3

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ INTERCEPTOR(int, nanosleep, const struct timespec *rqtp,
439439
return REAL(nanosleep)(rqtp, rmtp);
440440
}
441441

442+
INTERCEPTOR(int, sched_yield, void) {
443+
__rtsan_notify_intercepted_call("sched_yield");
444+
return REAL(sched_yield)();
445+
}
446+
442447
// Memory
443448

444449
INTERCEPTOR(void *, calloc, SIZE_T num, SIZE_T size) {
@@ -819,6 +824,7 @@ void __rtsan::InitializeInterceptors() {
819824
INTERCEPT_FUNCTION(sleep);
820825
INTERCEPT_FUNCTION(usleep);
821826
INTERCEPT_FUNCTION(nanosleep);
827+
INTERCEPT_FUNCTION(sched_yield);
822828

823829
INTERCEPT_FUNCTION(accept);
824830
INTERCEPT_FUNCTION(bind);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ TEST(TestRtsan, SleepingAThreadDiesWhenRealtime) {
6565
ExpectNonRealtimeSurvival(Func);
6666
}
6767

68+
TEST(TestRtsan, YieldingDiesWhenRealtime) {
69+
auto Func = []() { std::this_thread::yield(); };
70+
ExpectRealtimeDeath(Func);
71+
ExpectNonRealtimeSurvival(Func);
72+
}
73+
6874
TEST(TestRtsan, IfstreamCreationDiesWhenRealtime) {
6975
auto Func = []() { std::ifstream ifs{"./file.txt"}; };
7076
ExpectRealtimeDeath(Func);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ TEST(TestRtsanInterceptors, NanosleepDiesWhenRealtime) {
236236
ExpectNonRealtimeSurvival(Func);
237237
}
238238

239+
TEST(TestRtsanInterceptors, SchedYieldDiesWhenRealtime) {
240+
auto Func = []() { sched_yield(); };
241+
ExpectRealtimeDeath(Func, "sched_yield");
242+
ExpectNonRealtimeSurvival(Func);
243+
}
244+
239245
/*
240246
Filesystem
241247
*/

0 commit comments

Comments
 (0)