Skip to content

Commit b373278

Browse files
authored
[Sanitizers] Intercept timer_create (#112285)
1 parent 97ccd86 commit b373278

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

compiler-rt/lib/hwasan/hwasan_platform_interceptors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@
200200
#undef SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
201201
#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID 0
202202

203+
#undef SANITIZER_INTERCEPT_TIMER_CREATE
204+
#define SANITIZER_INTERCEPT_TIMER_CREATE 0
205+
203206
#undef SANITIZER_INTERCEPT_GETITIMER
204207
#define SANITIZER_INTERCEPT_GETITIMER 0
205208

compiler-rt/lib/msan/tests/msan_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4881,4 +4881,27 @@ TEST(MemorySanitizer, throw_catch) {
48814881
// pass
48824882
}
48834883
}
4884+
4885+
#if defined(__linux__)
4886+
TEST(MemorySanitizer, timer_create) {
4887+
timer_t timer;
4888+
EXPECT_POISONED(timer);
4889+
int res = timer_create(CLOCK_REALTIME, nullptr, &timer);
4890+
ASSERT_EQ(0, res);
4891+
EXPECT_NOT_POISONED(timer);
4892+
4893+
// Make sure the timer is usable.
4894+
struct itimerspec cur_value {};
4895+
cur_value.it_value.tv_sec = 1;
4896+
EXPECT_EQ(0, timer_settime(timer, 0, &cur_value, nullptr));
4897+
4898+
timer_t timer2;
4899+
EXPECT_POISONED(timer2);
4900+
// Use an invalid clock_id to make timer_create fail.
4901+
res = timer_create(INT_MAX, nullptr, &timer2);
4902+
ASSERT_EQ(-1, res);
4903+
EXPECT_POISONED(timer2);
4904+
timer_delete(timer);
4905+
}
4906+
#endif
48844907
} // namespace

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,24 @@ INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
22892289
#define INIT_CLOCK_GETCPUCLOCKID
22902290
#endif
22912291

2292+
#if SANITIZER_INTERCEPT_TIMER_CREATE
2293+
INTERCEPTOR(int, timer_create, __sanitizer_clockid_t clockid, void *sevp,
2294+
__sanitizer_timer_t *timer) {
2295+
void *ctx;
2296+
COMMON_INTERCEPTOR_ENTER(ctx, timer_create, clockid, sevp, timer);
2297+
int res = REAL(timer_create)(clockid, sevp, timer);
2298+
if (!res && timer) {
2299+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, timer, sizeof *timer);
2300+
}
2301+
return res;
2302+
}
2303+
2304+
# define INIT_TIMER_CREATE \
2305+
COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(timer_create, "GLIBC_2.3.3");
2306+
#else
2307+
# define INIT_TIMER_CREATE
2308+
#endif
2309+
22922310
#if SANITIZER_INTERCEPT_GETITIMER
22932311
INTERCEPTOR(int, getitimer, int which, void *curr_value) {
22942312
void *ctx;
@@ -10266,6 +10284,7 @@ static void InitializeCommonInterceptors() {
1026610284
INIT_SETPWENT;
1026710285
INIT_CLOCK_GETTIME;
1026810286
INIT_CLOCK_GETCPUCLOCKID;
10287+
INIT_TIMER_CREATE;
1026910288
INIT_GETITIMER;
1027010289
INIT_TIME;
1027110290
INIT_GLOB;

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@
237237
(SI_FREEBSD || SI_NETBSD || SI_LINUX || SI_SOLARIS)
238238
#define SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID \
239239
(SI_LINUX || SI_FREEBSD || SI_NETBSD)
240+
// TODO: This should be SI_POSIX, adding Linux first until I have time
241+
// to verify all timer_t typedefs on other platforms.
242+
#define SANITIZER_INTERCEPT_TIMER_CREATE SI_LINUX
240243
#define SANITIZER_INTERCEPT_GETITIMER SI_POSIX
241244
#define SANITIZER_INTERCEPT_TIME SI_POSIX
242245
#define SANITIZER_INTERCEPT_GLOB (SI_GLIBC || SI_SOLARIS)

compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,10 @@ extern const int si_SEGV_ACCERR;
15171517

15181518
#define SIGACTION_SYMNAME sigaction
15191519

1520+
# if SANITIZER_LINUX
1521+
typedef void *__sanitizer_timer_t;
1522+
# endif
1523+
15201524
#endif // SANITIZER_LINUX || SANITIZER_APPLE
15211525

15221526
#endif

0 commit comments

Comments
 (0)