blob: 861d765138972d6d86c089d7b9dcac775607210a [file]
// Copyright 2025 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use crate::mm::memory::MemoryObject;
use crate::mm::memory_manager::MemoryManagerState;
use crate::mm::{
FaultRegisterMode, GUARD_PAGE_COUNT_FOR_GROWSDOWN_MAPPINGS, MappingOptions, PAGE_SIZE,
ProtectionFlags,
};
use crate::vfs::FileMapping;
use crate::vfs::aio::AioContext;
use crate::vfs::pseudo::dynamic_file::DynamicFileBuf;
use bitflags::bitflags;
use flyweights::FlyByteStr;
use fuchsia_inspect::HistogramProperty;
use starnix_uapi::errors::Errno;
use starnix_uapi::file_mode::Access;
use starnix_uapi::user_address::UserAddress;
use starnix_uapi::{PROT_EXEC, PROT_READ, PROT_WRITE, errno};
use static_assertions::const_assert_eq;
use std::mem::MaybeUninit;
use std::ops::Range;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MappingMode {
Eager,
Lazy,
}
/// Describes a single memory mapping entry.
///
/// The size of this type *heavily* influences Starnix's heap usage in common scenarios, please
/// think twice about increasing its size. Compiling in "release" mode includes a compile
/// time check for size increase.
#[split_enum_storage::container] // this also derives Clone, Debug, PartialEq, Eq
#[must_use]
pub struct Mapping {
/// Object backing this mapping.
backing: MappingBacking,
/// The flags used by the mapping, including protection.
flags: MappingFlags,
/// The name for this mapping.
///
/// This may be a reference to the filesystem node backing this mapping or a userspace-assigned
/// name. The value of the name is orthogonal to whether this mapping is anonymous -
/// mappings of the file '/dev/zero' are treated as anonymous mappings and anonymous mappings
/// may have a name assigned.
///
/// Because of this exception, avoid using this to check if a mapping is anonymous.
/// use [private_anonymous] method instead.
///
/// NOTE: this *must* be accessed through `name()`/`set_name()` which are generated by below
/// macro.
#[split_enum_storage::decomposed]
name: MappingName,
}
// The size of this type *heavily* influences Starnix's heap usage in common scenarios, please
// think twice about increasing the size here.
#[cfg(not(any(test, debug_assertions)))]
static_assertions::assert_eq_size!(Mapping, [u8; 32]);
static_assertions::assert_eq_size!(MappingBacking, [u8; 16]);
impl Mapping {
pub fn new(backing: MappingBacking, flags: MappingFlags, mode: MappingMode) -> Mapping {
Self::with_name(backing, flags, MappingName::None, mode)
}
pub fn with_name(
backing: MappingBacking,
mut flags: MappingFlags,
name: MappingName,
mode: MappingMode,
) -> Mapping {
flags.set(MappingFlags::MAPPED_IN_VMAR, mode == MappingMode::Eager);
MappingUnsplit { backing, flags, name }.decompose()
}
pub fn flags(&self) -> MappingFlags {
self.flags
}
pub fn mapping_mode(&self) -> MappingMode {
if self.flags.contains(MappingFlags::MAPPED_IN_VMAR) {
MappingMode::Eager
} else {
MappingMode::Lazy
}
}
pub fn set_mapping_mode(&mut self, mode: MappingMode) {
self.flags.set(MappingFlags::MAPPED_IN_VMAR, mode == MappingMode::Eager);
}
pub fn set_flags(&mut self, new_flags: MappingFlags) {
self.flags = new_flags;
}
/// Maximum [`Access`] permissions allowed for this mapping, used by [`Mapping::write_vm_flags`] to
/// report `mr`, `mw`, and `me` in `/proc/[pid]/smaps` (see `proc_pid_smaps(5)`).
fn max_access(&self) -> Access {
match self.name() {
MappingNameRef::None
| MappingNameRef::Stack
| MappingNameRef::Heap
| MappingNameRef::Vma(_)
| MappingNameRef::Ashmem(_)
| MappingNameRef::AioContext(_) => Access::rwx(),
// The vDSO and vvar mappings are backed by read-only kernel VMOs (with execute rights
// for the vDSO) and cannot be made writable by userspace via `mprotect`.
MappingNameRef::Vdso => Access::READ | Access::EXEC,
MappingNameRef::Vvar => Access::READ,
MappingNameRef::File(file) => {
let mut access = file.file().max_access_for_memory_mapping();
// Private file mappings use copy-on-write, so they may always be made writable
// (e.g. via `mprotect(PROT_WRITE)`) even if the underlying file was opened
// read-only.
if !self.flags.contains(MappingFlags::SHARED) {
access |= Access::WRITE;
}
access
}
}
}
pub fn get_backing_internal(&self) -> &MappingBacking {
&self.backing
}
pub fn set_backing_internal(&mut self, backing: MappingBacking) {
self.backing = backing;
}
pub fn set_uffd(&mut self, mode: FaultRegisterMode) {
self.flags |= MappingFlags::UFFD;
if mode == FaultRegisterMode::MISSING {
self.flags |= MappingFlags::UFFD_MISSING;
}
}
pub fn clear_uffd(&mut self) {
self.flags = self.flags.difference(MappingFlags::UFFD | MappingFlags::UFFD_MISSING);
}
pub fn set_mlock(&mut self) {
self.flags |= MappingFlags::LOCKED;
}
pub fn clear_mlock(&mut self) {
self.flags = self.flags.difference(MappingFlags::LOCKED);
}
pub fn new_private_anonymous(
flags: MappingFlags,
name: MappingName,
mode: MappingMode,
) -> Mapping {
Self::with_name(MappingBacking::PrivateAnonymous, flags, name, mode)
}
pub fn inflate_to_include_guard_pages(&self, range: &Range<UserAddress>) -> Range<UserAddress> {
let start = if self.flags.contains(MappingFlags::GROWSDOWN) {
range
.start
.saturating_sub(*PAGE_SIZE as usize * GUARD_PAGE_COUNT_FOR_GROWSDOWN_MAPPINGS)
} else {
range.start
};
start..range.end
}
/// Converts a `UserAddress` to an offset in this mapping's memory object.
pub fn address_to_offset(&self, addr: UserAddress) -> u64 {
match &self.backing {
MappingBacking::Memory(backing) => backing.address_to_offset(addr),
MappingBacking::PrivateAnonymous => {
// For private, anonymous allocations the virtual address is the offset in the backing memory object.
addr.ptr() as u64
}
}
}
pub fn can_read(&self) -> bool {
self.flags.contains(MappingFlags::READ)
}
pub fn can_write(&self) -> bool {
self.flags.contains(MappingFlags::WRITE)
}
pub fn can_exec(&self) -> bool {
self.flags.contains(MappingFlags::EXEC)
}
/// Returns whether this mapping is allowed to be executable, as reported by `me` in
/// `/proc/[pid]/smaps`. For file mappings, this is false if the file is on a `noexec` mount
/// or is sealed against execution. Security checks (see `security::file_mprotect()`) may still
/// deny making the mapping executable.
pub fn may_exec(&self) -> bool {
self.max_access().contains(Access::EXEC)
}
/// Returns whether this is a private mapping of a file (as opposed to a `MAP_SHARED` mapping
/// or an anonymous mapping).
///
/// Unlike `MAP_SHARED` and anonymous mappings (which access their backing pages directly),
/// writes to a private file mapping perform copy-on-write against the underlying file VMO.
/// `MappingFlags::ANONYMOUS` must also be checked because some anonymous mappings (such as
/// `mmap` of `/dev/zero`) carry `MappingName::File` for `/proc/<pid>/maps` display while
/// setting `MappingFlags::ANONYMOUS`.
pub fn is_private_file(&self) -> bool {
!self.flags.intersects(MappingFlags::SHARED | MappingFlags::ANONYMOUS)
&& matches!(self.name(), MappingNameRef::File(_))
}
/// Returns whether this is a private file mapping that is not yet recorded as holding pages
/// modified via copy-on-write.
pub fn is_unmodified_private_file(&self) -> bool {
self.is_private_file() && !self.is_known_cow_modified()
}
/// Returns whether this is a private file mapping with pages that were modified via
/// copy-on-write, and are therefore no longer shared with the mapped file.
///
/// Copy-on-write modifications are tracked at the granularity of the backing `MemoryObject`
/// (and synchronized across all mappings sharing that backing memory when write access is
/// enabled), rather than per-page.
pub fn has_cow_modifications(&self) -> bool {
self.is_private_file() && self.is_known_cow_modified()
}
/// Records that the memory of this mapping holds pages modified via copy-on-write. See
/// `Mapping::has_cow_modifications`.
pub fn set_cow_modified(&mut self) {
debug_assert!(self.is_private_file());
if let MappingBacking::Memory(backing) = &mut self.backing {
backing.set_cow_modified();
}
}
/// Returns whether the memory of this mapping is already known to hold pages modified via
/// copy-on-write.
fn is_known_cow_modified(&self) -> bool {
matches!(&self.backing, MappingBacking::Memory(backing) if backing.is_cow_modified())
}
/// Returns the [`MappingFlags`] to apply in the user VMAR when mapping or restoring
/// permissions on populated pages of this mapping.
///
/// For writable private file mappings, `MappingFlags::WRITE` is omitted while the mapping is
/// unmodified (both when initially mapped and across any subsequent VMAR remaps or protection
/// updates) so that the first write faults. Once a write is recorded with
/// [`Mapping::set_cow_modified`], `vmar_flags()` includes `MappingFlags::WRITE`.
///
/// Note that `userfaultfd` (`MappingFlags::UFFD`) strips VMAR permissions separately on
/// unpopulated pages via `UserFault`, and uses `vmar_flags()` when restoring permissions on
/// populated or unregistered ranges.
pub fn vmar_flags(&self) -> MappingFlags {
if self.is_unmodified_private_file() {
self.flags - MappingFlags::WRITE
} else {
self.flags
}
}
pub fn private_anonymous(&self) -> bool {
if let MappingBacking::PrivateAnonymous = &self.backing {
return true;
}
!self.flags.contains(MappingFlags::SHARED) && self.flags.contains(MappingFlags::ANONYMOUS)
}
pub fn write_vm_flags(&self, sink: &mut DynamicFileBuf) {
// Linux defines ~38 three-byte VmFlags mnemonics ("xx ", <= 114 bytes), of which Starnix
// currently emits at most 15 (45 bytes).
let mut buf = [0u8; 128];
let mut len = 0;
let mut push = |flag: &[u8]| {
buf[len..len + flag.len()].copy_from_slice(flag);
len += flag.len();
};
let max_access = self.max_access();
// From <https://man7.org/linux/man-pages/man5/proc_pid_smaps.5.html>:
//
// rd - readable
if self.flags.contains(MappingFlags::READ) {
push(b"rd ");
}
// wr - writable
if self.flags.contains(MappingFlags::WRITE) {
push(b"wr ");
}
// ex - executable
if self.flags.contains(MappingFlags::EXEC) {
push(b"ex ");
}
// sh - shared
if self.flags.contains(MappingFlags::SHARED) && max_access.contains(Access::WRITE) {
push(b"sh ");
}
// mr - may read
if max_access.contains(Access::READ) {
push(b"mr ");
}
// mw - may write
if max_access.contains(Access::WRITE) {
push(b"mw ");
}
// me - may execute
if max_access.contains(Access::EXEC) {
push(b"me ");
}
// ms - may share
if self.flags.contains(MappingFlags::SHARED) {
push(b"ms ");
}
// gd - stack segment grows down
if self.flags.contains(MappingFlags::GROWSDOWN) {
push(b"gd ");
}
// pf - pure PFN range
// dw - disabled write to the mapped file
// lo - pages are locked in memory
if self.flags.contains(MappingFlags::LOCKED) {
push(b"lo ");
}
// io - memory mapped I/O area
// sr - sequential read advise provided
// rr - random read advise provided
// dc - do not copy area on fork
if self.flags.contains(MappingFlags::DONTFORK) {
push(b"dc ");
}
// de - do not expand area on remapping
if self.flags.contains(MappingFlags::DONT_EXPAND) {
push(b"de ");
}
// ac - area is accountable
push(b"ac ");
// nr - swap space is not reserved for the area
// ht - area uses huge tlb pages
// sf - perform synchronous page faults (since Linux 4.15)
// nl - non-linear mapping (removed in Linux 4.0)
// ar - architecture specific flag
// wf - wipe on fork (since Linux 4.14)
if self.flags.contains(MappingFlags::WIPEONFORK) {
push(b"wf ");
}
// dd - do not include area into core dump
// sd - soft-dirty flag (since Linux 3.13)
// mm - mixed map area
// hg - huge page advise flag
// nh - no-huge page advise flag
// mg - mergeable advise flag
// um - userfaultfd missing pages tracking (since Linux 4.3)
if self.flags.contains(MappingFlags::UFFD_MISSING) {
push(b"um ");
}
// uw - userfaultfd wprotect pages tracking (since Linux 4.3)
// ui - userfaultfd minor fault pages tracking (since Linux 5.13)
sink.write(&buf[..len]);
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum MappingBacking {
Memory(MappingBackingMemory),
PrivateAnonymous,
}
impl MappingBacking {
pub fn as_memory(&self) -> Option<&MappingBackingMemory> {
match self {
Self::Memory(memory) => Some(memory),
Self::PrivateAnonymous => None,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone, split_enum_storage::SplitStorage)]
pub enum MappingName {
/// No name.
None,
/// This mapping is the initial stack.
Stack,
/// This mapping is the heap.
Heap,
/// This mapping is the vdso.
Vdso,
/// This mapping is the vvar.
Vvar,
/// The file backing this mapping.
File(Arc<FileMapping>),
/// The name associated with the mapping. Set by prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...).
/// An empty name is distinct from an unnamed mapping. Mappings are initially created with no
/// name and can be reset to the unnamed state by passing NULL to
/// prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...).
Vma(FlyByteStr),
/// The name associated with the mapping of an ashmem region. Set by ioctl(fd, ASHMEM_SET_NAME, ...).
/// By default "dev/ashmem".
Ashmem(FlyByteStr),
/// This mapping is a context for asynchronous I/O.
AioContext(Arc<AioContext>),
}
impl<'a> MappingNameRef<'a> {
pub fn as_file(&self) -> Option<&'a Arc<FileMapping>> {
match self {
Self::File(file) => Some(file),
_ => None,
}
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct MappingBackingMemory {
/// The memory object that contains the memory used in this mapping.
memory: Arc<MemoryObject>,
/// The delta to convert from a user address to an offset in the memory object, and flags in
/// its lowest bits (see `ADDRESS_TO_OFFSET_DELTA_FLAGS`). Both the base address and the offset
/// of a mapping are page-aligned, so these bits of the delta are otherwise always zero.
address_to_offset_delta_and_flags: u64,
}
// Keep the size of the backing of memory mappings small, as it is stored inline in `Mapping`.
static_assertions::assert_eq_size!(MappingBackingMemory, [u8; 16]);
/// Set in `MappingBackingMemory::address_to_offset_delta_and_flags` when the backing memory is
/// known to hold pages modified via copy-on-write (tracked at the granularity of the backing
/// `MemoryObject` and synchronized across all mappings sharing that backing memory when write
/// access is enabled on a private file mapping) that are no longer shared with the mapped file.
/// See `Mapping::has_cow_modifications`.
const COW_MODIFIED: u64 = 1;
/// The flags stored in `MappingBackingMemory::address_to_offset_delta_and_flags`.
const ADDRESS_TO_OFFSET_DELTA_FLAGS: u64 = COW_MODIFIED;
impl MappingBackingMemory {
pub fn new(base: UserAddress, memory: Arc<MemoryObject>, memory_offset: u64) -> Self {
let address_to_offset_delta = memory_offset.wrapping_sub(base.ptr() as u64);
debug_assert_eq!(
address_to_offset_delta & ADDRESS_TO_OFFSET_DELTA_FLAGS,
0,
"mapping base {base:?} and memory offset {memory_offset:#x} must be page-aligned"
);
Self { memory, address_to_offset_delta_and_flags: address_to_offset_delta }
}
/// Records that the memory holds pages modified via copy-on-write.
pub fn set_cow_modified(&mut self) {
self.address_to_offset_delta_and_flags |= COW_MODIFIED;
}
/// Returns whether the memory is known to hold pages modified via copy-on-write.
fn is_cow_modified(&self) -> bool {
self.address_to_offset_delta_and_flags & COW_MODIFIED != 0
}
pub fn memory(&self) -> &Arc<MemoryObject> {
&self.memory
}
/// Reads exactly `bytes.len()` bytes of memory from `addr`.
///
/// # Parameters
/// - `addr`: The address to read data from.
/// - `bytes`: The byte array to read into.
pub fn read_memory<'a>(
&self,
addr: UserAddress,
bytes: &'a mut [MaybeUninit<u8>],
) -> Result<&'a mut [u8], Errno> {
self.memory.read_uninit(bytes, self.address_to_offset(addr)).map_err(|_| errno!(EFAULT))
}
/// Writes the provided bytes to `addr`.
///
/// # Parameters
/// - `addr`: The address to write to.
/// - `bytes`: The bytes to write to the memory object.
pub fn write_memory(&self, addr: UserAddress, bytes: &[u8]) -> Result<(), Errno> {
self.memory.write(bytes, self.address_to_offset(addr)).map_err(|_| errno!(EFAULT))
}
pub fn zero(&self, addr: UserAddress, length: usize) -> Result<usize, Errno> {
self.memory
.op_range(zx::VmoOp::ZERO, self.address_to_offset(addr), length as u64)
.map_err(|_| errno!(EFAULT))?;
Ok(length)
}
/// Converts a `UserAddress` to an offset in this mapping's memory object.
pub fn address_to_offset(&self, addr: UserAddress) -> u64 {
(addr.ptr() as u64)
.wrapping_add(self.address_to_offset_delta_and_flags & !ADDRESS_TO_OFFSET_DELTA_FLAGS)
}
}
bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[rustfmt::skip] // Preserve column alignment.
pub struct MappingFlags: u32 {
const READ = 1 << 0; // PROT_READ
const WRITE = 1 << 1; // PROT_WRITE
const EXEC = 1 << 2; // PROT_EXEC
const SHARED = 1 << 3;
const ANONYMOUS = 1 << 4;
const LOWER_32BIT = 1 << 5;
const GROWSDOWN = 1 << 6;
const ELF_BINARY = 1 << 7;
const DONTFORK = 1 << 8;
const WIPEONFORK = 1 << 9;
const DONT_SPLIT = 1 << 10;
const DONT_EXPAND = 1 << 11;
/// A `MAP_PRIVATE` file mapping that is still backed directly by the file's VMO, through
/// a handle without `WRITE` rights. It must be promoted to a copy-on-write child VMO
/// before it is written or becomes writable (see
/// `MemoryManagerState::promote_to_private_child`). Writable ones are mapped without
/// `WRITE` in the user VMAR, so that the first write faults.
const LAZY_PRIVATE_FILE = 1 << 12;
const LOCKED = 1 << 13;
const UFFD = 1 << 14;
const UFFD_MISSING = 1 << 15;
const MAPPED_IN_VMAR = 1 << 16;
}
}
// The low three bits of MappingFlags match ProtectionFlags.
const_assert_eq!(MappingFlags::READ.bits(), PROT_READ);
const_assert_eq!(MappingFlags::WRITE.bits(), PROT_WRITE);
const_assert_eq!(MappingFlags::EXEC.bits(), PROT_EXEC);
// The next bits of MappingFlags match MappingOptions, shifted up.
const_assert_eq!(MappingFlags::SHARED.bits(), MappingOptions::SHARED.bits() << 3);
const_assert_eq!(MappingFlags::ANONYMOUS.bits(), MappingOptions::ANONYMOUS.bits() << 3);
const_assert_eq!(MappingFlags::LOWER_32BIT.bits(), MappingOptions::LOWER_32BIT.bits() << 3);
const_assert_eq!(MappingFlags::GROWSDOWN.bits(), MappingOptions::GROWSDOWN.bits() << 3);
const_assert_eq!(MappingFlags::ELF_BINARY.bits(), MappingOptions::ELF_BINARY.bits() << 3);
const_assert_eq!(MappingFlags::DONTFORK.bits(), MappingOptions::DONTFORK.bits() << 3);
const_assert_eq!(MappingFlags::WIPEONFORK.bits(), MappingOptions::WIPEONFORK.bits() << 3);
const_assert_eq!(MappingFlags::DONT_SPLIT.bits(), MappingOptions::DONT_SPLIT.bits() << 3);
const_assert_eq!(MappingFlags::DONT_EXPAND.bits(), MappingOptions::DONT_EXPAND.bits() << 3);
const_assert_eq!(
MappingFlags::LAZY_PRIVATE_FILE.bits(),
MappingOptions::LAZY_PRIVATE_FILE.bits() << 3
);
/// `MappingOptions` that only affect how a mapping is created. They have no corresponding
/// `MappingFlags` bit and are dropped when converting between the two.
const CREATION_ONLY_OPTIONS: MappingOptions = MappingOptions::POPULATE;
// Every other `MappingOptions` bit must land exactly on one of the `MappingFlags` bits asserted
// above. Otherwise shifting the options up would silently set an unrelated flag.
const_assert_eq!(
MappingOptions::all().difference(CREATION_ONLY_OPTIONS).bits() << 3,
MappingFlags::SHARED.bits()
| MappingFlags::ANONYMOUS.bits()
| MappingFlags::LOWER_32BIT.bits()
| MappingFlags::GROWSDOWN.bits()
| MappingFlags::ELF_BINARY.bits()
| MappingFlags::DONTFORK.bits()
| MappingFlags::WIPEONFORK.bits()
| MappingFlags::DONT_SPLIT.bits()
| MappingFlags::DONT_EXPAND.bits()
| MappingFlags::LAZY_PRIVATE_FILE.bits()
);
impl MappingFlags {
pub fn access_flags(&self) -> ProtectionFlags {
ProtectionFlags::from_bits_truncate(self.bits() & ProtectionFlags::ACCESS_FLAGS.bits())
}
pub fn with_access_flags(&self, prot_flags: ProtectionFlags) -> Self {
let mapping_flags =
*self & (MappingFlags::READ | MappingFlags::WRITE | MappingFlags::EXEC).complement();
mapping_flags | Self::from_bits_truncate(prot_flags.access_flags().bits())
}
pub fn options(&self) -> MappingOptions {
MappingOptions::from_bits_truncate(self.bits() >> 3).difference(CREATION_ONLY_OPTIONS)
}
pub fn from_access_flags_and_options(
prot_flags: ProtectionFlags,
options: MappingOptions,
) -> Self {
Self::from_bits_truncate(prot_flags.access_flags().bits())
| Self::from_bits_truncate(options.difference(CREATION_ONLY_OPTIONS).bits() << 3)
}
}
#[derive(Debug, Default)]
pub struct MappingSummary {
no_kind: MappingKindSummary,
stack: MappingKindSummary,
heap: MappingKindSummary,
vdso: MappingKindSummary,
vvar: MappingKindSummary,
file: MappingKindSummary,
vma: MappingKindSummary,
ashmem: MappingKindSummary,
aiocontext: MappingKindSummary,
name_lengths: Vec<usize>,
}
impl MappingSummary {
pub fn add(&mut self, mm_state: &MemoryManagerState, mapping: &Mapping) {
let kind_summary = match mapping.name() {
MappingNameRef::None => &mut self.no_kind,
MappingNameRef::Stack => &mut self.stack,
MappingNameRef::Heap => &mut self.heap,
MappingNameRef::Vdso => &mut self.vdso,
MappingNameRef::Vvar => &mut self.vvar,
MappingNameRef::File(_) => &mut self.file,
MappingNameRef::Vma(name) => {
self.name_lengths.push(name.len());
&mut self.vma
}
MappingNameRef::Ashmem(name) => {
self.name_lengths.push(name.len());
&mut self.ashmem
}
MappingNameRef::AioContext(_) => &mut self.aiocontext,
};
kind_summary.count += 1;
if mapping.flags.contains(MappingFlags::SHARED) {
kind_summary.num_shared += 1;
} else {
kind_summary.num_private += 1;
}
match mm_state.get_mapping_backing(mapping) {
MappingBacking::Memory(_) => {
kind_summary.num_memory_objects += 1;
}
MappingBacking::PrivateAnonymous => kind_summary.num_private_anon += 1,
}
}
pub fn record(self, node: &fuchsia_inspect::Node) {
node.record_child("no_kind", |node| self.no_kind.record(node));
node.record_child("stack", |node| self.stack.record(node));
node.record_child("heap", |node| self.heap.record(node));
node.record_child("vdso", |node| self.vdso.record(node));
node.record_child("vvar", |node| self.vvar.record(node));
node.record_child("file", |node| self.file.record(node));
node.record_child("vma", |node| self.vma.record(node));
node.record_child("ashmem", |node| self.ashmem.record(node));
node.record_child("aiocontext", |node| self.aiocontext.record(node));
let name_lengths = node.create_uint_linear_histogram(
"name_lengths",
fuchsia_inspect::LinearHistogramParams { floor: 0, step_size: 8, buckets: 4 },
);
for l in self.name_lengths {
name_lengths.insert(l as u64);
}
node.record(name_lengths);
}
}
#[derive(Debug, Default)]
struct MappingKindSummary {
count: u64,
num_private: u64,
num_shared: u64,
num_memory_objects: u64,
num_private_anon: u64,
}
impl MappingKindSummary {
fn record(&self, node: &fuchsia_inspect::Node) {
node.record_uint("count", self.count);
node.record_uint("num_private", self.num_private);
node.record_uint("num_shared", self.num_shared);
node.record_uint("num_memory_objects", self.num_memory_objects);
node.record_uint("num_private_anon", self.num_private_anon);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[::fuchsia::test]
fn cow_modified_flag_preserves_address_to_offset() {
let memory = Arc::new(MemoryObject::from(zx::Vmo::create(0x10000).expect("create VMO")));
let base = UserAddress::from(0x200000u64);
let addr = UserAddress::from(0x201000u64);
// The memory offset may be larger or smaller than the base address.
for (memory_offset, expected_offset) in [(0x3000, 0x4000), (0, 0x1000)] {
let mut backing = MappingBackingMemory::new(base, memory.clone(), memory_offset);
assert!(!backing.is_cow_modified());
assert_eq!(backing.address_to_offset(addr), expected_offset);
backing.set_cow_modified();
assert!(backing.is_cow_modified());
assert_eq!(backing.address_to_offset(addr), expected_offset);
}
}
}