Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Oct 2020 14:59:56 +0000
From:      bugzilla-noreply@freebsd.org
To:        ports-bugs@FreeBSD.org
Subject:   [Bug 250345] devel/valgrind: 3.17.0-GIT valgrind not picking up right malloc on override
Message-ID:  <bug-250345-7788@https.bugs.freebsd.org/bugzilla/>

next in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D250345

            Bug ID: 250345
           Summary: devel/valgrind: 3.17.0-GIT valgrind not picking up
                    right malloc on override
           Product: Ports & Packages
           Version: Latest
          Hardware: amd64
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: Individual Port(s)
          Assignee: zeising@FreeBSD.org
          Reporter: karnajitw@gmail.com
          Assignee: zeising@FreeBSD.org
             Flags: maintainer-feedback?(zeising@FreeBSD.org)

Below is an example where I am calling __malloc from my own malloc function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void *__malloc(unsigned long);
void __free(void *);

void *malloc(size_t sz)
{
  void *ptr =3D NULL;
  size_t origsz =3D sz + 4;
  ptr =3D __malloc(origsz);
  *((int *)ptr) =3D sz;
  write(1, "malloc called\n", 14);
  return ptr + 4;
}

void myfree(void *ptr)
{
  void *orig_ptr =3D ptr - 4;
  write(1, "free called\n", 12);
  __free(orig_ptr);
}

int main()
{
  char *str1 =3D (char *)malloc(100);
  memcpy(str1, "Hello World", 12);
  myfree(str1);

  return 0;
}

Without valgrind the result looks like this

# ./a.out
malloc called
free called

With valgrind its like this

# valgrind ./a.out
=3D=3D14188=3D=3D Memcheck, a memory error detector
=3D=3D14188=3D=3D Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward =
et al.
=3D=3D14188=3D=3D Using Valgrind-3.17.0.GIT and LibVEX; rerun with -h for c=
opyright
info
=3D=3D14188=3D=3D Command: ./a.out
=3D=3D14188=3D=3D
=3D=3D14188=3D=3D Warning: set address range perms: large range [0x7fffdfff=
f000,
0x7ffffffdf000) (noaccess)
free called
=3D=3D14188=3D=3D Invalid free() / delete / delete[] / realloc()
=3D=3D14188=3D=3D    at 0x485068E: free
(src/paul-floyd-317-fbsd12/valgrind-freebsd/coregrind/m_replacemalloc/vg_re=
place_malloc.c:611)
=3D=3D14188=3D=3D    by 0x20162F: myfree (malloc_free.c:23)
=3D=3D14188=3D=3D    by 0x20167E: main (malloc_free.c:30)
=3D=3D14188=3D=3D  Address 0x540003c is 4 bytes before a block of size 100 =
alloc'd
=3D=3D14188=3D=3D    at 0x484F4B9: malloc
(src/paul-floyd-317-fbsd12/valgrind-freebsd/coregrind/m_replacemalloc/vg_re=
place_malloc.c:312)
=3D=3D14188=3D=3D    by 0x201658: main (malloc_free.c:28)
=3D=3D14188=3D=3D
=3D=3D14188=3D=3D
=3D=3D14188=3D=3D HEAP SUMMARY:
=3D=3D14188=3D=3D     in use at exit: 100 bytes in 1 blocks
=3D=3D14188=3D=3D   total heap usage: 1 allocs, 1 frees, 100 bytes allocated
=3D=3D14188=3D=3D
=3D=3D14188=3D=3D LEAK SUMMARY:
=3D=3D14188=3D=3D    definitely lost: 100 bytes in 1 blocks
=3D=3D14188=3D=3D    indirectly lost: 0 bytes in 0 blocks
=3D=3D14188=3D=3D      possibly lost: 0 bytes in 0 blocks
=3D=3D14188=3D=3D    still reachable: 0 bytes in 0 blocks
=3D=3D14188=3D=3D         suppressed: 0 bytes in 0 blocks
=3D=3D14188=3D=3D Rerun with --leak-check=3Dfull to see details of leaked m=
emory
=3D=3D14188=3D=3D
=3D=3D14188=3D=3D For lists of detected and suppressed errors, rerun with: =
-s
=3D=3D14188=3D=3D ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 fr=
om 0)

Only myfree is called in valgrind env and additional invalid free error cam=
e as
a result of this. Please verify. overriding reallocf doesn't have this issue
under valgrind.

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-250345-7788>