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 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)
+                       {
+                               char buf[256];
+
+                               snprintf(buf, sizeof (buf), "%s: %s: %s\n",
+                                   __FILE__, __func__, strerror(errno));
+                               write(STDERR_FILENO, buf, strlen(buf));
                                return;
+                       }
                        used = 0;
                }
                p = &page[used++];

Reply via email to