@@ -51,21 +51,6 @@ pub fn timestamp() -> u64 {
5151 . as_secs ( )
5252}
5353
54- /// Calls the system `mmap` via `libc` and returns the mapped address as a 32-bit integer.
55- ///
56- /// In Lind-WASM, addresses are represented as 32-bit values within the linear memory of a
57- /// Wasm module. The return value is truncated to 32 bits to match this representation.
58- /// On error, returns `-1` (corresponding to `MAP_FAILED`).
59- ///
60- /// # Safety
61- /// This function uses raw pointers and directly invokes `libc::mmap`. The caller must ensure
62- /// that all arguments are valid and that using the returned address as a 32-bit pointer is safe
63- /// in the Lind-WASM context.
64- pub fn libc_mmap ( addr : * mut u8 , len : usize , prot : i32 , flags : i32 , fildes : i32 , off : i64 ) -> i32 {
65- return ( ( unsafe { libc:: mmap ( addr as * mut c_void , len, prot, flags, fildes, off) } as i64 )
66- & 0xffffffff ) as i32 ;
67- }
68-
6954// Mimic shared memory in Linux by creating a file backing and truncating it to the segment size
7055// We can then safely unlink the file while still holding a descriptor to that segment,
7156// which we can use to map shared across cages.
@@ -149,7 +134,7 @@ impl ShmSegment {
149134 }
150135 // mmap shared segment into cage, and increase attachments
151136 // increase in cage references within attached_cages map
152- pub fn map_shm ( & mut self , shmaddr : * mut u8 , prot : i32 , cageid : u64 ) -> i32 {
137+ pub fn map_shm ( & mut self , shmaddr : * mut u8 , prot : i32 , cageid : u64 ) -> usize {
153138 let fobjfdno = self . filebacking . as_fd_handle_raw_int ( ) ;
154139 self . shminfo . shm_nattch += 1 ;
155140 self . shminfo . shm_atime = timestamp ( ) as isize ;
@@ -162,27 +147,34 @@ impl ShmSegment {
162147 vacant. insert ( 1 ) ;
163148 }
164149 } ;
165- libc_mmap (
166- shmaddr,
167- self . size as usize ,
168- prot,
169- ( MAP_SHARED as i32 ) | ( MAP_FIXED as i32 ) ,
170- fobjfdno,
171- 0 ,
172- )
150+
151+ unsafe {
152+ ( libc:: mmap (
153+ shmaddr as * mut c_void ,
154+ self . size as usize ,
155+ prot,
156+ ( MAP_SHARED as i32 ) | ( MAP_FIXED as i32 ) ,
157+ fobjfdno,
158+ 0 ,
159+ ) as usize )
160+ }
173161 }
174162
175163 // unmap shared segment, decrease attachments
176164 // decrease references within attached cages map
177165 pub fn unmap_shm ( & mut self , shmaddr : * mut u8 , cageid : u64 ) {
178- libc_mmap (
179- shmaddr,
180- self . size as usize ,
181- PROT_NONE ,
182- ( MAP_PRIVATE as i32 ) | ( MAP_ANONYMOUS as i32 ) | ( MAP_FIXED as i32 ) ,
183- -1 ,
184- 0 ,
185- ) ;
166+ let mmap_ret = unsafe {
167+ ( libc:: mmap (
168+ shmaddr as * mut c_void ,
169+ self . size as usize ,
170+ PROT_NONE ,
171+ ( MAP_PRIVATE as i32 ) | ( MAP_ANONYMOUS as i32 ) | ( MAP_FIXED as i32 ) ,
172+ -1 ,
173+ 0 ,
174+ ) as usize )
175+ } ;
176+ assert ! ( mmap_ret == shmaddr as usize ) ;
177+
186178 self . shminfo . shm_nattch -= 1 ;
187179 self . shminfo . shm_dtime = timestamp ( ) as isize ;
188180 match self . attached_cages . entry ( cageid) {
@@ -269,9 +261,9 @@ pub fn unmap_shm_mappings(cageid: u64) {
269261/// # Returns
270262/// * `Some(index)` if the address is found in the vector.
271263/// * `None` if the address does not exist in the mapping.
272- pub fn rev_shm_find_index_by_addr ( rev_shm : & Vec < ( u32 , i32 ) > , shmaddr : u32 ) -> Option < usize > {
264+ pub fn rev_shm_find_index_by_addr ( rev_shm : & Vec < ( u64 , i32 ) > , shmaddr : u64 ) -> Option < usize > {
273265 for ( index, val) in rev_shm. iter ( ) . enumerate ( ) {
274- if val. 0 == shmaddr as u32 {
266+ if val. 0 == shmaddr as u64 {
275267 return Some ( index) ;
276268 }
277269 }
@@ -285,9 +277,9 @@ pub fn rev_shm_find_index_by_addr(rev_shm: &Vec<(u32, i32)>, shmaddr: u32) -> Op
285277/// * `shmid` – The shared memory ID to search for.
286278///
287279/// # Returns
288- /// * A vector of all addresses (`u32 `) associated with the given `shmid`.
280+ /// * A vector of all addresses (`u64 `) associated with the given `shmid`.
289281/// * Returns an empty vector if no addresses are found.
290- pub fn rev_shm_find_addrs_by_shmid ( rev_shm : & Vec < ( u32 , i32 ) > , shmid : i32 ) -> Vec < u32 > {
282+ pub fn rev_shm_find_addrs_by_shmid ( rev_shm : & Vec < ( u64 , i32 ) > , shmid : i32 ) -> Vec < u64 > {
291283 let mut addrvec = Vec :: new ( ) ;
292284 for val in rev_shm. iter ( ) {
293285 if val. 1 == shmid as i32 {
@@ -308,15 +300,15 @@ pub fn rev_shm_find_addrs_by_shmid(rev_shm: &Vec<(u32, i32)>, shmid: i32) -> Vec
308300/// * `Some((base_addr, shmid))` if `search_addr` falls within the range of a known segment.
309301/// * `None` if the address is not within any tracked region.
310302pub fn search_for_addr_in_region (
311- rev_shm : & Vec < ( u32 , i32 ) > ,
312- search_addr : u32 ,
313- ) -> Option < ( u32 , i32 ) > {
303+ rev_shm : & Vec < ( u64 , i32 ) > ,
304+ search_addr : u64 ,
305+ ) -> Option < ( u64 , i32 ) > {
314306 let metadata = & SHM_METADATA ;
315307 for val in rev_shm. iter ( ) {
316308 let addr = val. 0 ;
317309 let shmid = val. 1 ;
318310 if let Some ( segment) = metadata. shmtable . get_mut ( & shmid) {
319- let range = addr..( addr + segment. size as u32 ) ;
311+ let range = addr..( addr + segment. size as u64 ) ;
320312 if range. contains ( & search_addr) {
321313 return Some ( ( addr, shmid) ) ;
322314 }
@@ -329,7 +321,7 @@ pub fn search_for_addr_in_region(
329321///
330322/// # Arguments
331323/// * `cageid` – ID of the calling cage.
332- /// * `shmaddr` – Address where the shared memory segment should be mapped.
324+ /// * `shmaddr` – System Address where the shared memory segment should be mapped.
333325/// * `shmflg` – Flags controlling access (e.g., `SHM_RDONLY`).
334326/// * `shmid` – Identifier of the shared memory segment to attach.
335327///
@@ -339,9 +331,9 @@ pub fn search_for_addr_in_region(
339331/// invalid, it returns an error.
340332///
341333/// # Returns
342- /// * On success – the mapped address as a `u32 `.
343- /// * On error – a negative errno value as a `u32 `.
344- pub fn shmat_helper ( cageid : u64 , shmaddr : * mut u8 , shmflg : i32 , shmid : i32 ) -> u32 {
334+ /// * On success – the mapped address as a `usize `.
335+ /// * On error – a negative errno value as a `usize `.
336+ pub fn shmat_helper ( cageid : u64 , shmaddr : * mut u8 , shmflg : i32 , shmid : i32 ) -> usize {
345337 let metadata = & SHM_METADATA ;
346338 let prot: i32 ;
347339
@@ -354,20 +346,20 @@ pub fn shmat_helper(cageid: u64, shmaddr: *mut u8, shmflg: i32, shmid: i32) -> u
354346 prot = PROT_READ | PROT_WRITE ;
355347 }
356348 let mut rev_shm = cage. rev_shm . lock ( ) ;
357- rev_shm. push ( ( shmaddr as u32 , shmid) ) ;
349+ rev_shm. push ( ( shmaddr as u64 , shmid) ) ;
358350 drop ( rev_shm) ;
359351
360- segment. map_shm ( shmaddr, prot, cageid) as u32
352+ segment. map_shm ( shmaddr, prot, cageid) as usize
361353 } else {
362- syscall_error ( Errno :: EINVAL , "shmat" , "Invalid shmid value" ) as u32
354+ syscall_error ( Errno :: EINVAL , "shmat" , "Invalid shmid value" ) as usize
363355 }
364356}
365357
366358/// Helper function that detaches a shared memory segment from the calling cage’s address space.
367359///
368360/// # Arguments
369361/// * `cageid` – ID of the calling cage.
370- /// * `shmaddr` – Address of the shared memory segment to detach.
362+ /// * `shmaddr` – System Address of the shared memory segment to detach.
371363///
372364/// This function searches the cage’s reverse mapping table for the segment mapped at `shmaddr`,
373365/// detaches it with `unmap_shm`, and removes the reverse mapping entry. If the segment was marked
@@ -382,7 +374,7 @@ pub fn shmdt_helper(cageid: u64, shmaddr: *mut u8) -> i32 {
382374 let mut rm = false ;
383375 let cage = get_cage ( cageid) . unwrap ( ) ;
384376 let mut rev_shm = cage. rev_shm . lock ( ) ;
385- let rev_shm_index = rev_shm_find_index_by_addr ( & rev_shm, shmaddr as u32 ) ;
377+ let rev_shm_index = rev_shm_find_index_by_addr ( & rev_shm, shmaddr as u64 ) ;
386378
387379 if let Some ( index) = rev_shm_index {
388380 let shmid = rev_shm[ index] . 1 ;
0 commit comments