Windows/x86: Make it safe to throw Lua errors in SEH contexts.

This commit is contained in:
Peter Cawley 2016-04-26 23:28:06 +01:00
parent 7b26e9c998
commit c31d693761
2 changed files with 66 additions and 3 deletions

View File

@ -60,6 +60,16 @@ enum {
/* These definitions must match with the arch-specific *.dasc files. */ /* These definitions must match with the arch-specific *.dasc files. */
#if LJ_TARGET_X86 #if LJ_TARGET_X86
#if LJ_ABI_WIN
#define CFRAME_OFS_ERRF (19*4)
#define CFRAME_OFS_NRES (18*4)
#define CFRAME_OFS_PREV (17*4)
#define CFRAME_OFS_L (16*4)
#define CFRAME_OFS_PC (6*4)
#define CFRAME_OFS_MULTRES (5*4)
#define CFRAME_SIZE (16*4)
#define CFRAME_SHIFT_MULTRES 0
#else
#define CFRAME_OFS_ERRF (15*4) #define CFRAME_OFS_ERRF (15*4)
#define CFRAME_OFS_NRES (14*4) #define CFRAME_OFS_NRES (14*4)
#define CFRAME_OFS_PREV (13*4) #define CFRAME_OFS_PREV (13*4)
@ -68,6 +78,7 @@ enum {
#define CFRAME_OFS_MULTRES (5*4) #define CFRAME_OFS_MULTRES (5*4)
#define CFRAME_SIZE (12*4) #define CFRAME_SIZE (12*4)
#define CFRAME_SHIFT_MULTRES 0 #define CFRAME_SHIFT_MULTRES 0
#endif
#elif LJ_TARGET_X64 #elif LJ_TARGET_X64
#if LJ_ABI_WIN #if LJ_ABI_WIN
#define CFRAME_OFS_PREV (13*8) #define CFRAME_OFS_PREV (13*8)

View File

@ -121,19 +121,62 @@
|//----------------------------------------------------------------------- |//-----------------------------------------------------------------------
|.if not X64 // x86 stack layout. |.if not X64 // x86 stack layout.
| |
|.if WIN
|.define CFRAME_SPACE, aword*9 // Delta for esp (see <--).
|.macro saveregs_
| push edi; push esi; push ebx
| push 0
| fs; push dword [0]
| sub esp, CFRAME_SPACE
|.endmacro
|.macro restoreregs
| add esp, CFRAME_SPACE
| fs; pop dword [0]
| pop edi // Just to increment esp by 4
| pop ebx; pop esi; pop edi; pop ebp
|.endmacro
|.else
|.define CFRAME_SPACE, aword*7 // Delta for esp (see <--). |.define CFRAME_SPACE, aword*7 // Delta for esp (see <--).
|.macro saveregs_ |.macro saveregs_
| push edi; push esi; push ebx | push edi; push esi; push ebx
| sub esp, CFRAME_SPACE | sub esp, CFRAME_SPACE
|.endmacro |.endmacro
|.macro saveregs
| push ebp; saveregs_
|.endmacro
|.macro restoreregs |.macro restoreregs
| add esp, CFRAME_SPACE | add esp, CFRAME_SPACE
| pop ebx; pop esi; pop edi; pop ebp | pop ebx; pop esi; pop edi; pop ebp
|.endmacro |.endmacro
|.endif
|.macro saveregs
| push ebp; saveregs_
|.endmacro
| |
|.if WIN
|.define SAVE_ERRF, aword [esp+aword*19] // vm_pcall/vm_cpcall only.
|.define SAVE_NRES, aword [esp+aword*18]
|.define SAVE_CFRAME, aword [esp+aword*17]
|.define SAVE_L, aword [esp+aword*16]
|//----- 16 byte aligned, ^^^ arguments from C caller
|.define SAVE_RET, aword [esp+aword*15] //<-- esp entering interpreter.
|.define SAVE_R4, aword [esp+aword*14]
|.define SAVE_R3, aword [esp+aword*13]
|.define SAVE_R2, aword [esp+aword*12]
|//----- 16 byte aligned
|.define SAVE_R1, aword [esp+aword*11]
|.define SEH_FUNC, aword [esp+aword*10]
|.define SEH_NEXT, aword [esp+aword*9] //<-- esp after register saves.
|.define UNUSED2, aword [esp+aword*8]
|//----- 16 byte aligned
|.define UNUSED1, aword [esp+aword*7]
|.define SAVE_PC, aword [esp+aword*6]
|.define TMP2, aword [esp+aword*5]
|.define TMP1, aword [esp+aword*4]
|//----- 16 byte aligned
|.define ARG4, aword [esp+aword*3]
|.define ARG3, aword [esp+aword*2]
|.define ARG2, aword [esp+aword*1]
|.define ARG1, aword [esp] //<-- esp while in interpreter.
|//----- 16 byte aligned, ^^^ arguments for C callee
|.else
|.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only. |.define SAVE_ERRF, aword [esp+aword*15] // vm_pcall/vm_cpcall only.
|.define SAVE_NRES, aword [esp+aword*14] |.define SAVE_NRES, aword [esp+aword*14]
|.define SAVE_CFRAME, aword [esp+aword*13] |.define SAVE_CFRAME, aword [esp+aword*13]
@ -154,6 +197,7 @@
|.define ARG2, aword [esp+aword*1] |.define ARG2, aword [esp+aword*1]
|.define ARG1, aword [esp] //<-- esp while in interpreter. |.define ARG1, aword [esp] //<-- esp while in interpreter.
|//----- 16 byte aligned, ^^^ arguments for C callee |//----- 16 byte aligned, ^^^ arguments for C callee
|.endif
| |
|// FPARGx overlaps ARGx and ARG(x+1) on x86. |// FPARGx overlaps ARGx and ARG(x+1) on x86.
|.define FPARG3, qword [esp+qword*1] |.define FPARG3, qword [esp+qword*1]
@ -555,6 +599,10 @@ static void build_subroutines(BuildCtx *ctx)
|.else |.else
| mov eax, FCARG2 // Error return status for vm_pcall. | mov eax, FCARG2 // Error return status for vm_pcall.
| mov esp, FCARG1 | mov esp, FCARG1
|.if WIN
| mov FCARG1, SEH_NEXT
| fs; mov [0], FCARG1
|.endif
|.endif |.endif
|->vm_unwind_c_eh: // Landing pad for external unwinder. |->vm_unwind_c_eh: // Landing pad for external unwinder.
| mov L:RB, SAVE_L | mov L:RB, SAVE_L
@ -578,6 +626,10 @@ static void build_subroutines(BuildCtx *ctx)
|.else |.else
| and FCARG1, CFRAME_RAWMASK | and FCARG1, CFRAME_RAWMASK
| mov esp, FCARG1 | mov esp, FCARG1
|.if WIN
| mov FCARG1, SEH_NEXT
| fs; mov [0], FCARG1
|.endif
|.endif |.endif
|->vm_unwind_ff_eh: // Landing pad for external unwinder. |->vm_unwind_ff_eh: // Landing pad for external unwinder.
| mov L:RB, SAVE_L | mov L:RB, SAVE_L