Board index » cppbuilder » QC 48623:Size and speed optimizations corrupt stack frame

QC 48623:Size and speed optimizations corrupt stack frame


2007-07-06 05:06:52 PM
cppbuilder99
I have just reported a critical error from C++Builder 2007 to
QualityCentral, see report number on subject.
This is the text of the report:
Quote
I have imported all of my work projects from BDS2006 to CB++2007, and just *one* has failed to run corrcetly after importing.

I have stripped down the offending part to a really small test case.

Test case will run OK when compiled with No Optimizations or with any Selected optimizations settings, but will throw exceptions when run with Size or Speed optimizations selected. The exception will be variable depoending on the rest of the code, because it is caused by a function return without correctly restoring ESP register.

Complete test case in test.cpp/cbproj attachment. Offending function decompiled on bith cases (ok and error case), in test.txt attachment.

BTW, which special optimizations options are applied in Size or Speed cases that are not listed for Selected optimizations? I woulkd really love to know.
Attachments located on the report!
Best regards,
zara
 
 

Re:QC 48623:Size and speed optimizations corrupt stack frame

On Jul 6, 9:06 pm, Zara < XXXX@XXXXX.COM >wrote:
Quote
Test case will run OK when compiled with No Optimizations or with any Selected optimizations settings, but will throw exceptions when run with Size or Speed optimizations selected. The exception will be variable depoending on the rest of the code, because it is caused by a function return without correctly restoring ESP register.

Attachments located on the report!
When I view the report it does not offer a link to download
the attachment. Can you post the source code here?
 

Re:QC 48623:Size and speed optimizations corrupt stack frame

On Sun, 08 Jul 2007 16:13:46 -0700, Old Wolf < XXXX@XXXXX.COM >
wrote:
Quote
On Jul 6, 9:06 pm, Zara < XXXX@XXXXX.COM >wrote:
>Test case will run OK when compiled with No Optimizations or with any Selected optimizations settings, but will throw exceptions when run with Size or Speed optimizations selected. The exception will be variable depoending on the rest of the code, because it is caused by a function return without correctly restoring ESP register.
>
>Attachments located on the report!

When I view the report it does not offer a link to download
the attachment. Can you post the source code here?
I donīt what may happen qith the report. Opening with QualityCentral
it offers all attachments, but anycase, here they are:
Quote
>>>>>test.cpp
//---------------------------------------------------------------------------
#include <algorithm>
#pragma hdrstop
//---------------------------------------------------------------------
static struct entry {
int index;
unsigned char value;
} const dictionary[]={
{0,0x00},
{1,0x80},
{2,0x90},
{3,0xA0},
{4,0xC0},
{5,0x7F}
};
struct is_it {
int which;
is_it(int c):which(c) {}
bool operator()(const entry& e) {return e.index==which;}
};
const size_t dictionary_size=sizeof dictionary/sizeof(entry);
unsigned char __fastcall get_selected() {
const entry *e=std::find_if
(dictionary,dictionary+dictionary_size,is_it(2));
if (e==dictionary+dictionary_size) return 0;
return e->value;
}
//---------------------------------------------------------------------------
int main()
{
return get_selected();
}
//---------------------------------------------------------------------------
Quote
>>>>>test.cbproj
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{fcf27f35-325d-4d2e-bf72-4c89cad53cf0}</ProjectGuid>
<Config Condition="'$(Config)'==''">Release</Config>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_1)'!=''">
<Base>true</Base>
<Cfg_1>true</Cfg_1>
<CfgParent>Base</CfgParent>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_2)'!=''">
<Base>true</Base>
<Cfg_2>true</Cfg_2>
<CfgParent>Base</CfgParent>
</PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''">
<OutputExt>exe</OutputExt>
<BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed>
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput>
<Defines>NO_STRICT</Defines>
<DynamicRTL>true</DynamicRTL>
<ILINK_ObjectSearchPath>test</ILINK_ObjectSearchPath>
<ProjectType>CppConsoleApplication</ProjectType>
<NoVCL>true</NoVCL>
<PackageImports>vcl.bpi;rtl.bpi;bcbie.bpi;vclx.bpi;vclactnband.bpi;xmlrtl.bpi;bcbsmp.bpi;dbrtl.bpi;vcldb.bpi;vcldbx.bpi;bdertl.bpi;IndyCore.bpi;IndySystem.bpi;IndyProtocols.bpi;inet.bpi;IntrawebDB_90_100.bpi;Intraweb_90_100.bpi;vclie.bpi;websnap.bpi;webdsnap.bpi;inetdbbde.bpi;inetdbxpress.bpi;soaprtl.bpi;dsnap.bpi</PackageImports>
<BCC_wpar>false</BCC_wpar>
<IncludePath>$(BDS)\include;$(BDS)\include\dinkumware;$(BDS)\include\vcl;test</IncludePath>
<AllPackageLibs>rtl.lib;vcl.lib</AllPackageLibs>
<ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;test</ILINK_LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1)'!=''">
<BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<DCC_Optimize>false</DCC_Optimize>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe>
<Defines>_DEBUG;$(Defines)</Defines>
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
<BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion>
<ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking>
<BCC_UseRegisterVariables>None</BCC_UseRegisterVariables>
<DCC_Define>DEBUG</DCC_Define>
<BCC_DebugLineNumbers>true</BCC_DebugLineNumbers>
<IntermediateOutputDir>Debug</IntermediateOutputDir>
<TASM_DisplaySourceLines>true</TASM_DisplaySourceLines>
<BCC_StackFrames>true</BCC_StackFrames>
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
<ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath>
<TASM_Debugging>Full</TASM_Debugging>
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''">
<BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
<Defines>NDEBUG;$(Defines)</Defines>
<ILINK_ClearState>true</ILINK_ClearState>
<DynamicRTL>false</DynamicRTL>
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
<BCC_EliminateDuplicateExpressions>true</BCC_EliminateDuplicateExpressions>
<BCC_FastExceptionPrologs>true</BCC_FastExceptionPrologs>
<BCC_InstructionSet>5</BCC_InstructionSet>
<BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion>
<ILINK_SelectedWarnings>false</ILINK_SelectedWarnings>
<BCC_ExpandIntrinsics>true</BCC_ExpandIntrinsics>
<BCC_SelectedOptimizations>true</BCC_SelectedOptimizations>
<ILINK_AllWarnings>true</ILINK_AllWarnings>
<BCC_DebugLineNumbers>true</BCC_DebugLineNumbers>
<BCC_ZeroLengthEmptyMemberFuncs>true</BCC_ZeroLengthEmptyMemberFuncs>
<BCC_PentiumInstructionScheduling>true</BCC_PentiumInstructionScheduling>
<ILINK_CaseSensitive>true</ILINK_CaseSensitive>
<BCC_SelectedWarnings>false</BCC_SelectedWarnings>
<BCC_ExtendedErrorInfo>true</BCC_ExtendedErrorInfo>
<IntermediateOutputDir>Release</IntermediateOutputDir>
<BCC_LoopInductionReduction>true</BCC_LoopInductionReduction>
<BCC_AllWarnings>true</BCC_AllWarnings>
<BCC_OptimizeVariables>true</BCC_OptimizeVariables>
<BCC_IntegerSizedEnums>false</BCC_IntegerSizedEnums>
<BCC_ZeroLengthEmptyBaseClass>true</BCC_ZeroLengthEmptyBaseClass>
<ILINK_LibraryPath>$(BDS)\lib\release;$(ILINK_LibraryPath)</ILINK_LibraryPath>
<TASM_Debugging>None</TASM_Debugging>
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
<BCC_OptimizeJumps>true</BCC_OptimizeJumps>
</PropertyGroup>
<ProjectExtensions>
<Borland.Personality>CPlusPlusBuilder.Personality</Borland.Personality>
<Borland.ProjectType>CppConsoleApplication</Borland.ProjectType>
<BorlandProject>
<BorlandProject><CPlusPlusBuilder.Personality><VersionInfo><VersionInfo
Name="IncludeVerInfo">False</VersionInfo><VersionInfo
Name="AutoIncBuild">False</VersionInfo><VersionInfo
Name="MajorVer">1</VersionInfo><VersionInfo
Name="MinorVer">0</VersionInfo><VersionInfo
Name="Release">0</VersionInfo><VersionInfo
Name="Build">0</VersionInfo><VersionInfo
Name="Debug">False</VersionInfo><VersionInfo
Name="PreRelease">False</VersionInfo><VersionInfo
Name="Special">False</VersionInfo><VersionInfo
Name="Private">False</VersionInfo><VersionInfo
Name="DLL">False</VersionInfo><VersionInfo
Name="Locale">3082</VersionInfo><VersionInfo
Name="CodePage">1252</VersionInfo></VersionInfo><VersionInfoKeys><VersionInfoKeys
Name="CompanyName"></VersionInfoKeys><VersionInfoKeys
Name="FileDescription"></VersionInfoKeys><VersionInfoKeys
Name="FileVersion">1.0.0.0</VersionInfoKeys><VersionInfoKeys
Name="InternalName"></VersionInfoKeys><VersionInfoKeys
Name="LegalCopyright"></VersionInfoKeys><VersionInfoKeys
Name="LegalTrademarks"></VersionInfoKeys><VersionInfoKeys
Name="OriginalFilename"></VersionInfoKeys><VersionInfoKeys
Name="ProductName"></VersionInfoKeys><VersionInfoKeys
Name="ProductVersion">1.0.0.0</VersionInfoKeys><VersionInfoKeys
Name="Comments"></VersionInfoKeys></VersionInfoKeys><Debugging><Debugging
Name="DebugSourceDirs"></Debugging></Debugging><Parameters><Parameters
Name="RunParams"></Parameters><Parameters
Name="Launcher"></Parameters><Parameters
Name="UseLauncher">False</Parameters><Parameters
Name="DebugCWD"></Parameters><Parameters
Name="HostApplication"></Parameters><Parameters
Name="RemoteHost"></Parameters><Parameters
Name="RemotePath"></Parameters><Parameters
Name="RemoteParams"></Parameters><Parameters
Name="RemoteLauncher"></Parameters><Parameters
Name="UseRemoteLauncher">False</Parameters><Parameters
Name="RemoteCWD"></Parameters><Parameters
Name="RemoteDebug">False</Parameters><Parameters Name="Debug Symbols
Search Path"></Parameters><Parameters
Name="LoadAllSymbols">True</Parameters><Parameters
Name="LoadUnspecifiedSymbols">False</Parameters></Parameters><Linker><Linker
Name="LibPrefix"></Linker><Linker Name="LibSuffix"></Linker><Linker
Name="LibVersion"></Linker></Linker><ProjectProperties><ProjectProperties
Name="AutoShowDeps">True</ProjectProperties><ProjectProperties
Name="ManagePaths">True</ProjectProperties><ProjectProperties
Name="VerifyPackages">True</ProjectProperties></ProjectProperties>
<HistoryLists_hlIncludePath>
<HistoryLists_hlIncludePath
Name="Count">1</HistoryLists_hlIncludePath>
<HistoryLists_hlIncludePath
Name="Item0">$(BDS)\include;$(BDS)\include\dinkumware;$(BDS)\include\vcl;test</HistoryLists_hlIncludePath>
</HistoryLists_hlIncludePath>
<HistoryLists_hlILINK_LibraryPath>
<HistoryLists_hlILINK_LibraryPath
Name="Count">1</HistoryLists_hlILINK_LibraryPath>
<HistoryLists_hlILINK_LibraryPath
Name="Item0">$(BDS)\lib\release;$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;test</HistoryLists_hlILINK_LibraryPath>
</HistoryLists_hlILINK_LibraryPath>
<HistoryLists_hlDefines>
<HistoryLists_hlDefines Name="Count">1</HistoryLists_hlDefines>
<HistoryLists_hlDefines
Name="Item0">NDEBUG;NO_STRICT</HistoryLists_hlDefines>
</HistoryLists_hlDefines>
<HistoryLists_hlIntermediateOutputDir>
<HistoryLists_hlIntermediateOutputDir
Name="Count">1</HistoryLists_hlIntermediateOutputDir>
<HistoryLists_hlIntermediateOutputDir
Name="Item0">Release</HistoryLists_hlIntermediateOutputDir>
</HistoryLists_hlIntermediateOutputDir>
<HistoryLists_hlILINK_MaxErrors>
<HistoryLists_hlILINK_MaxErrors
Name="Count">1</HistoryLists_hlILINK_MaxErrors>
<HistoryLists_hlILINK_MaxErrors
Name="Item0">0</HistoryLists_hlILINK_MaxErrors>
</HistoryLists_hlILINK_MaxErrors>
<HistoryLists_hlILINK_FileAlignment>
<HistoryLists_hlILINK_FileAlignment
Name="Count">1</HistoryLists_hlILINK_FileAlignment>
<HistoryLists_hlILINK_FileAlignment
Name="Item0">0x200</HistoryLists_hlILINK_FileAlignment>
</HistoryLists_hlILINK_FileAlignment>
<HistoryLists_hlILINK_ObjectAlignment>
<HistoryLists_hlILINK_ObjectAlignment
Name="Count">1</HistoryLists_hlILINK_ObjectAlignment>
<HistoryLists_hlILINK_ObjectAlignment
Name="Item0">4096</HistoryLists_hlILINK_ObjectAlignment>
</HistoryLists_hlILINK_ObjectAlignment>
<HistoryLists_hlILINK_BaseAddress>
<HistoryLists_hlILINK_BaseAddress
Name="Count">1</HistoryLists_hlILINK_BaseAddress>
<HistoryLists_hlILINK_BaseAddress
Name="Item0">0x00400000</HistoryLists_hlILINK_BaseAddress>
</HistoryLists_hlILINK_BaseAddress>
<HistoryLists_hlILINK_HeapCommitSize>
<HistoryLists_hlILINK_HeapCommitSize
Name="Count">1</HistoryLists_hlILINK_HeapCommitSize>
<HistoryLists_hlILINK_HeapCommitSize
Name="Item0">0x00001000</HistoryLists_hlILINK_HeapCommitSize>
</HistoryLists_hlILINK_HeapCommitSize>
<HistoryLists_hlILINK_HeapReserveSize>
<HistoryLists_hlILINK_HeapReserveSize
Name="Count">1</HistoryLists_hlILINK_HeapReserveSize>
<HistoryLists_hlILINK_HeapReserveSize
Name="Item0">0x00100000</HistoryLists_hlILINK_HeapReserveSize>
</HistoryLists_hlILINK_HeapReserveSize>
<HistoryLists_hlILINK_StackCommitSize>
<HistoryLists_hlILINK_StackCommitSize
Name="Count">1</HistoryLists_hlILINK_StackCommitSize>
<HistoryLists_hlILINK_StackCommitSize
Name="Item0">0x00002000</HistoryLists_hlILINK_StackCommitSize>
</HistoryLists_hlILINK_StackCommitSize>
<HistoryLists_hlILINK_StackReserveSize>
<HistoryLists_hlILINK_StackReserveSize
Name="Count">1</HistoryLists_hlILINK_StackReserveSize>
<HistoryLists_hlILINK_StackReserveSize
Name="Item0">0x00100000</HistoryLists_hlILINK_StackReserveSize>
</HistoryLists_hlILINK_StackReserveSize>
<HistoryLists_hlBCC_MaxIdentifierLength>
<HistoryLists_hlBCC_MaxIdentifierLength
Name="Count">1</HistoryLists_hlBCC_MaxIdentifierLength>
<HistoryLists_hlBCC_MaxIdentifierLength
Name="Item0">250</HistoryLists_hlBCC_MaxIdentifierLength>
</HistoryLists_hlBCC_MaxIdentifierLength>
</CPlusPlusBuilder.Personality></BorlandProject></BorlandProject>
</ProjectExtensions>
<Import Project="$(MSBuildBinPath)\Borland.Cpp.Targets" />
<ItemGroup>
<CppCompile Include="test.cpp">
<BuildOrder>0</BuildOrder>
</CppCompile>
<BuildConfiguration Include="Debug">
<Key>Cfg_1</Key>
</BuildConfiguration>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
</BuildConfiguration>
</ItemGroup>
</Project>
Quote
>>>>>test.txt
-- Speed or size optimization selected: (error)
test.cpp.26: const entry *e=std::find_if
00401234 B8FFFFFFFF mov eax,$ffffffff
00401239 81C400F0FFFF add esp,$fffff000
0040123F 851C24 test [esp],ebx
00401242 48 dec eax
00401243 79FA jns $0040123f
00401245 83C4FC add esp,-$04
00401248 6A02 push $02
0040124A 8D542404 lea edx,[esp+$04]
0040124E 52 push edx
0040124F E824000000 call is_it::is_it(int)
00401254 83C408 add esp,$08
00401257 68C8704100 push $004170c8
0040125C 6898704100 push $00417098
00401261 E85A000000 call std::find_if<const entry *,
is_it>(const entry *,const entry *,is_it)
00401266 83C40C add esp,$0c
test.cpp.28: if (e==dictionary+dictionary_size) return 0;
00401269 3DC8704100 cmp eax,$004170c8
0040126E 7503 jnz $00401273
00401270 33C0 xor eax,eax
00401272 C3 ret
test.cpp.29: return e->value;
00401273 8A4004 mov al,[eax+$04]
test.cpp.30: }
00401276 C3 ret
00401277 90 nop
-- No optimization selected: (ok)
test.cpp.25: unsigned char __fastcall get_selected() {
00401234 55 push ebp
00401235 8BEC mov ebp,esp
00401237 51 push ecx
test.cpp.26: const entry *e=std::find_if
00401238 83C4FC add esp,-$04
0040123B 6A02 push $02
0040123D 8D442404 lea eax,[esp+$04]
00401241 50 push eax
00401242 E835000000 call is_it::is_it(int)
00401247 83C408 add esp,$08
0040124A 68C8704100 push $004170c8
0040124F 6898704100 push $00417098
00401254 E86F000000 call std::find_if<const entry *,
is_it>(const entry *,const entry *,is_it)
00401259 83C40C add esp,$0c
0040125C 8945FC mov [ebp-$04],eax
test.cpp.28: if (e==dictionary+dictionary_size) return 0;
0040125F BAC8704100 mov edx,$004170c8
00401264 8B4DFC mov ecx,[ebp-$04]
00401267 3BD1 cmp edx,ecx
00401269 7505 jnz $00401270
0040126B 33C0 xor eax,eax
0040126D 59 pop ecx
0040126E 5D pop ebp
0040126F C3 ret
test.cpp.29: return e->value;
00401270 8B55FC mov edx,[ebp-$04]
00401273 8A4204 mov al,[edx+$04]
test.cpp.30: }
00401276 59 pop ecx
00401277 5D pop ebp
00401278 C3 ret
00401279 90 nop
0040127A 90 nop
0040127B 90 nop
 

{smallsort}