2005-02-22 17:18:27 +00:00
|
|
|
.\" Copyright (c) 2005 Christian Brueffer
|
|
|
|
.\" All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
2017-03-23 06:08:01 +00:00
|
|
|
.Dd March 22, 2017
|
2005-02-22 17:18:27 +00:00
|
|
|
.Dt MEMGUARD 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm MemGuard
|
|
|
|
.Nd "memory allocator for debugging purposes"
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Cd "options DEBUG_MEMGUARD"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Nm
|
|
|
|
is a simple and small replacement memory allocator designed
|
|
|
|
to help detect tamper-after-free scenarios.
|
|
|
|
These problems are more and more common and likely with
|
|
|
|
multithreaded kernels where race conditions are more prevalent.
|
|
|
|
.Pp
|
|
|
|
.Nm
|
2010-08-11 22:10:37 +00:00
|
|
|
can take over
|
2005-02-22 17:18:27 +00:00
|
|
|
.Fn malloc ,
|
|
|
|
.Fn realloc
|
|
|
|
and
|
|
|
|
.Fn free
|
2010-08-11 22:10:37 +00:00
|
|
|
for a single malloc type.
|
2011-10-12 18:46:48 +00:00
|
|
|
Alternatively
|
2010-08-11 22:10:37 +00:00
|
|
|
.Nm
|
2011-10-12 18:46:48 +00:00
|
|
|
can take over
|
|
|
|
.Fn uma_zalloc ,
|
|
|
|
.Fn uma_zalloc_arg
|
|
|
|
and
|
|
|
|
.Fn uma_free
|
|
|
|
for a single
|
2017-03-23 06:08:01 +00:00
|
|
|
.Xr uma 9
|
2011-10-12 18:46:48 +00:00
|
|
|
zone.
|
|
|
|
Also
|
|
|
|
.Nm
|
|
|
|
can guard all allocations larger than
|
2010-08-11 22:10:37 +00:00
|
|
|
.Dv PAGE_SIZE ,
|
|
|
|
and can guard a random fraction of all allocations.
|
|
|
|
There is also a knob to prevent allocations smaller than a specified
|
|
|
|
size from being guarded, to limit memory waste.
|
2005-02-22 17:18:27 +00:00
|
|
|
.Sh EXAMPLES
|
2006-09-18 15:24:20 +00:00
|
|
|
To use
|
|
|
|
.Nm
|
2010-08-11 22:10:37 +00:00
|
|
|
for a memory type, either add an entry to
|
2005-12-30 12:28:19 +00:00
|
|
|
.Pa /boot/loader.conf :
|
|
|
|
.Bd -literal -offset indent
|
|
|
|
vm.memguard.desc=<memory_type>
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2010-08-11 22:10:37 +00:00
|
|
|
Or set the
|
2005-12-30 12:28:19 +00:00
|
|
|
.Va vm.memguard.desc
|
|
|
|
.Xr sysctl 8
|
2010-08-11 22:10:37 +00:00
|
|
|
variable at run-time:
|
2005-12-30 12:28:19 +00:00
|
|
|
.Bd -literal -offset indent
|
|
|
|
sysctl vm.memguard.desc=<memory_type>
|
|
|
|
.Ed
|
|
|
|
.Pp
|
2010-08-11 22:10:37 +00:00
|
|
|
Where
|
|
|
|
.Ar memory_type
|
2011-10-12 18:46:48 +00:00
|
|
|
can be either a short description of the memory type to monitor,
|
|
|
|
either name of
|
|
|
|
.Xr uma 9
|
|
|
|
zone.
|
2010-08-11 22:10:37 +00:00
|
|
|
Only allocations from that
|
|
|
|
.Ar memory_type
|
|
|
|
made after
|
|
|
|
.Va vm.memguard.desc
|
|
|
|
is set will potentially be guarded.
|
|
|
|
If
|
|
|
|
.Va vm.memguard.desc
|
|
|
|
is modified at run-time then only allocations of the new
|
|
|
|
.Ar memory_type
|
|
|
|
will potentially be guarded once the
|
|
|
|
.Xr sysctl 8
|
|
|
|
is set.
|
|
|
|
Existing guarded allocations will still be properly released by
|
2011-10-12 18:46:48 +00:00
|
|
|
either
|
|
|
|
.Xr free 9
|
|
|
|
or
|
|
|
|
.Xr uma_zfree 9 ,
|
|
|
|
depending on what kind of allocation was taken over.
|
2010-08-11 22:10:37 +00:00
|
|
|
.Pp
|
2011-10-12 18:46:48 +00:00
|
|
|
To determine short description of a
|
2010-08-11 22:10:37 +00:00
|
|
|
.Xr malloc 9
|
2011-10-12 18:46:48 +00:00
|
|
|
type one can either take it from the first column of
|
|
|
|
.Xr vmstat 8 Fl m
|
|
|
|
output, or to find it in the kernel source.
|
|
|
|
It is the second argument to
|
|
|
|
.Xr MALLOC_DEFINE 9
|
|
|
|
macro.
|
|
|
|
To determine name of
|
|
|
|
.Xr uma 9
|
|
|
|
zone one can either take it from the first column of
|
|
|
|
.Xr vmstat 8 Fl z
|
|
|
|
output, or to find it in the kernel source.
|
|
|
|
It is the first argument to the
|
|
|
|
.Xr uma_zcreate 9
|
|
|
|
function.
|
2010-08-11 22:10:37 +00:00
|
|
|
.Pp
|
2005-12-30 12:28:19 +00:00
|
|
|
The
|
|
|
|
.Va vm.memguard.divisor
|
2010-08-11 22:10:37 +00:00
|
|
|
boot-time tunable is used to scale how much of the system's physical
|
|
|
|
memory
|
|
|
|
.Nm
|
|
|
|
is allowed to consume.
|
|
|
|
The default is 10, so up to
|
2014-03-22 10:26:09 +00:00
|
|
|
.Va vm_cnt.v_page_count Ns /10
|
2010-08-11 22:10:37 +00:00
|
|
|
pages can be used.
|
|
|
|
.Nm
|
|
|
|
will reserve
|
|
|
|
.Va vm_kmem_max
|
|
|
|
/
|
|
|
|
.Va vm.memguard.divisor
|
|
|
|
bytes of virtual address space, limited by twice the physical memory
|
|
|
|
size.
|
|
|
|
The physical limit is reported as
|
|
|
|
.Va vm.memguard.phys_limit
|
|
|
|
and the virtual space reserved for
|
|
|
|
.Nm
|
|
|
|
is reported as
|
|
|
|
.Va vm.memguard.mapsize .
|
|
|
|
.Pp
|
|
|
|
.Nm
|
|
|
|
will not do page promotions for any allocation smaller than
|
|
|
|
.Va vm.memguard.minsize
|
|
|
|
bytes.
|
|
|
|
The default is 0, meaning all allocations can potentially be guarded.
|
|
|
|
.Nm
|
|
|
|
can guard sufficiently large allocations randomly, with average
|
|
|
|
frequency of every one in 100000 /
|
|
|
|
.Va vm.memguard.frequency
|
|
|
|
allocations.
|
|
|
|
The default is 0, meaning no allocations are randomly guarded.
|
|
|
|
.Pp
|
|
|
|
.Nm
|
|
|
|
can optionally add unmapped guard pages around each allocation to
|
|
|
|
detect overflow and underflow, if
|
|
|
|
.Va vm.memguard.options
|
|
|
|
has the 1 bit set.
|
|
|
|
This option is enabled by default.
|
|
|
|
.Nm
|
|
|
|
will optionally guard all allocations of
|
|
|
|
.Dv PAGE_SIZE
|
|
|
|
or larger if
|
|
|
|
.Va vm.memguard.options
|
|
|
|
has the 2 bit set.
|
|
|
|
This option is off by default.
|
2011-10-12 18:46:48 +00:00
|
|
|
By default
|
|
|
|
.Nm
|
2017-03-23 06:08:01 +00:00
|
|
|
does not guard
|
2011-10-12 18:46:48 +00:00
|
|
|
.Xr uma 9
|
|
|
|
zones that have been initialized with the
|
|
|
|
.Dv UMA_ZONE_NOFREE
|
|
|
|
flag set, since it can produce false positives on them.
|
|
|
|
However, this safety measure can be turned off by setting bit 3
|
|
|
|
of the
|
|
|
|
.Va vm.memguard.options
|
|
|
|
tunable.
|
2005-02-22 17:18:27 +00:00
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr sysctl 8 ,
|
|
|
|
.Xr vmstat 8 ,
|
|
|
|
.Xr contigmalloc 9 ,
|
2006-01-31 17:29:27 +00:00
|
|
|
.Xr malloc 9 ,
|
2011-10-12 18:46:48 +00:00
|
|
|
.Xr redzone 9 ,
|
|
|
|
.Xr uma 9
|
2005-02-22 17:18:27 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
.Nm
|
|
|
|
first appeared in
|
|
|
|
.Fx 6.0 .
|
|
|
|
.Sh AUTHORS
|
|
|
|
.An -nosplit
|
|
|
|
.Nm
|
2010-08-11 22:10:37 +00:00
|
|
|
was originally written by
|
2014-06-26 21:44:30 +00:00
|
|
|
.An Bosko Milekic Aq Mt bmilekic@FreeBSD.org .
|
2010-08-11 22:10:37 +00:00
|
|
|
This manual page was originally written by
|
2014-06-26 21:44:30 +00:00
|
|
|
.An Christian Brueffer Aq Mt brueffer@FreeBSD.org .
|
2010-08-11 22:10:37 +00:00
|
|
|
Additions have been made by
|
2014-06-26 21:44:30 +00:00
|
|
|
.An Matthew Fleming Aq Mt mdf@FreeBSD.org
|
2011-10-12 18:46:48 +00:00
|
|
|
and
|
2014-06-26 21:44:30 +00:00
|
|
|
.An Gleb Smirnoff Aq Mt glebius@FreeBSD.org
|
2010-08-11 22:10:37 +00:00
|
|
|
to both the implementation and the documentation.
|