Bug 372909

Summary: glibc 2.23+ fopen/printf change causes valgrind to report 1024 byte allocation
Product: [Developer tools] valgrind Reporter: David Rankin <drankinatty>
Component: generalAssignee: Julian Seward <jseward>
Status: RESOLVED NOT A BUG    
Severity: normal CC: mark, tom
Priority: NOR    
Version: 3.12 SVN   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:

Description David Rankin 2016-11-25 06:44:04 UTC
OS: Archlinux x86_64, valgrind 3.12, glibc 2.24

I'm not sure whether this has been previously reported, but with the change to printf in glibc 2.23, valgrind now reports 1024 bytes allocated when printf (or any of the printf family of function) are used. (It was explained to me as being related to the following mmap to malloc change: https://sourceware.org/bugzilla/show_bug.cgi?id=16734) This results in the unexpected reporting of memory allocated, where before the same code would report none. Example:

#include <stdio.h>

int main (int argc, char **argv) {

    char *s = argc > 1 ? argv[1] : "hello valgrind";
    printf ("%s\n", s);
    return 0;
}

Expected Heap Summary:

$ valgrind ./bin/hello
==9626== Memcheck, a memory error detector
==9626== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9626== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==9626== Command: ./bin/hello
<snip>
total heap usage: 0 allocs, 0 frees, 0 bytes allocated

Actual Heap Summary:

$ valgrind ./hello
==18061== Memcheck, a memory error detector
==18061== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18061== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==18061== Command: ./hello
<snip>
total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated

I don't know whether a new exclusion needs to be written or what, but I cannot see this being expected behavior from valgrind.
Comment 1 Tom Hughes 2016-11-25 07:09:43 UTC
What exactly is the problem here? Your program allocates and frees some memory, and valgrind accurately reports that.

This is no different to any other library function that you use happening to allocate some memory and is the expected behaviour.
Comment 2 David Rankin 2016-12-03 15:44:54 UTC
Really?? You don't see the difference??

total heap usage: 0 allocs, 0 frees, 0 bytes allocated  -- CORRECT

total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated -- FUBAR

Same code...
Comment 3 Mark Wielaard 2016-12-03 16:06:48 UTC
Whether or not different implementations of that function allocate and free memory is an implementation detail of that library. valgrind accurately reports the memory usage. If you believe the allocation of memory in your program is wrong because of the particular glibc printf implementation report a bug against glibc.