Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11699 closed Bugs (fixed)

Forward declarations of std templates causes stack corruption under Visual Studio 2015

Reported by: Attila Mitrócsák <attila.mitrocsak@…> Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: interprocess
Version: Boost 1.59.0 Severity: Problem
Keywords: Cc:

Description

In Visual Studio 2015 it can lead to stack corruption if I instantiate std::basic_istream with the same types in two compilation units, and one of them forward declares the template and the other doesn't.

I originally run into this problem when I called a funtion in a library that called boost::lexical_cast. The source file containing the function included a header from interprocess, which included interprocess/detail/std_fwd.hpp which contains a forward declaration of std::basic_istream.

Unfortunately std::basic_istream was also instatiated with the same types in my main program (but there was no forward declaration).

I presume this can happen with other templates as well, but so far this is the only one I found.

The same code was working fine with Visual Studio 2013.

I created two small solutions:

  • BoostStackCorruption_VS2015: using boost::lexical_cast and an interprocess header
  • StdStackCorruption_VS2015: using only std but with lines coming from the insides of lexical_cast and interprocess/detail/std_fwd.hpp

Attachments (2)

StdStackCorruption_VS2015.zip (6.2 KB ) - added by Attila Mitrócsák <attila.mitrocsak@…> 7 years ago.
Stack corruption with std only
BoostStackCorruption_VS2015.zip (5.3 KB ) - added by Attila Mitrócsák <attila.mitrocsak@…> 7 years ago.
Stack corruption with boost

Download all attachments as: .zip

Change History (7)

by Attila Mitrócsák <attila.mitrocsak@…>, 7 years ago

Stack corruption with std only

by Attila Mitrócsák <attila.mitrocsak@…>, 7 years ago

Stack corruption with boost

comment:1 by Ion Gaztañaga, 7 years ago

Thanks for the report. It seems that it is a compiler bug, and standard libraries before Visual 2013 used some dummy virtual functions in basic_istream and basic_ostream to fix it. Starting with Visual 2013 <istream> and <ostream> contain a pragma to fix the bug with the comment:

 #pragma vtordisp(push, 2) // compiler bug workaround

The fix is to use the same pragma when forward declaring the basic_istream and basic_ostream.

Thanks a lot for the detailed report and MSVC solutions, they helped a lot.

Last edited 7 years ago by Ion Gaztañaga (previous) (diff)

comment:2 by Ion Gaztañaga, 7 years ago

Resolution: fixed
Status: newclosed

Fixed in develop branch, soon to be merged to master. Commit:

https://github.com/boostorg/interprocess/commit/845d352287902b535b75b5545ff448744fa79820

comment:3 by Attila Mitrócsák <attila.mitrocsak@…>, 7 years ago

Thanks for the fast fix.

Does that mean that only std::basic_istream and std::basic_ostream are affected? Can we be sure that all the other forward declarations of templates in std used by boost are safe?

comment:4 by Ion Gaztañaga, 7 years ago

Those are the only classes in the MSVC STL that are marked with that bug-fixing pragma. It's related to virtual inheritance, and only ios classes use that type of inheritance. We can't be 100% sure, but I'm pretty confident about that.

comment:5 by Attila Mitrócsák <attila.mitrocsak@…>, 7 years ago

Ok, thanks.

Note: See TracTickets for help on using tickets.