xref: /freebsd/share/man/man7/memory_model.7 (revision 2f7a8222b6a5c7bedf6835eb6a93b35346e50777)
11366a87cSBrooks Davis.\"-
21366a87cSBrooks Davis.\" Copyright (c) 2026 Capabilities Limited
31366a87cSBrooks Davis.\"
41366a87cSBrooks Davis.\" SPDX-License-Identifier: BSD-2-Clause
51366a87cSBrooks Davis.\"
61366a87cSBrooks Davis.\" This software was developed by Capabilities Limited with funding from
71366a87cSBrooks Davis.\" Innovate UK and the Department for Science, Innovation and Technology
81366a87cSBrooks Davis.\" for the adoption and diffusion of CHERI technology under project
91366a87cSBrooks Davis.\" 10168042 (“CheriBSD feature extraction, maturity, and testing”).
101366a87cSBrooks Davis.\"
111366a87cSBrooks Davis.Dd June 26, 2026
121366a87cSBrooks Davis.Dt MEMORY_MODEL 7
131366a87cSBrooks Davis.Os
141366a87cSBrooks Davis.Sh NAME
151366a87cSBrooks Davis.Nm memory model
161366a87cSBrooks Davis.Nd Overview of the memory model
171366a87cSBrooks Davis.Sh DESCRIPTION
181366a87cSBrooks DavisDescription of the memory model implemented by
191366a87cSBrooks Davis.Fx .
201366a87cSBrooks Davis.Ss Introduction
211366a87cSBrooks DavisThis document covers various aspects of the memory model implemented by
221366a87cSBrooks Davis.Fx Ns 's
231366a87cSBrooks Davissupported architectures, compilers, and language runtimes.
241366a87cSBrooks DavisSome aspects are currently documented elsewhere, particularly in
251366a87cSBrooks Davis.Xr atomic 9 .
261366a87cSBrooks Davis.Ss Pointer Provenance
271366a87cSBrooks DavisOn the surface, pointers are integer addresses within a (usually
281366a87cSBrooks Davisvirtual) address space.
291366a87cSBrooks DavisIn systems programming languages, pointers also have provenance which
301366a87cSBrooks Davisindicates where and when the pointer can access memory.
311366a87cSBrooks DavisCompilers use provenance information to perform alias analysis to
321366a87cSBrooks Davisjustify optimizations.
331366a87cSBrooks Davis.Pp
341366a87cSBrooks DavisOn CHERI targets, the bounds, permissions, and validity tag of
351366a87cSBrooks Daviscapabilities make some aspects of provenance concrete.
361366a87cSBrooks DavisCHERI capabilities may only be derived from other capabilities and are
371366a87cSBrooks Davissubject to monotonicity guarantees.
381366a87cSBrooks DavisSpecifically, no manipulation of a CHERI capability can produce a
391366a87cSBrooks Daviscapability with more permissions than the original.
401366a87cSBrooks Davis.Pp
411366a87cSBrooks DavisDevelopers must take care to ensure that pointer provenance is not lost
421366a87cSBrooks Davisunless intended.
431366a87cSBrooks DavisSpecifically:
441366a87cSBrooks Davis.Bl -dash
451366a87cSBrooks Davis.It
461366a87cSBrooks DavisWhen copying or manipulating pointers, use pointer types
471366a87cSBrooks Davis.Pq e.g., Vt char * ,
481366a87cSBrooks Davis.Vt intptr_t ,
491366a87cSBrooks Davisor
501366a87cSBrooks Davis.Vt uintptr_t
511366a87cSBrooks Davisto preserve provenance.
521366a87cSBrooks DavisOther integer types do not preserve provenance.
531366a87cSBrooks Davis.It
541366a87cSBrooks DavisEnsure that expressions using
551366a87cSBrooks Davis.Vt intptr_t
561366a87cSBrooks Davisor
571366a87cSBrooks Davis.Vt uintptr_t
581366a87cSBrooks Davishave a single, clear source of provenance.
591366a87cSBrooks DavisE.g., when adding two variables of type
601366a87cSBrooks Davis.Vt intptr_t
611366a87cSBrooks Daviscast the one that is an offset to
621366a87cSBrooks Davis.Vt size_t .
631366a87cSBrooks Davis.It
641366a87cSBrooks DavisEnsure that pointers are stored at their natural alignment.
651366a87cSBrooks DavisThis is required by CHERI, and accessing an object through an improperly
661366a87cSBrooks Davisaligned pointer is undefined behavior in C.
671366a87cSBrooks Davis.It
681366a87cSBrooks DavisCast pointers to a provenance-free type such as
691366a87cSBrooks Davis.Vt ptraddr_t
701366a87cSBrooks Daviswhen the address of a pointer is desired without provenance.
711366a87cSBrooks Davis.It
721366a87cSBrooks DavisAvoid manipulating pointer addresses such that they fall outside of the
731366a87cSBrooks Davisunderlying allocation except one past the end as permitted by ISO C.
741366a87cSBrooks DavisTaking pointers further out of bounds
751366a87cSBrooks Davis.Pq even temporarily
761366a87cSBrooks Davisis undefined behavior in the C standard.
771366a87cSBrooks DavisIn practice CHERI capabilities may be taken some distance out of bounds,
781366a87cSBrooks Davisbut if taken too far out of bounds, the validity tag will be stripped.
791366a87cSBrooks Davis.El
801366a87cSBrooks Davis.Pp
811366a87cSBrooks DavisDevelopers must also take care not to leak valid pointers across address
821366a87cSBrooks Davisspace boundaries.
831366a87cSBrooks DavisSpecifically:
841366a87cSBrooks Davis.Bl -dash
851366a87cSBrooks Davis.It
86*2f7a8222SEd MasteCopy objects containing pointers with provenance-preserving APIs such
871366a87cSBrooks Davisas:
881366a87cSBrooks Davis.Xr copyinptr 9 ,
891366a87cSBrooks Davis.Xr copyoutptr 9 ,
901366a87cSBrooks Davis.Xr memcpy 3 ,
911366a87cSBrooks Davisand
921366a87cSBrooks Davis.Xr memmove 3 .
931366a87cSBrooks Davis.It
941366a87cSBrooks DavisCopy objects that should not contain pointers using
951366a87cSBrooks Davisnon-provenance-preserving APIs such as:
961366a87cSBrooks Davis.Xr copyin 9 ,
971366a87cSBrooks Davis.Xr copyout 9 ,
981366a87cSBrooks Davis.Xr memcpy_data 9 ,
991366a87cSBrooks Davisand
1001366a87cSBrooks Davis.Xr memmove_data 9 .
1011366a87cSBrooks Davis.El
1021366a87cSBrooks Davis.Pp
1031366a87cSBrooks DavisFor practical advice on adapting to CHERI C/C++'s notion of provenance, see the
1041366a87cSBrooks Davis.Lk https://ctsrd-cheri.github.io/cheri-c-programming/ CHERI C/C++ Programming Guide .
1051366a87cSBrooks DavisA provenance-aware memory object model of C is documented in
1061366a87cSBrooks DavisISO/IEC TS 6010:2025:
1071366a87cSBrooks Davis.Dq Programming languages – A Provenance-aware memory object model for C
1081366a87cSBrooks Daviswhich can be read in draft form as WG14 paper
1091366a87cSBrooks Davis.Lk https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3057.pdf N3057 .
1101366a87cSBrooks DavisFurther background on provenance in systems programming languages can
1111366a87cSBrooks Davisbe found in the Rust RFC
1121366a87cSBrooks Davis.Lk https://rust-lang.github.io/rfcs/3559-rust-has-provenance.html 3559-rust-has-provenance .
1131366a87cSBrooks Davis.Sh SEE ALSO
1141366a87cSBrooks Davis.Xr arch 7 ,
1151366a87cSBrooks Davis.Xr atomic 9
1161366a87cSBrooks Davis.Sh AUTHORS
1171366a87cSBrooks DavisThis software and this manual page were developed by Capabilities
1181366a87cSBrooks DavisLimited with funding from Innovate UK and the Department for Science,
1191366a87cSBrooks DavisInnovation and Technology for the adoption and diffusion of CHERI
1201366a87cSBrooks Davistechnology under project 10168042
1211366a87cSBrooks Davis.Pq Do CheriBSD feature extraction, maturity, and testing Dc .
122