Author Topic: allocating the shadow space  (Read 2178 times)

Offline JohnG

  • Jr. Member
  • *
  • Posts: 33
allocating the shadow space
« on: January 06, 2022, 11:48:18 PM »
Hi All,

I understand the principle around the shadow space, just not the times that its required.
I have read here that if the code and functions is all yours, you do not have to worry about it, but if you call an external API/C/C++ function you do have to worry about it ?.

My confusion comes from seeing example code where this is only partly followed.
If there is multiple function calls, do you have to do the shadow space for all the calls you make  sub rsp, 32  add rsp, 32  plus any stack args  or can you do one at the start of the code only, not every call ?  .

John

Offline JohnG

  • Jr. Member
  • *
  • Posts: 33
Re: allocating the shadow space
« Reply #1 on: January 09, 2022, 03:57:48 AM »
Hi all,

After further reading I see the idea is to use the stack over again within the same program. But I think for my own peace of mind I will align the stack and then place(and remove) the shadow space and any additional 8 byte balancing on each function.  Then maybe clean the code up after its working.

John

Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: allocating the shadow space
« Reply #2 on: January 09, 2022, 06:49:31 AM »
I have always created a single stack for my small code examples, but will be looking into local stack frames (I think that's the same as what you call a shadow space) when I get back into writing a simple operating system this year (I am going to rewrite the windowing system I wrote previously in C++ as the interface to my OS, but the kernel will be small with modules communicating via the kernel).

The only real advice I can offer is to remember to clean up your local stacks and restore the callers stack when you leave a routine that changes it.

The easiest way to create a stack frame is to use the ENTER and LEAVE instructions.
My graphics card database: www.gpuzoo.com

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: allocating the shadow space
« Reply #3 on: January 10, 2022, 01:33:09 AM »
Hi Debs,

I am confused about "shadow space". I think it is above the return address, rather than below as you'd get with "enter". I may well be wrong about this. Caution!

Best,
Frank


Offline debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 221
  • Country: gb
    • GPUZoo
Re: allocating the shadow space
« Reply #4 on: January 10, 2022, 06:31:38 AM »
Ah, yes, you are right. According to the first hit on Google, it is 32 bytes above the return address, and has to be set up by the caller. I'd have to read up more to understand how this wouldn't destroy code that may be re-executed (in the case of loops or callable routines routines, etc) :)
My graphics card database: www.gpuzoo.com