summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libc/stdlib/malloc.3124
1 files changed, 118 insertions, 6 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index c0472ee2b9..49a5b993f4 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,7 +30,7 @@
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE. 31.\" SUCH DAMAGE.
32.\" 32.\"
33.\" $OpenBSD: malloc.3,v 1.120 2018/11/21 06:57:04 otto Exp $ 33.\" $OpenBSD: malloc.3,v 1.121 2018/11/21 09:22:58 jmc Exp $
34.\" 34.\"
35.Dd $Mdocdate: November 21 2018 $ 35.Dd $Mdocdate: November 21 2018 $
36.Dt MALLOC 3 36.Dt MALLOC 3
@@ -234,6 +234,115 @@ If
234is not a multiple of 234is not a multiple of
235.Fa alignment , 235.Fa alignment ,
236behavior is undefined. 236behavior is undefined.
237.Sh MALLOC OPTIONS
238Upon the first call to the
239.Fn malloc
240family of functions, an initialization sequence inspects the
241value of the
242.Va vm.malloc_conf
243.Xr sysctl 2 ,
244next checks the environment for a variable called
245.Ev MALLOC_OPTIONS ,
246and finally looks at the global variable
247.Va malloc_options
248in the program.
249Each is scanned for the flags documented below.
250Unless otherwise noted uppercase means on, lowercase means off.
251.Bl -tag -width indent
252.It Cm C
253.Dq Canaries .
254Add canaries at the end of allocations in order to detect
255heap overflows.
256The canary's content is checked when
257.Nm free
258is called.
259If it has been corrupted, the process is aborted.
260.It Cm D
261.Dq Dump .
262.Fn malloc
263will dump statistics to the file
264.Pa ./malloc.out ,
265if it already exists,
266at exit.
267This option requires the library to have been compiled with -DMALLOC_STATS in
268order to have any effect.
269.It Cm F
270.Dq Freecheck .
271Enable more extensive double free and use after free detection.
272All chunks in the delayed free list will be checked for double frees.
273Unused pages on the freelist are read and write protected to
274cause a segmentation fault upon access.
275.It Cm G
276.Dq Guard .
277Enable guard pages.
278Each page size or larger allocation is followed by a guard page that will
279cause a segmentation fault upon any access.
280.It Cm J
281.Dq More junking .
282Increase the junk level by one if it is smaller than 2.
283.It Cm j
284.Dq Less junking .
285Decrease the junk level by one if it is larger than 0.
286Junking writes some junk bytes into the area allocated.
287Junk is bytes of 0xdb when allocating;
288freed chunks are filled with 0xdf.
289By default the junk level is 1: after free,
290small chunks are completely junked;
291for pages the first part is junked.
292After a delay,
293the filling pattern is validated and the process is aborted if the pattern
294was modified.
295For junk level 2, junking is done on allocation as well and without size
296restrictions.
297If the junk level is zero, no junking is performed.
298.It Cm R
299.Dq realloc .
300Always reallocate when
301.Fn realloc
302is called, even if the initial allocation was big enough.
303.\".Pp
304.\".It Cm U
305.\".Dq utrace .
306.\"Generate entries for
307.\".Xr ktrace 1
308.\"for all operations.
309.\"Consult the source for this one.
310.It Cm S
311Enable all options suitable for security auditing.
312.It Cm U
313.Dq Free unmap .
314Enable use after free protection for larger allocations.
315Unused pages on the freelist are read and write protected to
316cause a segmentation fault upon access.
317.It Cm X
318.Dq xmalloc .
319Rather than return failure,
320.Xr abort 3
321the program with a diagnostic message on stderr.
322It is the intention that this option be set at compile time by
323including in the source:
324.Bd -literal -offset indent
325extern char *malloc_options;
326malloc_options = "X";
327.Ed
328.Pp
329Note that this will cause code that is supposed to handle
330out-of-memory conditions gracefully to abort instead.
331.It Cm <
332.Dq Halve the cache size .
333Decrease the size of the free page cache by a factor of two.
334.It Cm >
335.Dq Double the cache size .
336Increase the size of the free page cache by a factor of two.
337.El
338.Pp
339If a program changes behavior if any of these options (except
340.Cm X )
341are used,
342it is buggy.
343.Pp
344The default number of free pages cached is 64 per malloc pool.
345Multi-threaded programs use multiple pools.
237.Sh RETURN VALUES 346.Sh RETURN VALUES
238Upon successful completion, the allocation functions 347Upon successful completion, the allocation functions
239return a pointer to the allocated space; otherwise, 348return a pointer to the allocated space; otherwise,
@@ -404,8 +513,7 @@ Deallocation of such an object should be done by calling
404.Sh ENVIRONMENT 513.Sh ENVIRONMENT
405.Bl -tag -width "MALLOC_OPTIONS" 514.Bl -tag -width "MALLOC_OPTIONS"
406.It Ev MALLOC_OPTIONS 515.It Ev MALLOC_OPTIONS
407String of flags documented in 516String of option flags.
408.Xr malloc.conf 5 .
409.El 517.El
410.Sh EXAMPLES 518.Sh EXAMPLES
411If 519If
@@ -460,6 +568,11 @@ or at the cost of initialization:
460if ((p = calloc(num, size)) == NULL) 568if ((p = calloc(num, size)) == NULL)
461 err(1, NULL); 569 err(1, NULL);
462.Ed 570.Ed
571.Pp
572Set a systemwide reduction of the cache to a quarter of the
573default size and use guard pages:
574.Pp
575.Dl # sysctl vm.malloc_conf='G<<'
463.Sh DIAGNOSTICS 576.Sh DIAGNOSTICS
464If any of the functions detect an error condition, 577If any of the functions detect an error condition,
465a message will be printed to file descriptor 578a message will be printed to file descriptor
@@ -525,11 +638,10 @@ consult sources and/or wizards.
525.Xr brk 2 , 638.Xr brk 2 ,
526.Xr mmap 2 , 639.Xr mmap 2 ,
527.Xr munmap 2 , 640.Xr munmap 2 ,
641.Xr sysctl 2 ,
528.Xr alloca 3 , 642.Xr alloca 3 ,
529.Xr getpagesize 3 , 643.Xr getpagesize 3 ,
530.Xr posix_memalign 3 , 644.Xr posix_memalign 3
531.Xr sysconf 3 ,
532.Xr malloc.conf 5
533.Sh STANDARDS 645.Sh STANDARDS
534The 646The
535.Fn malloc , 647.Fn malloc ,