VC++ seems to put odd unused gaps in the local stack?

We had a driver stack overflow the other day, and so we were looking at the
amount of stack used by some of our functions for large local variables by
using the *.COD files that Microsoft VC++ can generate. We discovered that
there are sometimes large gaps of unused space in the stack variable
allocations. We’ve only found examples of this in stacks that are large (>
2 KB or so), so it is probably related to the size somehow. For example, we
might see:
_numThisThing$ = -1000
_numThatThing$ = -1004
_numAnotherThing$ = -1420
So, it would be as if some random ULONG would be using up 400 or so bytes!
Sometimes, we’d even see multiple gaps in the same function’s stack. We
would move code around, and the gaps would still be there, but between
different variables, and of seemingly different sizes. The gaps always seem
to be large (multi-hundred-bytes), so they aren’t there for padding reasons.
We examined the assembly code, and that space is really unused. It’s very
odd and there’s seemingly no reason for the compiler to be doing that.

I did some Net searching, but came up with nothing.

Has anyone else seen this sort of behavior, or can think of a reason for it
to exist? I’m loathe to think it’s a compiler code-generation bug, but I
have found a few in my life, so maybe…

Taed,

What version of VC++ are you using?

Chuck

----- Original Message -----
From: “Taed Wynnell”
To: “Windows System Software Developers Interest List”

Sent: Friday, August 29, 2003 5:08 AM
Subject: [ntdev] VC++ seems to put odd unused gaps in the local stack?

> We had a driver stack overflow the other day, and so we were looking
at the
> amount of stack used by some of our functions for large local
variables by
> using the *.COD files that Microsoft VC++ can generate. We discovered
that
> there are sometimes large gaps of unused space in the stack variable
> allocations. We’ve only found examples of this in stacks that are
large (>
> 2 KB or so), so it is probably related to the size somehow. For
example, we
> might see:
> _numThisThing$ = -1000
> _numThatThing$ = -1004
> _numAnotherThing$ = -1420
> So, it would be as if some random ULONG would be using up 400 or so
bytes!
> Sometimes, we’d even see multiple gaps in the same function’s stack.
We
> would move code around, and the gaps would still be there, but between
> different variables, and of seemingly different sizes. The gaps
always seem
> to be large (multi-hundred-bytes), so they aren’t there for padding
reasons.
> We examined the assembly code, and that space is really unused. It’s
very
> odd and there’s seemingly no reason for the compiler to be doing that.
>
> I did some Net searching, but came up with nothing.
>
> Has anyone else seen this sort of behavior, or can think of a reason
for it
> to exist? I’m loathe to think it’s a compiler code-generation bug,
but I
> have found a few in my life, so maybe…

I observed a similar behaviour some times ago using the “Microsoft Visual
C++ 6.0 Processor Pack”:
http://groups.google.de/groups?hl=de
http:m%241%40hermes.ppp.net>
&lr=&ie=UTF-8&oe=UTF-8&selm=9klmvb%244gm%241%40hermes.ppp.net

But this “Processor Pack” is obsolete meanwhile by subsequent Service Packs
for VC 6 resp. the compiler shipped with current DDKs.

Regards
Volker

-----Original Message-----
From: Taed Wynnell [mailto:xxxxx@vertical.com]
Sent: Friday, August 29, 2003 12:09 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] VC++ seems to put odd unused gaps in the local stack?

We had a driver stack overflow the other day, and so we were looking at the
amount of stack used by some of our functions for large local variables by
using the *.COD files that Microsoft VC++ can generate. We discovered that
there are sometimes large gaps of unused space in the stack variable
allocations. We’ve only found examples of this in stacks that are large (>
2 KB or so), so it is probably related to the size somehow. For example, we
might see:

_numThisThing$ = -1000
_numThatThing$ = -1004
_numAnotherThing$ = -1420
So, it would be as if some random ULONG would be using up 400 or so bytes!
Sometimes, we’d even see multiple gaps in the same function’s stack. We
would move code around, and the gaps would still be there, but between
different variables, and of seemingly different sizes. The gaps always seem
to be large (multi-hundred-bytes), so they aren’t there for padding reasons.
We examined the assembly code, and that space is really unused. It’s very
odd and there’s seemingly no reason for the compiler to be doing that.

I did some Net searching, but came up with nothing.

Has anyone else seen this sort of behavior, or can think of a reason for it
to exist? I’m loathe to think it’s a compiler code-generation bug, but I
have found a few in my life, so maybe…


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@baslerweb.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</http:>

Thanks for the info, Volker. That does sound very similar to my problem
(but I don’t have the volatile keyword in reference to any of the variables
around the gap). Unfortunately, your excellent small example of your
problem:
VOID f()
{
LARGE_INTEGER StartTime_tm;
KeQueryTickCount( &StartTime_tm );
}
Generates the correct code for me:
_TEXT SEGMENT
_StartTime_tm$ = -8
__TickCount$7981 = -12
xxxxx@0 PROC NEAR ; COMDAT

So, at least with our current compiler (VC++ 6.0 with SP 5), that aspect of
the issue is resolved. We’ve also tried my code with the NT, 2000, and XP
DDKs, and they generate slightly different results (due to macro
differences, I assume), but do all have the various stack gaps.

I suppose that I’ll have to try to create a small example (which might take
a while since the functions that I see it in are fairly complex) and submit
it to Microsoft… :frowning:

The issue turned out to be not a compiler bug, but a compiler oddity
involving the following:
– For reasons I don’t understand, I’m not getting the “error C4101:
unreferenced local variable” warning. If I turn it on with a
#pragma(default:4101), it works, but it’s not working by default. Very odd
since I know that it used to work. I thought that maybe someone else turned
them off with a #pragma in a shared include file, but that doesn’t seem to
be the case.
– If there is an unreferenced local variable, it still allocates stack
space for the variable, but does NOT put it in the listing file’s “symbol
table”. Thus, as far as I could tell, it was an unused gap in the stack.

Once I got the warning turned back on, I found some unused large structures
on the stack, and removing them solved the problem.

Taed,

That’s strange about the warning. Are you using /W4? Check again about
one of your header files is inadvertently disabling this warning via a
pragma. Try building a preprocessed version (/P) of this file and
searching for “4101”. Or, if you’re using precompiled headers, try
deleting the .pch file and rebuilding.

Chuck

----- Original Message -----
From: “Taed Wynnell”
To: “Windows System Software Developers Interest List”

Sent: Wednesday, September 03, 2003 10:30 PM
Subject: [ntdev] RE: VC++ seems to put odd unused gaps in the local
stack?

> The issue turned out to be not a compiler bug, but a compiler oddity
> involving the following:
> – For reasons I don’t understand, I’m not getting the “error C4101:
> unreferenced local variable” warning. If I turn it on with a
> #pragma(default:4101), it works, but it’s not working by default.
Very odd
> since I know that it used to work. I thought that maybe someone else
turned
> them off with a #pragma in a shared include file, but that doesn’t
seem to
> be the case.
> – If there is an unreferenced local variable, it still allocates
stack
> space for the variable, but does NOT put it in the listing file’s
“symbol
> table”. Thus, as far as I could tell, it was an unused gap in the
stack.
>
> Once I got the warning turned back on, I found some unused large
structures
> on the stack, and removing them solved the problem.