Skip to main content

detcore/syscalls/
memory.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Deterministic policies for memory-management advice.
10
11use reverie::Error;
12use reverie::Guest;
13use reverie::syscalls;
14use reverie::syscalls::AddrMut;
15use reverie::syscalls::Errno;
16use reverie::syscalls::MemoryAccess;
17
18use crate::Detcore;
19use crate::RecordOrReplay;
20
21const PAGE_SIZE: usize = 4096;
22// Added in Linux 6.13 and not yet exposed by the pinned libc crate.
23const MADV_GUARD_INSTALL: i32 = 102;
24const MADV_GUARD_REMOVE: i32 = 103;
25
26#[derive(Debug, Clone, Copy, Eq, PartialEq)]
27enum MadviseAction {
28    ForwardHint,
29    ForwardSemantic,
30    Ignore,
31    Reject(Errno),
32    Unknown,
33}
34
35const fn madvise_action(advice: i32) -> MadviseAction {
36    match advice {
37        // Pure access-pattern and prefetch hints have no required memory-content
38        // side effect. Backends without native madvise support accept them as no-ops.
39        libc::MADV_NORMAL | libc::MADV_RANDOM | libc::MADV_SEQUENTIAL | libc::MADV_WILLNEED => {
40            MadviseAction::ForwardHint
41        }
42
43        // Operations with guest-visible memory, fork, backing-store, dump, or guard
44        // semantics must reach a backend that implements native madvise behavior.
45        libc::MADV_DONTNEED
46        | libc::MADV_DONTFORK
47        | libc::MADV_DOFORK
48        | libc::MADV_DONTDUMP
49        | libc::MADV_DODUMP
50        | libc::MADV_WIPEONFORK
51        | libc::MADV_KEEPONFORK
52        | libc::MADV_DONTNEED_LOCKED
53        | MADV_GUARD_INSTALL
54        | MADV_GUARD_REMOVE => MadviseAction::ForwardSemantic,
55
56        // These are optional reclaim or asynchronous VM-policy controls. Their host
57        // effects depend on memory pressure, KSM, and THP activity. Hermit accepts
58        // them as fixed no-ops after deterministic argument validation; it deliberately
59        // does not reproduce each advice's host- and mapping-specific EINVAL cases.
60        libc::MADV_FREE
61        | libc::MADV_MERGEABLE
62        | libc::MADV_UNMERGEABLE
63        | libc::MADV_HUGEPAGE
64        | libc::MADV_NOHUGEPAGE
65        | libc::MADV_COLD
66        | libc::MADV_PAGEOUT => MadviseAction::Ignore,
67
68        // Hole punching mutates backing storage and every mapping alias. Refuse it
69        // until Detcore can update file resources and replay all affected aliases.
70        libc::MADV_REMOVE => MadviseAction::Reject(Errno::EINVAL),
71
72        // Successful population and collapse promise synchronous, resource-
73        // dependent work. Report the same deterministic error Linux uses when
74        // an advice value is unsupported so callers can take their fallback.
75        libc::MADV_POPULATE_READ | libc::MADV_POPULATE_WRITE | libc::MADV_COLLAPSE => {
76            MadviseAction::Reject(Errno::EINVAL)
77        }
78
79        // Never let a guest inject host memory failures, even if the container
80        // unexpectedly has enough privilege to make these operations succeed.
81        libc::MADV_HWPOISON | libc::MADV_SOFT_OFFLINE => MadviseAction::Reject(Errno::EPERM),
82
83        _ => MadviseAction::Unknown,
84    }
85}
86
87fn validate_common_args(call: syscalls::Madvise) -> Result<(), Error> {
88    let start = call.addr().map(AddrMut::as_raw).unwrap_or(0);
89    if !start.is_multiple_of(PAGE_SIZE) {
90        return Err(Errno::EINVAL.into());
91    }
92    if call.len() == 0 {
93        return Ok(());
94    }
95
96    let end = start.checked_add(call.len()).ok_or(Errno::EINVAL)?;
97    end.checked_add(PAGE_SIZE - 1).ok_or(Errno::EINVAL)?;
98    Ok(())
99}
100
101impl<T: RecordOrReplay> Detcore<T> {
102    /// Apply a deterministic policy to madvise(2).
103    ///
104    /// Ptrace/DBT forward hints and supported advice with guest-visible semantics.
105    /// Record/replay accepts pure hints as no-ops and passes guest-semantic advice
106    /// to the recorder and replayer, which record and restore the effects that
107    /// differ because replay replaces file mappings with anonymous mappings.
108    /// Reclaim and asynchronous VM-policy advice receives fixed success without exposing host memory pressure. Resource-
109    /// dependent, backing-store, and hardware-failure operations receive fixed errors.
110    /// KVM accepts pure hints as no-ops, implements MADV_DONTNEED in its executor
111    /// (`supports_madv_dontneed`), and reports ENOSYS for the other guest-visible
112    /// semantics it cannot provide.
113    // AUTONOMOUS-BOT-IMPLEMENTED
114    // TODO-HUMAN-REVIEW(#548): Recheck advice policy and record/replay boundaries.
115    pub async fn handle_madvise<G: Guest<Self>>(
116        &self,
117        guest: &mut G,
118        call: syscalls::Madvise,
119    ) -> Result<i64, Error> {
120        let advice = call.advice();
121        let action = madvise_action(advice);
122        validate_common_args(call)?;
123
124        if call.len() == 0 {
125            return match action {
126                MadviseAction::Unknown => Err(Errno::EINVAL.into()),
127                _ => Ok(0),
128            };
129        }
130        if self.cfg.recordreplay_modes && action == MadviseAction::ForwardHint {
131            crate::detlog!(
132                "[dtid {}] madvise hint {} accepted as record/replay no-op",
133                guest.thread_state().dettid,
134                advice,
135            );
136            return Ok(0);
137        }
138
139        match action {
140            MadviseAction::ForwardHint if self.cfg.backend.supports_madvise => {
141                Ok(self.record_or_replay(guest, call).await?)
142            }
143            MadviseAction::ForwardHint => {
144                crate::detlog!(
145                    "[dtid {}] madvise hint {} accepted as backend no-op",
146                    guest.thread_state().dettid,
147                    advice,
148                );
149                Ok(0)
150            }
151            MadviseAction::ForwardSemantic
152                if self.cfg.backend.supports_madvise
153                    || (advice == libc::MADV_DONTNEED
154                        && self.cfg.backend.supports_madv_dontneed) =>
155            {
156                Ok(self.record_or_replay(guest, call).await?)
157            }
158            MadviseAction::ForwardSemantic => {
159                crate::detlog!(
160                    "[dtid {}] madvise advice {} is unsupported by this backend",
161                    guest.thread_state().dettid,
162                    advice,
163                );
164                Err(Errno::ENOSYS.into())
165            }
166            MadviseAction::Ignore => {
167                crate::detlog!(
168                    "[dtid {}] madvise advice {} accepted as deterministic no-op",
169                    guest.thread_state().dettid,
170                    advice,
171                );
172                Ok(0)
173            }
174            MadviseAction::Reject(errno) => {
175                crate::detlog!(
176                    "[dtid {}] madvise advice {} rejected with {}",
177                    guest.thread_state().dettid,
178                    advice,
179                    errno,
180                );
181                Err(errno.into())
182            }
183            MadviseAction::Unknown => {
184                crate::detlog!(
185                    "[dtid {}] unknown madvise advice {} rejected with EINVAL",
186                    guest.thread_state().dettid,
187                    advice,
188                );
189                Err(Errno::EINVAL.into())
190            }
191        }
192    }
193
194    /// Deterministic `mincore(2)`.
195    ///
196    /// Real page residency reflects host memory pressure and is therefore
197    /// nondeterministic, which is why mincore was previously classified as an
198    /// unsupported syscall. GNU grep (and other glibc consumers) invoke mincore
199    /// under the KVM backend on a code path the ptrace backend does not take, so
200    /// leaving it unsupported aborts the guest under `--strict`.
201    ///
202    /// Inject the call first so the backend preserves Linux pointer and mapping
203    /// validation, then report every mapped page as resident. The residency
204    /// vector is only an advisory hint, so replacing those nondeterministic bits
205    /// with a constant answer remains bitwise-identical across runs.
206    // AUTONOMOUS-BOT-IMPLEMENTED
207    // TODO-HUMAN-REVIEW(#775): Review deterministic mincore residency emulation.
208    pub async fn handle_mincore<G: Guest<Self>>(
209        &self,
210        guest: &mut G,
211        call: syscalls::Mincore,
212    ) -> Result<i64, Error> {
213        // Linux rejects a start address that is not page-aligned with EINVAL.
214        let start = call.addr().map(AddrMut::as_raw).unwrap_or(0);
215        if !start.is_multiple_of(PAGE_SIZE) {
216            return Err(Errno::EINVAL.into());
217        }
218        let len = call.len();
219        if len == 0 {
220            return Ok(0);
221        }
222        // Match madvise's overflow guard on the requested range.
223        start.checked_add(len).ok_or(Errno::EINVAL)?;
224        let page_count = len.div_ceil(PAGE_SIZE);
225        let vec = call.vec().ok_or(Errno::EFAULT)?;
226        guest.inject(syscalls::Syscall::from(call)).await?;
227        let residency = vec![1u8; page_count];
228        guest.memory().write_exact(vec, &residency)?;
229        Ok(0)
230    }
231}
232
233/// The page size `brk(2)` rounds the break to.
234const BRK_PAGE: u64 = 4096;
235
236fn brk_page_align(address: u64) -> Option<u64> {
237    Some(address.checked_add(BRK_PAGE - 1)? & !(BRK_PAGE - 1))
238}
239
240/// Linux's default `stack_guard_gap`: 256 pages kept free below a mapping
241/// that grows down.
242const STACK_GUARD_GAP: u64 = 256 * BRK_PAGE;
243
244/// One mapping of a `/proc/<pid>/smaps` text: its range, and the gap Linux
245/// keeps free below it (`stack_guard_start_gap`): `stack_guard_gap` below a
246/// mapping that grows down (`gd` among its `VmFlags`), a page below a
247/// shadow stack (`ss`), none below any other.
248#[derive(Debug, Clone, Copy, PartialEq, Eq)]
249struct Vma {
250    start: u64,
251    end: u64,
252    start_gap: u64,
253}
254
255/// The mappings of a `/proc/<pid>/smaps` text, in address order, parsed as
256/// they are asked for: nothing is allocated, so where the tool shares the
257/// guest's process its allocator maps nothing between the read and the
258/// injected call that relies on it.
259fn smaps_vmas(smaps: &[u8]) -> SmapsVmas<'_> {
260    SmapsVmas { rest: smaps }
261}
262
263#[derive(Clone)]
264struct SmapsVmas<'a> {
265    rest: &'a [u8],
266}
267
268impl<'a> SmapsVmas<'a> {
269    /// The next line, without consuming it.
270    fn peek(&self) -> Option<&'a [u8]> {
271        (!self.rest.is_empty()).then(|| {
272            self.rest
273                .split(|&byte| byte == b'\n')
274                .next()
275                .unwrap_or_default()
276        })
277    }
278
279    fn advance(&mut self) {
280        self.rest = match self.rest.iter().position(|&byte| byte == b'\n') {
281            Some(newline) => &self.rest[newline + 1..],
282            None => &[],
283        };
284    }
285}
286
287/// A mapping's range, from the first field of its header line; `None` for
288/// any other line. Bytes, not text: the pathname that follows is whatever
289/// bytes the file was named with.
290fn smaps_range(line: &[u8]) -> Option<(u64, u64)> {
291    let range = line.split(|&byte| byte == b' ').next()?;
292    let (start, end) = std::str::from_utf8(range).ok()?.split_once('-')?;
293    Some((
294        u64::from_str_radix(start, 16).ok()?,
295        u64::from_str_radix(end, 16).ok()?,
296    ))
297}
298
299impl Iterator for SmapsVmas<'_> {
300    type Item = Vma;
301
302    fn next(&mut self) -> Option<Vma> {
303        let (start, end) = loop {
304            let line = self.peek()?;
305            self.advance();
306            if let Some(range) = smaps_range(line) {
307                break range;
308            }
309        };
310        let mut vma = Vma {
311            start,
312            end,
313            start_gap: 0,
314        };
315        // The mapping's fields, up to the next mapping's header.
316        while let Some(line) = self.peek() {
317            if smaps_range(line).is_some() {
318                break;
319            }
320            self.advance();
321            if let Some(flags) = line.strip_prefix(b"VmFlags:") {
322                let mut flags = flags.split(|byte| byte.is_ascii_whitespace());
323                vma.start_gap = if flags.clone().any(|flag| flag == b"gd") {
324                    STACK_GUARD_GAP
325                } else if flags.any(|flag| flag == b"ss") {
326                    BRK_PAGE
327                } else {
328                    0
329                };
330            }
331        }
332        Some(vma)
333    }
334}
335
336/// Whether Linux's `brk` grows a break whose page-aligned end is `old_end`
337/// to `new_end`: no mapping may meet `[old_end, new_end + PAGE)`, nor a
338/// mapping that grows down have its guard gap there
339/// (`vma_find(&vmi, newbrk + PAGE_SIZE + stack_guard_gap)` and
340/// `vm_start_gap`).
341fn brk_may_grow(mut vmas: impl Iterator<Item = Vma>, old_end: u64, new_end: u64) -> bool {
342    let Some(limit) = new_end.checked_add(BRK_PAGE) else {
343        return false;
344    };
345    let search_end = limit.saturating_add(STACK_GUARD_GAP);
346    let Some(next) = vmas.find(|vma| vma.end > old_end && vma.start < search_end) else {
347        return true;
348    };
349    // `vm_start_gap`, which gives 0 where the gap would pass it.
350    let gap_start = next.start.saturating_sub(next.start_gap);
351    limit <= gap_start
352}
353
354/// Whether Linux's `brk` shrinks a break whose page-aligned end is `old_end`
355/// to `new_end`: only where a mapping meets `[new_end, old_end)`
356/// (`vma_find(&vmi, oldbrk)` from `newbrk`); otherwise it keeps the old
357/// break.
358fn brk_may_shrink(mut vmas: impl Iterator<Item = Vma>, new_end: u64, old_end: u64) -> bool {
359    vmas.any(|vma| vma.end > new_end && vma.start < old_end)
360}
361
362// Bounded DEBUG diagnostic: no new procfs read and no formatting between
363// the metadata capture and a mapping operation. Only the first rejected moving
364// request in this process is retained. Fork inherits the diagnostic latch.
365struct BrkDiagnostic<'a> {
366    dettid: i32,
367    model: &'a crate::memory::BrkModel,
368    requested: u64,
369}
370
371struct BrkDiagnosticHex<'a>(&'a [u8]);
372
373impl std::fmt::Display for BrkDiagnosticHex<'_> {
374    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
375        for byte in self.0 {
376            write!(formatter, "{byte:02x}")?;
377        }
378        Ok(())
379    }
380}
381
382impl BrkDiagnostic<'_> {
383    fn rejected(
384        &self,
385        branch: &'static str,
386        limit: Option<u64>,
387        smaps: Option<&[u8]>,
388        kernel_result: &dyn std::fmt::Debug,
389    ) {
390        use std::sync::atomic::AtomicBool;
391        use std::sync::atomic::Ordering;
392        static CAPTURED: AtomicBool = AtomicBool::new(false);
393        const MAX_BYTES: usize = 256 * 1024;
394        if self.requested == 0
395            || self.requested == self.model.current
396            || !tracing::enabled!(target: "detcore::brk_diagnostic", tracing::Level::DEBUG)
397            || CAPTURED.swap(true, Ordering::Relaxed)
398        {
399            return;
400        }
401        let complete = smaps.is_some_and(|bytes| bytes.len() <= MAX_BYTES);
402        tracing::debug!(
403            target: "detcore::brk_diagnostic",
404            dettid = self.dettid,
405            requested = self.requested,
406            start = self.model.start,
407            current = self.model.current,
408            data_span = self.model.data_span,
409            launcher_break = self.model.launcher_break,
410            ?limit,
411            branch,
412            ?kernel_result,
413            smaps_len = ?smaps.map(<[u8]>::len),
414            complete,
415            "BRK_DENIAL_V1 begin",
416        );
417        if complete {
418            for (index, chunk) in smaps.unwrap().chunks(1024).enumerate() {
419                tracing::debug!(
420                    target: "detcore::brk_diagnostic",
421                    dettid = self.dettid,
422                    index,
423                    bytes = %BrkDiagnosticHex(chunk),
424                    "BRK_DENIAL_V1 chunk",
425                );
426            }
427        }
428        tracing::debug!(
429            target: "detcore::brk_diagnostic",
430            dettid = self.dettid,
431            complete,
432            "BRK_DENIAL_V1 end",
433        );
434    }
435}
436
437/// The mapping-dependent part of a modeled program-break request. Endpoints
438/// are page-aligned; resource limits and the actual mapping operation stay
439/// with the caller, independently of where the smaps bytes were captured.
440#[derive(
441    Debug,
442    Clone,
443    Copy,
444    PartialEq,
445    Eq,
446    serde::Serialize,
447    serde::Deserialize
448)]
449pub enum BrkMappingCheck {
450    /// Check new pages and Linux's guard gap above the old break.
451    Grow { old_end: u64, new_end: u64 },
452    /// Check that some mapping overlaps the pages being removed.
453    Shrink { new_end: u64, old_end: u64 },
454}
455
456impl BrkMappingCheck {
457    pub(crate) fn allows(self, smaps: &[u8]) -> bool {
458        match self {
459            Self::Grow { old_end, new_end } => brk_may_grow(smaps_vmas(smaps), old_end, new_end),
460            Self::Shrink { new_end, old_end } => {
461                brk_may_shrink(smaps_vmas(smaps), new_end, old_end)
462            }
463        }
464    }
465}
466
467impl<T: RecordOrReplay> Detcore<T> {
468    /// `brk(addr)` in a run that declares a launcher
469    /// (`Detcore::declares_launcher`), where the kernel's program break is the
470    /// launcher's: answered from Detcore's model of the guest's break
471    /// ([`crate::memory::BrkModel`]), as Linux's `brk` (mm/mmap.c) answers
472    /// from `mm->brk`. A request below the start, one past `RLIMIT_DATA`,
473    /// growth that would meet another mapping or its guard gap, or a shrink
474    /// over a range with no mapping leaves the break where it is; the call
475    /// returns the break in effect afterwards, never an error. Growth maps
476    /// exactly the new anonymous zero pages (`MAP_FIXED_NOREPLACE`, so an
477    /// existing mapping is never replaced) and shrinking unmaps them.
478    ///
479    /// Refused by name, where an answer would be a plausible wrong one: a
480    /// brk without a model for the image (with the reason there is none),
481    /// and a request at or above the launcher's break but below the model's
482    /// start. Only a libc that cached the launcher's break before Detcore's
483    /// first callback asks for that, and it would take Linux's answer, the
484    /// break in effect above its request, for a grant at its stale address.
485    pub(crate) async fn handle_brk<G: Guest<Self>>(
486        &self,
487        guest: &mut G,
488        call: syscalls::Syscall,
489    ) -> Result<i64, Error> {
490        let (model, unmodeled) = {
491            let metadata = guest
492                .thread_state()
493                .memory_metadata
494                .lock()
495                .expect("memory metadata mutex poisoned");
496            (metadata.brk_model, metadata.brk_unmodeled.clone())
497        };
498        let Some(model) = model else {
499            let reason = format!(
500                "unsupported: brk where the kernel's program break is the launcher's and Detcore has no model of the guest's: {}",
501                unmodeled
502                    .as_deref()
503                    .unwrap_or("the image was never modeled"),
504            );
505            return self
506                .refuse_launched_exe_because(guest, syscalls::Sysno::brk, reason)
507                .await;
508        };
509        let requested = syscalls::SyscallInfo::into_parts(call).1.arg0 as u64;
510        let granted = match self.granted_brk(guest, &model, requested).await {
511            Ok(granted) => granted,
512            Err(reason) => {
513                return self
514                    .refuse_launched_exe_because(guest, syscalls::Sysno::brk, reason.to_owned())
515                    .await;
516            }
517        };
518        guest
519            .thread_state()
520            .memory_metadata
521            .lock()
522            .expect("memory metadata mutex poisoned")
523            .brk_model = Some(crate::memory::BrkModel {
524            current: granted,
525            ..model
526        });
527        Ok(granted as i64)
528    }
529
530    /// The break `brk(requested)` leaves in effect, from `model`, mapping or
531    /// unmapping the pages between the old and the new break; an error where
532    /// Detcore could not keep the address space and the model in step (a
533    /// mapping it could neither place nor remove).
534    async fn granted_brk<G: Guest<Self>>(
535        &self,
536        guest: &mut G,
537        model: &crate::memory::BrkModel,
538        requested: u64,
539    ) -> Result<u64, &'static str> {
540        let current = model.current;
541        let diagnostic = BrkDiagnostic {
542            dettid: guest.thread_state().dettid.as_raw(),
543            model,
544            requested,
545        };
546        if requested < model.start {
547            if requested >= model.launcher_break {
548                diagnostic.rejected("below_start_launcher", None, None, &"not_injected");
549                return Err(
550                    "unsupported: brk from the launcher's break, which the guest's libc read before Detcore modeled the guest's",
551                );
552            }
553            diagnostic.rejected("below_start", None, None, &"not_injected");
554            return Ok(current);
555        }
556        let limit = guest
557            .thread_state()
558            .resource_limits
559            .lock()
560            .unwrap()
561            .get(libc::RLIMIT_DATA)
562            .map_or(libc::RLIM64_INFINITY, |limit| limit.current);
563        let used = (requested - model.start).saturating_add(model.data_span);
564        if limit != libc::RLIM64_INFINITY && used > limit {
565            diagnostic.rejected("rlimit_data", Some(limit), None, &"not_injected");
566            return Ok(current);
567        }
568        let (Some(new_end), Some(old_end)) = (brk_page_align(requested), brk_page_align(current))
569        else {
570            diagnostic.rejected("page_overflow", Some(limit), None, &"not_injected");
571            return Ok(current);
572        };
573        if new_end == old_end {
574            return Ok(requested);
575        }
576        // The address space as Linux's brk inspects it, read with raw
577        // syscalls and no allocation between reads where the tool shares the
578        // guest's process; the guest's other threads are held by the
579        // sequentialized schedule.
580        let smaps = self
581            .guest_task_directory(guest)
582            .and_then(|directory| crate::util::raw_read_listing(&directory.join("smaps")).ok());
583        let check = if requested <= current {
584            BrkMappingCheck::Shrink { new_end, old_end }
585        } else {
586            BrkMappingCheck::Grow { old_end, new_end }
587        };
588        let allowed = match smaps.as_deref() {
589            Some(smaps) => Some(check.allows(smaps)),
590            None if guest
591                .thread_state()
592                .changed_root
593                .load(std::sync::atomic::Ordering::SeqCst) =>
594            {
595                crate::tool_global::check_brk_mappings(guest, check)
596            }
597            None => None,
598        }
599        .ok_or_else(|| {
600            diagnostic.rejected("smaps_unavailable", Some(limit), None, &"not_injected");
601            "unsupported: brk where Detcore cannot read the guest's mappings"
602        })?;
603        if !allowed {
604            diagnostic.rejected(
605                if requested <= current {
606                    "shrink_predicate"
607                } else {
608                    "grow_predicate"
609                },
610                Some(limit),
611                smaps.as_deref(),
612                &"not_injected",
613            );
614            return Ok(current);
615        }
616        if requested <= current {
617            // As Linux, which keeps the old break where the unmap fails.
618            let unmapped = guest
619                .inject_with_retry(syscalls::Syscall::Munmap(
620                    syscalls::Munmap::new()
621                        .with_addr(syscalls::Addr::from_raw(new_end as usize))
622                        .with_len((old_end - new_end) as usize),
623                ))
624                .await;
625            if unmapped.is_err() {
626                diagnostic.rejected("munmap_error", Some(limit), smaps.as_deref(), &unmapped);
627            }
628            return Ok(if unmapped.is_ok() { requested } else { current });
629        }
630        let length = new_end - old_end;
631        let mapped = guest
632            .inject_with_retry(syscalls::Syscall::Mmap(
633                syscalls::Mmap::new()
634                    .with_addr(syscalls::Addr::from_raw(old_end as usize))
635                    .with_len(length as usize)
636                    .with_prot(syscalls::ProtFlags::PROT_READ | syscalls::ProtFlags::PROT_WRITE)
637                    .with_flags(
638                        syscalls::MapFlags::MAP_PRIVATE
639                            | syscalls::MapFlags::MAP_ANONYMOUS
640                            | syscalls::MapFlags::MAP_FIXED_NOREPLACE,
641                    )
642                    .with_fd(-1)
643                    .with_offset(0),
644            ))
645            .await;
646        match &mapped {
647            Ok(address) if *address as u64 == old_end => Ok(requested),
648            // A kernel without MAP_FIXED_NOREPLACE treats the address as a
649            // hint and may map elsewhere: give that mapping back, or refuse
650            // where it cannot be.
651            Ok(address) => {
652                let removed = guest
653                    .inject_with_retry(syscalls::Syscall::Munmap(
654                        syscalls::Munmap::new()
655                            .with_addr(syscalls::Addr::from_raw(*address as usize))
656                            .with_len(length as usize),
657                    ))
658                    .await;
659                diagnostic.rejected(
660                    "mmap_wrong_address",
661                    Some(limit),
662                    smaps.as_deref(),
663                    &(&mapped, &removed),
664                );
665                if removed.is_ok() {
666                    Ok(current)
667                } else {
668                    Err(
669                        "unsupported: brk growth the kernel mapped elsewhere and Detcore could not remove",
670                    )
671                }
672            }
673            Err(_) => {
674                diagnostic.rejected("mmap_error", Some(limit), smaps.as_deref(), &mapped);
675                Ok(current)
676            }
677        }
678    }
679}
680
681#[cfg(test)]
682mod tests {
683    use super::*;
684
685    #[test]
686    fn a_shared_brk_check_uses_the_existing_mapping_policy() {
687        let smaps = b"00500000-00501000 rw-p 00000000 00:00 0\nVmFlags: rd wr\n";
688        let grow = |new_end| BrkMappingCheck::Grow {
689            old_end: 0x400000,
690            new_end,
691        };
692        assert!(grow(0x4ff000).allows(smaps));
693        assert!(!grow(0x500000).allows(smaps));
694        assert!(!grow(u64::MAX - BRK_PAGE + 1).allows(smaps));
695        assert!(
696            BrkMappingCheck::Shrink {
697                new_end: 0x500000,
698                old_end: 0x501000,
699            }
700            .allows(smaps)
701        );
702        assert!(
703            !BrkMappingCheck::Shrink {
704                new_end: 0x400000,
705                old_end: 0x500000,
706            }
707            .allows(smaps)
708        );
709        assert!(grow(0x500000).allows(b""));
710        assert!(
711            !BrkMappingCheck::Shrink {
712                new_end: 0x500000,
713                old_end: 0x501000,
714            }
715            .allows(b"")
716        );
717    }
718
719    #[test]
720    fn a_break_grows_and_shrinks_only_where_linuxs_brk_would() {
721        let smaps = "\
72200400000-00401000 r-xp 00000000 00:20 1234 /image
723Size:                  4 kB
724VmFlags: rd ex mr mw me
72500406000-00407000 r--p 00000000 00:00 0
726VmFlags: rd mr mw me
7277ffffff00000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
728VmFlags: rd wr mr mw me gd ac
729";
730        let vmas: Vec<Vma> = smaps_vmas(smaps.as_bytes()).collect();
731        assert_eq!(vmas.len(), 3);
732        // A pathname that is not UTF-8 does not hide the mappings.
733        let mut named =
734            b"00500000-00501000 r--p 00000000 00:20 42 /n\xffme\nVmFlags: rd\n".to_vec();
735        named.extend_from_slice(smaps.as_bytes());
736        let named: Vec<Vma> = smaps_vmas(&named).collect();
737        assert_eq!(named.len(), 4);
738        assert_eq!(named[1..], vmas[..]);
739        assert_eq!(
740            vmas[2],
741            Vma {
742                start: 0x7ffffff00000,
743                end: 0x7ffffffff000,
744                start_gap: STACK_GUARD_GAP
745            }
746        );
747        // A mapping at 0x406000: growth to 0x405000 needs 0x405000-0x406000
748        // free above it, which it is; growth to 0x406000 meets the mapping.
749        assert!(brk_may_grow(vmas.iter().copied(), 0x401000, 0x405000));
750        assert!(!brk_may_grow(vmas.iter().copied(), 0x401000, 0x406000));
751        // Below a stack, the guard gap is kept as well.
752        let below_stack = 0x7ffffff00000 - STACK_GUARD_GAP;
753        assert!(brk_may_grow(
754            vmas.iter().copied(),
755            0x500000,
756            below_stack - BRK_PAGE
757        ));
758        assert!(!brk_may_grow(vmas.iter().copied(), 0x500000, below_stack));
759        // Below a shadow stack, one page.
760        let shadow: Vec<Vma> =
761            smaps_vmas(b"00410000-00411000 rw-p 00000000 00:00 0\nVmFlags: rd wr mr mw me ss\n")
762                .collect();
763        assert_eq!(shadow[0].start_gap, BRK_PAGE);
764        assert!(brk_may_grow(shadow.iter().copied(), 0x401000, 0x40e000));
765        assert!(!brk_may_grow(shadow.iter().copied(), 0x401000, 0x40f000));
766        // Shrinking needs a mapping to unmap in the range.
767        assert!(brk_may_shrink(vmas.iter().copied(), 0x405000, 0x407000));
768        assert!(!brk_may_shrink(vmas.iter().copied(), 0x402000, 0x406000));
769    }
770
771    #[test]
772    fn madvise_known_linux_advice_has_an_explicit_policy() {
773        for advice in [
774            libc::MADV_NORMAL,
775            libc::MADV_RANDOM,
776            libc::MADV_SEQUENTIAL,
777            libc::MADV_WILLNEED,
778        ] {
779            assert_eq!(madvise_action(advice), MadviseAction::ForwardHint);
780        }
781
782        for advice in [
783            libc::MADV_DONTNEED,
784            libc::MADV_DONTFORK,
785            libc::MADV_DOFORK,
786            libc::MADV_DONTDUMP,
787            libc::MADV_DODUMP,
788            libc::MADV_WIPEONFORK,
789            libc::MADV_KEEPONFORK,
790            libc::MADV_DONTNEED_LOCKED,
791            MADV_GUARD_INSTALL,
792            MADV_GUARD_REMOVE,
793        ] {
794            assert_eq!(madvise_action(advice), MadviseAction::ForwardSemantic);
795        }
796
797        for advice in [
798            libc::MADV_FREE,
799            libc::MADV_MERGEABLE,
800            libc::MADV_UNMERGEABLE,
801            libc::MADV_HUGEPAGE,
802            libc::MADV_NOHUGEPAGE,
803            libc::MADV_COLD,
804            libc::MADV_PAGEOUT,
805        ] {
806            assert_eq!(madvise_action(advice), MadviseAction::Ignore);
807        }
808
809        for advice in [
810            libc::MADV_REMOVE,
811            libc::MADV_POPULATE_READ,
812            libc::MADV_POPULATE_WRITE,
813            libc::MADV_COLLAPSE,
814        ] {
815            assert_eq!(madvise_action(advice), MadviseAction::Reject(Errno::EINVAL));
816        }
817        for advice in [libc::MADV_HWPOISON, libc::MADV_SOFT_OFFLINE] {
818            assert_eq!(madvise_action(advice), MadviseAction::Reject(Errno::EPERM));
819        }
820        assert_eq!(madvise_action(i32::MAX), MadviseAction::Unknown);
821    }
822
823    #[test]
824    fn common_argument_validation_is_host_independent() {
825        let aligned = unsafe { AddrMut::<libc::c_void>::from_raw_unchecked(0x1000) };
826        assert!(
827            validate_common_args(
828                syscalls::Madvise::new()
829                    .with_addr(Some(aligned))
830                    .with_len(0)
831                    .with_advice(libc::MADV_FREE),
832            )
833            .is_ok()
834        );
835
836        let unaligned = unsafe { AddrMut::<libc::c_void>::from_raw_unchecked(0x1001) };
837        assert!(
838            validate_common_args(
839                syscalls::Madvise::new()
840                    .with_addr(Some(unaligned))
841                    .with_len(0)
842                    .with_advice(libc::MADV_FREE),
843            )
844            .is_err()
845        );
846
847        let near_end =
848            unsafe { AddrMut::<libc::c_void>::from_raw_unchecked(usize::MAX & !(PAGE_SIZE - 1)) };
849        assert!(
850            validate_common_args(
851                syscalls::Madvise::new()
852                    .with_addr(Some(near_end))
853                    .with_len(PAGE_SIZE)
854                    .with_advice(libc::MADV_FREE),
855            )
856            .is_err()
857        );
858    }
859}