On Tue, Sep 05, 2023 at 03:00:12PM +0900, Masato Asou wrote:
> From: Otto Moerbeek <[email protected]>
> Date: Tue, 5 Sep 2023 07:40:18 +0200
>
> > On Tue, Sep 05, 2023 at 09:38:40AM +0900, Masato Asou wrote:
> >
> >> hi,
> >>
> >> I have fixed a bug in Valgrind. The Valgrind could not detect access
> >> outside the range of malloc.
> >>
> >> comments, ok?
> >
> > This works much better that before. Thanks for working on this!
> >
> > It now detects out of bounds read and writes correctly. A double
> > free is detected.
> > Also, the spurious reports for accesses to errno are gone.
>
> Thank you for your comment.
>
> > It does not report proper locations though, even if I compile my test
> > program with -g:
>
> Could you show me your test program?
Here it is:
#include <stdlib.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
size_t sz = atoi(argv[1]);
unsigned char *p = malloc(sz);
printf("%p\n", p);
p[sz] = 0;
printf("%x\n", p[sz]);
free(p);
free(p);
return 0;
}
> --
> ASOU Masato
>
> > ==23912== Invalid read of size 1
> > ==23912== at 0x109B5D: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> > ==23912== Address 0x4a42840 is 0 bytes after a block of size 10,240 alloc'd
> > ==23912== at 0x493A3A9: malloc (vg_replace_malloc.c:435)
> > ==23912== by 0x109B32: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> > ==23912==
> > 0
> > ==23912== Invalid free() / delete / delete[] / realloc()
> > ==23912== at 0x493C981: free (vg_replace_malloc.c:978)
> > ==23912== by 0x109B80: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> > ==23912== Address 0x4a40040 is 0 bytes inside a block of size 10,240 free'd
> > ==23912== at 0x493C981: free (vg_replace_malloc.c:978)
> > ==23912== by 0x109B77: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> > ==23912== Block was alloc'd at
> > ==23912== at 0x493A3A9: malloc (vg_replace_malloc.c:435)
> > ==23912== by 0x109B32: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> >
> > addr2line -e ./a.out 0x109B80 also does not succeed in translating the
> > address.
> >
> > There are also a few spurious reports of the form:
> >
> > ==23912== Use of uninitialised value of size 8
> > ==23912== at 0x499518D: write (sys/w_write.c:26)
> > ==23912== by 0x49E9FB2: __sflush (stdio/fflush.c:80)
> > ==23912== by 0x49DA548: __sfvwrite (stdio/fvwrite.c:191)
> > ==23912== by 0x49E42ED: __sprint (stdio/vfprintf.c:108)
> > ==23912== by 0x49E42ED: __vfprintf (stdio/vfprintf.c:1064)
> > ==23912== by 0x49E0FA5: vfprintf (stdio/vfprintf.c:263)
> > ==23912== by 0x49F7C74: printf (stdio/printf.c:44)
> > ==23912== by 0x109B48: ??? (in ./a.out)
> > ==23912== by 0x1098D1: ??? (in ./a.out)
> >
> > But it is a great improvement,
> >
> > -Otto
> >
> >> --
> >> ASOU Masato
> >>
> >> Index: devel/valgrind/Makefile
> >> ===================================================================
> >> RCS file: /cvs/ports/devel/valgrind/Makefile,v
> >> retrieving revision 1.31
> >> diff -u -p -r1.31 Makefile
> >> --- devel/valgrind/Makefile 5 Aug 2023 03:32:22 -0000 1.31
> >> +++ devel/valgrind/Makefile 4 Sep 2023 23:14:33 -0000
> >> @@ -5,7 +5,7 @@ CATEGORIES = devel
> >>
> >> V = 3.21.0
> >> DISTNAME = valgrind-${V}
> >> -REVISION = 0
> >> +REVISION = 1
> >> EXTRACT_SUFX = .tar.bz2
> >>
> >> MASTER_SITES = https://sourceware.org/pub/valgrind/
> >> Index:
> >> devel/valgrind/patches/patch-coregrind_m_replacemalloc_vg_replace_malloc_c
> >> ===================================================================
> >> RCS file:
> >> devel/valgrind/patches/patch-coregrind_m_replacemalloc_vg_replace_malloc_c
> >> diff -N
> >> devel/valgrind/patches/patch-coregrind_m_replacemalloc_vg_replace_malloc_c
> >> --- /dev/null 1 Jan 1970 00:00:00 -0000
> >> +++
> >> devel/valgrind/patches/patch-coregrind_m_replacemalloc_vg_replace_malloc_c
> >> 4 Sep 2023 23:14:33 -0000
> >> @@ -0,0 +1,263 @@
> >> +--- coregrind/m_replacemalloc/vg_replace_malloc.c.orig
> >> ++++ coregrind/m_replacemalloc/vg_replace_malloc.c
> >> +@@ -222,7 +222,7 @@
> >> + #define SET_ERRNO_ENOMEM if (__errno_location) \
> >> + (*__errno_location ()) = VKI_ENOMEM;
> >> + #define SET_ERRNO_EINVAL {}
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + extern int *__error (void) __attribute__((weak));
> >> + #define SET_ERRNO_ENOMEM if (__error) \
> >> + (*__error ()) = VKI_ENOMEM;
> >> +@@ -430,7 +430,7 @@
> >> + ALLOC_or_NULL(VG_Z_LIBC_SONAME, malloc, malloc);
> >> + ALLOC_or_NULL(SO_SYN_MALLOC, malloc, malloc);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + ALLOC_or_NULL(VG_Z_LIBC_SONAME, malloc, malloc);
> >> + ALLOC_or_NULL(SO_SYN_MALLOC, malloc, malloc);
> >> +
> >> +@@ -472,7 +472,7 @@
> >> + ALLOC_or_BOMB(SO_SYN_MALLOC, _Znwm, __builtin_new);
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new(unsigned int)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_BOMB(VG_Z_LIBSTDCXX_SONAME, _Znwj, __builtin_new);
> >> +@@ -532,7 +532,7 @@
> >> + ALLOC_or_BOMB_ALIGNED(SO_SYN_MALLOC, _ZnwmSt11align_val_t,
> >> __builtin_new_aligned);
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new(unsigned int)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_BOMB_ALIGNED(VG_Z_LIBSTDCXX_SONAME, _ZnwjSt11align_val_t,
> >> __builtin_new_aligned);
> >> +@@ -592,7 +592,7 @@
> >> + ALLOC_or_NULL(SO_SYN_MALLOC, _ZnwmRKSt9nothrow_t,
> >> __builtin_new);
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new(unsigned, std::nothrow_t const&)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_NULL(VG_Z_LIBSTDCXX_SONAME, _ZnwjRKSt9nothrow_t,
> >> __builtin_new);
> >> +@@ -652,7 +652,7 @@
> >> + ALLOC_or_NULL_ALIGNED(SO_SYN_MALLOC,
> >> _ZnwmSt11align_val_tRKSt9nothrow_t, __builtin_new_aligned);
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new(unsigned int, std::align_val_t, std::nothrow_t const&)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_NULL_ALIGNED(VG_Z_LIBSTDCXX_SONAME,
> >> _ZnwjSt11align_val_tRKSt9nothrow_t, __builtin_new_aligned);
> >> +@@ -714,7 +714,7 @@
> >> + ALLOC_or_BOMB(SO_SYN_MALLOC, _Znam,
> >> __builtin_vec_new );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new[](unsigned int)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_BOMB(VG_Z_LIBSTDCXX_SONAME, _Znaj,
> >> __builtin_vec_new );
> >> +@@ -774,7 +774,7 @@
> >> + ALLOC_or_BOMB_ALIGNED(SO_SYN_MALLOC, _ZnamSt11align_val_t,
> >> __builtin_vec_new_aligned );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new[](unsigned int, std::align_val_t)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_BOMB_ALIGNED(VG_Z_LIBSTDCXX_SONAME, _ZnajSt11align_val_t,
> >> __builtin_vec_new_aligned );
> >> +@@ -835,7 +835,7 @@
> >> + ALLOC_or_NULL(SO_SYN_MALLOC, _ZnamRKSt9nothrow_t,
> >> __builtin_vec_new );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new[](unsigned, std::nothrow_t const&)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_NULL(VG_Z_LIBSTDCXX_SONAME, _ZnajRKSt9nothrow_t,
> >> __builtin_vec_new );
> >> +@@ -895,7 +895,7 @@
> >> + ALLOC_or_NULL_ALIGNED(SO_SYN_MALLOC,
> >> _ZnamSt11align_val_tRKSt9nothrow_t, __builtin_vec_new_aligned );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator new[](unsigned int, std::align_val_t, std::nothrow_t const&)
> >> + #if VG_WORDSIZE == 4
> >> + ALLOC_or_NULL_ALIGNED(VG_Z_LIBSTDCXX_SONAME,
> >> _ZnajSt11align_val_tRKSt9nothrow_t, __builtin_vec_new_aligned );
> >> +@@ -973,7 +973,7 @@
> >> + FREE(VG_Z_LIBC_SONAME, free, free );
> >> + FREE(SO_SYN_MALLOC, free, free );
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + FREE(VG_Z_LIBC_SONAME, free, free );
> >> + FREE(SO_SYN_MALLOC, free, free );
> >> +
> >> +@@ -1024,7 +1024,7 @@
> >> + FREE(VG_Z_LIBC_SONAME, _ZdlPv, __builtin_delete );
> >> + FREE(SO_SYN_MALLOC, _ZdlPv, __builtin_delete );
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + FREE(VG_Z_LIBSTDCXX_SONAME, _ZdlPv, __builtin_delete );
> >> + FREE(VG_Z_LIBCXX_SONAME, _ZdlPv, __builtin_delete );
> >> + FREE(SO_SYN_MALLOC, _ZdlPv, __builtin_delete );
> >> +@@ -1072,7 +1072,7 @@
> >> + DELETE_SIZED(SO_SYN_MALLOC, _ZdlPvm,
> >> __builtin_delete );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete(void*, unsigned int)
> >> + #if __SIZEOF_SIZE_T__ == 4
> >> + DELETE_SIZED(VG_Z_LIBSTDCXX_SONAME, _ZdlPvj,
> >> __builtin_delete );
> >> +@@ -1160,7 +1160,7 @@
> >> + DELETE_SIZED_ALIGNED(SO_SYN_MALLOC, _ZdlPvmSt11align_val_t,
> >> __builtin_delete_aligned );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete(void*, std::align_val_t)
> >> + DELETE_ALIGNED(VG_Z_LIBSTDCXX_SONAME, _ZdlPvSt11align_val_t,
> >> __builtin_delete_aligned );
> >> + DELETE_ALIGNED(VG_Z_LIBCXX_SONAME, _ZdlPvSt11align_val_t,
> >> __builtin_delete_aligned );
> >> +@@ -1224,7 +1224,7 @@
> >> + FREE(VG_Z_LIBC_SONAME, _ZdlPvRKSt9nothrow_t, __builtin_delete );
> >> + FREE(SO_SYN_MALLOC, _ZdlPvRKSt9nothrow_t, __builtin_delete );
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete(void*, std::nothrow_t const&)
> >> + FREE(VG_Z_LIBSTDCXX_SONAME, _ZdlPvRKSt9nothrow_t, __builtin_delete );
> >> + FREE(VG_Z_LIBCXX_SONAME, _ZdlPvRKSt9nothrow_t, __builtin_delete );
> >> +@@ -1254,7 +1254,7 @@
> >> +
> >> + // no sized version of this operator
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete(void*, std::align_val_t, std::nothrow_t const&)
> >> + DELETE_ALIGNED(VG_Z_LIBSTDCXX_SONAME,
> >> _ZdlPvSt11align_val_tRKSt9nothrow_t, __builtin_delete_aligned );
> >> + DELETE_ALIGNED(VG_Z_LIBCXX_SONAME,
> >> _ZdlPvSt11align_val_tRKSt9nothrow_t, __builtin_delete_aligned );
> >> +@@ -1288,7 +1288,7 @@
> >> + FREE(VG_Z_LIBC_SONAME, _ZdaPv, __builtin_vec_delete
> >> );
> >> + FREE(SO_SYN_MALLOC, _ZdaPv, __builtin_vec_delete
> >> );
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete[](void*)
> >> + FREE(VG_Z_LIBSTDCXX_SONAME, _ZdaPv, __builtin_vec_delete
> >> );
> >> + FREE(VG_Z_LIBCXX_SONAME, _ZdaPv, __builtin_vec_delete
> >> );
> >> +@@ -1323,7 +1323,7 @@
> >> + DELETE_SIZED(SO_SYN_MALLOC, _ZdaPvm,
> >> __builtin_vec_delete );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete[](void*, unsigned int)
> >> + #if __SIZEOF_SIZE_T__ == 4
> >> + DELETE_SIZED(VG_Z_LIBSTDCXX_SONAME, _ZdaPvj,
> >> __builtin_vec_delete );
> >> +@@ -1383,7 +1383,7 @@
> >> + DELETE_SIZED_ALIGNED(SO_SYN_MALLOC, _ZdaPvmSt11align_val_t,
> >> __builtin_vec_delete_aligned );
> >> + #endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete[](void*, std::align_val_t)
> >> + DELETE_ALIGNED(VG_Z_LIBSTDCXX_SONAME, _ZdaPvSt11align_val_t,
> >> __builtin_vec_delete_aligned );
> >> + DELETE_ALIGNED(VG_Z_LIBCXX_SONAME, _ZdaPvSt11align_val_t,
> >> __builtin_vec_delete_aligned );
> >> +@@ -1447,7 +1447,7 @@
> >> + FREE(VG_Z_LIBC_SONAME, _ZdaPvRKSt9nothrow_t, __builtin_vec_delete
> >> );
> >> + FREE(SO_SYN_MALLOC, _ZdaPvRKSt9nothrow_t, __builtin_vec_delete
> >> );
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete[](void*, std::nothrow_t const&)
> >> + FREE(VG_Z_LIBSTDCXX_SONAME, _ZdaPvRKSt9nothrow_t, __builtin_vec_delete
> >> );
> >> + FREE(VG_Z_LIBCXX_SONAME, _ZdaPvRKSt9nothrow_t, __builtin_vec_delete
> >> );
> >> +@@ -1477,7 +1477,7 @@
> >> +
> >> + // no sized version of this operator
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + // operator delete[](void*, std::align_val_t, std::nothrow_t const&)
> >> + DELETE_ALIGNED(VG_Z_LIBSTDCXX_SONAME,
> >> _ZdaPvSt11align_val_tRKSt9nothrow_t, __builtin_vec_delete_aligned );
> >> + DELETE_ALIGNED(VG_Z_LIBCXX_SONAME,
> >> _ZdaPvSt11align_val_tRKSt9nothrow_t, __builtin_vec_delete_aligned );
> >> +@@ -1553,7 +1553,7 @@
> >> + CALLOC(VG_Z_LIBC_SONAME, calloc);
> >> + CALLOC(SO_SYN_MALLOC, calloc);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + CALLOC(VG_Z_LIBC_SONAME, calloc);
> >> + CALLOC(SO_SYN_MALLOC, calloc);
> >> +
> >> +@@ -1648,7 +1648,7 @@
> >> + REALLOC(VG_Z_LIBC_SONAME, realloc);
> >> + REALLOC(SO_SYN_MALLOC, realloc);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + REALLOC(VG_Z_LIBC_SONAME, realloc);
> >> + REALLOC(SO_SYN_MALLOC, realloc);
> >> + REALLOCF(VG_Z_LIBC_SONAME, reallocf);
> >> +@@ -1734,7 +1734,7 @@
> >> + return v; \
> >> + }
> >> +
> >> +-#if defined(VGO_freebsd)
> >> ++#if defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + #define VG_MEMALIGN_MAKE_SIZE_MULTIPLE_ALIGN 1
> >> + #else
> >> + #define VG_MEMALIGN_MAKE_SIZE_MULTIPLE_ALIGN 0
> >> +@@ -1834,7 +1834,7 @@
> >> + MEMALIGN(VG_Z_LIBC_SONAME, memalign);
> >> + MEMALIGN(SO_SYN_MALLOC, memalign);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + MEMALIGN(VG_Z_LIBC_SONAME, memalign);
> >> + MEMALIGN(SO_SYN_MALLOC, memalign);
> >> +
> >> +@@ -1890,7 +1890,7 @@
> >> + VALLOC(VG_Z_LIBC_SONAME, valloc);
> >> + VALLOC(SO_SYN_MALLOC, valloc);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + VALLOC(VG_Z_LIBC_SONAME, valloc);
> >> + VALLOC(SO_SYN_MALLOC, valloc);
> >> +
> >> +@@ -2031,7 +2031,7 @@
> >> + POSIX_MEMALIGN(VG_Z_LIBC_SONAME, posix_memalign);
> >> + POSIX_MEMALIGN(SO_SYN_MALLOC, posix_memalign);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + POSIX_MEMALIGN(VG_Z_LIBC_SONAME, posix_memalign);
> >> + POSIX_MEMALIGN(SO_SYN_MALLOC, posix_memalign);
> >> +
> >> +@@ -2176,7 +2176,7 @@
> >> + ALIGNED_ALLOC(VG_Z_LIBC_SONAME, aligned_alloc);
> >> + ALIGNED_ALLOC(SO_SYN_MALLOC, aligned_alloc);
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + ALIGNED_ALLOC(G_Z_LIBC_SONAME, aligned_alloc);
> >> + ALIGNED_ALLOC(SO_SYN_MALLOC, aligned_alloc);
> >> +
> >> +@@ -2220,7 +2220,7 @@
> >> + MALLOC_USABLE_SIZE(SO_SYN_MALLOC, dlmalloc_usable_size);
> >> + # endif
> >> +
> >> +-#elif defined(VGO_freebsd)
> >> ++#elif defined(VGO_freebsd) || defined(VGO_openbsd)
> >> + MALLOC_USABLE_SIZE(VG_Z_LIBC_SONAME, malloc_usable_size);
> >> + MALLOC_USABLE_SIZE(SO_SYN_MALLOC, malloc_usable_size);
> >> +
> >>
> >