Description of problem: 1. Exec ./libc_mcheck_03 ,according to the code, the execution should cause the memory double free and fail, but with glibc-2.27 the program succeed. 2. Set the environment variable MALLOC_CHECK_=1 (MALLOC_CHECK_=1 in mallopt man means "Print a detailed error message and continue execution",see http://man7.org/linux/man-pages/man3/mallopt.3.html),the execution should print a detailed error message and continue execution, but with glibc-2.27 the program aborted. Version-Release number of selected component (if applicable): OS-version: Fedora 28 glibc-version: glibc 2.27 How reproducible: Steps to Reproduce: 1.# ./libc_mcheck_03 2.# echo $? 3.# export MALLOC_CHECK_=1 4.# ./libc_mcheck_03 5.# echo $? 6.here is the libc_mcheck_03.c #include <stdio.h> #include <dlfcn.h> #include <errno.h> #include <string.h> int TC_RET=0; int main(void){ char *ptr1, *ptr2; ptr1 = (char*)malloc(512); ptr2 = (char*)malloc(512); if (ptr1 == NULL || ptr2 == NULL){ printf("Error: malloc fail\n"); TC_RET++; } else{ printf("Pass: malloc success\n"); *ptr1 = 'a'; *ptr2 = 'b'; } ptr2 = ptr1; printf("Info: ptr2 = ptr1\n"); free(ptr2); free(ptr1); printf("Info: after free memory\n"); return TC_RET; } Actual results: # ./libc_mcheck_03 Pass: malloc success Info: ptr2 = ptr1 Info: after free memory # echo $? 0 # export MALLOC_CHECK_=1 # ./libc_mcheck_03 Pass: malloc success Info: ptr2 = ptr1 free(): invalid pointer Aborted # echo $? 134 Expected results: # ./libc_mcheck_03 Pass: malloc success Info: ptr2 = ptr1 free(): invalid pointer Aborted # echo $? 134 # export MALLOC_CHECK_=1 # ./libc_mcheck_03 Pass: malloc success Info: ptr2 = ptr1 *** Error in `./libc_mcheck_03': free(): invalid pointer: 0x00000000011a0010 *** Info: after free memory # echo $? 0 Additional info: I have tried this execution on glibc-2.25(fedora 26), there was no problem. And i tried this execution on glic-2.26(fedora 27), only "Description of problem: 1" occured. Then i tried this execution on glic-2.27(fedora 28), both description 1 and 2 occured. The problem occurs with the glibc update, i want to use the glibc with no problem on the new version. Can any one fix this problem, thanks very much.
I think MALLOC_CHECK_=1 should disable the tcache. DJ, what do you think?
I think it makes sense to bypass the tcache when debugging malloc-using applications (so that mcheck sees all the activity), but that doesn't preclude the need to fix tcache to detect these problems too.
(In reply to DJ Delorie from comment #2) > I think it makes sense to bypass the tcache when debugging malloc-using > applications (so that mcheck sees all the activity), but that doesn't > preclude the need to fix tcache to detect these problems too. Thanks a lot for your and Florian's comment. I can't understand the relationship between my question and your reply about 'tcache'.(what is the tcache?) as `man 3p free` saying: ``` Any use of a pointer that refers to freed space results in undefined behavior. ``` All I want to know is whether glic-2.27 do some special changes about both the MALLOC_CHECK_=1 and MALLOC_CHECK_=0 on the 'undefined behavior' compared to older version . if any,what is the changes? and are they reasonable? So I can fix my testcase according to the new undefined behavior Hope that helps.
tcache is a per-thread cache added to malloc in glibc 2.26. It sits between the application and the core allocator, so it can affect how some things behave. Since _MALLOC_CHECK is implemented in the core allocator, the tcache causes some malloc/free calls to not be seen by _MALLOC_CHECK.
tcache havs chanege the default behavior of freeing the freed memory. Are the changes acceptable and reasonable? What's your opinion?
(In reply to Shuai Song from comment #5) > tcache havs chanege the default behavior of freeing the freed memory. > Are the changes acceptable and reasonable? What's your opinion? Yes, the changes are acceptable. The tcache performance improvement is important and the behaviour of a double-free is undefined and so we make use of this to provide as much performance as possible. DJ has just posted a ligh-weight check for double-free from the same thread: https://www.sourceware.org/ml/libc-alpha/2018-11/msg00118.html So there are some improvements we can make, but they will be limited and we don't want to reduce performance.
I'm moving this bug to Rawhide and we'll fix it there, but we will not backport to Fedora 28. We may backport this to Fedora 29 depending on the final form of the changes.
glibc-2.28-23.fc29 has been submitted as an update to Fedora 29. https://bodhi.fedoraproject.org/updates/FEDORA-2018-c69aee3e63
glibc-2.28-23.fc29 has been pushed to the Fedora 29 testing repository. If problems still persist, please make note of it in this bug report. See https://fedoraproject.org/wiki/QA:Updates_Testing for instructions on how to install test updates. You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2018-c69aee3e63
glibc-2.28-23.fc29 has been pushed to the Fedora 29 stable repository. If problems still persist, please make note of it in this bug report.
glibc-2.27-36.fc28 has been submitted as an update to Fedora 28. https://bodhi.fedoraproject.org/updates/FEDORA-2018-2efb53dc71
glibc-2.27-36.fc28 has been pushed to the Fedora 28 testing repository. If problems still persist, please make note of it in this bug report. See https://fedoraproject.org/wiki/QA:Updates_Testing for instructions on how to install test updates. You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2018-2efb53dc71
glibc-2.27-37.fc28 has been pushed to the Fedora 28 testing repository. If problems still persist, please make note of it in this bug report. See https://fedoraproject.org/wiki/QA:Updates_Testing for instructions on how to install test updates. You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2018-2efb53dc71
glibc-2.27-37.fc28 has been pushed to the Fedora 28 stable repository. If problems still persist, please make note of it in this bug report.