Hi,
I have modified diff. comments, ok?
$ MALLOC_OPTIONS=D ktrace -tu ./a.out 1073490000
a.out(99781) in unknown(): putleakinfo(): Cannot allocate memory
--
ASOU Masato
Index: stdlib/malloc.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.289
diff -u -p -r1.289 malloc.c
--- stdlib/malloc.c 30 Jun 2023 06:24:58 -0000 1.289
+++ stdlib/malloc.c 7 Sep 2023 20:30:01 -0000
@@ -344,6 +344,22 @@ wrterror(struct dir_info *d, char *msg,
}
static void
+wrtwarning(char *func, char *msg, ...)
+{
+ int saved_errno = errno;
+ va_list ap;
+
+ dprintf(STDERR_FILENO, "%s(%d) in %s(): ", __progname,
+ getpid(), func == NULL ? func : "unknown");
+ va_start(ap, msg);
+ vdprintf(STDERR_FILENO, msg, ap);
+ va_end(ap);
+ dprintf(STDERR_FILENO, "\n");
+
+ errno = saved_errno;
+}
+
+static void
rbytes_init(struct dir_info *d)
{
arc4random_buf(d->rbytes, sizeof(d->rbytes));
@@ -2353,8 +2369,11 @@ putleakinfo(struct leaktree *leaks, void
if (page == NULL ||
used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) {
page = MMAP(MALLOC_PAGESIZE, 0);
- if (page == MAP_FAILED)
+ if (page == MAP_FAILED) {
+ wrtwarning(f, "%s(): %s", __func__,
+ strerror(errno));
return;
+ }
used = 0;
}
p = &page[used++];
From: Otto Moerbeek <[email protected]>
Date: Thu, 7 Sep 2023 12:43:04 +0200
> On Thu, Sep 07, 2023 at 06:45:55PM +0900, Masato Asou wrote:
>
>> Hi,
>>
>> I am using MALLOC_OPTIONS=D and kdump report no information when
>> malloc() exceeds 1073500000 bytes on my OpenBSD box. I have
>> investigated and found that mmap() faild in putleakinfo().
>
> I'm ok with the general idea, but see inline
>
>>
>>
>> I used the following program:
>>
>> #include <err.h>
>> #include <limits.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <sys/types.h>
>>
>> int
>> main(int argc, char *argv[])
>> {
>> size_t size;
>> void *buf;
>> char *end;
>>
>> if (argc != 2) {
>> fprintf(stderr, "usage: ./a.out size\n");
>> exit(1);
>> }
>>
>> size = strtol(argv[1], &end, 0);
>> if (argv[1] == end) {
>> fprintf(stderr, "Invalid argument: %s\n", argv[1]);
>> exit(1);
>> }
>> if (*end == 'k')
>> size *= 1024;
>> else if (*end == 'm')
>> size *= 1024 * 1024;
>> else if (*end == 'g')
>> size *= 1024 * 1024 * 1024;
>>
>> if ((buf = malloc(size)) == NULL)
>> err(1, "malloc");
>>
>> return (0);
>> }
>>
>> I used this program to perform the following:
>>
>> $ MALLOC_OPTIONS=D ktrace -tu ./a.out 1073490000
>> $ kdump -u malloc
>> ******** Start dump a.out *******
>> M=8 I=1 F=0 U=0 J=1 R=0 X=0 C=0 cache=64 G=0
>> Leak report:
>> f sum # avg
>> 0xeb017e21c6f 1073490000 1 1073490000 addr2line -e ./a.out
>> 0x1c6f
>>
>> ******** End dump a.out *******
>> $ MALLOC_OPTIONS=D ktrace -tu ./a.out 1073500000
>> $ kdump -u malloc
>> $ ls -l ktrace.out
>> -rw------- 1 asou asou 64 Sep 7 18:38 ktrace.out
>> $ export LD_LIBRARY_PATH=/usr/obj/lib/libc
>> $ MALLOC_OPTIONS=D ktrace -tu ./a.out 1073500000
>> /usr/src/lib/libc/stdlib/malloc.c: putleakinfo: Cannot allocate memory
>> $
>>
>>
>> In such a case, shouldn't an error message be displayed? I made a
>> patch for print error message if mmap() was failed.
>>
>> comments, ok?
>> --
>> ASOU Masato
>>
>> Index: lib/libc/stdlib/malloc.c
>> ===================================================================
>> RCS file: /cvs/src/lib/libc/stdlib/malloc.c,v
>> retrieving revision 1.289
>> diff -u -p -r1.289 malloc.c
>> --- lib/libc/stdlib/malloc.c 30 Jun 2023 06:24:58 -0000 1.289
>> +++ lib/libc/stdlib/malloc.c 7 Sep 2023 09:44:59 -0000
>> @@ -2354,7 +2354,14 @@ putleakinfo(struct leaktree *leaks, void
>> used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) {
>> page = MMAP(MALLOC_PAGESIZE, 0);
>> if (page == MAP_FAILED)
>> + {
>
> Brace should be after if (... )
>
>> + char buf[256];
>> +
>> + snprintf(buf, sizeof (buf), "%s: %s: %s\n",
>> + __FILE__, __func__, strerror(errno));
>> + write(STDERR_FILENO, buf, strlen(buf));
>> return;
>
> Please format the message to be like what wrterror() does (without the
> abort of ccurse), using dprintf().
>
> -Otto
>
>> + }
>> used = 0;
>> }
>> p = &page[used++];
>>
>