Skip to content

Commit

Permalink
i#58 MacOS: drwrap stack alignment
Browse files Browse the repository at this point in the history
Preserve stack alignment inside drwrap's replace_native_xfer asm routine,
as the callouts it makes can end up going to the dyld resolver and crashing
if not aligned.

SVN-Revision: 2540
  • Loading branch information
derekbruening committed Feb 19, 2014
1 parent 12c4990 commit bda4964
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions ext/drwrap/drwrap_asm.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* **********************************************************
* Copyright (c) 2012-2013 Google, Inc. All rights reserved.
* Copyright (c) 2012-2014 Google, Inc. All rights reserved.
* ********************************************************** */

/*
Expand Down Expand Up @@ -65,12 +65,18 @@ GLOBAL_LABEL(FUNCNAME:)
*
* XXX: what about xmm0 return reg?
*/
#ifdef MACOS
push 0 /* maintain 16-byte alignment for call */
#endif
push REG_XAX
push REG_XDX
CALLC0(GLOBAL_REF(replace_native_xfer_stack_adjust))
mov ecx, eax
pop REG_XDX
pop REG_XAX
#ifdef MACOS
lea REG_XSP, [ ARG_SZ + REG_XSP ]
#endif
/* DrMem i#1217: zero out these slots as they can contain retaddrs, which
* messes up high-performance callstack stack scans. This is beyond TOS,
* but these are writes so it's signal-safe.
Expand All @@ -82,16 +88,37 @@ GLOBAL_LABEL(FUNCNAME:)
sub REG_XSP, REG_XCX
/* make space for app retaddr */
push REG_XAX
/* put app retaddr into the slot we made above the 2 pushes */

push REG_XAX
push REG_XDX

#ifdef MACOS
/* We have to align the stack for our calls: o/w dyld lazy resolution
* crashes there. Current alignment could be anything so we save a ptr
* and mask off esp.
*/
push REG_XBX
mov REG_XBX, REG_XSP
and REG_XSP, -16 /* align to 16 */
#endif

/* put app retaddr into the slot we made above the 2 pushes */
CALLC0(GLOBAL_REF(replace_native_xfer_app_retaddr))
#ifdef MACOS
mov PTRSZ [3 * ARG_SZ + REG_XBX], REG_XAX
#else
mov PTRSZ [2 * ARG_SZ + REG_XSP], REG_XAX
#endif

/* now get our target */
CALLC0(GLOBAL_REF(replace_native_xfer_target))
mov REG_XCX, REG_XAX

#ifdef MACOS
mov REG_XSP, REG_XBX
pop REG_XBX
#endif

pop REG_XDX
pop REG_XAX
/* DrMem i#1217: zero out these slots as they can contain retaddrs, which
Expand Down

0 comments on commit bda4964

Please sign in to comment.