Stack Problem

HI,
Could you please tell the various reasons for stack problems.
I am getting 7F (KMODE Exception) along with double fault. When I decrease
some of my variable sizes (i.e. memory used by my variables) the driver
works perfectly.
I am writing a driver which would be incorporating huge stacks (I
suppose), is there any method to increase stack. Please tell the various
causes of Stack problems.
Regards
Lalit.

Hi Lalit,
kernelmode stack is very limited. I also ran in such problems some time ago
and solved them by dynamically allocating from the nonpaged pool.

Tobias

----- Original Message -----
From: “Lalit S. Rana”
To: “File Systems Developers”
Sent: Thursday, August 07, 2003 9:25 PM
Subject: [ntfsd] Stack Problem

> HI,
> Could you please tell the various reasons for stack problems.
> I am getting 7F (KMODE Exception) along with double fault. When I decrease
> some of my variable sizes (i.e. memory used by my variables) the driver
> works perfectly.
> I am writing a driver which would be incorporating huge stacks (I
> suppose), is there any method to increase stack. Please tell the various
> causes of Stack problems.
> Regards
> Lalit.
>
> —
> You are currently subscribed to ntfsd as: xxxxx@linkwave.org
> To unsubscribe send a blank email to xxxxx@lists.osr.com

You should assume your stack is limited to 12KB. There is a larger stack
for certain threads (related to Win32) so you could try to restrict
processing to only one of those (but I’ve never tried this approach, so I
can’t give you much guidance). A better way to do this from my experience
is just to allocate memory from an alternate location as needed.

For example, if your function has 36 different variables, use a single
structure definition for those 36 variables. At the beginning of your
function, allocate the space (ExAllocatePoolWitTag or if you want to get
fancy, use a lookaside list) and then at the end of your function free the
space. It isn’t PERFECT, but it means you can operate even in small stack
spaces.

A radically different architecture is to use user-mode service threads to
perform processing, since they have much larger stacks.

Kernel stacks are limited. They are allocated from *non-paged pool* which
is a scarce resource. This is not likely to change in the near-term future.

Regards,

Tony

Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com

-----Original Message-----
From: Lalit S. Rana [mailto:xxxxx@epatra.com]
Sent: Thursday, August 07, 2003 3:25 PM
To: File Systems Developers
Subject: [ntfsd] Stack Problem

HI,
Could you please tell the various reasons for stack problems.
I am getting 7F (KMODE Exception) along with double fault. When I decrease
some of my variable sizes (i.e. memory used by my variables) the driver
works perfectly. I am writing a driver which would be incorporating huge
stacks (I suppose), is there any method to increase stack. Please tell the
various causes of Stack problems. Regards Lalit.


You are currently subscribed to ntfsd as: xxxxx@osr.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Thanks Tobias & Tony,
for your answer.
I wanted to ask about the various things which add up to the stack.
The locally defined variables should automatically get destroyed when out
of scope, but I think this is not happening in my program.
Please help
Lalit.

As far as I understand the stack, the stackpointer is saved before your
variables are put there. When the function is left, the stackpointer is
restored. So all locale variables are freed in one strike and will simply be
overwritten by the next function acquiring that portion of the stack. A
global pointer to a locale variable will point to a valid value as long, as
the referenced stack position isn’t initialized by another locale variable
of a later called function. The stack-pointer will increase with each
function you call without returning (e.g. recursive calls). I don’t know if
in this case the 12kb limit applies for all the function calls.

Values can (depending on the compiler) stay virtually persistant when
calling the same function more than once in a row without initializing the
locale variables.

To really corrupt the stack you must be messing arround with machine-code or
use very bad compiler setting. I assume you don’t.

Tobias

----- Original Message -----
From: “Lalit S. Rana”
To: “File Systems Developers”
Sent: Tuesday, August 12, 2003 10:30 AM
Subject: [ntfsd] RE: Stack Problem

> Thanks Tobias & Tony,
> for your answer.
> I wanted to ask about the various things which add up to the stack.
> The locally defined variables should automatically get destroyed when out
> of scope, but I think this is not happening in my program.
> Please help
> Lalit.
>
> —
> You are currently subscribed to ntfsd as: xxxxx@linkwave.org
> To unsubscribe send a blank email to xxxxx@lists.osr.com

Thanks Tobias,
My error is : -

Bugcheck code 0000007F
Arguments 00000008 00000000 00000000 00000000
ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available.

What this warning means I am not doing any recursive calling of functions.
I suppose my code is also perfect.
Thanks,
Lalit.

What do you mean by local variables? When function returns stack allocated
for its local variables is released.
If you define several blocks within a functions, local variables from
disjoined blocks use separate space on stack:

void f ()
{
{
int A;
}
{
int B;
}
}

C++ compiler generates such code that allocates space from stack separatly
for A and B; these variables consume stack during all time you are in the
function f.
I don’t know why it doesn’t use the same memory for A and B.
You can build assembler listing for your function and check how much stack
is consumes. You may restructure your code and replace blocks with function
calls - then stack will be released when leave the scope.
I observed this with Visual C++ v 6. I didn’t check what code is produced
for C programs or whether the latest compiler expose the same problem.

Alexei.

“Lalit S. Rana” wrote in message news:xxxxx@ntfsd…
>
> Thanks Tobias & Tony,
> for your answer.
> I wanted to ask about the various things which add up to the stack.
> The locally defined variables should automatically get destroyed when out
> of scope, but I think this is not happening in my program.
> Please help
> Lalit.
>
>

> void f ()

{
{
int A;
}
{
int B;
}
}

C++ compiler generates such code that allocates space from stack separatly
for A and B; these variables consume stack during all time you are in the
function f.
I don’t know why it doesn’t use the same memory for A and B.

MSVC6 compiler can reuse the variable stack space if even minor optimizations
are enabled, even while compiling the .C file. That’s why it is always a good
idea to set -Od in checked builds, otherwise, WinDbg will go mad with locals.

Also, for WinDbg use, it is a good idea to never declare any variables in
blocks. Only declare them in function. Otherwise, WinDbg sometimes goes mad.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com