Skip to content

RamBlock::new uses libc::mmap and fails to compile on Windows #3

@luojia65

Description

@luojia65

RamBlock::new in memory/src/ram.rs currently uses libc::mmap directly:

pub fn new(size: u64) -> Self {
    // SAFETY: mmap with MAP_ANONYMOUS returns a fresh
    // zero-filled mapping or MAP_FAILED.
    let ptr = unsafe {
        libc::mmap(
            ptr::null_mut(),
            size as usize,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
            -1,
            0,
        ) as *mut u8
    };
    assert!(
        !ptr.is_null() && ptr != libc::MAP_FAILED as *mut u8,
        "mmap failed for size {size}"
    );
    Self { ptr, size }
}

This implementation is Unix / Linux-specific and fails to compile on Windows.

Problem

The code assumes the availability of libc::mmap, libc::munmap, and related constants such as PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANONYMOUS, and MAP_FAILED. These are not available in libc on Windows, so the project cannot be built there.

Reproduction

On Windows PowerShell:

PS D:\RustSBI\machina> cargo build
   Compiling machina-core v0.1.0 (D:\RustSBI\machina\core)
   Compiling libc v0.2.183
   Compiling machina-memory v0.1.0 (D:\RustSBI\machina\memory)
error[E0425]: cannot find function `mmap` in crate `libc`
  --> memory\src\ram.rs:17:19
   |
17 |             libc::mmap(
   |                   ^^^^ not found in `libc`

error[E0425]: cannot find value `PROT_READ` in crate `libc`
  --> memory\src\ram.rs:20:23
   |
20 |                 libc::PROT_READ | libc::PROT_WRITE,
   |                       ^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `PROT_WRITE` in crate `libc`
  --> memory\src\ram.rs:20:41
   |
20 |                 libc::PROT_READ | libc::PROT_WRITE,
   |                                         ^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `MAP_PRIVATE` in crate `libc`
  --> memory\src\ram.rs:21:23
   |
21 |                 libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
   |                       ^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `MAP_ANONYMOUS` in crate `libc`
  --> memory\src\ram.rs:21:43
   |
21 |                 libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
   |                                           ^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find value `MAP_FAILED` in crate `libc`
  --> memory\src\ram.rs:27:44
   |
27 |             !ptr.is_null() && ptr != libc::MAP_FAILED as *mut u8,
   |                                            ^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `munmap` in crate `libc`
  --> memory\src\ram.rs:52:19
   |
52 |             libc::munmap(self.ptr as *mut libc::c_void, self.size as usize);
   |                   ^^^^^^ not found in `libc`

For more information about this error, try `rustc --explain E0425`.
error: could not compile `machina-memory` (lib) due to 7 previous errors

Expected behavior

The project should either:

  1. support Windows with a platform-specific implementation of RamBlock::new and Drop, or
  2. explicitly restrict this code path to Unix platforms using cfg(unix) and provide a clearer unsupported-platform error.

Suggested direction

Introduce a small cross-platform RamBlock abstraction: use mmap on Unix, VirtualAlloc on Windows, a boxed-slice backend for Wasm, and a generic heap-based fallback for other targets. We can name them UnixRamBlock, etc.

Why this matters

At the moment, machina-memory does not compile on Windows because the RAM backend is hardcoded to a Unix-specific API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions