diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 803 |
1 files changed, 0 insertions, 803 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 deleted file mode 100644 index dc3e691ece..0000000000 --- a/src/lib/libc/stdlib/malloc.3 +++ /dev/null | |||
| @@ -1,803 +0,0 @@ | |||
| 1 | .\" | ||
| 2 | .\" Copyright (c) 1980, 1991, 1993 | ||
| 3 | .\" The Regents of the University of California. All rights reserved. | ||
| 4 | .\" | ||
| 5 | .\" This code is derived from software contributed to Berkeley by | ||
| 6 | .\" the American National Standards Committee X3, on Information | ||
| 7 | .\" Processing Systems. | ||
| 8 | .\" | ||
| 9 | .\" Redistribution and use in source and binary forms, with or without | ||
| 10 | .\" modification, are permitted provided that the following conditions | ||
| 11 | .\" are met: | ||
| 12 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer. | ||
| 14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 16 | .\" documentation and/or other materials provided with the distribution. | ||
| 17 | .\" 3. Neither the name of the University nor the names of its contributors | ||
| 18 | .\" may be used to endorse or promote products derived from this software | ||
| 19 | .\" without specific prior written permission. | ||
| 20 | .\" | ||
| 21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | .\" SUCH DAMAGE. | ||
| 32 | .\" | ||
| 33 | .\" $OpenBSD: malloc.3,v 1.128 2021/04/09 06:04:15 otto Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd $Mdocdate: April 9 2021 $ | ||
| 36 | .Dt MALLOC 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm malloc , | ||
| 40 | .Nm calloc , | ||
| 41 | .Nm realloc , | ||
| 42 | .Nm free , | ||
| 43 | .Nm reallocarray , | ||
| 44 | .Nm recallocarray , | ||
| 45 | .Nm freezero , | ||
| 46 | .Nm aligned_alloc , | ||
| 47 | .Nm malloc_conceal , | ||
| 48 | .Nm calloc_conceal | ||
| 49 | .Nd memory allocation and deallocation | ||
| 50 | .Sh SYNOPSIS | ||
| 51 | .In stdlib.h | ||
| 52 | .Ft void * | ||
| 53 | .Fn malloc "size_t size" | ||
| 54 | .Ft void * | ||
| 55 | .Fn calloc "size_t nmemb" "size_t size" | ||
| 56 | .Ft void * | ||
| 57 | .Fn realloc "void *ptr" "size_t size" | ||
| 58 | .Ft void | ||
| 59 | .Fn free "void *ptr" | ||
| 60 | .Ft void * | ||
| 61 | .Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" | ||
| 62 | .Ft void * | ||
| 63 | .Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size" | ||
| 64 | .Ft void | ||
| 65 | .Fn freezero "void *ptr" "size_t size" | ||
| 66 | .Ft void * | ||
| 67 | .Fn aligned_alloc "size_t alignment" "size_t size" | ||
| 68 | .Ft void * | ||
| 69 | .Fn malloc_conceal "size_t size" | ||
| 70 | .Ft void * | ||
| 71 | .Fn calloc_conceal "size_t nmemb" "size_t size" | ||
| 72 | .Vt char *malloc_options ; | ||
| 73 | .Sh DESCRIPTION | ||
| 74 | The standard functions | ||
| 75 | .Fn malloc , | ||
| 76 | .Fn calloc , | ||
| 77 | and | ||
| 78 | .Fn realloc | ||
| 79 | allocate | ||
| 80 | .Em objects , | ||
| 81 | regions of memory to store values. | ||
| 82 | The | ||
| 83 | .Fn malloc | ||
| 84 | function allocates uninitialized space for an object of | ||
| 85 | the specified | ||
| 86 | .Fa size . | ||
| 87 | .Fn malloc | ||
| 88 | maintains multiple lists of free objects according to size, allocating | ||
| 89 | from the appropriate list or requesting memory from the kernel. | ||
| 90 | The allocated space is suitably aligned (after possible pointer coercion) for | ||
| 91 | storage of any type of object. | ||
| 92 | .Pp | ||
| 93 | The | ||
| 94 | .Fn calloc | ||
| 95 | function allocates space for an array of | ||
| 96 | .Fa nmemb | ||
| 97 | objects, each of the specified | ||
| 98 | .Fa size . | ||
| 99 | The space is initialized to zero. | ||
| 100 | .Pp | ||
| 101 | The | ||
| 102 | .Fn realloc | ||
| 103 | function changes the size of the object pointed to by | ||
| 104 | .Fa ptr | ||
| 105 | to | ||
| 106 | .Fa size | ||
| 107 | bytes and returns a pointer to the (possibly moved) object. | ||
| 108 | If | ||
| 109 | .Fa ptr | ||
| 110 | is not | ||
| 111 | .Dv NULL , | ||
| 112 | it must be a pointer returned by an earlier call to an allocation or | ||
| 113 | reallocation function that was not freed in between. | ||
| 114 | The contents of the object are unchanged up to the lesser | ||
| 115 | of the new and old sizes. | ||
| 116 | If the new size is larger, the value of the newly allocated portion | ||
| 117 | of the object is indeterminate and uninitialized. | ||
| 118 | If the space cannot be allocated, the object | ||
| 119 | pointed to by | ||
| 120 | .Fa ptr | ||
| 121 | is unchanged. | ||
| 122 | If | ||
| 123 | .Fa ptr | ||
| 124 | is | ||
| 125 | .Dv NULL , | ||
| 126 | .Fn realloc | ||
| 127 | behaves like | ||
| 128 | .Fn malloc | ||
| 129 | and allocates a new object. | ||
| 130 | .Pp | ||
| 131 | The | ||
| 132 | .Fn free | ||
| 133 | function causes the space pointed to by | ||
| 134 | .Fa ptr | ||
| 135 | to be either placed on a list of free blocks to make it available for future | ||
| 136 | allocation or, when appropriate, to be returned to the kernel using | ||
| 137 | .Xr munmap 2 . | ||
| 138 | If | ||
| 139 | .Fa ptr | ||
| 140 | is | ||
| 141 | .Dv NULL , | ||
| 142 | no action occurs. | ||
| 143 | If | ||
| 144 | .Fa ptr | ||
| 145 | was previously freed by | ||
| 146 | .Fn free | ||
| 147 | or a reallocation function, | ||
| 148 | the behavior is undefined and the double free is a security concern. | ||
| 149 | .Pp | ||
| 150 | Designed for safe allocation of arrays, | ||
| 151 | the | ||
| 152 | .Fn reallocarray | ||
| 153 | function is similar to | ||
| 154 | .Fn realloc | ||
| 155 | except it operates on | ||
| 156 | .Fa nmemb | ||
| 157 | members of size | ||
| 158 | .Fa size | ||
| 159 | and checks for integer overflow in the calculation | ||
| 160 | .Fa nmemb | ||
| 161 | * | ||
| 162 | .Fa size . | ||
| 163 | .Pp | ||
| 164 | Used for the allocation of memory holding sensitive data, | ||
| 165 | the | ||
| 166 | .Fn recallocarray | ||
| 167 | and | ||
| 168 | .Fn freezero | ||
| 169 | functions guarantee that memory becoming unallocated is explicitly | ||
| 170 | .Em discarded , | ||
| 171 | meaning pages of memory are disposed via | ||
| 172 | .Xr munmap 2 | ||
| 173 | and cached free objects are cleared with | ||
| 174 | .Xr explicit_bzero 3 . | ||
| 175 | .Pp | ||
| 176 | The | ||
| 177 | .Fn recallocarray | ||
| 178 | function is similar to | ||
| 179 | .Fn reallocarray | ||
| 180 | except it ensures newly allocated memory is cleared similar to | ||
| 181 | .Fn calloc . | ||
| 182 | If | ||
| 183 | .Fa ptr | ||
| 184 | is | ||
| 185 | .Dv NULL , | ||
| 186 | .Fa oldnmemb | ||
| 187 | is ignored and the call is equivalent to | ||
| 188 | .Fn calloc . | ||
| 189 | If | ||
| 190 | .Fa ptr | ||
| 191 | is not | ||
| 192 | .Dv NULL , | ||
| 193 | .Fa oldnmemb | ||
| 194 | must be a value such that | ||
| 195 | .Fa oldnmemb | ||
| 196 | * | ||
| 197 | .Fa size | ||
| 198 | is the size of the earlier allocation that returned | ||
| 199 | .Fa ptr , | ||
| 200 | otherwise the behavior is undefined. | ||
| 201 | .Pp | ||
| 202 | The | ||
| 203 | .Fn freezero | ||
| 204 | function is similar to the | ||
| 205 | .Fn free | ||
| 206 | function except it ensures memory is explicitly discarded. | ||
| 207 | If | ||
| 208 | .Fa ptr | ||
| 209 | is | ||
| 210 | .Dv NULL , | ||
| 211 | no action occurs. | ||
| 212 | If | ||
| 213 | .Fa ptr | ||
| 214 | is not | ||
| 215 | .Dv NULL , | ||
| 216 | the | ||
| 217 | .Fa size | ||
| 218 | argument must be equal to or smaller than the size of the earlier allocation | ||
| 219 | that returned | ||
| 220 | .Fa ptr . | ||
| 221 | .Fn freezero | ||
| 222 | guarantees the memory range starting at | ||
| 223 | .Fa ptr | ||
| 224 | with length | ||
| 225 | .Fa size | ||
| 226 | is discarded while deallocating the whole object originally allocated. | ||
| 227 | .Pp | ||
| 228 | The | ||
| 229 | .Fn aligned_alloc | ||
| 230 | function allocates | ||
| 231 | .Fa size | ||
| 232 | bytes of memory such that the allocation's base address is a multiple of | ||
| 233 | .Fa alignment . | ||
| 234 | The requested | ||
| 235 | .Fa alignment | ||
| 236 | must be a power of 2. | ||
| 237 | If | ||
| 238 | .Fa size | ||
| 239 | is not a multiple of | ||
| 240 | .Fa alignment , | ||
| 241 | behavior is undefined. | ||
| 242 | .Pp | ||
| 243 | The | ||
| 244 | .Fn malloc_conceal | ||
| 245 | and | ||
| 246 | .Fn calloc_conceal | ||
| 247 | functions behave the same as | ||
| 248 | .Fn malloc | ||
| 249 | and | ||
| 250 | .Fn calloc | ||
| 251 | respectively, | ||
| 252 | with the exception that the allocation returned is marked with the | ||
| 253 | .Dv MAP_CONCEAL | ||
| 254 | .Xr mmap 2 | ||
| 255 | flag and calling | ||
| 256 | .Fn free | ||
| 257 | on the allocation will discard the contents explicitly. | ||
| 258 | A reallocation of a concealed allocation will leave these properties intact. | ||
| 259 | .Sh MALLOC OPTIONS | ||
| 260 | Upon the first call to the | ||
| 261 | .Fn malloc | ||
| 262 | family of functions, an initialization sequence inspects the | ||
| 263 | value of the | ||
| 264 | .Va vm.malloc_conf | ||
| 265 | .Xr sysctl 2 , | ||
| 266 | next checks the environment for a variable called | ||
| 267 | .Ev MALLOC_OPTIONS , | ||
| 268 | and finally looks at the global variable | ||
| 269 | .Va malloc_options | ||
| 270 | in the program. | ||
| 271 | Each is scanned for the flags documented below. | ||
| 272 | Unless otherwise noted uppercase means on, lowercase means off. | ||
| 273 | During initialization, flags occurring later modify the behaviour | ||
| 274 | that was requested by flags processed earlier. | ||
| 275 | .Bl -tag -width indent | ||
| 276 | .It Cm C | ||
| 277 | .Dq Canaries . | ||
| 278 | Add canaries at the end of allocations in order to detect | ||
| 279 | heap overflows. | ||
| 280 | The canary's content is checked when | ||
| 281 | .Nm free | ||
| 282 | is called. | ||
| 283 | If it has been corrupted, the process is aborted. | ||
| 284 | .It Cm D | ||
| 285 | .Dq Dump . | ||
| 286 | .Fn malloc | ||
| 287 | will dump statistics to the file | ||
| 288 | .Pa ./malloc.out , | ||
| 289 | if it already exists, | ||
| 290 | at exit. | ||
| 291 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
| 292 | order to have any effect. | ||
| 293 | .It Cm F | ||
| 294 | .Dq Freecheck . | ||
| 295 | Enable more extensive double free and use after free detection. | ||
| 296 | All chunks in the delayed free list will be checked for double frees. | ||
| 297 | Unused pages on the freelist are read and write protected to | ||
| 298 | cause a segmentation fault upon access. | ||
| 299 | .It Cm G | ||
| 300 | .Dq Guard . | ||
| 301 | Enable guard pages. | ||
| 302 | Each page size or larger allocation is followed by a guard page that will | ||
| 303 | cause a segmentation fault upon any access. | ||
| 304 | .It Cm J | ||
| 305 | .Dq More junking . | ||
| 306 | Increase the junk level by one if it is smaller than 2. | ||
| 307 | .It Cm j | ||
| 308 | .Dq Less junking . | ||
| 309 | Decrease the junk level by one if it is larger than 0. | ||
| 310 | Junking writes some junk bytes into the area allocated. | ||
| 311 | Junk is bytes of 0xdb when allocating; | ||
| 312 | freed chunks are filled with 0xdf. | ||
| 313 | By default the junk level is 1: after free, | ||
| 314 | small chunks are completely junked; | ||
| 315 | for pages the first part is junked. | ||
| 316 | After a delay, | ||
| 317 | the filling pattern is validated and the process is aborted if the pattern | ||
| 318 | was modified. | ||
| 319 | For junk level 2, junking is done on allocation as well and without size | ||
| 320 | restrictions. | ||
| 321 | If the junk level is zero, no junking is performed. | ||
| 322 | .It Cm R | ||
| 323 | .Dq realloc . | ||
| 324 | Always reallocate when | ||
| 325 | .Fn realloc | ||
| 326 | is called, even if the initial allocation was big enough. | ||
| 327 | .\".Pp | ||
| 328 | .\".It Cm U | ||
| 329 | .\".Dq utrace . | ||
| 330 | .\"Generate entries for | ||
| 331 | .\".Xr ktrace 1 | ||
| 332 | .\"for all operations. | ||
| 333 | .\"Consult the source for this one. | ||
| 334 | .It Cm S | ||
| 335 | .\" Malloc option S is vaguely documented on purpose. | ||
| 336 | Enable all options suitable for security auditing. | ||
| 337 | .It Cm U | ||
| 338 | .Dq Free unmap . | ||
| 339 | Enable use after free protection for larger allocations. | ||
| 340 | Unused pages on the freelist are read and write protected to | ||
| 341 | cause a segmentation fault upon access. | ||
| 342 | .It Cm X | ||
| 343 | .Dq xmalloc . | ||
| 344 | Rather than return failure, | ||
| 345 | .Xr abort 3 | ||
| 346 | the program with a diagnostic message on stderr. | ||
| 347 | It is the intention that this option be set at compile time by | ||
| 348 | including in the source: | ||
| 349 | .Bd -literal -offset indent | ||
| 350 | extern char *malloc_options; | ||
| 351 | malloc_options = "X"; | ||
| 352 | .Ed | ||
| 353 | .Pp | ||
| 354 | Note that this will cause code that is supposed to handle | ||
| 355 | out-of-memory conditions gracefully to abort instead. | ||
| 356 | .It Cm < | ||
| 357 | .Dq Halve the cache size . | ||
| 358 | Decrease the size of the free page cache by a factor of two. | ||
| 359 | .It Cm > | ||
| 360 | .Dq Double the cache size . | ||
| 361 | Increase the size of the free page cache by a factor of two. | ||
| 362 | .El | ||
| 363 | .Pp | ||
| 364 | If a program changes behavior if any of these options (except | ||
| 365 | .Cm X ) | ||
| 366 | are used, | ||
| 367 | it is buggy. | ||
| 368 | .Pp | ||
| 369 | The default size of the cache is 64 single page allocations. | ||
| 370 | It also caches a number of larger regions. | ||
| 371 | Multi-threaded programs use multiple pools. | ||
| 372 | .Sh RETURN VALUES | ||
| 373 | Upon successful completion, the allocation functions | ||
| 374 | return a pointer to the allocated space; otherwise, | ||
| 375 | .Dv NULL | ||
| 376 | is returned and | ||
| 377 | .Va errno | ||
| 378 | is set to | ||
| 379 | .Er ENOMEM . | ||
| 380 | The function | ||
| 381 | .Fn aligned_alloc | ||
| 382 | returns | ||
| 383 | .Dv NULL | ||
| 384 | and sets | ||
| 385 | .Va errno | ||
| 386 | to | ||
| 387 | .Er EINVAL | ||
| 388 | if | ||
| 389 | .Fa alignment | ||
| 390 | is not a power of 2. | ||
| 391 | .Pp | ||
| 392 | If | ||
| 393 | .Fa nmemb | ||
| 394 | or | ||
| 395 | .Fa size | ||
| 396 | is equal to 0, a unique pointer to an access protected, | ||
| 397 | zero sized object is returned. | ||
| 398 | Access via this pointer will generate a | ||
| 399 | .Dv SIGSEGV | ||
| 400 | exception. | ||
| 401 | .Pp | ||
| 402 | If multiplying | ||
| 403 | .Fa nmemb | ||
| 404 | and | ||
| 405 | .Fa size | ||
| 406 | results in integer overflow, | ||
| 407 | .Fn calloc , | ||
| 408 | .Fn reallocarray | ||
| 409 | and | ||
| 410 | .Fn recallocarray | ||
| 411 | return | ||
| 412 | .Dv NULL | ||
| 413 | and set | ||
| 414 | .Va errno | ||
| 415 | to | ||
| 416 | .Er ENOMEM . | ||
| 417 | .Pp | ||
| 418 | If | ||
| 419 | .Fa ptr | ||
| 420 | is not | ||
| 421 | .Dv NULL | ||
| 422 | and multiplying | ||
| 423 | .Fa oldnmemb | ||
| 424 | and | ||
| 425 | .Fa size | ||
| 426 | results in integer overflow | ||
| 427 | .Fn recallocarray | ||
| 428 | returns | ||
| 429 | .Dv NULL | ||
| 430 | and sets | ||
| 431 | .Va errno | ||
| 432 | to | ||
| 433 | .Er EINVAL . | ||
| 434 | .Sh IDIOMS | ||
| 435 | Consider | ||
| 436 | .Fn calloc | ||
| 437 | or the extensions | ||
| 438 | .Fn reallocarray | ||
| 439 | and | ||
| 440 | .Fn recallocarray | ||
| 441 | when there is multiplication in the | ||
| 442 | .Fa size | ||
| 443 | argument of | ||
| 444 | .Fn malloc | ||
| 445 | or | ||
| 446 | .Fn realloc . | ||
| 447 | For example, avoid this common idiom as it may lead to integer overflow: | ||
| 448 | .Bd -literal -offset indent | ||
| 449 | if ((p = malloc(num * size)) == NULL) | ||
| 450 | err(1, NULL); | ||
| 451 | .Ed | ||
| 452 | .Pp | ||
| 453 | A drop-in replacement is the | ||
| 454 | .Ox | ||
| 455 | extension | ||
| 456 | .Fn reallocarray : | ||
| 457 | .Bd -literal -offset indent | ||
| 458 | if ((p = reallocarray(NULL, num, size)) == NULL) | ||
| 459 | err(1, NULL); | ||
| 460 | .Ed | ||
| 461 | .Pp | ||
| 462 | Alternatively, | ||
| 463 | .Fn calloc | ||
| 464 | may be used at the cost of initialization overhead. | ||
| 465 | .Pp | ||
| 466 | When using | ||
| 467 | .Fn realloc , | ||
| 468 | be careful to avoid the following idiom: | ||
| 469 | .Bd -literal -offset indent | ||
| 470 | size += 50; | ||
| 471 | if ((p = realloc(p, size)) == NULL) | ||
| 472 | return (NULL); | ||
| 473 | .Ed | ||
| 474 | .Pp | ||
| 475 | Do not adjust the variable describing how much memory has been allocated | ||
| 476 | until the allocation has been successful. | ||
| 477 | This can cause aberrant program behavior if the incorrect size value is used. | ||
| 478 | In most cases, the above sample will also result in a leak of memory. | ||
| 479 | As stated earlier, a return value of | ||
| 480 | .Dv NULL | ||
| 481 | indicates that the old object still remains allocated. | ||
| 482 | Better code looks like this: | ||
| 483 | .Bd -literal -offset indent | ||
| 484 | newsize = size + 50; | ||
| 485 | if ((newp = realloc(p, newsize)) == NULL) { | ||
| 486 | free(p); | ||
| 487 | p = NULL; | ||
| 488 | size = 0; | ||
| 489 | return (NULL); | ||
| 490 | } | ||
| 491 | p = newp; | ||
| 492 | size = newsize; | ||
| 493 | .Ed | ||
| 494 | .Pp | ||
| 495 | As with | ||
| 496 | .Fn malloc , | ||
| 497 | it is important to ensure the new size value will not overflow; | ||
| 498 | i.e. avoid allocations like the following: | ||
| 499 | .Bd -literal -offset indent | ||
| 500 | if ((newp = realloc(p, num * size)) == NULL) { | ||
| 501 | ... | ||
| 502 | .Ed | ||
| 503 | .Pp | ||
| 504 | Instead, use | ||
| 505 | .Fn reallocarray : | ||
| 506 | .Bd -literal -offset indent | ||
| 507 | if ((newp = reallocarray(p, num, size)) == NULL) { | ||
| 508 | ... | ||
| 509 | .Ed | ||
| 510 | .Pp | ||
| 511 | Calling | ||
| 512 | .Fn realloc | ||
| 513 | with a | ||
| 514 | .Dv NULL | ||
| 515 | .Fa ptr | ||
| 516 | is equivalent to calling | ||
| 517 | .Fn malloc . | ||
| 518 | Instead of this idiom: | ||
| 519 | .Bd -literal -offset indent | ||
| 520 | if (p == NULL) | ||
| 521 | newp = malloc(newsize); | ||
| 522 | else | ||
| 523 | newp = realloc(p, newsize); | ||
| 524 | .Ed | ||
| 525 | .Pp | ||
| 526 | Use the following: | ||
| 527 | .Bd -literal -offset indent | ||
| 528 | newp = realloc(p, newsize); | ||
| 529 | .Ed | ||
| 530 | .Pp | ||
| 531 | The | ||
| 532 | .Fn recallocarray | ||
| 533 | function should be used for resizing objects containing sensitive data like | ||
| 534 | keys. | ||
| 535 | To avoid leaking information, | ||
| 536 | it guarantees memory is cleared before placing it on the internal free list. | ||
| 537 | Deallocation of such an object should be done by calling | ||
| 538 | .Fn freezero . | ||
| 539 | .Sh ENVIRONMENT | ||
| 540 | .Bl -tag -width "MALLOC_OPTIONS" | ||
| 541 | .It Ev MALLOC_OPTIONS | ||
| 542 | String of option flags. | ||
| 543 | .El | ||
| 544 | .Sh EXAMPLES | ||
| 545 | If | ||
| 546 | .Fn malloc | ||
| 547 | must be used with multiplication, be sure to test for overflow: | ||
| 548 | .Bd -literal -offset indent | ||
| 549 | size_t num, size; | ||
| 550 | \&... | ||
| 551 | |||
| 552 | /* Check for size_t overflow */ | ||
| 553 | if (size && num > SIZE_MAX / size) | ||
| 554 | errc(1, EOVERFLOW, "overflow"); | ||
| 555 | |||
| 556 | if ((p = malloc(num * size)) == NULL) | ||
| 557 | err(1, NULL); | ||
| 558 | .Ed | ||
| 559 | .Pp | ||
| 560 | The above test is not sufficient in all cases. | ||
| 561 | For example, multiplying ints requires a different set of checks: | ||
| 562 | .Bd -literal -offset indent | ||
| 563 | int num, size; | ||
| 564 | \&... | ||
| 565 | |||
| 566 | /* Avoid invalid requests */ | ||
| 567 | if (size < 0 || num < 0) | ||
| 568 | errc(1, EOVERFLOW, "overflow"); | ||
| 569 | |||
| 570 | /* Check for signed int overflow */ | ||
| 571 | if (size && num > INT_MAX / size) | ||
| 572 | errc(1, EOVERFLOW, "overflow"); | ||
| 573 | |||
| 574 | if ((p = malloc(num * size)) == NULL) | ||
| 575 | err(1, NULL); | ||
| 576 | .Ed | ||
| 577 | .Pp | ||
| 578 | Assuming the implementation checks for integer overflow as | ||
| 579 | .Ox | ||
| 580 | does, it is much easier to use | ||
| 581 | .Fn calloc , | ||
| 582 | .Fn reallocarray , | ||
| 583 | or | ||
| 584 | .Fn recallocarray . | ||
| 585 | .Pp | ||
| 586 | The above examples could be simplified to: | ||
| 587 | .Bd -literal -offset indent | ||
| 588 | if ((p = reallocarray(NULL, num, size)) == NULL) | ||
| 589 | err(1, NULL); | ||
| 590 | .Ed | ||
| 591 | .Pp | ||
| 592 | or at the cost of initialization: | ||
| 593 | .Bd -literal -offset indent | ||
| 594 | if ((p = calloc(num, size)) == NULL) | ||
| 595 | err(1, NULL); | ||
| 596 | .Ed | ||
| 597 | .Pp | ||
| 598 | Set a systemwide reduction of the cache to a quarter of the | ||
| 599 | default size and use guard pages: | ||
| 600 | .Pp | ||
| 601 | .Dl # sysctl vm.malloc_conf='G<<' | ||
| 602 | .Sh DIAGNOSTICS | ||
| 603 | If any of the functions detect an error condition, | ||
| 604 | a message will be printed to file descriptor | ||
| 605 | 2 (not using stdio). | ||
| 606 | Errors will result in the process being aborted. | ||
| 607 | .Pp | ||
| 608 | Here is a brief description of the error messages and what they mean: | ||
| 609 | .Bl -tag -width Ds | ||
| 610 | .It Dq out of memory | ||
| 611 | If the | ||
| 612 | .Cm X | ||
| 613 | option is specified it is an error for the allocation functions | ||
| 614 | to return | ||
| 615 | .Dv NULL . | ||
| 616 | .It Dq bogus pointer (double free?) | ||
| 617 | An attempt to | ||
| 618 | .Fn free | ||
| 619 | or | ||
| 620 | reallocate an unallocated pointer was made. | ||
| 621 | .It Dq chunk is already free | ||
| 622 | There was an attempt to free a chunk that had already been freed. | ||
| 623 | .It Dq write after free | ||
| 624 | A chunk has been modified after it was freed. | ||
| 625 | .It Dq modified chunk-pointer | ||
| 626 | The pointer passed to | ||
| 627 | .Fn free | ||
| 628 | or a reallocation function has been modified. | ||
| 629 | .It Dq chunk canary corrupted address offset@length | ||
| 630 | A byte after the requested size has been overwritten, | ||
| 631 | indicating a heap overflow. | ||
| 632 | The offset at which corruption was detected is printed before the @, | ||
| 633 | and the requested length of the allocation after the @. | ||
| 634 | .It Dq recorded old size oldsize != size | ||
| 635 | .Fn recallocarray | ||
| 636 | has detected that the given old size does not equal the recorded size in its | ||
| 637 | meta data. | ||
| 638 | Enabling option | ||
| 639 | .Cm C | ||
| 640 | allows | ||
| 641 | .Fn recallocarray | ||
| 642 | to catch more of these cases. | ||
| 643 | .It Dq recursive call | ||
| 644 | An attempt was made to call recursively into these functions, i.e., from a | ||
| 645 | signal handler. | ||
| 646 | This behavior is not supported. | ||
| 647 | In particular, signal handlers should | ||
| 648 | .Em not | ||
| 649 | use any of the | ||
| 650 | .Fn malloc | ||
| 651 | functions nor utilize any other functions which may call | ||
| 652 | .Fn malloc | ||
| 653 | (e.g., | ||
| 654 | .Xr stdio 3 | ||
| 655 | routines). | ||
| 656 | .It Dq unknown char in MALLOC_OPTIONS | ||
| 657 | We found something we didn't understand. | ||
| 658 | .It any other error | ||
| 659 | .Fn malloc | ||
| 660 | detected an internal error; | ||
| 661 | consult sources and/or wizards. | ||
| 662 | .El | ||
| 663 | .Sh SEE ALSO | ||
| 664 | .Xr brk 2 , | ||
| 665 | .Xr mmap 2 , | ||
| 666 | .Xr munmap 2 , | ||
| 667 | .Xr sysctl 2 , | ||
| 668 | .Xr alloca 3 , | ||
| 669 | .Xr getpagesize 3 , | ||
| 670 | .Xr posix_memalign 3 | ||
| 671 | .Sh STANDARDS | ||
| 672 | The | ||
| 673 | .Fn malloc , | ||
| 674 | .Fn calloc , | ||
| 675 | .Fn realloc , | ||
| 676 | and | ||
| 677 | .Fn free | ||
| 678 | functions conform to | ||
| 679 | .St -ansiC . | ||
| 680 | The | ||
| 681 | .Fn aligned_alloc | ||
| 682 | function conforms to | ||
| 683 | .St -isoC-2011 . | ||
| 684 | .Pp | ||
| 685 | If | ||
| 686 | .Fa nmemb | ||
| 687 | or | ||
| 688 | .Fa size | ||
| 689 | are 0, the return value is implementation defined; | ||
| 690 | other conforming implementations may return | ||
| 691 | .Dv NULL | ||
| 692 | in this case. | ||
| 693 | .Pp | ||
| 694 | The | ||
| 695 | .Ev MALLOC_OPTIONS | ||
| 696 | environment variable, the | ||
| 697 | .Va vm.malloc_conf | ||
| 698 | sysctl and the | ||
| 699 | .Sx DIAGNOSTICS | ||
| 700 | output are extensions to the standard. | ||
| 701 | .Sh HISTORY | ||
| 702 | A | ||
| 703 | .Fn free | ||
| 704 | internal kernel function and a predecessor to | ||
| 705 | .Fn malloc , | ||
| 706 | .Fn alloc , | ||
| 707 | first appeared in | ||
| 708 | .At v1 . | ||
| 709 | C library functions | ||
| 710 | .Fn alloc | ||
| 711 | and | ||
| 712 | .Fn free | ||
| 713 | appeared in | ||
| 714 | .At v6 . | ||
| 715 | The functions | ||
| 716 | .Fn malloc , | ||
| 717 | .Fn calloc , | ||
| 718 | and | ||
| 719 | .Fn realloc | ||
| 720 | first appeared in | ||
| 721 | .At v7 . | ||
| 722 | .Pp | ||
| 723 | A new implementation by Chris Kingsley was introduced in | ||
| 724 | .Bx 4.2 , | ||
| 725 | followed by a complete rewrite by Poul-Henning Kamp which appeared in | ||
| 726 | .Fx 2.2 | ||
| 727 | and was included in | ||
| 728 | .Ox 2.0 . | ||
| 729 | These implementations were all | ||
| 730 | .Xr sbrk 2 | ||
| 731 | based. | ||
| 732 | In | ||
| 733 | .Ox 3.8 , | ||
| 734 | Thierry Deval rewrote | ||
| 735 | .Nm | ||
| 736 | to use the | ||
| 737 | .Xr mmap 2 | ||
| 738 | system call, | ||
| 739 | making the page addresses returned by | ||
| 740 | .Nm | ||
| 741 | random. | ||
| 742 | A rewrite by Otto Moerbeek introducing a new central data structure and more | ||
| 743 | randomization appeared in | ||
| 744 | .Ox 4.4 . | ||
| 745 | .Pp | ||
| 746 | The | ||
| 747 | .Fn reallocarray | ||
| 748 | function appeared in | ||
| 749 | .Ox 5.6 . | ||
| 750 | The | ||
| 751 | .Fn recallocarray | ||
| 752 | function appeared in | ||
| 753 | .Ox 6.1 . | ||
| 754 | The | ||
| 755 | .Fn freezero | ||
| 756 | function appeared in | ||
| 757 | .Ox 6.2 . | ||
| 758 | The | ||
| 759 | .Fn aligned_alloc | ||
| 760 | function appeared in | ||
| 761 | .Ox 6.5 . | ||
| 762 | The | ||
| 763 | .Fn malloc_conceal | ||
| 764 | and | ||
| 765 | .Fn calloc_conceal | ||
| 766 | functions appeared in | ||
| 767 | .Ox 6.6 . | ||
| 768 | .Sh CAVEATS | ||
| 769 | When using | ||
| 770 | .Fn malloc , | ||
| 771 | be wary of signed integer and | ||
| 772 | .Vt size_t | ||
| 773 | overflow especially when there is multiplication in the | ||
| 774 | .Fa size | ||
| 775 | argument. | ||
| 776 | .Pp | ||
| 777 | Signed integer overflow will cause undefined behavior which compilers | ||
| 778 | typically handle by wrapping back around to negative numbers. | ||
| 779 | Depending on the input, this can result in allocating more or less | ||
| 780 | memory than intended. | ||
| 781 | .Pp | ||
| 782 | An unsigned overflow has defined behavior which will wrap back around and | ||
| 783 | return less memory than intended. | ||
| 784 | .Pp | ||
| 785 | A signed or unsigned integer overflow is a | ||
| 786 | .Em security | ||
| 787 | risk if less memory is returned than intended. | ||
| 788 | Subsequent code may corrupt the heap by writing beyond the memory that was | ||
| 789 | allocated. | ||
| 790 | An attacker may be able to leverage this heap corruption to execute arbitrary | ||
| 791 | code. | ||
| 792 | .Pp | ||
| 793 | Consider using | ||
| 794 | .Fn calloc , | ||
| 795 | .Fn reallocarray | ||
| 796 | or | ||
| 797 | .Fn recallocarray | ||
| 798 | instead of using multiplication in | ||
| 799 | .Fn malloc | ||
| 800 | and | ||
| 801 | .Fn realloc | ||
| 802 | to avoid these problems on | ||
| 803 | .Ox . | ||
