-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix misaligned memory access bug (affects non-64 bit ARM)
- Loading branch information
1 parent
4189c5c
commit 4c8575d
Showing
2 changed files
with
20 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,7 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
use std::{ | ||
fmt::{Debug, Display}, | ||
mem::size_of, | ||
}; | ||
use std::fmt::{Debug, Display}; | ||
|
||
use rand::Rng; | ||
use shared_memory::{Shmem, ShmemConf, ShmemError}; | ||
|
@@ -63,15 +60,14 @@ where | |
// If creation fails because segment already exists for this id, | ||
// the creation attempt will be repeated with another id | ||
match ShmemConf::new() | ||
.size(alloc_size + size_of::<usize>()) | ||
.size(alloc_size) | ||
.os_id(Self::os_id(id.clone(), id_prefix)) | ||
.create() | ||
{ | ||
Ok(shmem) => { | ||
tracing::debug!( | ||
"Created SHM segment, size: {alloc_size}, prefix: {id_prefix}, id: {id}" | ||
); | ||
unsafe { *(shmem.as_ptr() as *mut usize) = alloc_size }; | ||
return Ok(Segment { shmem, id }); | ||
} | ||
Err(ShmemError::LinkExists) => {} | ||
|
@@ -94,10 +90,6 @@ where | |
) | ||
})?; | ||
|
||
if shmem.len() <= size_of::<usize>() { | ||
bail!("SHM segment too small") | ||
} | ||
|
||
tracing::debug!("Opened SHM segment, prefix: {id_prefix}, id: {id}"); | ||
|
||
Ok(Self { shmem, id }) | ||
|
@@ -110,17 +102,21 @@ where | |
} | ||
|
||
pub fn as_ptr(&self) -> *mut u8 { | ||
unsafe { self.shmem.as_ptr().add(size_of::<usize>()) } | ||
self.shmem.as_ptr() | ||
} | ||
|
||
/// Returns the length of this [`Segment<ID>`]. | ||
/// NOTE: one some platforms (at least windows) the returned len will be the actual length of an shm segment | ||
/// (a required len rounded up to the nearest multiply of page size), on other (at least linux and macos) this | ||
/// returns a value requested upon segment creation | ||
pub fn len(&self) -> usize { | ||
unsafe { *(self.shmem.as_ptr() as *mut usize) } | ||
self.shmem.len() | ||
} | ||
|
||
// TODO: dead code warning occurs because of `tested_crate_module!()` macro when feature `test` is not enabled. Better to fix that | ||
#[allow(dead_code)] | ||
pub fn is_empty(&self) -> bool { | ||
unsafe { *(self.shmem.as_ptr() as *mut usize) == 0 } | ||
self.len() == 0 | ||
} | ||
|
||
pub fn id(&self) -> ID { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters