I have a piece of C code as follows:
int main() {
char buf[4] = { 'A', 'B', 'C', 'D' };
buf[3] = 42;
return 0;
}
When I compile it in Microsoft Visual Studio Community 2015 (debug mode, no optimisations) and debug it, I would expect the compiler to add an additional 8 bytes of 0xCC above and below my buffer as a sanity check with an instruction like sub esp,Fh
or something similar before the rep stos
. Instead, what I'm actually seeing is sub esp,0D0h
which results in 196 bytes of 0xCC on the stack above the space allocated for the buffer. I'm just wondering why? It seems excessive for a canary. I've included the full disassembly of the function for reference:
push ebp
mov ebp,esp
sub esp,0D0h
push ebx
push esi
push edi
lea edi,[ebp-0D0h]
mov ecx,34h
mov eax,0CCCCCCCCh
rep stos dword ptr es:[edi]
mov byte ptr [buf],41h
mov byte ptr [ebp-0Bh],42h
mov byte ptr [ebp-0Ah],43h
mov byte ptr [ebp-9],44h
xor eax,eax
mov dword ptr [ebp-8],eax
mov eax,1
imul ecx,eax,3
mov byte ptr buf[ecx],2Ah
mov eax,0B100Dh
push edx
mov ecx,ebp
push eax
lea edx,ds:[0F916E0h]
call @_RTC_CheckStackVars@8 (0F91244h)
pop eax
pop edx
pop edi
pop esi
pop ebx
mov esp,ebp
pop ebp
ret
Here is a screenshot of the stack (condensed a bit for space).
I've also experimented with just creating a buffer of a single byte, and the compiler allocates 0xCC bytes for this. For a forty byte buffer it allocates 0xF0. I'm just trying to make sense of this. Why is so much space being allocated?
Compiling in 32-bit mode on Windows 10 64, Microsoft Visual Studio Community 2015 Version 14.