| 1 |
#![cfg(target_os = "android")] |
| 2 |
//! Provides a wrapper around Android's ASharedMemory API. |
| 3 |
//! |
| 4 |
//! ashmem has existed in Android as a non-public API for some time. |
| 5 |
//! Originally accessed via ioctl, it was later available via libcutils as ashmem_create_region etc. |
| 6 |
//! ASharedMemory is the new public API, but it isn't available until API 26 (Android 8). |
| 7 |
//! Builds targeting Android 10 (API 29) are no longer permitted to access ashmem via the ioctl interface. |
| 8 |
//! This makes life for a portable program difficult - you can't reliably use the old or new interface during this transition period. |
| 9 |
//! We try to dynamically load the new API first, then fall back to the ioctl interface. |
| 10 |
//! |
| 11 |
//! References: |
| 12 |
//! - [ASharedMemory documentation](https://developer.android.com/ndk/reference/group/memory) |
| 13 |
//! - [Linux ashmem.h definitions](https://elixir.bootlin.com/linux/v5.11.8/source/drivers/staging/android/uapi/ashmem.h) |
| 14 |
|
| 15 |
#[macro_use] |
| 16 |
extern crate ioctl_sys; |
| 17 |
|
| 18 |
const __ASHMEMIOC: u32 = 0x77; |
| 19 |
|
| 20 |
static mut LIBANDROID_ASHAREDMEMORY_CREATE: Option< |
| 21 |
extern "C" fn(*const libc::c_char, libc::size_t) -> libc::c_int, |
| 22 |
> = None; |
| 23 |
static mut LIBANDROID_ASHAREDMEMORY_GETSIZE: Option<extern "C" fn(libc::c_int) -> libc::size_t> = |
| 24 |
None; |
| 25 |
static mut LIBANDROID_ASHAREDMEMORY_SETPROT: Option< |
| 26 |
extern "C" fn(libc::c_int, libc::c_int) -> libc::c_int, |
| 27 |
> = None; |
| 28 |
|
| 29 |
unsafe fn maybe_init() { |
| 30 |
const LIBANDROID_NAME: *const libc::c_char = "libandroid.so\0".as_ptr() as *const libc::c_char; |
| 31 |
const LIBANDROID_ASHAREDMEMORY_CREATE_NAME: *const libc::c_char = |
| 32 |
"ASharedMemory_create\0".as_ptr() as _; |
| 33 |
const LIBANDROID_ASHAREDMEMORY_GETSIZE_NAME: *const libc::c_char = |
| 34 |
"ASharedMemory_getSize\0".as_ptr() as _; |
| 35 |
const LIBANDROID_ASHAREDMEMORY_SETPROT_NAME: *const libc::c_char = |
| 36 |
"ASharedMemory_setProt\0".as_ptr() as _; |
| 37 |
static ONCE: std::sync::Once = std::sync::Once::new(); |
| 38 |
ONCE.call_once(|| { |
| 39 |
// Leak the handle, there's no safe time to close it. |
| 40 |
let handle = libc::dlopen(LIBANDROID_NAME, libc::RTLD_LAZY | libc::RTLD_LOCAL); |
| 41 |
if handle.is_null() { |
| 42 |
return; |
| 43 |
} |
| 44 |
// Transmute guarantee for `fn -> Option<fn>`: https://doc.rust-lang.org/std/option/#representation |
| 45 |
LIBANDROID_ASHAREDMEMORY_CREATE = |
| 46 |
std::mem::transmute(libc::dlsym(handle, LIBANDROID_ASHAREDMEMORY_CREATE_NAME)); |
| 47 |
LIBANDROID_ASHAREDMEMORY_GETSIZE = |
| 48 |
std::mem::transmute(libc::dlsym(handle, LIBANDROID_ASHAREDMEMORY_GETSIZE_NAME)); |
| 49 |
LIBANDROID_ASHAREDMEMORY_SETPROT = |
| 50 |
std::mem::transmute(libc::dlsym(handle, LIBANDROID_ASHAREDMEMORY_SETPROT_NAME)); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
/// See [ASharedMemory_create NDK documentation](https://developer.android.com/ndk/reference/group/memory#asharedmemory_create) |
| 55 |
/// |
| 56 |
/// # Safety |
| 57 |
/// |
| 58 |
/// Directly calls C or kernel APIs. |
| 59 |
#[allow(non_snake_case)] |
| 60 |
pub unsafe fn ASharedMemory_create(name: *const libc::c_char, size: libc::size_t) -> libc::c_int { |
| 61 |
const ASHMEM_NAME_DEF: *const libc::c_char = "/dev/ashmem\0".as_ptr() as _; |
| 62 |
const ASHMEM_NAME_LEN: usize = 256; |
| 63 |
const ASHMEM_SET_NAME: libc::c_int = iow!( |
| 64 |
__ASHMEMIOC, |
| 65 |
1, |
| 66 |
std::mem::size_of::<[libc::c_char; ASHMEM_NAME_LEN]>() |
| 67 |
) as _; |
| 68 |
const ASHMEM_SET_SIZE: libc::c_int = |
| 69 |
iow!(__ASHMEMIOC, 3, std::mem::size_of::<libc::size_t>()) as _; |
| 70 |
|
| 71 |
maybe_init(); |
| 72 |
if let Some(fun) = LIBANDROID_ASHAREDMEMORY_CREATE { |
| 73 |
return fun(name, size); |
| 74 |
} |
| 75 |
|
| 76 |
let fd = libc::open(ASHMEM_NAME_DEF, libc::O_RDWR, 0o600); |
| 77 |
if fd < 0 { |
| 78 |
return fd; |
| 79 |
} |
| 80 |
|
| 81 |
if !name.is_null() { |
| 82 |
// NOTE: libcutils uses a local stack copy of `name`. |
| 83 |
let r = libc::ioctl(fd, ASHMEM_SET_NAME, name); |
| 84 |
if r != 0 { |
| 85 |
libc::close(fd); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
let r = libc::ioctl(fd, ASHMEM_SET_SIZE, size); |
| 91 |
if r != 0 { |
| 92 |
libc::close(fd); |
| 93 |
return -1; |
| 94 |
} |
| 95 |
|
| 96 |
fd |
| 97 |
} |
| 98 |
|
| 99 |
/// See [ASharedMemory_getSize NDK documentation](https://developer.android.com/ndk/reference/group/memory#asharedmemory_getsize) |
| 100 |
/// |
| 101 |
/// # Safety |
| 102 |
/// |
| 103 |
/// Directly calls C or kernel APIs. |
| 104 |
#[allow(non_snake_case)] |
| 105 |
pub unsafe fn ASharedMemory_getSize(fd: libc::c_int) -> libc::size_t { |
| 106 |
const ASHMEM_GET_SIZE: libc::c_int = io!(__ASHMEMIOC, 4) as _; |
| 107 |
|
| 108 |
maybe_init(); |
| 109 |
if let Some(fun) = LIBANDROID_ASHAREDMEMORY_GETSIZE { |
| 110 |
return fun(fd); |
| 111 |
} |
| 112 |
|
| 113 |
libc::ioctl(fd, ASHMEM_GET_SIZE) as libc::size_t |
| 114 |
} |
| 115 |
|
| 116 |
/// See [ASharedMemory_setProt NDK documentation](https://developer.android.com/ndk/reference/group/memory#asharedmemory_setprot) |
| 117 |
/// |
| 118 |
/// # Safety |
| 119 |
/// |
| 120 |
/// Directly calls C or kernel APIs. |
| 121 |
#[allow(non_snake_case)] |
| 122 |
pub unsafe fn ASharedMemory_setProt(fd: libc::c_int, prot: libc::c_int) -> libc::c_int { |
| 123 |
const ASHMEM_SET_PROT_MASK: libc::c_int = |
| 124 |
iow!(__ASHMEMIOC, 5, std::mem::size_of::<libc::c_ulong>()) as _; |
| 125 |
|
| 126 |
maybe_init(); |
| 127 |
if let Some(fun) = LIBANDROID_ASHAREDMEMORY_SETPROT { |
| 128 |
return fun(fd, prot); |
| 129 |
} |
| 130 |
|
| 131 |
let r = libc::ioctl(fd, ASHMEM_SET_PROT_MASK, prot); |
| 132 |
if r != 0 { |
| 133 |
return -1; |
| 134 |
} |
| 135 |
r |
| 136 |
} |
| 137 |
|
| 138 |
#[cfg(test)] |
| 139 |
mod tests { |
| 140 |
#[test] |
| 141 |
fn basic() { |
| 142 |
unsafe { |
| 143 |
let name = std::ffi::CString::new("/test-ashmem").unwrap(); |
| 144 |
let fd = super::ASharedMemory_create(name.as_ptr(), 128); |
| 145 |
assert!(fd >= 0); |
| 146 |
assert_eq!(super::ASharedMemory_getSize(fd), 128); |
| 147 |
assert_eq!(super::ASharedMemory_setProt(fd, 0), 0); |
| 148 |
libc::close(fd); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn anonymous() { |
| 154 |
unsafe { |
| 155 |
let fd = super::ASharedMemory_create(std::ptr::null(), 128); |
| 156 |
assert!(fd >= 0); |
| 157 |
libc::close(fd); |
| 158 |
} |
| 159 |
} |
| 160 |
} |