From ebb87ccdde2df922f0f61bc720e3036466def53a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 24 Sep 2024 16:28:44 +0300 Subject: [PATCH] Kernel: Add asserts for overflow checks with ms -> ns conversions --- kernel/include/kernel/Thread.h | 6 +++--- kernel/include/kernel/ThreadBlocker.h | 5 +++-- kernel/include/kernel/Timer/Timer.h | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 5813ae91dc..139268bf8a 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -49,11 +49,11 @@ namespace Kernel bool add_signal(int signal); // blocks current thread and returns either on unblock, eintr, spuriously or after timeout - BAN::ErrorOr sleep_or_eintr_ms(uint64_t ms) { return sleep_or_eintr_ns(ms * 1'000'000); } + BAN::ErrorOr sleep_or_eintr_ms(uint64_t ms) { ASSERT(!BAN::Math::will_multiplication_overflow(ms, 1'000'000)); return sleep_or_eintr_ns(ms * 1'000'000); } BAN::ErrorOr sleep_or_eintr_ns(uint64_t ns); BAN::ErrorOr block_or_eintr_indefinite(ThreadBlocker& thread_blocker); - BAN::ErrorOr block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout) { return block_or_eintr_or_timeout_ns(thread_blocker, timeout_ms * 1'000'000, etimedout); } - BAN::ErrorOr block_or_eintr_or_waketime_ms(ThreadBlocker& thread_blocker, uint64_t wake_time_ms, bool etimedout) { return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ms * 1'000'000, etimedout); } + BAN::ErrorOr block_or_eintr_or_timeout_ms(ThreadBlocker& thread_blocker, uint64_t timeout_ms, bool etimedout) { ASSERT(!BAN::Math::will_multiplication_overflow(timeout_ms, 1'000'000)); return block_or_eintr_or_timeout_ns(thread_blocker, timeout_ms * 1'000'000, etimedout); } + BAN::ErrorOr block_or_eintr_or_waketime_ms(ThreadBlocker& thread_blocker, uint64_t wake_time_ms, bool etimedout) { ASSERT(!BAN::Math::will_multiplication_overflow(wake_time_ms, 1'000'000)); return block_or_eintr_or_waketime_ns(thread_blocker, wake_time_ms * 1'000'000, etimedout); } BAN::ErrorOr block_or_eintr_or_timeout_ns(ThreadBlocker& thread_blocker, uint64_t timeout_ns, bool etimedout); BAN::ErrorOr block_or_eintr_or_waketime_ns(ThreadBlocker& thread_blocker, uint64_t wake_time_ns, bool etimedout); diff --git a/kernel/include/kernel/ThreadBlocker.h b/kernel/include/kernel/ThreadBlocker.h index 81e58b3025..8c8cc2d354 100644 --- a/kernel/include/kernel/ThreadBlocker.h +++ b/kernel/include/kernel/ThreadBlocker.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -10,8 +11,8 @@ namespace Kernel { public: void block_indefinite(); - void block_with_timeout_ms(uint64_t timeout_ms) { return block_with_timeout_ns(timeout_ms * 1'000'000); } - void block_with_wake_time_ms(uint64_t wake_time_ms) { return block_with_wake_time_ns(wake_time_ms * 1'000'000); } + void block_with_timeout_ms(uint64_t timeout_ms) { ASSERT(!BAN::Math::will_multiplication_overflow(timeout_ms, 1'000'000)); return block_with_timeout_ns(timeout_ms * 1'000'000); } + void block_with_wake_time_ms(uint64_t wake_time_ms) { ASSERT(!BAN::Math::will_multiplication_overflow(wake_time_ms, 1'000'000)); return block_with_wake_time_ns(wake_time_ms * 1'000'000); } void block_with_timeout_ns(uint64_t timeout_ns); void block_with_wake_time_ns(uint64_t wake_time_ns); void unblock(); diff --git a/kernel/include/kernel/Timer/Timer.h b/kernel/include/kernel/Timer/Timer.h index 1a44be2a45..e292ed12f3 100644 --- a/kernel/include/kernel/Timer/Timer.h +++ b/kernel/include/kernel/Timer/Timer.h @@ -43,7 +43,7 @@ namespace Kernel virtual bool pre_scheduler_sleep_needs_lock() const override; virtual void pre_scheduler_sleep_ns(uint64_t) override; - void sleep_ms(uint64_t ms) const { return sleep_ns(ms * 1'000'000); } + void sleep_ms(uint64_t ms) const { ASSERT(!BAN::Math::will_multiplication_overflow(ms, 1'000'000)); return sleep_ns(ms * 1'000'000); } void sleep_ns(uint64_t ns) const; timespec real_time() const;