Skip to content

Commit

Permalink
fix: assure memory maps are created with MAP_PRIVATE (#1312)
Browse files Browse the repository at this point in the history
That way, the mmap process should work under more circumstances.
  • Loading branch information
Byron committed Mar 9, 2024
1 parent 35592c9 commit 3b60a07
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 11 deletions.
6 changes: 2 additions & 4 deletions gix-commitgraph/src/file/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ use std::{
path::Path,
};

use bstr::ByteSlice;
use memmap2::Mmap;

use crate::{
file::{
ChunkId, BASE_GRAPHS_LIST_CHUNK_ID, COMMIT_DATA_CHUNK_ID, COMMIT_DATA_ENTRY_SIZE_SANS_HASH,
EXTENDED_EDGES_LIST_CHUNK_ID, FAN_LEN, HEADER_LEN, OID_FAN_CHUNK_ID, OID_LOOKUP_CHUNK_ID, SIGNATURE,
},
File,
};
use bstr::ByteSlice;

/// The error used in [`File::at()`].
#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -246,7 +244,7 @@ impl TryFrom<&Path> for File {
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
#[allow(unsafe_code)]
unsafe {
Mmap::map(&file)
memmap2::MmapOptions::new().map_copy_read_only(&file)
}
})
.map_err(|e| Error::Io {
Expand Down
4 changes: 1 addition & 3 deletions gix-index/src/file/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use std::path::{Path, PathBuf};

use memmap2::Mmap;

use crate::{decode, extension, File, State};

mod error {
Expand Down Expand Up @@ -64,7 +62,7 @@ impl File {
let mut file = std::fs::File::open(&path)?;
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
#[allow(unsafe_code)]
let data = unsafe { Mmap::map(&file)? };
let data = unsafe { memmap2::MmapOptions::new().map_copy_read_only(&file)? };

if !skip_hash {
// Note that even though it's trivial to offload this into a thread, which is worth it for all but the smallest
Expand Down
2 changes: 1 addition & 1 deletion gix-pack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod mmap {
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
#[allow(unsafe_code)]
unsafe {
memmap2::Mmap::map(&file)
memmap2::MmapOptions::new().map_copy_read_only(&file)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gix-pack/tests/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod version {
desired_kind,
|| {
let file = std::fs::File::open(fixture_path(data_path))?;
let map = unsafe { memmap2::Mmap::map(&file)? };
let map = unsafe { memmap2::MmapOptions::map_copy_read_only(&file)? };
Ok((slice_map, map))
},
pack_iter,
Expand Down
3 changes: 1 addition & 2 deletions gix-ref/src/store/packed/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl AsRef<[u8]> for packed::Backing {
pub mod open {
use std::path::PathBuf;

use memmap2::Mmap;
use winnow::{prelude::*, stream::Offset};

use crate::store_impl::packed;
Expand Down Expand Up @@ -81,7 +80,7 @@ pub mod open {
// SAFETY: we have to take the risk of somebody changing the file underneath. Git never writes into the same file.
#[allow(unsafe_code)]
unsafe {
Mmap::map(&std::fs::File::open(&path)?)?
memmap2::MmapOptions::new().map_copy_read_only(&std::fs::File::open(&path)?)?
},
)
};
Expand Down

0 comments on commit 3b60a07

Please sign in to comment.