Skip to content

Commit cc4583e

Browse files
committed
mmap skeleton
1 parent 396812d commit cc4583e

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stdlib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ ahash = "0.7.6"
6363
libz-sys = { version = "1.1.5", optional = true }
6464
num_enum = "0.5.7"
6565
ascii = "1.0.0"
66+
memmap2 = "0.5.0"
67+
page_size = "0.4.2"
6668

6769
[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
6870
termios = "0.3.3"

stdlib/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod gc;
1616
mod hashlib;
1717
mod json;
1818
mod math;
19+
#[cfg(unix)]
20+
mod mmap;
1921
mod platform;
2022
mod pyexpat;
2123
mod pystruct;
@@ -125,6 +127,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
125127
{
126128
"_posixsubprocess" => posixsubprocess::make_module,
127129
"syslog" => syslog::make_module,
130+
"mmap" => mmap::make_module,
128131
}
129132
#[cfg(target_os = "macos")]
130133
{

stdlib/src/mmap.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
pub(crate) use mmap::make_module;
2+
3+
#[pymodule]
4+
mod mmap {
5+
use crate::vm::{
6+
builtins::PyTypeRef, convert::ToPyResult, function::OptionalArg, types::Constructor,
7+
FromArgs, PyObject, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
8+
};
9+
use memmap2::{MmapMut, MmapOptions};
10+
11+
#[repr(C)]
12+
#[derive(PartialEq, Eq, Debug)]
13+
enum AccessMode {
14+
Default = 0,
15+
Read = 1,
16+
Write = 2,
17+
Copy = 3,
18+
}
19+
20+
impl TryFromBorrowedObject for AccessMode {
21+
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Self> {
22+
let i = u32::try_from_borrowed_object(vm, obj)?;
23+
Ok(match i {
24+
0 => Self::Default,
25+
1 => Self::Read,
26+
2 => Self::Write,
27+
3 => Self::Copy,
28+
_ => return Err(vm.new_value_error("Not a valid AccessMode value".to_owned())),
29+
})
30+
}
31+
}
32+
33+
#[pyattr]
34+
use libc::{MAP_ANON, MAP_ANONYMOUS, MAP_PRIVATE, MAP_SHARED, PROT_READ, PROT_WRITE};
35+
#[pyattr]
36+
const ACCESS_DEFAULT: u32 = AccessMode::Default as u32;
37+
#[pyattr]
38+
const ACCESS_READ: u32 = AccessMode::Read as u32;
39+
#[pyattr]
40+
const ACCESS_WRITE: u32 = AccessMode::Write as u32;
41+
#[pyattr]
42+
const ACCESS_COPY: u32 = AccessMode::Copy as u32;
43+
44+
#[pyattr(name = "PAGESIZE")]
45+
fn pagesize(vm: &VirtualMachine) -> usize {
46+
page_size::get()
47+
}
48+
49+
#[pyattr]
50+
#[pyclass(name = "mmap")]
51+
#[derive(Debug, PyPayload)]
52+
struct PyMmap {
53+
mmap: MmapMut,
54+
exports: usize,
55+
// PyObject *weakreflist;
56+
access: AccessMode,
57+
}
58+
59+
#[derive(FromArgs)]
60+
struct MmapNewArgs {
61+
#[pyarg(any)]
62+
fileno: std::os::unix::io::RawFd,
63+
#[pyarg(any)]
64+
length: isize,
65+
#[pyarg(any, default = "MAP_SHARED")]
66+
flags: libc::c_int,
67+
#[pyarg(any, default = "PROT_WRITE|PROT_READ")]
68+
prot: libc::c_int,
69+
#[pyarg(any, default = "AccessMode::Default")]
70+
access: AccessMode,
71+
#[pyarg(any, default = "0")]
72+
offset: u64,
73+
}
74+
75+
impl Constructor for PyMmap {
76+
type Args = MmapNewArgs;
77+
78+
fn py_new(
79+
cls: PyTypeRef,
80+
MmapNewArgs {
81+
fileno: fd,
82+
length,
83+
flags,
84+
prot,
85+
access,
86+
offset,
87+
}: Self::Args,
88+
vm: &VirtualMachine,
89+
) -> PyResult {
90+
if length < 0 {
91+
return Err(
92+
vm.new_overflow_error("memory mapped length must be positive".to_owned())
93+
);
94+
}
95+
// if offset < 0 {
96+
// return Err(vm.new_overflow_error("memory mapped offset must be positive".to_owned()));
97+
// }
98+
if (access != AccessMode::Default)
99+
&& ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))
100+
{
101+
return Err(vm.new_value_error(
102+
"mmap can't specify both access and flags, prot.".to_owned(),
103+
));
104+
}
105+
106+
let (flags, prot, access) = match access {
107+
AccessMode::Read => (MAP_SHARED, PROT_READ, access),
108+
AccessMode::Write => (MAP_SHARED, PROT_READ | PROT_WRITE, access),
109+
AccessMode::Copy => (MAP_PRIVATE, PROT_READ | PROT_WRITE, access),
110+
AccessMode::Default => {
111+
let access = if (prot & PROT_READ) != 0 && (prot & PROT_WRITE) != 0 {
112+
access
113+
} else if (prot & PROT_WRITE) != 0 {
114+
AccessMode::Write
115+
} else {
116+
AccessMode::Read
117+
};
118+
(flags, prot, access)
119+
}
120+
_ => return Err(vm.new_value_error("mmap invalid access parameter.".to_owned())),
121+
};
122+
123+
let mut mmap_opt = MmapOptions::new();
124+
let mmap_opt = mmap_opt.offset(offset);
125+
// .len(map_size)
126+
let mmap = match access {
127+
AccessMode::Write => unsafe { mmap_opt.map_mut(fd) },
128+
// AccessMode::Read => mmap_opt.map(fd),
129+
AccessMode::Copy => unsafe { mmap_opt.map_copy(fd) },
130+
_ => unreachable!("access must be decided before here"),
131+
}
132+
.map_err(|_| vm.new_value_error("FIXME: mmap error".to_owned()))?;
133+
134+
let m_obj = Self {
135+
mmap,
136+
exports: 0,
137+
access,
138+
};
139+
140+
m_obj.to_pyresult(vm)
141+
}
142+
}
143+
144+
#[pyimpl]
145+
impl PyMmap {
146+
#[pymethod]
147+
fn close(&self) -> PyResult<()> {
148+
if self.exports > 0 {
149+
// PyErr_SetString(PyExc_BufferError, "cannot close "\
150+
// "exported pointers exist");
151+
}
152+
// self.mmap = MmapMut::map_anon(0).unwrap();
153+
Ok(())
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)