From 3b60a079a7c6809624dcedde0730de11141b6441 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 9 Mar 2024 19:10:08 +0100 Subject: [PATCH] fix: assure memory maps are created with `MAP_PRIVATE` (#1312) That way, the mmap process should work under more circumstances. --- gix-commitgraph/src/file/init.rs | 6 ++---- gix-index/src/file/init.rs | 4 +--- gix-pack/src/lib.rs | 2 +- gix-pack/tests/pack/index.rs | 2 +- gix-ref/src/store/packed/buffer.rs | 3 +-- 5 files changed, 6 insertions(+), 11 deletions(-) diff --git a/gix-commitgraph/src/file/init.rs b/gix-commitgraph/src/file/init.rs index 51c2739501f..78e621ab0ae 100644 --- a/gix-commitgraph/src/file/init.rs +++ b/gix-commitgraph/src/file/init.rs @@ -4,9 +4,6 @@ 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, @@ -14,6 +11,7 @@ use crate::{ }, File, }; +use bstr::ByteSlice; /// The error used in [`File::at()`]. #[derive(thiserror::Error, Debug)] @@ -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 { diff --git a/gix-index/src/file/init.rs b/gix-index/src/file/init.rs index 88dd86af9ac..10c01015fcb 100644 --- a/gix-index/src/file/init.rs +++ b/gix-index/src/file/init.rs @@ -2,8 +2,6 @@ use std::path::{Path, PathBuf}; -use memmap2::Mmap; - use crate::{decode, extension, File, State}; mod error { @@ -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 diff --git a/gix-pack/src/lib.rs b/gix-pack/src/lib.rs index b56d1fe983e..ae6f3ffe99c 100755 --- a/gix-pack/src/lib.rs +++ b/gix-pack/src/lib.rs @@ -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) } } } diff --git a/gix-pack/tests/pack/index.rs b/gix-pack/tests/pack/index.rs index 1a168b28e79..ddc90cc87c0 100644 --- a/gix-pack/tests/pack/index.rs +++ b/gix-pack/tests/pack/index.rs @@ -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, diff --git a/gix-ref/src/store/packed/buffer.rs b/gix-ref/src/store/packed/buffer.rs index 40fbf88e24a..2283cf9873b 100644 --- a/gix-ref/src/store/packed/buffer.rs +++ b/gix-ref/src/store/packed/buffer.rs @@ -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; @@ -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)?)? }, ) };