Bug 209886 - devel/valgrind: spurious invalid free() when using aligned_alloc()
Summary: devel/valgrind: spurious invalid free() when using aligned_alloc()
Status: New
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: amd64 Any
: --- Affects Some People
Assignee: Niclas Zeising
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-31 11:47 UTC by Robert Clausecker
Modified: 2020-03-28 14:19 UTC (History)
2 users (show)

See Also:
bugzilla: maintainer-feedback? (bdrewery)


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Clausecker 2016-05-31 11:47:14 UTC
The simple program

    #include <stdlib.h>

    int main()
    {
	    free(aligned_alloc(8, 8));
	    return (0);
    }

causes valgrind(1) to complain about a spurious invalid free():


    ==1557== Memcheck, a memory error detector
    ==1557== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
    ==1557== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
    ==1557== Command: ./a
    ==1557== 
    ==1557== Invalid free() / delete / delete[] / realloc()
    ==1557==    at 0x4C232BC: free (in /usr/local/lib/valgrind/vgpreload_memcheck-amd64-freebsd.so)
    ==1557==    by 0x4007CB: main (in /tmp/a)
    ==1557==  Address 0x5806058 is not stack'd, malloc'd or (recently) free'd
    ==1557== 
    ==1557== 
    ==1557== HEAP SUMMARY:
    ==1557==     in use at exit: 0 bytes in 0 blocks
    ==1557==   total heap usage: 0 allocs, 1 frees, 0 bytes allocated
    ==1557== 
    ==1557== All heap blocks were freed -- no leaks are possible
    ==1557== 
    ==1557== For counts of detected and suppressed errors, rerun with: -v
    ==1557== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

I suspect that valgrind doesn't properly intercept aligned_alloc(3) and thus doesn't
recognize the address when it is passed to free(). This issue does not occur on Linux,
I suspect it comes from the interaction between jemalloc(3) and valgrind.
Comment 1 Walter Schwarzenfeld freebsd_triage 2018-01-14 01:22:29 UTC
Maintainer feedback?
Comment 2 Walter Schwarzenfeld freebsd_triage 2018-03-07 16:27:16 UTC
see also Bug #220943.
Comment 3 Walter Schwarzenfeld freebsd_triage 2019-03-09 08:36:07 UTC
Maintainership dropped ports r495096.
Comment 4 Walter Schwarzenfeld freebsd_triage 2019-03-10 11:46:23 UTC
Assign to new maintainer.
Comment 5 Paul Floyd 2020-03-06 21:06:38 UTC
This feature is not yet supported in mainline Valgrind. See

https://bugs.kde.org/show_bug.cgi?id=407589

I'll look into adding a patch for this.
Comment 6 Paul Floyd 2020-03-28 13:49:58 UTC
I created a patch (see the kde.org bugzilla item), but it doesn't work well on Linux because aligned_alloc and memalign alias the same function in libc. I'll check on FreeBSD.
Comment 7 Paul Floyd 2020-03-28 14:19:13 UTC
On FreeBSD I get the output below, so this would be a worthwhile change.

==9965== Memcheck, a memory error detector
==9965== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9965== Using Valgrind-3.16.0.GIT and LibVEX; rerun with -h for copyright info
==9965== Command: ./aligned_alloc
==9965== 
default-aligned addr:   0x5800040
1024-byte aligned addr: 0x5801400
==9965== 
==9965== HEAP SUMMARY:
==9965==     in use at exit: 4,096 bytes in 1 blocks
==9965==   total heap usage: 3 allocs, 2 frees, 8,232 bytes allocated
==9965== 
==9965== LEAK SUMMARY:
==9965==    definitely lost: 0 bytes in 0 blocks
==9965==    indirectly lost: 0 bytes in 0 blocks
==9965==      possibly lost: 0 bytes in 0 blocks
==9965==    still reachable: 4,096 bytes in 1 blocks
==9965==         suppressed: 0 bytes in 0 blocks
==9965== Rerun with --leak-check=full to see details of leaked memory
==9965== 
==9965== For lists of detected and suppressed errors, rerun with: -s
==9965== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)