https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86873
Bug ID: 86873 Summary: "gcc -lmcheck" aborts on free when using posix_memalign Product: gcc Version: 4.9.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: ben at tawesoft dot co.uk Target Milestone: --- Created attachment 44513 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44513&action=edit Minimal example (.i version) Simple C programs using `posix_memalign()` and `free()`, compiled with `gcc -lmcheck`, cause the resulting binary to raise a runtime error such as `munmap_chunk(): invalid pointer`. For example, #include <stdlib.h> #include <stdio.h> int main() { void *p = 0; int ret = posix_memalign(&p, sizeof(void*), 0); printf("ret was %d, pointer is %p\n", ret, p); free(p); return 0; } The full example including headers is attached. Note that "POSIX requires that memory obtained from posix_memalign() can be freed using free". Output (my system): $ gcc test.c -lmcheck -ggdb $ ./a.out ret was 0, pointer is 0xcb6090 *** Error in `./a.out': munmap_chunk(): invalid pointer: 0x0000000000cb6060 *** Aborted Output (NVIDIA developer): $ gcc test.c -lmcheck -ggdb $ ./a.out ret was 0, pointer is 0x558c32d102e0 munmap_chunk(): invalid pointer Aborted (core dumped) Expected output: $ gcc test.c -lmcheck -ggdb $ ./a.out ret was 0, pointer is 0x<some-address> This was initially discovered due to posix_memalign's use in the Nvidia graphics driver -- discussion here, three people report same symptoms: https://devtalk.nvidia.com/default/topic/1037171/linux/memory-error-in-glx-programs-when-linked-with-gcc-lmcheck/ For me, here is the GCC version that raises this. I am aware this is an old version of GCC as provided by what is currently Debian odstable (Jessie), but others have reported the same results. $ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.9.2-10+deb8u1' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.9.2 (Debian 4.9.2-10+deb8u1) Obviously, I am using an old version of GCC but I hope this bug report may be of use to anyone using a search engine to lookup a problem with this possible lmcheck false positive.