From fa743f1630941afb4b4d5cc152f8c61071c6f7b2 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:02:56 +0100 Subject: [PATCH 1/9] mark: aatifsyed/sweep From 00cd7669bdb8b9d04d06d8ae81a37292f10892bf Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:04:58 +0100 Subject: [PATCH 2/9] chore: add rust-toolchain.toml --- rust-toolchain.toml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 rust-toolchain.toml diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..9370ee8 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly-2025-10-13" From 12f0f76a4b6441f8cd51103aacf5bbd80d4ea160 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:07:48 +0100 Subject: [PATCH 3/9] fix: use new prefetch signature as of https://github.com/rust-lang/rust/pull/145626 --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 3624ff6..ec1980f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -111,5 +111,5 @@ pub const fn const_len(con: &[T]) -> usize { #[allow(clippy::not_unsafe_ptr_arg_deref)] #[inline] fn prefetch(p: *const u8, offset: isize) { - unsafe { core::intrinsics::prefetch_read_data(p.offset(offset) as *const u8, 3) }; + unsafe { core::intrinsics::prefetch_read_data::<_, 3>(p.offset(offset) as *const u8) }; } From 45a8c17aab311ccedce5207f643787548dc02dd0 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:08:46 +0100 Subject: [PATCH 4/9] fix: const_size_of_val is stable so does not need feature gate --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 075dfb5..bacd24e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ along with this program. If not, see . */ #![allow(clippy::missing_safety_doc, clippy::uninit_assumed_init, dead_code)] -#![feature(const_size_of_val, core_intrinsics)] +#![feature(core_intrinsics)] pub mod const_config; pub mod const_http; From ae878769285a04f467954dd7632068f61fa204d2 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:10:53 +0100 Subject: [PATCH 5/9] refactor: remove unused util::prefetch --- src/util.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/util.rs b/src/util.rs index ec1980f..56c01c6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -96,8 +96,7 @@ where #[derive(Copy, Clone)] struct Both(A, B); - let arr: Both = - Both(*transmute::<_, *const First>(a.as_ptr()), *transmute::<_, *const Second>(b.as_ptr())); + let arr: Both = Both(*transmute::<_, *const First>(a.as_ptr()), *transmute::<_, *const Second>(b.as_ptr())); transmute(arr) } @@ -106,10 +105,3 @@ where pub const fn const_len(con: &[T]) -> usize { con.len() } - -// _mm_prefetch -#[allow(clippy::not_unsafe_ptr_arg_deref)] -#[inline] -fn prefetch(p: *const u8, offset: isize) { - unsafe { core::intrinsics::prefetch_read_data::<_, 3>(p.offset(offset) as *const u8) }; -} From 9259dd6b662503878f067a26aec67f5cd9ad709c Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:12:44 +0100 Subject: [PATCH 6/9] refactor: trivial improvement to http_date::get_buff_with_date --- src/http_date.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/http_date.rs b/src/http_date.rs index 6d7c899..9c31862 100644 --- a/src/http_date.rs +++ b/src/http_date.rs @@ -8,13 +8,7 @@ #[inline(always)] pub const fn get_buff_with_date() -> [u8; 35] { - let buf: [u8; 35] = [ - // Writing as the following only gives us a reference: b"Date: Thu, 01 Jan 1970 00:00:00 GMT" - b'D', b'a', b't', b'e', b':', b' ', b' ', b' ', b' ', b',', b' ', b'0', b'0', b' ', b' ', b' ', b' ', b' ', b'0', - b'0', b'0', b'0', b' ', b'0', b'0', b':', b'0', b'0', b':', b'0', b'0', b' ', b'G', b'M', b'T', - ]; - - buf + *b"Date: Thu, 01 Jan 1970 00:00:00 GMT" } #[repr(C, align(32))] From 74febff62a87ed9671acf7a2e4a5e8243b4f29b7 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:15:13 +0100 Subject: [PATCH 7/9] fix: trivial UB in http_date::get_http_date this might still be UB because we're not checking the return code... --- src/http_date.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/http_date.rs b/src/http_date.rs index 9c31862..d0f278b 100644 --- a/src/http_date.rs +++ b/src/http_date.rs @@ -26,8 +26,9 @@ const CLOCK_REALTIME: i32 = 0; #[inline(always)] pub fn get_http_date(buf: &mut [u8; 35]) { - let mut ts: timespec = unsafe { core::mem::MaybeUninit::uninit().assume_init() }; - unsafe { clock_gettime(CLOCK_REALTIME, &mut ts as *mut timespec) }; + let mut ts = core::mem::MaybeUninit::uninit(); + unsafe { clock_gettime(CLOCK_REALTIME, ts.as_mut_ptr()) }; + let ts = unsafe { ts.assume_init() }; let secs_since_epoch = ts.tv_sec; From 3b6e09e06ea1dd3b0363f64ccb629a696a229bca Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:20:54 +0100 Subject: [PATCH 8/9] refactor: feature-gate nightly code --- Cargo.toml | 1 + src/epoll.rs | 2 +- src/http_request_path.rs | 2 +- src/lib.rs | 2 +- src/util.rs | 15 +++++++++++++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4778173..0beb509 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ edition = "2021" [features] default = [] +nightly = [] [dependencies] diff --git a/src/epoll.rs b/src/epoll.rs index dcbc6ae..783d924 100644 --- a/src/epoll.rs +++ b/src/epoll.rs @@ -22,7 +22,7 @@ use crate::http_date; use crate::http_request_path; use crate::net; use crate::sys_call; -use core::intrinsics::{likely, unlikely}; +use crate::util::{likely, unlikely}; use core::ops::Add; use core::ops::Sub; diff --git a/src/http_request_path.rs b/src/http_request_path.rs index e43cae5..e9ef4dc 100644 --- a/src/http_request_path.rs +++ b/src/http_request_path.rs @@ -32,7 +32,7 @@ along with this program. If not, see . use core::arch::x86_64::{ __m128i, _mm_cmpestri, _mm_load_si128, _mm_loadu_si128, _SIDD_CMP_EQUAL_ORDERED, _SIDD_UBYTE_OPS, }; -use core::intrinsics::{likely, unlikely}; +use crate::util::{likely, unlikely}; const SPACE: i8 = b' ' as i8; diff --git a/src/lib.rs b/src/lib.rs index bacd24e..4f48c07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ along with this program. If not, see . */ #![allow(clippy::missing_safety_doc, clippy::uninit_assumed_init, dead_code)] -#![feature(core_intrinsics)] +#![cfg_attr(feature = "nightly", feature(core_intrinsics))] pub mod const_config; pub mod const_http; diff --git a/src/util.rs b/src/util.rs index 56c01c6..807b97d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -16,6 +16,21 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ +#[cfg(feature = "nightly")] +pub use core::intrinsics::{likely, unlikely}; + +#[cfg(not(feature = "nightly"))] +#[inline] +pub const fn likely(b: bool) -> bool { + b +} + +#[cfg(not(feature = "nightly"))] +#[inline] +pub const fn unlikely(b: bool) -> bool { + b +} + const _SC_NPROCESSORS_ONLN: i32 = 84; extern "C" { From f7596a2315faa4f597e674f1aad15848e7226c81 Mon Sep 17 00:00:00 2001 From: Aatif Syed Date: Tue, 14 Oct 2025 19:27:28 +0100 Subject: [PATCH 9/9] refactor: trivial array initialization --- src/epoll.rs | 8 ++++---- src/net.rs | 2 +- src/util.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/epoll.rs b/src/epoll.rs index 783d924..798562d 100644 --- a/src/epoll.rs +++ b/src/epoll.rs @@ -133,10 +133,10 @@ fn threaded_worker( let mut saved_event: AlignedEpollEvent = unsafe { core::mem::zeroed() }; saved_event.0.events = EPOLLIN; - let mut reqbuf: ReqBufAligned = unsafe { core::mem::zeroed() }; + let mut reqbuf: ReqBufAligned = ReqBufAligned([0; _]); // Init state for tracking request buffer position across events - let mut reqbuf_cur_addr: [isize; MAX_CONN] = unsafe { core::mem::zeroed() }; + let mut reqbuf_cur_addr: [isize; MAX_CONN] = [0; _]; { let reqbuf_start_address = &mut reqbuf.0[0] as *mut _ as isize; (0..reqbuf_cur_addr.len()).for_each(|i| { @@ -146,9 +146,9 @@ fn threaded_worker( // If we receive part of a request due to a split read, we track how many bytes were unparsable so // this number of bytes can be prepended to the next time we try to parse the buffer - let mut reqbuf_residual: [usize; MAX_CONN] = unsafe { core::mem::zeroed() }; + let mut reqbuf_residual: [usize; MAX_CONN] = [0; _]; - let mut resbuf: ResBufAligned = unsafe { core::mem::zeroed() }; + let mut resbuf: ResBufAligned = ResBufAligned([0; _]); let resbuf_start_address = &mut resbuf.0[0] as *mut _ as isize; let mut epoll_wait_type = EPOLL_TIMEOUT_BLOCKING; diff --git a/src/net.rs b/src/net.rs index 4efcc19..a1520cb 100644 --- a/src/net.rs +++ b/src/net.rs @@ -147,7 +147,7 @@ pub fn get_listener_fd(port: u16) -> (isize, sockaddr_in, u32) { sin_family: AF_INET as u16, sin_port: htons(port), sin_addr: in_addr { s_addr: htonl(INADDR_ANY) }, - sin_zero: core::mem::zeroed(), + sin_zero: [0; _], }; sys_call!(SYS_BIND as isize, fd_listener, &addr as *const _ as _, core::mem::size_of_val(&addr) as isize); diff --git a/src/util.rs b/src/util.rs index 807b97d..bd2a26a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -68,12 +68,12 @@ const CURRENT_THREAD_CONTROL_PID: i32 = 0; #[inline] pub fn set_current_thread_cpu_affinity_to(cpu_num: usize) { - let mut set: cpu_set_t = unsafe { core::mem::MaybeUninit::zeroed().assume_init() }; + let mut set: cpu_set_t = cpu_set_t([0; _]); unsafe { sched_getaffinity(CURRENT_THREAD_CONTROL_PID, core::mem::size_of::(), &mut set) }; if !cpu_isset(cpu_num, &set) { eprintln!("Cannot set affinity for cpu {}", cpu_num); } else { - let mut set_control: cpu_set_t = unsafe { core::mem::MaybeUninit::zeroed().assume_init() }; + let mut set_control: cpu_set_t = cpu_set_t([0; _]); cpu_set(cpu_num, &mut set_control); unsafe { sched_setaffinity(0, core::mem::size_of::(), &set_control) }; }