This commit is contained in:
purplerain 2023-06-22 18:19:48 +00:00
parent 3f8ae0d130
commit be633fa56a
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
84 changed files with 420 additions and 347 deletions

View file

@ -30,9 +30,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $OpenBSD: malloc.3,v 1.133 2023/06/04 06:58:33 otto Exp $
.\" $OpenBSD: malloc.3,v 1.134 2023/06/22 11:04:16 otto Exp $
.\"
.Dd $Mdocdate: June 4 2023 $
.Dd $Mdocdate: June 22 2023 $
.Dt MALLOC 3
.Os
.Sh NAME
@ -294,6 +294,17 @@ To record the dump:
To view the leak report:
.Pp
.Dl $ kdump -u malloc ...
.Pp
By default, the immediate caller of a
.Nm
function will be recorded.
Use malloc options
.Cm 2
or
.Cm 3
to record the caller one or two stack frames deeper instead.
These malloc options imply
.Cm D .
.It Cm F
.Dq Freecheck .
Enable more extensive double free and use after free detection.
@ -813,3 +824,14 @@ and
.Fn realloc
to avoid these problems on
.Ox .
.Pp
The mechanism to record caller functions when using malloc options
.Cm 2
or
.Cm 3
is not guaranteed to work for all platforms, compilers or compilation
options,
and might even crash your program.
Use
.Em only
for debugging purposes.

View file

@ -1,4 +1,4 @@
/* $OpenBSD: malloc.c,v 1.286 2023/06/07 12:56:22 aoyama Exp $ */
/* $OpenBSD: malloc.c,v 1.287 2023/06/22 11:04:16 otto Exp $ */
/*
* Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net>
* Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@ -255,7 +255,11 @@ void malloc_dump(void);
PROTO_NORMAL(malloc_dump);
static void malloc_exit(void);
#endif
#define CALLER (DO_STATS ? __builtin_return_address(0) : NULL)
#define CALLER ( \
DO_STATS == 0 ? NULL : (__builtin_extract_return_addr( \
DO_STATS == 1 ? __builtin_return_address(0) : \
DO_STATS == 2 ? __builtin_return_address(1) : \
DO_STATS == 3 ? __builtin_return_address(2) : NULL)))
/* low bits of r->p determine size: 0 means >= page size and r->size holding
* real size, otherwise low bits is the bucket + 1
@ -365,8 +369,15 @@ omalloc_parseopt(char opt)
mopts.malloc_stats = 0;
break;
case 'D':
case '1':
mopts.malloc_stats = 1;
break;
case '2':
mopts.malloc_stats = 2;
break;
case '3':
mopts.malloc_stats = 3;
break;
#endif /* MALLOC_STATS */
case 'f':
mopts.malloc_freecheck = 0;