[I wrote this for my development group, and thought I’d share it here.]
I know that many of us have seen the “error linking with __chkstk” error in
the past. We’ve learned that it had something to do with stack usage, and
typically just allocate things from the heap/pool or break apart our
functions in order to fix it.
Today, we tracked down what’s really going on.
By default, the compiler inserts a call to __chkstk whenever your local
function’s stack usage is larger than 4 KB. Well, this function just does
verification of the stack pointer to make sure you don’t overflow the stack,
so it’s a useful debugging function. Unfortunately, that function is not
defined in kernel mode, only in one of the user-mode libraries. (This is
similar to the issue with __chkesp with the /GZ, which you can search the
NTDev archives for the solution.)
Thus, whenever you use more than 4 KB of local stack space, you will get
this error. If you shrink it down to under 4 KB (for example, 3999 bytes),
you will not get the error.
It can be argued that one could “fix” this (by providing the stub function)
since nothing is actually wrong with the code. However, since there is only
12 KB of stack for the entire kernel stack (per thread), it is not clean to
allow some functions to suck up more than their share. Thus, I think the
current error is a good thing.
If you want to find out how much stack your function is using, add the flag
“/FAcs” to your compiler options line in SOURCES. This will then cause
*.COD files to be generated when you compile, which is the assembly code
output. You can then search for either calls to “chkstk” (and look at the
number on the previous line) or you can search for “sub esp,”, and the
number following it is the size.
Personally, as a rule of thumb, any function that uses more than about 1 KB
is probably naughty, but that depends on a number of factors, so it’s not
hard-and-fast.
You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com