summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorotto <>2023-06-22 11:04:16 +0000
committerotto <>2023-06-22 11:04:16 +0000
commit9f41a64eb230fd1c343999400564e3e14747c3e9 (patch)
treefeea18069b28cbfa101b72240970a4afce3dde4b
parent4c0df1809176af2c27699157cf128a4348f6daa5 (diff)
downloadopenbsd-9f41a64eb230fd1c343999400564e3e14747c3e9.tar.gz
openbsd-9f41a64eb230fd1c343999400564e3e14747c3e9.tar.bz2
openbsd-9f41a64eb230fd1c343999400564e3e14747c3e9.zip
Allow to ask for deeper callers for leak reports using malloc options.
ok deraadt@
-rw-r--r--src/lib/libc/stdlib/malloc.326
-rw-r--r--src/lib/libc/stdlib/malloc.c15
2 files changed, 37 insertions, 4 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index d893626051..855f217c27 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,9 +30,9 @@
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.133 2023/06/04 06:58:33 otto Exp $ 33.\" $OpenBSD: malloc.3,v 1.134 2023/06/22 11:04:16 otto Exp $
34.\" 34.\"
35.Dd $Mdocdate: June 4 2023 $ 35.Dd $Mdocdate: June 22 2023 $
36.Dt MALLOC 3 36.Dt MALLOC 3
37.Os 37.Os
38.Sh NAME 38.Sh NAME
@@ -294,6 +294,17 @@ To record the dump:
294To view the leak report: 294To view the leak report:
295.Pp 295.Pp
296.Dl $ kdump -u malloc ... 296.Dl $ kdump -u malloc ...
297.Pp
298By default, the immediate caller of a
299.Nm
300function will be recorded.
301Use malloc options
302.Cm 2
303or
304.Cm 3
305to record the caller one or two stack frames deeper instead.
306These malloc options imply
307.Cm D .
297.It Cm F 308.It Cm F
298.Dq Freecheck . 309.Dq Freecheck .
299Enable more extensive double free and use after free detection. 310Enable more extensive double free and use after free detection.
@@ -813,3 +824,14 @@ and
813.Fn realloc 824.Fn realloc
814to avoid these problems on 825to avoid these problems on
815.Ox . 826.Ox .
827.Pp
828The mechanism to record caller functions when using malloc options
829.Cm 2
830or
831.Cm 3
832is not guaranteed to work for all platforms, compilers or compilation
833options,
834and might even crash your program.
835Use
836.Em only
837for debugging purposes.
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 04f211c4ef..7bc660525b 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.286 2023/06/07 12:56:22 aoyama Exp $ */ 1/* $OpenBSD: malloc.c,v 1.287 2023/06/22 11:04:16 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> 4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -255,7 +255,11 @@ void malloc_dump(void);
255PROTO_NORMAL(malloc_dump); 255PROTO_NORMAL(malloc_dump);
256static void malloc_exit(void); 256static void malloc_exit(void);
257#endif 257#endif
258#define CALLER (DO_STATS ? __builtin_return_address(0) : NULL) 258#define CALLER ( \
259 DO_STATS == 0 ? NULL : (__builtin_extract_return_addr( \
260 DO_STATS == 1 ? __builtin_return_address(0) : \
261 DO_STATS == 2 ? __builtin_return_address(1) : \
262 DO_STATS == 3 ? __builtin_return_address(2) : NULL)))
259 263
260/* low bits of r->p determine size: 0 means >= page size and r->size holding 264/* low bits of r->p determine size: 0 means >= page size and r->size holding
261 * real size, otherwise low bits is the bucket + 1 265 * real size, otherwise low bits is the bucket + 1
@@ -365,8 +369,15 @@ omalloc_parseopt(char opt)
365 mopts.malloc_stats = 0; 369 mopts.malloc_stats = 0;
366 break; 370 break;
367 case 'D': 371 case 'D':
372 case '1':
368 mopts.malloc_stats = 1; 373 mopts.malloc_stats = 1;
369 break; 374 break;
375 case '2':
376 mopts.malloc_stats = 2;
377 break;
378 case '3':
379 mopts.malloc_stats = 3;
380 break;
370#endif /* MALLOC_STATS */ 381#endif /* MALLOC_STATS */
371 case 'f': 382 case 'f':
372 mopts.malloc_freecheck = 0; 383 mopts.malloc_freecheck = 0;