diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 837 |
1 files changed, 0 insertions, 837 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 deleted file mode 100644 index b700add823..0000000000 --- a/src/lib/libc/stdlib/malloc.3 +++ /dev/null | |||
| @@ -1,837 +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.137 2023/07/01 18:35:14 otto Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd $Mdocdate: July 1 2023 $ | ||
| 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 a leak report using | ||
| 288 | .Xr utrace 2 | ||
| 289 | at exit. | ||
| 290 | To record the dump: | ||
| 291 | .Pp | ||
| 292 | .Dl $ MALLOC_OPTIONS=D ktrace -tu program ... | ||
| 293 | .Pp | ||
| 294 | To view the leak report: | ||
| 295 | .Pp | ||
| 296 | .Dl $ kdump -u malloc ... | ||
| 297 | .Pp | ||
| 298 | By default, the immediate caller of a | ||
| 299 | .Nm | ||
| 300 | function will be recorded. | ||
| 301 | Use malloc options | ||
| 302 | .Cm 2 | ||
| 303 | or | ||
| 304 | .Cm 3 | ||
| 305 | to record the caller one or two stack frames deeper instead. | ||
| 306 | These malloc options imply | ||
| 307 | .Cm D . | ||
| 308 | .It Cm F | ||
| 309 | .Dq Freecheck . | ||
| 310 | Enable more extensive double free and use after free detection. | ||
| 311 | All chunks in the delayed free list will be checked for double frees and | ||
| 312 | write after frees. | ||
| 313 | Unused pages on the freelist are read and write protected to | ||
| 314 | cause a segmentation fault upon access. | ||
| 315 | .It Cm G | ||
| 316 | .Dq Guard . | ||
| 317 | Enable guard pages. | ||
| 318 | Each page size or larger allocation is followed by a guard page that will | ||
| 319 | cause a segmentation fault upon any access. | ||
| 320 | .It Cm J | ||
| 321 | .Dq More junking . | ||
| 322 | Increase the junk level by one if it is smaller than 2. | ||
| 323 | .It Cm j | ||
| 324 | .Dq Less junking . | ||
| 325 | Decrease the junk level by one if it is larger than 0. | ||
| 326 | Junking writes some junk bytes into the area allocated. | ||
| 327 | Junk is bytes of 0xdb when allocating; | ||
| 328 | small allocations are initially junked with 0xdf as are freed allocations. | ||
| 329 | By default the junk level is 1: after free, | ||
| 330 | small chunks are completely junked; | ||
| 331 | for pages the first part is junked. | ||
| 332 | After a delay, | ||
| 333 | the filling pattern is validated and the process is aborted if the pattern | ||
| 334 | was modified. | ||
| 335 | For junk level 2, junking is done on allocation as well and without size | ||
| 336 | restrictions. | ||
| 337 | If the junk level is zero, no junking is performed. | ||
| 338 | .It Cm R | ||
| 339 | .Dq realloc . | ||
| 340 | Always reallocate when | ||
| 341 | .Fn realloc | ||
| 342 | is called, even if the initial allocation was big enough. | ||
| 343 | .\".Pp | ||
| 344 | .\".It Cm U | ||
| 345 | .\".Dq utrace . | ||
| 346 | .\"Generate entries for | ||
| 347 | .\".Xr ktrace 1 | ||
| 348 | .\"for all operations. | ||
| 349 | .\"Consult the source for this one. | ||
| 350 | .It Cm S | ||
| 351 | .\" Malloc option S is vaguely documented on purpose. | ||
| 352 | Enable all options suitable for security auditing. | ||
| 353 | .It Cm U | ||
| 354 | .Dq Free unmap . | ||
| 355 | Enable use after free protection for larger allocations. | ||
| 356 | Unused pages on the freelist are read and write protected to | ||
| 357 | cause a segmentation fault upon access. | ||
| 358 | .It Cm V | ||
| 359 | .Dq Verbose . | ||
| 360 | Use with | ||
| 361 | .Cm D | ||
| 362 | to get a verbose dump of malloc's internal state. | ||
| 363 | .It Cm X | ||
| 364 | .Dq xmalloc . | ||
| 365 | Rather than return failure, | ||
| 366 | .Xr abort 3 | ||
| 367 | the program with a diagnostic message on stderr. | ||
| 368 | It is the intention that this option be set at compile time by | ||
| 369 | including in the source: | ||
| 370 | .Bd -literal -offset indent | ||
| 371 | extern char *malloc_options; | ||
| 372 | malloc_options = "X"; | ||
| 373 | .Ed | ||
| 374 | .Pp | ||
| 375 | Note that this will cause code that is supposed to handle | ||
| 376 | out-of-memory conditions gracefully to abort instead. | ||
| 377 | .It Cm < | ||
| 378 | .Dq Halve the cache size . | ||
| 379 | Decrease the size of the free page cache by a factor of two. | ||
| 380 | .It Cm > | ||
| 381 | .Dq Double the cache size . | ||
| 382 | Increase the size of the free page cache by a factor of two. | ||
| 383 | .El | ||
| 384 | .Pp | ||
| 385 | If a program changes behavior if any of these options (except | ||
| 386 | .Cm X ) | ||
| 387 | are used, | ||
| 388 | it is buggy. | ||
| 389 | .Pp | ||
| 390 | The default size of the cache is 64 single page allocations. | ||
| 391 | It also caches a number of larger regions. | ||
| 392 | Multi-threaded programs use multiple pools. | ||
| 393 | .Sh RETURN VALUES | ||
| 394 | Upon successful completion, the allocation functions | ||
| 395 | return a pointer to the allocated space; otherwise, | ||
| 396 | .Dv NULL | ||
| 397 | is returned and | ||
| 398 | .Va errno | ||
| 399 | is set to | ||
| 400 | .Er ENOMEM . | ||
| 401 | The function | ||
| 402 | .Fn aligned_alloc | ||
| 403 | returns | ||
| 404 | .Dv NULL | ||
| 405 | and sets | ||
| 406 | .Va errno | ||
| 407 | to | ||
| 408 | .Er EINVAL | ||
| 409 | if | ||
| 410 | .Fa alignment | ||
| 411 | is not a power of 2. | ||
| 412 | .Pp | ||
| 413 | If | ||
| 414 | .Fa nmemb | ||
| 415 | or | ||
| 416 | .Fa size | ||
| 417 | is equal to 0, a unique pointer to an access protected, | ||
| 418 | zero sized object is returned. | ||
| 419 | Access via this pointer will generate a | ||
| 420 | .Dv SIGSEGV | ||
| 421 | exception. | ||
| 422 | .Pp | ||
| 423 | If multiplying | ||
| 424 | .Fa nmemb | ||
| 425 | and | ||
| 426 | .Fa size | ||
| 427 | results in integer overflow, | ||
| 428 | .Fn calloc , | ||
| 429 | .Fn reallocarray | ||
| 430 | and | ||
| 431 | .Fn recallocarray | ||
| 432 | return | ||
| 433 | .Dv NULL | ||
| 434 | and set | ||
| 435 | .Va errno | ||
| 436 | to | ||
| 437 | .Er ENOMEM . | ||
| 438 | .Pp | ||
| 439 | If | ||
| 440 | .Fa ptr | ||
| 441 | is not | ||
| 442 | .Dv NULL | ||
| 443 | and multiplying | ||
| 444 | .Fa oldnmemb | ||
| 445 | and | ||
| 446 | .Fa size | ||
| 447 | results in integer overflow, | ||
| 448 | .Fn recallocarray | ||
| 449 | returns | ||
| 450 | .Dv NULL | ||
| 451 | and sets | ||
| 452 | .Va errno | ||
| 453 | to | ||
| 454 | .Er EINVAL . | ||
| 455 | .Sh IDIOMS | ||
| 456 | Consider | ||
| 457 | .Fn calloc | ||
| 458 | or the extensions | ||
| 459 | .Fn reallocarray | ||
| 460 | and | ||
| 461 | .Fn recallocarray | ||
| 462 | when there is multiplication in the | ||
| 463 | .Fa size | ||
| 464 | argument of | ||
| 465 | .Fn malloc | ||
| 466 | or | ||
| 467 | .Fn realloc . | ||
| 468 | For example, avoid this common idiom as it may lead to integer overflow: | ||
| 469 | .Bd -literal -offset indent | ||
| 470 | if ((p = malloc(num * size)) == NULL) | ||
| 471 | err(1, NULL); | ||
| 472 | .Ed | ||
| 473 | .Pp | ||
| 474 | A drop-in replacement is the | ||
| 475 | .Ox | ||
| 476 | extension | ||
| 477 | .Fn reallocarray : | ||
| 478 | .Bd -literal -offset indent | ||
| 479 | if ((p = reallocarray(NULL, num, size)) == NULL) | ||
| 480 | err(1, NULL); | ||
| 481 | .Ed | ||
| 482 | .Pp | ||
| 483 | Alternatively, | ||
| 484 | .Fn calloc | ||
| 485 | may be used at the cost of initialization overhead. | ||
| 486 | .Pp | ||
| 487 | When using | ||
| 488 | .Fn realloc , | ||
| 489 | be careful to avoid the following idiom: | ||
| 490 | .Bd -literal -offset indent | ||
| 491 | size += 50; | ||
| 492 | if ((p = realloc(p, size)) == NULL) | ||
| 493 | return (NULL); | ||
| 494 | .Ed | ||
| 495 | .Pp | ||
| 496 | Do not adjust the variable describing how much memory has been allocated | ||
| 497 | until the allocation has been successful. | ||
| 498 | This can cause aberrant program behavior if the incorrect size value is used. | ||
| 499 | In most cases, the above sample will also result in a leak of memory. | ||
| 500 | As stated earlier, a return value of | ||
| 501 | .Dv NULL | ||
| 502 | indicates that the old object still remains allocated. | ||
| 503 | Better code looks like this: | ||
| 504 | .Bd -literal -offset indent | ||
| 505 | newsize = size + 50; | ||
| 506 | if ((newp = realloc(p, newsize)) == NULL) { | ||
| 507 | free(p); | ||
| 508 | p = NULL; | ||
| 509 | size = 0; | ||
| 510 | return (NULL); | ||
| 511 | } | ||
| 512 | p = newp; | ||
| 513 | size = newsize; | ||
| 514 | .Ed | ||
| 515 | .Pp | ||
| 516 | As with | ||
| 517 | .Fn malloc , | ||
| 518 | it is important to ensure the new size value will not overflow; | ||
| 519 | i.e. avoid allocations like the following: | ||
| 520 | .Bd -literal -offset indent | ||
| 521 | if ((newp = realloc(p, num * size)) == NULL) { | ||
| 522 | ... | ||
| 523 | .Ed | ||
| 524 | .Pp | ||
| 525 | Instead, use | ||
| 526 | .Fn reallocarray : | ||
| 527 | .Bd -literal -offset indent | ||
| 528 | if ((newp = reallocarray(p, num, size)) == NULL) { | ||
| 529 | ... | ||
| 530 | .Ed | ||
| 531 | .Pp | ||
| 532 | Calling | ||
| 533 | .Fn realloc | ||
| 534 | with a | ||
| 535 | .Dv NULL | ||
| 536 | .Fa ptr | ||
| 537 | is equivalent to calling | ||
| 538 | .Fn malloc . | ||
| 539 | Instead of this idiom: | ||
| 540 | .Bd -literal -offset indent | ||
| 541 | if (p == NULL) | ||
| 542 | newp = malloc(newsize); | ||
| 543 | else | ||
| 544 | newp = realloc(p, newsize); | ||
| 545 | .Ed | ||
| 546 | .Pp | ||
| 547 | Use the following: | ||
| 548 | .Bd -literal -offset indent | ||
| 549 | newp = realloc(p, newsize); | ||
| 550 | .Ed | ||
| 551 | .Pp | ||
| 552 | The | ||
| 553 | .Fn recallocarray | ||
| 554 | function should be used for resizing objects containing sensitive data like | ||
| 555 | keys. | ||
| 556 | To avoid leaking information, | ||
| 557 | it guarantees memory is cleared before placing it on the internal free list. | ||
| 558 | Deallocation of such an object should be done by calling | ||
| 559 | .Fn freezero . | ||
| 560 | .Sh ENVIRONMENT | ||
| 561 | .Bl -tag -width "MALLOC_OPTIONS" | ||
| 562 | .It Ev MALLOC_OPTIONS | ||
| 563 | String of option flags. | ||
| 564 | .El | ||
| 565 | .Sh EXAMPLES | ||
| 566 | If | ||
| 567 | .Fn malloc | ||
| 568 | must be used with multiplication, be sure to test for overflow: | ||
| 569 | .Bd -literal -offset indent | ||
| 570 | size_t num, size; | ||
| 571 | \&... | ||
| 572 | |||
| 573 | /* Check for size_t overflow */ | ||
| 574 | if (size && num > SIZE_MAX / size) | ||
| 575 | errc(1, EOVERFLOW, "overflow"); | ||
| 576 | |||
| 577 | if ((p = malloc(num * size)) == NULL) | ||
| 578 | err(1, NULL); | ||
| 579 | .Ed | ||
| 580 | .Pp | ||
| 581 | The above test is not sufficient in all cases. | ||
| 582 | For example, multiplying ints requires a different set of checks: | ||
| 583 | .Bd -literal -offset indent | ||
| 584 | int num, size; | ||
| 585 | \&... | ||
| 586 | |||
| 587 | /* Avoid invalid requests */ | ||
| 588 | if (size < 0 || num < 0) | ||
| 589 | errc(1, EOVERFLOW, "overflow"); | ||
| 590 | |||
| 591 | /* Check for signed int overflow */ | ||
| 592 | if (size && num > INT_MAX / size) | ||
| 593 | errc(1, EOVERFLOW, "overflow"); | ||
| 594 | |||
| 595 | if ((p = malloc(num * size)) == NULL) | ||
| 596 | err(1, NULL); | ||
| 597 | .Ed | ||
| 598 | .Pp | ||
| 599 | Assuming the implementation checks for integer overflow as | ||
| 600 | .Ox | ||
| 601 | does, it is much easier to use | ||
| 602 | .Fn calloc , | ||
| 603 | .Fn reallocarray , | ||
| 604 | or | ||
| 605 | .Fn recallocarray . | ||
| 606 | .Pp | ||
| 607 | The above examples could be simplified to: | ||
| 608 | .Bd -literal -offset indent | ||
| 609 | if ((p = reallocarray(NULL, num, size)) == NULL) | ||
| 610 | err(1, NULL); | ||
| 611 | .Ed | ||
| 612 | .Pp | ||
| 613 | or at the cost of initialization: | ||
| 614 | .Bd -literal -offset indent | ||
| 615 | if ((p = calloc(num, size)) == NULL) | ||
| 616 | err(1, NULL); | ||
| 617 | .Ed | ||
| 618 | .Pp | ||
| 619 | Set a systemwide reduction of the cache to a quarter of the | ||
| 620 | default size and use guard pages: | ||
| 621 | .Pp | ||
| 622 | .Dl # sysctl vm.malloc_conf='G<<' | ||
| 623 | .Sh DIAGNOSTICS | ||
| 624 | If any of the functions detect an error condition, | ||
| 625 | a message will be printed to file descriptor | ||
| 626 | 2 (not using stdio). | ||
| 627 | Errors will result in the process being aborted. | ||
| 628 | .Pp | ||
| 629 | Here is a brief description of the error messages and what they mean: | ||
| 630 | .Bl -tag -width Ds | ||
| 631 | .It Dq out of memory | ||
| 632 | If the | ||
| 633 | .Cm X | ||
| 634 | option is specified, it is an error for the allocation functions | ||
| 635 | to return | ||
| 636 | .Dv NULL . | ||
| 637 | .It Dq bogus pointer (double free?) | ||
| 638 | An attempt to | ||
| 639 | .Fn free | ||
| 640 | or | ||
| 641 | reallocate an unallocated pointer was made. | ||
| 642 | .It Dq double free | ||
| 643 | There was an attempt to free an allocation that had already been freed. | ||
| 644 | .It Dq write after free | ||
| 645 | An allocation has been modified after it was freed. | ||
| 646 | .It Dq modified chunk-pointer | ||
| 647 | The pointer passed to | ||
| 648 | .Fn free | ||
| 649 | or a reallocation function has been modified. | ||
| 650 | .It Dq canary corrupted address offset@length | ||
| 651 | A byte after the requested size has been overwritten, | ||
| 652 | indicating a heap overflow. | ||
| 653 | The offset at which corruption was detected is printed before the @, | ||
| 654 | and the requested length of the allocation after the @. | ||
| 655 | .It Dq recorded size oldsize inconsistent with size | ||
| 656 | .Fn recallocarray | ||
| 657 | or | ||
| 658 | .Fn freezero | ||
| 659 | has detected that the given old size does not match the recorded size in its | ||
| 660 | meta data. | ||
| 661 | Enabling option | ||
| 662 | .Cm C | ||
| 663 | allows | ||
| 664 | .Fn recallocarray | ||
| 665 | to catch more of these cases. | ||
| 666 | .It Dq recursive call | ||
| 667 | An attempt was made to call recursively into these functions, i.e., from a | ||
| 668 | signal handler. | ||
| 669 | This behavior is not supported. | ||
| 670 | In particular, signal handlers should | ||
| 671 | .Em not | ||
| 672 | use any of the | ||
| 673 | .Fn malloc | ||
| 674 | functions nor utilize any other functions which may call | ||
| 675 | .Fn malloc | ||
| 676 | (e.g., | ||
| 677 | .Xr stdio 3 | ||
| 678 | routines). | ||
| 679 | .It Dq unknown char in MALLOC_OPTIONS | ||
| 680 | We found something we didn't understand. | ||
| 681 | .It any other error | ||
| 682 | .Fn malloc | ||
| 683 | detected an internal error; | ||
| 684 | consult sources and/or wizards. | ||
| 685 | .El | ||
| 686 | .Sh SEE ALSO | ||
| 687 | .Xr brk 2 , | ||
| 688 | .Xr mmap 2 , | ||
| 689 | .Xr munmap 2 , | ||
| 690 | .Xr sysctl 2 , | ||
| 691 | .Xr alloca 3 , | ||
| 692 | .Xr getpagesize 3 , | ||
| 693 | .Xr posix_memalign 3 | ||
| 694 | .Sh STANDARDS | ||
| 695 | The | ||
| 696 | .Fn malloc , | ||
| 697 | .Fn calloc , | ||
| 698 | .Fn realloc , | ||
| 699 | and | ||
| 700 | .Fn free | ||
| 701 | functions conform to | ||
| 702 | .St -ansiC . | ||
| 703 | The | ||
| 704 | .Fn aligned_alloc | ||
| 705 | function conforms to | ||
| 706 | .St -isoC-2011 . | ||
| 707 | .Pp | ||
| 708 | If | ||
| 709 | .Fa nmemb | ||
| 710 | or | ||
| 711 | .Fa size | ||
| 712 | are 0, the return value is implementation defined; | ||
| 713 | other conforming implementations may return | ||
| 714 | .Dv NULL | ||
| 715 | in this case. | ||
| 716 | .Pp | ||
| 717 | The | ||
| 718 | .Ev MALLOC_OPTIONS | ||
| 719 | environment variable, the | ||
| 720 | .Va vm.malloc_conf | ||
| 721 | sysctl and the | ||
| 722 | .Sx DIAGNOSTICS | ||
| 723 | output are extensions to the standard. | ||
| 724 | .Sh HISTORY | ||
| 725 | A | ||
| 726 | .Fn free | ||
| 727 | internal kernel function and a predecessor to | ||
| 728 | .Fn malloc , | ||
| 729 | .Fn alloc , | ||
| 730 | first appeared in | ||
| 731 | .At v1 . | ||
| 732 | C library functions | ||
| 733 | .Fn alloc | ||
| 734 | and | ||
| 735 | .Fn free | ||
| 736 | appeared in | ||
| 737 | .At v6 . | ||
| 738 | The functions | ||
| 739 | .Fn malloc , | ||
| 740 | .Fn calloc , | ||
| 741 | and | ||
| 742 | .Fn realloc | ||
| 743 | first appeared in | ||
| 744 | .At v7 . | ||
| 745 | .Pp | ||
| 746 | A new implementation by Chris Kingsley was introduced in | ||
| 747 | .Bx 4.2 , | ||
| 748 | followed by a complete rewrite by Poul-Henning Kamp which appeared in | ||
| 749 | .Fx 2.2 | ||
| 750 | and was included in | ||
| 751 | .Ox 2.0 . | ||
| 752 | These implementations were all | ||
| 753 | .Xr sbrk 2 | ||
| 754 | based. | ||
| 755 | In | ||
| 756 | .Ox 3.8 , | ||
| 757 | Thierry Deval rewrote | ||
| 758 | .Nm | ||
| 759 | to use the | ||
| 760 | .Xr mmap 2 | ||
| 761 | system call, | ||
| 762 | making the page addresses returned by | ||
| 763 | .Nm | ||
| 764 | random. | ||
| 765 | A rewrite by Otto Moerbeek introducing a new central data structure and more | ||
| 766 | randomization appeared in | ||
| 767 | .Ox 4.4 . | ||
| 768 | .Pp | ||
| 769 | The | ||
| 770 | .Fn reallocarray | ||
| 771 | function appeared in | ||
| 772 | .Ox 5.6 . | ||
| 773 | The | ||
| 774 | .Fn recallocarray | ||
| 775 | function appeared in | ||
| 776 | .Ox 6.1 . | ||
| 777 | The | ||
| 778 | .Fn freezero | ||
| 779 | function appeared in | ||
| 780 | .Ox 6.2 . | ||
| 781 | The | ||
| 782 | .Fn aligned_alloc | ||
| 783 | function appeared in | ||
| 784 | .Ox 6.5 . | ||
| 785 | The | ||
| 786 | .Fn malloc_conceal | ||
| 787 | and | ||
| 788 | .Fn calloc_conceal | ||
| 789 | functions appeared in | ||
| 790 | .Ox 6.6 . | ||
| 791 | .Sh CAVEATS | ||
| 792 | When using | ||
| 793 | .Fn malloc , | ||
| 794 | be wary of signed integer and | ||
| 795 | .Vt size_t | ||
| 796 | overflow especially when there is multiplication in the | ||
| 797 | .Fa size | ||
| 798 | argument. | ||
| 799 | .Pp | ||
| 800 | Signed integer overflow will cause undefined behavior which compilers | ||
| 801 | typically handle by wrapping back around to negative numbers. | ||
| 802 | Depending on the input, this can result in allocating more or less | ||
| 803 | memory than intended. | ||
| 804 | .Pp | ||
| 805 | An unsigned overflow has defined behavior which will wrap back around and | ||
| 806 | return less memory than intended. | ||
| 807 | .Pp | ||
| 808 | A signed or unsigned integer overflow is a | ||
| 809 | .Em security | ||
| 810 | risk if less memory is returned than intended. | ||
| 811 | Subsequent code may corrupt the heap by writing beyond the memory that was | ||
| 812 | allocated. | ||
| 813 | An attacker may be able to leverage this heap corruption to execute arbitrary | ||
| 814 | code. | ||
| 815 | .Pp | ||
| 816 | Consider using | ||
| 817 | .Fn calloc , | ||
| 818 | .Fn reallocarray | ||
| 819 | or | ||
| 820 | .Fn recallocarray | ||
| 821 | instead of using multiplication in | ||
| 822 | .Fn malloc | ||
| 823 | and | ||
| 824 | .Fn realloc | ||
| 825 | to avoid these problems on | ||
| 826 | .Ox . | ||
| 827 | .Pp | ||
| 828 | The mechanism to record caller functions when using malloc options | ||
| 829 | .Cm 2 | ||
| 830 | or | ||
| 831 | .Cm 3 | ||
| 832 | is not guaranteed to work for all platforms, compilers or compilation | ||
| 833 | options, | ||
| 834 | and might even crash your program. | ||
| 835 | Use | ||
| 836 | .Em only | ||
| 837 | for debugging purposes. | ||
