subetha-cxc 0.1.0

MMF-backed cross-process IPC primitives for SubEtha: SharedRing, SharedHashMap, SharedRWLock, SharedSemaphore, SharedLRUCache, OwnerLease, HeartbeatTable, plus 30+ more. One byte layout serves cross-thread, cross-process, and disk-persistent.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! `large_pages`: Windows-only large-page memory helpers. The
//! Windows sibling of the Linux transparent-huge-page path, with
//! one extra capability that path does not have: cross-process
//! named sections (`LargePageSection`).
//!
//! Two primitives:
//!
//! - [`LargePageRegion`]: private (single-process) memory backed
//!   by large pages via `VirtualAlloc(MEM_LARGE_PAGES)`. Direct
//!   parity with Linux `HugepageRegion`.
//! - [`LargePageSection`]: NAMED pagefile-backed section created
//!   with `CreateFileMappingW(SEC_LARGE_PAGES)` + mapped via
//!   `MapViewOfFile`. Two processes that open the same section
//!   name share the same large-page-backed physical memory - the
//!   "huge memory table shared across processes" capability.
//!
//! # The privilege gate (different from Linux)
//!
//! Linux gates hugepages on RESERVATION (`/proc/sys/vm/nr_hugepages`);
//! Windows gates large pages on an ACCOUNT PRIVILEGE:
//! `SeLockMemoryPrivilege` ("Lock pages in memory" in Local
//! Security Policy). The privilege must be (a) granted to the
//! user account (admin grants it in secpol.msc under Local
//! Policies > User Rights Assignment, then the user logs off and
//! on again so the token picks it up), AND (b) enabled in the
//! process token at runtime via [`enable_lock_memory_privilege`].
//!
//! When the privilege is absent, allocation fails with
//! `ERROR_PRIVILEGE_NOT_HELD` (1314); callers fall back to
//! standard 4KB-page allocation exactly as they do on Linux when
//! no hugepages are reserved.
//!
//! # File-backed mappings can NEVER use large pages on Windows
//!
//! `SEC_LARGE_PAGES` requires the section be pagefile-backed
//! (`INVALID_HANDLE_VALUE` as the file handle). A mapping over a
//! real on-disk file is always 4KB-paged by OS design. So the
//! substrate's File locale stays standard-paged on Windows; the
//! Anon and named-section locales are the large-page candidates.

#![cfg(windows)]

use std::io;

use windows_sys::Win32::Foundation::{
    CloseHandle, GetLastError, ERROR_NOT_ALL_ASSIGNED, HANDLE,
    INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::Security::{
    AdjustTokenPrivileges, LookupPrivilegeValueW, LUID_AND_ATTRIBUTES,
    SE_PRIVILEGE_ENABLED, TOKEN_ADJUST_PRIVILEGES, TOKEN_PRIVILEGES,
    TOKEN_QUERY,
};
use windows_sys::Win32::System::Memory::{
    CreateFileMappingW, GetLargePageMinimum, MapViewOfFile,
    OpenFileMappingW, UnmapViewOfFile, VirtualAlloc, VirtualFree,
    FILE_MAP_ALL_ACCESS, MEM_COMMIT, MEM_LARGE_PAGES, MEM_RELEASE,
    MEM_RESERVE, PAGE_READWRITE, SEC_COMMIT, SEC_LARGE_PAGES,
};
use windows_sys::Win32::System::Threading::{
    GetCurrentProcess, OpenProcessToken,
};

/// Win32 error code surfaced when the calling token lacks
/// `SeLockMemoryPrivilege`. Callers match on
/// `err.raw_os_error() == Some(ERROR_PRIVILEGE_NOT_HELD as i32)`
/// to distinguish "privilege not granted" from transient
/// allocation failures.
pub const ERROR_PRIVILEGE_NOT_HELD: u32 = 1314;

/// Win32 error code for "not enough contiguous physical memory to
/// satisfy a large-page allocation" (large pages are never paged
/// out, so the system needs free contiguous physical RAM).
pub const ERROR_NO_SYSTEM_RESOURCES: u32 = 1450;

/// The minimum large-page size on this host, in bytes (2MB on all
/// current x86_64 / ARM64 Windows). Returns 0 when the processor
/// or OS does not support large pages at all.
pub fn large_page_minimum() -> usize {
    unsafe { GetLargePageMinimum() }
}

/// Enable `SeLockMemoryPrivilege` in the current process token.
///
/// This can only ENABLE a privilege the account already HOLDS.
/// If "Lock pages in memory" has not been granted to the user in
/// Local Security Policy, `AdjustTokenPrivileges` reports
/// `ERROR_NOT_ALL_ASSIGNED` and this function returns an error
/// naming that exact condition.
pub fn enable_lock_memory_privilege() -> io::Result<()> {
    // "SeLockMemoryPrivilege" as UTF-16, NUL-terminated.
    let name: Vec<u16> = "SeLockMemoryPrivilege\0".encode_utf16().collect();

    unsafe {
        let mut token: HANDLE = std::ptr::null_mut();
        if OpenProcessToken(
            GetCurrentProcess(),
            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
            &mut token,
        ) == 0
        {
            return Err(io::Error::last_os_error());
        }

        let mut tp = TOKEN_PRIVILEGES {
            PrivilegeCount: 1,
            Privileges: [LUID_AND_ATTRIBUTES {
                Luid: std::mem::zeroed(),
                Attributes: SE_PRIVILEGE_ENABLED,
            }],
        };
        if LookupPrivilegeValueW(
            std::ptr::null(),
            name.as_ptr(),
            &mut tp.Privileges[0].Luid,
        ) == 0
        {
            let e = io::Error::last_os_error();
            CloseHandle(token);
            return Err(e);
        }

        let ok = AdjustTokenPrivileges(
            token,
            0,
            &tp,
            0,
            std::ptr::null_mut(),
            std::ptr::null_mut(),
        );
        // AdjustTokenPrivileges returns success even when nothing
        // was assigned; the real verdict is in GetLastError.
        let last = GetLastError();
        CloseHandle(token);

        if ok == 0 {
            return Err(io::Error::from_raw_os_error(last as i32));
        }
        if last == ERROR_NOT_ALL_ASSIGNED {
            return Err(io::Error::new(
                io::ErrorKind::PermissionDenied,
                "SeLockMemoryPrivilege is not granted to this account. \
                 Grant 'Lock pages in memory' in Local Security Policy \
                 (secpol.msc > Local Policies > User Rights Assignment), \
                 then log off and on so the token picks it up.",
            ));
        }
        Ok(())
    }
}

/// Round `bytes` up to the next multiple of the host's large-page
/// minimum. Returns `None` when the host does not support large
/// pages.
pub fn round_to_large_page(bytes: usize) -> Option<usize> {
    let min = large_page_minimum();
    if min == 0 {
        return None;
    }
    Some(bytes.div_ceil(min) * min)
}

/// Private (single-process) large-page-backed memory region.
/// Windows parity for the Linux `HugepageRegion`.
pub struct LargePageRegion {
    ptr: *mut u8,
    len: usize,
}

unsafe impl Send for LargePageRegion {}
unsafe impl Sync for LargePageRegion {}

impl LargePageRegion {
    /// Allocate at least `bytes` of large-page-backed private
    /// memory (rounded up to the large-page minimum). The caller
    /// must have run [`enable_lock_memory_privilege`] first.
    ///
    /// Failure modes callers handle:
    /// - `ERROR_PRIVILEGE_NOT_HELD` (1314): account lacks the
    ///   privilege; fall back to standard allocation.
    /// - `ERROR_NO_SYSTEM_RESOURCES` (1450): not enough contiguous
    ///   physical RAM right now; fall back or retry later.
    pub fn allocate(bytes: usize) -> io::Result<Self> {
        assert!(bytes > 0);
        let len = round_to_large_page(bytes).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "this host does not support large pages (GetLargePageMinimum() == 0)",
            )
        })?;
        let raw = unsafe {
            VirtualAlloc(
                std::ptr::null(),
                len,
                MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES,
                PAGE_READWRITE,
            )
        };
        if raw.is_null() {
            return Err(io::Error::last_os_error());
        }
        Ok(Self { ptr: raw as *mut u8, len })
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
    }

    pub fn as_slice(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
    }

    pub fn len(&self) -> usize { self.len }
    pub fn is_empty(&self) -> bool { self.len == 0 }
}

impl Drop for LargePageRegion {
    fn drop(&mut self) {
        unsafe { VirtualFree(self.ptr as *mut core::ffi::c_void, 0, MEM_RELEASE) };
    }
}

/// Cross-process NAMED large-page section. Pagefile-backed
/// (`SEC_LARGE_PAGES` requires it); two processes that pass the
/// same `name` share the same large-page-backed physical memory.
///
/// This is the "huge memory table shared across processes"
/// primitive: a multi-GB lookup table mapped once per process
/// with 2MB TLB entries instead of 4KB.
pub struct LargePageSection {
    handle: HANDLE,
    ptr: *mut u8,
    len: usize,
}

unsafe impl Send for LargePageSection {}
unsafe impl Sync for LargePageSection {}

impl LargePageSection {
    /// Create a named large-page section of at least `bytes`
    /// (rounded up to the large-page minimum). The name is
    /// kernel-namespace-global (`Local\` prefix recommended for
    /// per-session scoping, e.g. `Local\my_table`).
    pub fn create(name: &str, bytes: usize) -> io::Result<Self> {
        assert!(bytes > 0);
        let len = round_to_large_page(bytes).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "this host does not support large pages (GetLargePageMinimum() == 0)",
            )
        })?;
        let wname: Vec<u16> = name.encode_utf16().chain(std::iter::once(0)).collect();
        let handle = unsafe {
            CreateFileMappingW(
                INVALID_HANDLE_VALUE, // pagefile-backed (required for SEC_LARGE_PAGES)
                std::ptr::null(),
                PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES,
                (len >> 32) as u32,
                (len & 0xFFFF_FFFF) as u32,
                wname.as_ptr(),
            )
        };
        if handle.is_null() {
            return Err(io::Error::last_os_error());
        }
        Self::map(handle, len)
    }

    /// Open an existing named large-page section created by
    /// another process (or this one). `bytes` must match the
    /// creator's request (it determines the view length).
    pub fn open(name: &str, bytes: usize) -> io::Result<Self> {
        let len = round_to_large_page(bytes).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::Unsupported,
                "this host does not support large pages (GetLargePageMinimum() == 0)",
            )
        })?;
        let wname: Vec<u16> = name.encode_utf16().chain(std::iter::once(0)).collect();
        let handle = unsafe {
            OpenFileMappingW(FILE_MAP_ALL_ACCESS, 0, wname.as_ptr())
        };
        if handle.is_null() {
            return Err(io::Error::last_os_error());
        }
        Self::map(handle, len)
    }

    fn map(handle: HANDLE, len: usize) -> io::Result<Self> {
        let ptr = unsafe {
            MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, len)
        };
        if ptr.Value.is_null() {
            let e = io::Error::last_os_error();
            unsafe { CloseHandle(handle) };
            return Err(e);
        }
        Ok(Self {
            handle,
            ptr: ptr.Value as *mut u8,
            len,
        })
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
    }

    pub fn as_slice(&self) -> &[u8] {
        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
    }

    pub fn len(&self) -> usize { self.len }
    pub fn is_empty(&self) -> bool { self.len == 0 }
}

impl Drop for LargePageSection {
    fn drop(&mut self) {
        unsafe {
            UnmapViewOfFile(
                windows_sys::Win32::System::Memory::MEMORY_MAPPED_VIEW_ADDRESS {
                    Value: self.ptr as *mut core::ffi::c_void,
                },
            );
            CloseHandle(self.handle);
        }
    }
}

impl crate::spsc_ring::RegionOwner for LargePageRegion {
    fn region_ptr(&mut self) -> *mut u8 {
        self.as_mut_slice().as_mut_ptr()
    }
    fn region_len(&self) -> usize {
        self.len()
    }
}

impl crate::spsc_ring::RegionOwner for LargePageSection {
    fn region_ptr(&mut self) -> *mut u8 {
        self.as_mut_slice().as_mut_ptr()
    }
    fn region_len(&self) -> usize {
        self.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn large_page_minimum_is_pow2_or_zero() {
        let min = large_page_minimum();
        // Every x86_64 / ARM64 Windows reports 2MB; 0 means the
        // host genuinely lacks support. Both are valid; anything
        // else (non-pow2) is a binding bug.
        if min != 0 {
            assert!(min.is_power_of_two(), "large-page min {min} not a power of 2");
            assert!(min >= 2 * 1024 * 1024, "large-page min {min} below 2MB");
        }
    }

    #[test]
    fn round_to_large_page_rounds_up() {
        if large_page_minimum() == 0 {
            // Host lacks large pages entirely; the rounding helper
            // contract is None.
            assert_eq!(round_to_large_page(1), None);
            return;
        }
        let min = large_page_minimum();
        assert_eq!(round_to_large_page(1), Some(min));
        assert_eq!(round_to_large_page(min), Some(min));
        assert_eq!(round_to_large_page(min + 1), Some(2 * min));
    }

    /// Exact-contract test: allocation either succeeds (account
    /// holds SeLockMemoryPrivilege) and the memory is usable, or
    /// fails with PRECISELY the documented privilege /
    /// resource error. Any other outcome is a bug.
    #[test]
    fn allocate_succeeds_or_fails_with_documented_error() {
        if large_page_minimum() == 0 {
            return; // host lacks large pages; nothing to assert
        }
        // Try to enable the privilege; remember the verdict.
        let priv_ok = enable_lock_memory_privilege().is_ok();

        match LargePageRegion::allocate(1) {
            Ok(mut region) => {
                assert!(priv_ok, "allocation succeeded but privilege-enable failed");
                assert_eq!(region.len() % large_page_minimum(), 0);
                // Write + read through the mapping.
                let last = region.len() - 1;
                let s = region.as_mut_slice();
                s[0] = 0xAB;
                s[last] = 0xCD;
                assert_eq!(region.as_slice()[0], 0xAB);
                assert_eq!(region.as_slice()[last], 0xCD);
            }
            Err(e) => {
                let code = e.raw_os_error().unwrap_or(-1) as u32;
                assert!(
                    code == ERROR_PRIVILEGE_NOT_HELD
                        || code == ERROR_NO_SYSTEM_RESOURCES
                        || !priv_ok,
                    "allocation failed with undocumented error: {e:?} (code {code})"
                );
            }
        }
    }

    /// Same exact contract for the named cross-process section.
    #[test]
    fn section_succeeds_or_fails_with_documented_error() {
        if large_page_minimum() == 0 {
            return;
        }
        let priv_ok = enable_lock_memory_privilege().is_ok();
        let name = format!("Local\\subetha_lp_test_{}", std::process::id());

        match LargePageSection::create(&name, 1) {
            Ok(mut section) => {
                assert!(priv_ok, "section create succeeded but privilege-enable failed");
                let s = section.as_mut_slice();
                s[0] = 0x5A;
                // Open a second view of the SAME section (cross-
                // handle, same process) and verify the byte is
                // visible through it: proves the views alias the
                // same physical large pages.
                let view2 = LargePageSection::open(&name, 1).expect("open second view");
                assert_eq!(view2.as_slice()[0], 0x5A);
            }
            Err(e) => {
                let code = e.raw_os_error().unwrap_or(-1) as u32;
                assert!(
                    code == ERROR_PRIVILEGE_NOT_HELD
                        || code == ERROR_NO_SYSTEM_RESOURCES
                        || !priv_ok,
                    "section create failed with undocumented error: {e:?} (code {code})"
                );
            }
        }
    }
}