Repository navigation
time: un-shift absolute deadlines only on the wall clock - #359
Conversation
| } | ||
|
|
||
| /// Read tv_sec from child memory at `addr`, subtract `offset`, write back. | ||
| fn adjust_tv_sec(notif_fd: RawFd, notif_id: u64, pid: u32, addr: u64, offset: i64) { |
There was a problem hiding this comment.
This rewrites the caller's timespec in place, so a reused deadline is un-shifted twice. A periodic loop (ts.tv_sec += 1; clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL)) or an EINTR retry with the same ts sleeps for decades on the second call. Pre-existing, but this PR makes it reachable for realtime clock_nanosleep. Could you restore the original value after the syscall (or keep the rewrite out of the child's view), and add a second iteration reusing the struct to the test?
There was a problem hiding this comment.
Good catch, thanks. Restoring it after the syscall doesn't work with Continue, since the supervisor never sees the call come back, and writing the old value back right after the response would race the kernel's read. So I went the other way in bf364c2: for an absolute CLOCK_REALTIME clock_nanosleep the timespec is only read, and the supervisor sleeps until the real deadline in a patient deferral (like the FIFO open wait) and answers 0. Because WAIT_KILLABLE_RECV holds signals until the notification is answered, it checks every 20ms for a pending signal the thread doesn't block and answers EINTR, and it stops when the notification id goes invalid. One difference is that the wakeup goes through the tokio timer, so it's millisecond rather than microsecond precise.
The test now sleeps twice reusing the same struct, and also retries with the same struct after an EINTR. The reuse case hangs until the 30s guard on the previous commit, and the EINTR case fails without the signal check.
timerfd_settime and timer_settime still adjust the itimerspec in place, so reusing one there has the same problem. The timerfd one could be set on the child's behalf through pidfd_getfd, POSIX timers can't, so I left both as they were. Happy to do the timerfd part here or in a follow-up if you want it.
…instead of rewriting the caller's timespec
| // deadline. One that can't be read or isn't valid goes to the kernel, | ||
| // which fails it as it would without time_start. | ||
| return match read_timespec(notif_fd, notif.id, notif.pid, ts_addr) { | ||
| Some((sec, nsec)) if (0..NANOS_PER_SEC).contains(&nsec) => { |
There was a problem hiding this comment.
Negative seconds are accepted as a sleep deadline ? Linux returns EINVAL for a negative tv_sec.
There was a problem hiding this comment.
Good catch, it slept until the past deadline and returned 0. Fixed in c282798: a negative tv_sec now goes to the kernel untouched, so it fails with EINVAL like it would without time_start, and the test checks that for a negative second and for an out of range nanosecond.
…ernel so it still fails with EINVAL
With time_start set, the vDSO stubs add the offset to CLOCK_REALTIME and CLOCK_REALTIME_COARSE only, the monotonic clocks are left alone. handle_timer had it the other way round for clock_nanosleep: it subtracted the offset from TIMER_ABSTIME deadlines on the monotonic clocks and left CLOCK_REALTIME deadlines untouched. timerfd_settime and timer_settime subtracted it whatever clock the timer was created on. With a start time in the past the offset is negative, so every absolute monotonic deadline got pushed decades out. Python 3.11+ implements time.sleep() as clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME), so python3 -c 'import time; time.sleep(1)' in a sandbox with a 2000 start time never returns, and an absolute realtime deadline fires straight away instead.
Now only deadlines on the shifted clocks lose the offset. clock_nanosleep carries the clock in its arguments. For timerfd_settime I read it from the clockid line in /proc//fdinfo/, and for timer_settime from the ClockID line in /proc//timers. If the clock can't be found the call goes through unchanged.
I added an integration test that sleeps 0.2s under a 2000 start time through time.sleep, clock_nanosleep, a timerfd and a POSIX timer, on both CLOCK_REALTIME and CLOCK_MONOTONIC, and checks each one takes about that long. On main it hangs until the 30s guard in the test fires, with the change it passes. There are unit tests for the /proc//timers parsing too, and I ran cargo test -p sandlock-core --lib time:: and the test_determinism integration tests.