Tob*_*ann 16 c++ optimization stack-corruption c++11 visual-studio-2017
以下问题是从一个庞大的项目中提炼出来的,这是我能够提出的最小问题的例子.
我知道,源于std::string它是坏的,它已经在我们的代码库中发生了变化,但我试图了解这里发生的事情.
代码在Visual C++ 2017上崩溃
Microsoft Visual Studio Community 2017
Version 15.2 (26430.14) Release
Visual C++ 2017 00369-60000-00001-AA257
Run Code Online (Sandbox Code Playgroud)
仅在发布模式下(具有速度优化).没有速度优化,它不会在发布模式下崩溃.
#include <string>
#include <string_view>
#include <vector>
struct my_string : public std::string
{
__declspec(noinline)
my_string::my_string( const std::string_view& str ) :
std::string( str.data(), str.size() )
{}
template <typename T>
my_string& arg( T )
{
return *this;
}
};
struct my_string_view : public std::string_view
{
my_string_view( const std::string_view::value_type* val ) :
std::string_view( val ) {}
template <typename... PARAMS>
my_string arg( PARAMS&&... prms ) {
return my_string( *this ).arg( std::forward<PARAMS>( prms )... );
}
};
template <typename T>
struct basic_color
{
T r, g, b, a;
basic_color() : r( 0 ), g( 0 ), b( 0 ), a( 255 ) {}
template <typename U>
explicit basic_color( const basic_color<U>& c ) :
r( c.r ), g( c.g ), b( c.b ), a( c.a )
{}
};
using color = basic_color<std::uint8_t>;
using float_color = basic_color<float>;
__declspec(noinline)
void change_float_color( float_color& color )
{
color.r = 0.1f;
}
int main()
{
std::vector<float_color> colors = { {} };
float sum = 0;
for ( std::uint32_t i = 0; i < 1; ++i )
{
float_color fc;
change_float_color( fc );
color c( fc );
std::vector<std::string> msgs;
msgs.push_back( my_string_view( "" ).arg( c.r ) );
msgs.push_back( my_string_view( "" ).arg( c.g ) );
sum += fc.b - colors[i].b;
}
return static_cast<int>(sqrt( sum ));
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio中的错误是这样的(看看的破碎大小msgs,并colors在底部):
我的猜测是,std::vector<std::string>::push_back(std::string&&)使用a 的调用my_string是有问题的(切片般的行为).但是如何破坏堆栈(或堆栈指针)呢?
有没有人知道这里会发生什么或我怎么知道?
这是我的项目,以防任何人有兴趣重现问题.
我认为这是一个编译器错误。
这是我从反汇编中看到的内容:main()进入后,esp被保存到ebx. 最后,esp从 恢复ebx。但是,在中间(调用后std::_Destroy_range1)ebx值会被其他内容覆盖。因此,最后,该ret指令使用了虚假esp值,并跳转到无效位置。
所以,实际上,堆栈没有损坏(这个错误不能像汉斯建议的那样用数据断点捕获),但堆栈指针损坏了。
| 归档时间: |
|
| 查看次数: |
510 次 |
| 最近记录: |