bobjervis / parasol

The Parasol Language and related core development tools
Apache License 2.0
2 stars 2 forks source link

Stack alignment #10

Closed bobjervis closed 8 years ago

bobjervis commented 9 years ago

Windows 64 mandates a stack alignment on a 16 byte boundary. The compiler does not enforce this alignment.

There are several ways that stack adjustments happen:

  1. Initial register pushes (RBP, RSI, etc.) on entry to a function.
  2. Reserving stack space for locals.
  3. Pushing stack arguments.
  4. Spilling temporary registers.

In each of these circumstances, we need to figure out how to do the stack alignment. For cases 1 & 2, they are done at the same time, so it should be possible to adjust the reserved space and add 8 bytes if that were to create an issue. I don't think that there are any addressing issues that could arise form ensuring that the initially allocated stack frame is 16-byte aligned, including register pushes. Spills and stack arguments are a bit more of a challenge, as that requires careful tracking of stack alignment and then judicious insertion of stack alignment code, just after temps that span a call are pushed, but just before stack arguments are pushed, we need to align the stack. This last calculation has to take into account the stack arguments that will be pushed for the upcoming call.

bobjervis commented 9 years ago

I fixed cases 1 & 2, which allows some non-trivial amount of code to work properly, but there are still ways to generate junk onto the stack to misalign things, and if they are anywhere in the call stack when calling out to C++ code that cares (and random things do), you could still get access exceptions.

bobjervis commented 8 years ago

Spilling temps (case 4 above) have been fixed. I have not seen a stack alignment issue surface in a while and I keep expanding the native bindings to include more code (recently including socket methods).

Even handling of stack arguments now works correctly in most instances.