summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2023-06-22 11:04:17 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2023-06-22 11:04:17 +0000
commit04d7c050d3ee6096ecf8aef2c7a758ea08cfc618 (patch)
tree7adf7a726fcb357e185858ec05afd749b447deb6 /lib/libc
parent57e5a31e1e1f06c1c861e59ab84bc2ccb1941eda (diff)
Allow to ask for deeper callers for leak reports using malloc options.
ok deraadt@
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdlib/malloc.326
-rw-r--r--lib/libc/stdlib/malloc.c15
2 files changed, 37 insertions, 4 deletions
diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3
index d8936260510..855f217c276 100644
--- a/lib/libc/stdlib/malloc.3
+++ b/lib/libc/stdlib/malloc.3
@@ -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.
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c
index 04f211c4eff..7bc660525bd 100644
--- a/lib/libc/stdlib/malloc.c
+++ b/lib/libc/stdlib/malloc.c
@@ -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;