-
Notifications
You must be signed in to change notification settings - Fork 26
Saturate timerfd timeout conversions #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+186
−28
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright 2026 elfuse contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <poll.h> | ||
| #include <unistd.h> | ||
| #include <time.h> | ||
| #include <sys/syscall.h> | ||
| #include <sys/timerfd.h> | ||
|
|
||
| #include "test-harness.h" | ||
|
|
||
| static int64_t timespec_ns(struct timespec value) | ||
| { | ||
| if (value.tv_sec < 0 || value.tv_sec > INT64_MAX / 1000000000 || | ||
| value.tv_nsec < 0 || value.tv_nsec >= 1000000000) | ||
| return -1; | ||
|
|
||
| int64_t whole = value.tv_sec * 1000000000; | ||
| if (value.tv_nsec > INT64_MAX - whole) | ||
| return -1; | ||
| return whole + value.tv_nsec; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| int passes = 0, fails = 0; | ||
| const struct { | ||
| const char *name; | ||
| int clockid, flags; | ||
| int64_t seconds, min_remaining_sec; | ||
| bool interval, rearm; | ||
| } cases[] = { | ||
| {"relative nanosecond overflow", CLOCK_MONOTONIC, 0, | ||
| INT64_MAX / 1000000000, 1000000000, false, false}, | ||
| {"relative microsecond overflow", CLOCK_MONOTONIC, 0, | ||
| INT64_MAX / 1000000, 1000000000, false, false}, | ||
| {"relative maximum seconds", CLOCK_MONOTONIC, 0, INT64_MAX, 1000000000, | ||
| false, false}, | ||
| {"interval nanosecond overflow", CLOCK_MONOTONIC, 0, | ||
| INT64_MAX / 1000000000, 30, true, false}, | ||
| {"absolute monotonic overflow", CLOCK_MONOTONIC, TFD_TIMER_ABSTIME, | ||
| INT64_MAX / 1000000000, 1000000000, false, false}, | ||
| {"absolute realtime overflow", CLOCK_REALTIME, TFD_TIMER_ABSTIME, | ||
| INT64_MAX / 1000000000, 1000000000, false, false}, | ||
| {"large interval rearm", CLOCK_MONOTONIC, 0, INT64_MAX / 1000000000, | ||
| 1000000000, true, true}, | ||
| }; | ||
|
|
||
| for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { | ||
| TEST(cases[i].name); | ||
| int fd = timerfd_create(cases[i].clockid, TFD_NONBLOCK | TFD_CLOEXEC); | ||
| if (fd < 0) { | ||
| FAIL("timerfd_create"); | ||
| continue; | ||
| } | ||
|
|
||
| struct timespec large = {.tv_sec = cases[i].seconds, | ||
| .tv_nsec = 999999999}; | ||
| struct itimerspec requested = {0}; | ||
| if (cases[i].interval) { | ||
| if (cases[i].rearm) | ||
| requested.it_value.tv_nsec = 20000000; | ||
| else | ||
| requested.it_value.tv_sec = 60; | ||
| requested.it_interval = large; | ||
| } else { | ||
| requested.it_value = large; | ||
| } | ||
| int64_t before_ns = 0; | ||
| if (cases[i].flags & TFD_TIMER_ABSTIME) { | ||
| /* Bypass the vDSO so bounds use the timer backend's clock. */ | ||
| struct timespec before; | ||
| if (syscall(SYS_clock_gettime, cases[i].clockid, &before) < 0 || | ||
| (before_ns = timespec_ns(before)) < 0) { | ||
| FAIL("clock_gettime before settime"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
| } | ||
| if (timerfd_settime(fd, cases[i].flags, &requested, NULL) < 0) { | ||
| FAIL("timerfd_settime rejected a valid large timeout"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
|
|
||
| if (cases[i].rearm) { | ||
| struct pollfd pfd = {.fd = fd, .events = POLLIN}; | ||
| if (poll(&pfd, 1, 5000) != 1 || !(pfd.revents & POLLIN)) { | ||
| FAIL("first timer expiration not readable"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
| uint64_t expirations = 0; | ||
| if (read(fd, &expirations, sizeof(expirations)) != | ||
| (ssize_t) sizeof(expirations) || | ||
| expirations == 0) { | ||
| FAIL("read first timer expiration"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| struct itimerspec current; | ||
| if (timerfd_gettime(fd, ¤t) < 0) { | ||
| FAIL("timerfd_gettime"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
| int64_t remaining_ns = timespec_ns(current.it_value); | ||
| bool value_ok = remaining_ns >= 0 && | ||
| current.it_value.tv_sec > cases[i].min_remaining_sec; | ||
| if (cases[i].flags & TFD_TIMER_ABSTIME) { | ||
| struct timespec after; | ||
| if (syscall(SYS_clock_gettime, cases[i].clockid, &after) < 0) { | ||
| FAIL("clock_gettime after gettime"); | ||
| close(fd); | ||
| continue; | ||
| } | ||
| int64_t after_ns = timespec_ns(after); | ||
| value_ok = value_ok && after_ns >= before_ns && | ||
| remaining_ns >= INT64_MAX - after_ns && | ||
| remaining_ns <= INT64_MAX - before_ns; | ||
| } | ||
| uint64_t count; | ||
| errno = 0; | ||
| ssize_t got = read(fd, &count, sizeof(count)); | ||
| int read_errno = errno; | ||
| bool interval_ok = | ||
| !cases[i].interval || | ||
| (current.it_interval.tv_sec == INT64_MAX / 1000000000 && | ||
| current.it_interval.tv_nsec == INT64_MAX % 1000000000); | ||
| if (value_ok && interval_ok && got == -1 && read_errno == EAGAIN) { | ||
| PASS(); | ||
| } else { | ||
| FAIL("large timer returned an invalid remaining time or interval"); | ||
| printf(" remaining=%lld.%09ld interval=%lld.%09ld read=%ld\n", | ||
| (long long) current.it_value.tv_sec, | ||
| current.it_value.tv_nsec, | ||
| (long long) current.it_interval.tv_sec, | ||
| current.it_interval.tv_nsec, (long) got); | ||
| } | ||
| if (cases[i].rearm) { | ||
| TEST("rearmed timer old value"); | ||
| struct itimerspec disarmed = {0}, old = {0}; | ||
| EXPECT_TRUE(timerfd_settime(fd, 0, &disarmed, &old) == 0 && | ||
| timespec_ns(old.it_value) >= 0 && | ||
| old.it_value.tv_sec > cases[i].min_remaining_sec && | ||
| old.it_interval.tv_sec == INT64_MAX / 1000000000 && | ||
| old.it_interval.tv_nsec == INT64_MAX % 1000000000, | ||
| "disarm returned invalid old timer state"); | ||
| } | ||
| close(fd); | ||
| } | ||
|
|
||
| SUMMARY("test-timerfd-overflow"); | ||
| return fails > 0 ? 1 : 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
timespec_to_ns_satsaturates atsec > TIMESPEC_SEC_MAX;ktime_setsaturates atsec >= KTIME_SEC_MAX, same 9223372036. For{9223372036, 0}Linux storesKTIME_MAXandtimerfd_gettimereports9223372036.854775807, this reports9223372036.000000000. 0.85 s on a 292 year timer, so the behavior may not be worth changing, but the comment above claims the conversion matchestimespec64_to_ktimeand the thresholds differ. Either clampsec >= TIMESPEC_SEC_MAXhere or soften the claim.