6

I'm inspecting Visual C++ 10 optimization capabilities and found a rather curious thing. All code herein is compiled with /O2.

In the following code:

int _tmain(int argc, _TCHAR* argv[])
{
    char buffer[1024] = {};
    MessageBoxA( 0, buffer, buffer, 0 );
    memset( buffer, 0, sizeof( buffer ) );
    return 0;
}

the call to memset() before return is eliminated from the machine code (I inspect the disassembly). This is perfectly reasonable - if there're no reads from buffer afterwards then memset() is useless and if the developers really want to overwrite the buffer thay can use SecureZeroMemory() instead.

However in the following code:

int _tmain(int argc, _TCHAR* argv[])
{
    char buffer[1024] = {};
    MessageBoxA( 0, buffer, buffer, 0 );
    memset( buffer, 0, sizeof( buffer ) );
    Sleep( 0 ); //<<<<<<<<<<<<<<<<<<<<<<<<<<< Extra code
    return 0;
}

the call to memset() is not eliminated. That call has no influence on observed behavior and could be eliminated just as well as in the first snippet.

This can be a compiler deficiency or it can be useful somehow - I can't decide.

Why could leaving memset() call in the machine code emitted for the second snippet be useful?

3
  • 4
    You know that Sleep() has no side-effects. How is the compiler supposed to know? Sep 1, 2011 at 12:50
  • 2
    @HansPassant: How would Sleep() be affected by a local variable that it is unaware of?
    – sharptooth
    Sep 1, 2011 at 12:53
  • 6
    @sharptooth: The address of buffer is passed to other functions, which are allowed to store it a global or static variable that Sleep may be able to access.
    – YSN
    Sep 1, 2011 at 13:27

2 Answers 2

18

The compiler probably can't tell that MessageBoxA doesn't create an alias to buffer that's then used by Sleep later. Thus it fails the "as-if" check.

2

The compiler can look at the contents of memset and determine what it does. Sleep() is a system call that interacts with the kernel and its behavior is dependent on what version of Windows the code is run on; including the possibility of yet to implemented versions of Windows. There is simply no way for the compiler to know what the function will do and therefore there is no way for it optimize around it.

The same can be said of MessageBox which makes me surprised that memset got removed in the first version.

It is a safe bet that the call to memset will not be a problem on any current or future version of Windows, but that's not something I want the compiler to try to guess about.

4
  • in the first version, the memset is called after MessageBoxA. Thus does not influence it. We could argue that MessageBoxA could have posted the address to something that is watching it, but the C++ virtual machine makes no timing guarantees, so eliminating the call is akin to simply waiting for an infinite time before returning. TL;DR only instruction after the call to memset may influence the optimizer. Sep 1, 2011 at 13:53
  • @Matthieu M. I don't agree. If the compiler could be sure that the local buffer isn't "leaked" (ie some method saves its address) it could easily remove the memset even if Sleep() is called afterwards. So functions BEFORE the memset can as well influence the optimizer.
    – Voo
    Sep 3, 2011 at 3:07
  • @Voo: yes obviously, that would be a Dead Store. My summary was not intended to sound as general as it does. Sep 3, 2011 at 9:56
  • @Voo: If outside code got an an alias to buffer (which it could have, via the MessageBox call), it could legitimately use it as long as it had some way of knowing the method hadn't returned yet. Without the Sleep call, even if buffer would still exist during the memset, outside code would have no way of knowing that the method hadn't returned and thus would not be able to legitimately access it. While Sleep() is executing, however, outside code would know buffer still exists.
    – supercat
    Feb 10, 2014 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.