@@ -29,13 +29,17 @@ use bytemuck::Pod;
2929/// # Safety
3030///
3131/// Implementations must ensure that:
32- /// - Pointers passed to methods are valid for the duration of the call
33- /// - Memory ordering guarantees are upheld as documented
34- /// - Reads and writes don't cause undefined behavior (alignment, validity)
32+ /// - Addresses accepted by these methods are translated according to the
33+ /// backend's memory model.
34+ /// - Invalid or inaccessible addresses are reported with `Self::Error` rather
35+ /// than causing undefined behavior.
36+ /// - Memory ordering guarantees are upheld as documented.
37+ /// - Typed reads/writes and atomic operations honor alignment and initialized
38+ /// memory requirements for the translated addresses.
3539///
3640/// [`RingProducer`]: super::RingProducer
3741/// [`RingConsumer`]: super::RingConsumer
38- pub trait MemOps {
42+ pub unsafe trait MemOps {
3943 type Error ;
4044
4145 /// Read bytes from physical memory.
@@ -47,9 +51,8 @@ pub trait MemOps {
4751 /// * `addr` - Guest physical address to read from
4852 /// * `dst` - Destination buffer to fill
4953 ///
50- /// # Safety
51- ///
52- /// The caller must ensure `addr` is valid and points to at least `dst.len()` bytes.
54+ /// Implementations must return an error if `addr` cannot be read for
55+ /// at least `dst.len()` bytes.
5356 fn read ( & self , addr : u64 , dst : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > ;
5457
5558 /// Write bytes to physical memory.
@@ -59,23 +62,20 @@ pub trait MemOps {
5962 /// * `addr` - address to write to
6063 /// * `src` - Source data to write
6164 ///
62- /// # Safety
63- ///
64- /// The caller must ensure `addr` is valid and points to at least `src.len()` bytes.
65+ /// Implementations must return an error if `addr` cannot be written for
66+ /// at least `src.len()` bytes.
6567 fn write ( & self , addr : u64 , src : & [ u8 ] ) -> Result < ( ) , Self :: Error > ;
6668
6769 /// Load a u16 with acquire semantics.
6870 ///
69- /// # Safety
70- ///
71- /// `addr` must translate to a valid, aligned `AtomicU16` in shared memory.
71+ /// Implementations must return an error if `addr` does not translate to a
72+ /// valid, aligned `AtomicU16` in shared memory.
7273 fn load_acquire ( & self , addr : u64 ) -> Result < u16 , Self :: Error > ;
7374
7475 /// Store a u16 with release semantics.
7576 ///
76- /// # Safety
77- ///
78- /// `addr` must translate to a valid `AtomicU16` in shared memory.
77+ /// Implementations must return an error if `addr` does not translate to a
78+ /// valid, aligned `AtomicU16` in shared memory.
7979 fn store_release ( & self , addr : u64 , val : u16 ) -> Result < ( ) , Self :: Error > ;
8080
8181 /// Get a direct read-only slice into shared memory.
@@ -106,9 +106,8 @@ pub trait MemOps {
106106
107107 /// Read a Pod type at the given pointer.
108108 ///
109- /// # Safety
110- ///
111- /// The caller must ensure `addr` is valid, aligned, and translates to initialized memory.
109+ /// Implementations must return an error if `addr` is not valid, aligned,
110+ /// and initialized for `T`.
112111 fn read_val < T : Pod > ( & self , addr : u64 ) -> Result < T , Self :: Error > {
113112 let mut val = T :: zeroed ( ) ;
114113 let bytes = bytemuck:: bytes_of_mut ( & mut val) ;
@@ -119,17 +118,18 @@ pub trait MemOps {
119118
120119 /// Write a Pod type at the given pointer.
121120 ///
122- /// # Safety
123- ///
124- /// The caller ensures that `ptr` is valid.
121+ /// Implementations must return an error if `addr` is not valid and aligned
122+ /// for `T`.
125123 fn write_val < T : Pod > ( & self , addr : u64 , val : T ) -> Result < ( ) , Self :: Error > {
126124 let bytes = bytemuck:: bytes_of ( & val) ;
127125 self . write ( addr, bytes) ?;
128126 Ok ( ( ) )
129127 }
130128}
131129
132- impl < T : MemOps > MemOps for Arc < T > {
130+ // SAFETY: Arc delegates all memory operations to the wrapped backend, preserving
131+ // that backend's MemOps contract.
132+ unsafe impl < T : MemOps > MemOps for Arc < T > {
133133 type Error = T :: Error ;
134134
135135 fn read ( & self , addr : u64 , dst : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
0 commit comments