ANDROID: ashmem_rust: Add support for retrieving an ashmem area's vmfile

Given a pointer to a file structure that represents an ashmem region,
return the shmem vmfile that backs the region. This should only be
used for dumping information about ashmem regions in debug scenarios.

Bug: 193397560
Change-Id: I6ecd4ed0e02caac29a8bff9c52052066219a5a8c
Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com>
This commit is contained in:
Isaac J. Manjarres
2025-07-02 23:16:24 +00:00
committed by Isaac Manjarres
parent 0be74214c0
commit d893caf112
3 changed files with 30 additions and 0 deletions

View File

@@ -34,5 +34,6 @@ enum {
bool is_ashmem_file(struct file *file);
int ashmem_area_name(struct file *file, char *name);
long ashmem_area_size(struct file *file);
struct file *ashmem_area_vmfile(struct file *file);
#endif /* _LINUX_ASHMEM_H */

View File

@@ -724,3 +724,31 @@ unsafe extern "C" fn ashmem_area_size(file: *mut bindings::file) -> isize {
Err(_err) => 0,
}
}
/// # Safety
///
/// The caller must ensure that `file` is valid for the duration of this function.
///
/// If this function returns a non-NULL pointer to a file structure, the refcount for that
/// file will be incremented by 1. It is the caller's responsibility to decrement the refcount
/// when the file is no longer needed.
#[no_mangle]
unsafe extern "C" fn ashmem_area_vmfile(file: *mut bindings::file) -> *mut bindings::file {
// SAFETY: file is valid for the duration of this function.
let ashmem = match unsafe { get_ashmem_area(file) } {
Ok(area) => area,
Err(_err) => return null_mut(),
};
let asma = &mut *ashmem.inner.lock();
match asma.file.as_ref() {
Some(shmem_file) => {
let shmem_file_ptr = shmem_file.file().as_ptr();
// SAFETY: file is valid for the duration of the function, which means shmem file is
// also valid at this point.
unsafe { bindings::get_file(shmem_file_ptr) };
shmem_file_ptr
}
None => null_mut(),
}
}

View File

@@ -19,3 +19,4 @@
EXPORT_SYMBOL_GPL(is_ashmem_file);
EXPORT_SYMBOL_GPL(ashmem_area_name);
EXPORT_SYMBOL_GPL(ashmem_area_size);
EXPORT_SYMBOL_GPL(ashmem_area_vmfile);