Skip to content
/ mmarinus Public
forked from enarx/mmarinus

A safe mmap implementation

Notifications You must be signed in to change notification settings

dpal/mmarinus

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Workflow Status Average time to resolve an issue Percentage of issues still open Maintenance

mmarinus

The mmarinus crate wraps the underlying system mmap() call in safe semantics.

For example:

use mmarinus::{Map, perms};
use std::io::Read;

let mut hosts = std::fs::File::open("/etc/hosts").unwrap();

let mut chunk = [0u8; 32];
hosts.read_exact(&mut chunk).unwrap();

let map = Map::bytes(32)
    .near(128 * 1024 * 1024)
    .from(&mut hosts, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &chunk);

You can also remap an existing mapping:

use mmarinus::{Map, perms};
use std::io::Read;

let mut hosts = std::fs::File::open("/etc/hosts").unwrap();

let mut chunk = [0u8; 32];
hosts.read_exact(&mut chunk).unwrap();

let mut map = Map::bytes(32)
    .anywhere()
    .from(&mut hosts, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &chunk);

let mut map = map.remap()
    .from(&mut hosts, 0)
    .with(perms::ReadWrite)
    .unwrap();

assert_eq!(&*map, &chunk);

for i in map.iter_mut() {
    *i = 255;
}
assert_eq!(&*map, &[255; 32]);

Alternatively, you can just change the permissions:

use mmarinus::{Map, perms};
use std::io::Read;

let mut hosts = std::fs::File::open("/etc/hosts").unwrap();

let mut chunk = [0u8; 32];
hosts.read_exact(&mut chunk).unwrap();

let map = Map::bytes(32)
    .near(128 * 1024 * 1024)
    .from(&mut hosts, 0)
    .with(perms::Read)
    .unwrap();

assert_eq!(&*map, &chunk);

let mut map = map.reprotect(perms::ReadWrite).unwrap();
assert_eq!(&*map, &chunk);

for i in map.iter_mut() {
    *i = 255;
}
assert_eq!(&*map, &[255; 32]);

Mapping a whole file into memory is easy:

use mmarinus::{Map, Private, perms};

let map = Map::load("/etc/hosts", Private, perms::Read).unwrap();

License: Apache-2.0

About

A safe mmap implementation

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%