Is there an API call or any another similar way, that uses only ntdll.dll
, to allocate memory on the stack?
I know alloca()
does that, but I can't use it because I can use only function from ntdll.dll
.
Thanks!
Is there an API call or any another similar way, that uses only ntdll.dll
, to allocate memory on the stack?
I know alloca()
does that, but I can't use it because I can use only function from ntdll.dll
.
Thanks!
alloca is partially intrinsic function, implemented by compiler. but internally it call _alloca_probe_16
(for x86) or __chkstk
(x64) for move guard page down on stack. implementation of this functions exist in alloca16.obj
and chkstk.obj
which can be found in VC
subfolder (where exacly depended from VC version) - you can add this obj for link process or even first convert it to lib. also in latest WDK libs - exist ntdllp.lib
(not confuse with ntdll.lib
) - it also containing all need for implementation ( ntdll.dll
export _chkstk
(for x86) and __chkstk
(for x64))
again in more details:
when you write in src code
alloca(cb)
CL
compiler generate in x86
mov eax,cb
call _alloca_probe_16 ; do actual stack allocation and probe
and in x64 version
mov ecx,eax
add rcx,0Fh
cmp rcx,rax
ja @@0
mov rcx,0FFFFFFFFFFFFFF0h
@@0:
and rcx,0FFFFFFFFFFFFFFF0h
mov rax,rcx
call __chkstk ; probe only
sub rsp,rax ; actual stack allocation
so _alloca_probe_16
and/or __chkstk
must be implemented somewhere or you got link error - unresolved external symbol.
in latest WDK builds exist ntdllp.lib
(note about p
- not ntdll.lib
) which containing this implementation. in this case your PE will be import __chkstk
or _alloca_probe
from ntdll.dll
(this functions exported how minimum from XP - both this functions is point to same code, simply alias)
another solution - in VC
folders can be found alloca16.obj
and chkstk.obj
- you can use this obj as link input (or merge alloca16.obj
+ chkstk.obj
in single lib file). in this case your PE will be nothing import.
You don't need something architecture dependent because allocation on the stack is (generally) architecture independent.
If you're using C99 you have a standard way of doing this, using Variable Length Arrays: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
You'd quite simply write something like this:
char mybuffer[my_size];
And it will be allocated on the stack.
Because alloca
manipulates the stack pointer, it isn't a "real" function, it's a "compiler intrinsic". If you compile a function that uses alloca
to assembly language, you should see that it is translated directly to sub esp, NNN
rather than call alloca
. (There might be a call to a function in addition to the sub esp, NNN
. In that case you need to find out what that function does, where it's normally defined, and arrange for your application to provide a substitute. You're already jumping through all sorts of unusual hoops to use nothing but NTDLL, this is just one more.)
If you do see call alloca
and no sub esp, NNN
, that is very likely to mean that your compiler has only a fake implementation of alloca
that is not giving you memory allocated from the stack, and you shouldn't use it at all.