This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/64504] Invalid free() with _GLIBCXX_DEBUG and -fwhole-program


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64504

--- Comment #3 from Andrey Vihrov <andrey.vihrov at gmail dot com> ---
I compiled the example program without and with -fwhole-program to assembly
code, and here are the differences: http://pastie.org/9859649

As I understand, normally the ".weak" directive ensures that there is only one
definition of std::string::_Rep::_S_empty_rep_storage per whole program. But
with -fwhole-program the .weak directive disappears, and instead another local
definition is created.

Looking at libstdc++ source code, I see

// The following storage is init'd to 0 by the linker, resulting
// (carefully) in an empty string with one reference.
static size_type _S_empty_rep_storage[];

. . .

// Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
// at static init time (before static ctors are run).
template<typename _CharT, typename _Traits, typename _Alloc>
typename basic_string<_CharT, _Traits, _Alloc>::size_type
basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
(sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
sizeof(size_type)];

I can get kind of the same difference in assembly by compiling this code:

template<typename T = void>
struct S
{
    static char arr[];
};

template<typename T>
char S<T>::arr[3];

int main()
{
    return S<>::arr[1];
}

Without -fwhole-program ".weak" and stuff is emitted, with the option the array
probably becomes static and so the program is compiled to "return 0".

So it looks like the problem here is that with -fwhole-program GCC does not
consider the possible existence of the same template instantiation in other
translation units.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]