EDIT:
I accepted the answer below and also added my own with my final version of the code. Hopefully he shows people actual examples of highlighting shadow space, not more words.
EDIT 2: I also managed to find a link to the PDF calling conventions in the YouTube video annotation (total), which has interesting interesting snippets on Shadow Space and Red Zone on Linux. It can be found here: http://www.agner.org/optimize/calling_conventions.pdf
ORIGINAL:
I looked at several other issues here and around the Internet, but I cannot find a suitable example of highlighting "Shadow Space" when calling a routine / Windows API in a 64-bit Windows assembly.
My understanding is this:
- Caller must
sub rsp,<bytes here>
beforecall callee
- Callee should use it to store registers if necessary (or local variables if register preservation is not required).
- Caller clears it, for example:
add rsp,<bytes here>
- The allocated amount should be aligned to 32 bytes.
With that in mind, this is what I tried:
section .text
start:
sub rsp,0x20 ; <---- Allocate 32 bytes of "Shadow space"
mov rcx,msg1
mov rdx,msg1.len
call write
add rsp,0x20
mov rcx,NULL
call ExitProcess
ret
write:
mov [rsp+0x08],rcx ; <-- use the Shadow space
mov [rsp+0x10],rdx ; <-- and again
mov rcx,STD_OUTPUT_HANDLE ; Get handle to StdOut
call GetStdHandle
mov rcx,rax ; hConsoleOutput
mov rdx,[rsp+0x08] ; lpBuffer
mov r8,[rsp+0x10] ; nNumberOfCharsToWrite
mov r9,empty ; lpNumberOfCharsWritten
push NULL ; lpReserved
call WriteConsoleA
ret
My two lines: "Hello" and "Peace! \ N". It manages to print "Hello" before the crash. I have a suspicion that I am doing it right ... except that I have to clean up somehow (and I'm not sure how).
What am I doing wrong? I tried a combination of sizes, and also tried to “highlight Shadow Space” before WinAPI calls (should I do this?).
, , . , ABI, write
WinAPI ( ).