Skip to content

Commit

Permalink
bolts limit ashmem concept to Linux/Android only.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Dec 30, 2024
1 parent d8ec991 commit a667df3
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions libafl_bolts/src/shmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,14 +1154,15 @@ pub mod unix_shmem {
};

/// An ashmem based impl for linux/android
#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Debug)]
pub struct AshmemShMem {
id: ShMemId,
map: *mut u8,
map_size: usize,
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[allow(non_camel_case_types)] // expect somehow breaks here
#[derive(Copy, Clone)]
#[repr(C)]
Expand All @@ -1170,11 +1171,15 @@ pub mod unix_shmem {
pub len: c_uint,
}

#[cfg(any(target_os = "linux", target_os = "android"))]
const ASHMEM_GET_SIZE: c_ulong = 0x00007704;
#[cfg(any(target_os = "linux", target_os = "android"))]
const ASHMEM_UNPIN: c_ulong = 0x40087708;
//const ASHMEM_SET_NAME: c_long = 0x41007701;
#[cfg(any(target_os = "linux", target_os = "android"))]
const ASHMEM_SET_SIZE: c_ulong = 0x40087703;

#[cfg(any(target_os = "linux", target_os = "android"))]
impl AshmemShMem {
/// Create a new shared memory mapping, using shmget/shmat
pub fn new(map_size: usize) -> Result<Self, Error> {
Expand Down Expand Up @@ -1273,13 +1278,14 @@ pub mod unix_shmem {
}
}

#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
impl ShMem for AshmemShMem {
fn id(&self) -> ShMemId {
self.id
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl Deref for AshmemShMem {
type Target = [u8];

Expand All @@ -1288,14 +1294,15 @@ pub mod unix_shmem {
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl DerefMut for AshmemShMem {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.map, self.map_size) }
}
}

/// [`Drop`] implementation for [`AshmemShMem`], which cleans up the mapping.
#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Drop for AshmemShMem {
#[expect(trivial_numeric_casts)]
fn drop(&mut self) {
Expand All @@ -1318,21 +1325,22 @@ pub mod unix_shmem {
}

/// A [`ShMemProvider`] which uses ashmem to provide shared memory mappings.
#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
#[derive(Clone, Debug)]
pub struct AshmemShMemProvider {}

#[cfg(any(target_os = "linux", target_os = "android"))]
unsafe impl Send for AshmemShMemProvider {}

#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Default for AshmemShMemProvider {
fn default() -> Self {
Self::new().unwrap()
}
}

/// Implement [`ShMemProvider`] for [`AshmemShMemProvider`], for the Android `ShMem`.
#[cfg(unix)]
#[cfg(any(target_os = "linux", target_os = "android"))]
impl ShMemProvider for AshmemShMemProvider {
type ShMem = AshmemShMem;

Expand Down

0 comments on commit a667df3

Please sign in to comment.