Skip to content

Commit 473be98

Browse files
parjongjkotas
authored andcommitted
[x86/Linux] Enforce 16-byte stack alignment (dotnet/coreclr#8587)
Clang (and GCC) requires 16-byte stack alignment, but the current implementation of CallDescrInternal and ThePreStub does not provide any guarantee on stack alignment. This commit adds 16-byte stack alignment adjust code inside these functions. Commit migrated from dotnet/coreclr@db52950
1 parent 74bcd97 commit 473be98

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/coreclr/src/pal/inc/unixasmmacrosx86.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ C_FUNC(\Name\()_End):
7676
movl C_FUNC(\Name)@GOT(%\Reg), %\Reg
7777
.intel_syntax noprefix
7878
.endm
79+
80+
.macro CHECK_STACK_ALIGNMENT
81+
#ifdef _DEBUG
82+
test esp, 0Fh
83+
je 0f
84+
int3
85+
0:
86+
#endif // _DEBUG
87+
.endm

src/coreclr/src/vm/i386/asmhelpers.S

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,15 @@ NESTED_ENTRY CallDescrWorkerInternal, _TEXT, NoHandler
405405

406406
mov ebx, [esp + ((2 + 1) * 4)]
407407

408+
// compute padding size
409+
mov eax, esp
410+
mov ecx, [ebx + CallDescrData__numStackSlots]
411+
shl ecx, 2
412+
sub eax, ecx
413+
and eax, 15
414+
// adjust stack offset
415+
sub esp, eax
416+
408417
// copy the stack
409418
mov ecx, [ebx +CallDescrData__numStackSlots]
410419
mov eax, [ebx +CallDescrData__pSrc]
@@ -431,6 +440,7 @@ LOCAL_LABEL(donestack):
431440
mov edx, DWORD PTR [eax]
432441
mov ecx, DWORD PTR [eax + 4]
433442

443+
CHECK_STACK_ALIGNMENT
434444
call [ebx + CallDescrData__pTarget]
435445
#ifdef _DEBUG
436446
nop // This is a tag that we use in an assert. Fcalls expect to
@@ -455,6 +465,9 @@ LOCAL_LABEL(ReturnsInt):
455465
mov [ebx + CallDescrData__returnValue + 4], edx
456466

457467
LOCAL_LABEL(Epilog):
468+
// restore the stake pointer
469+
lea esp, [ebp - 4]
470+
458471
EPILOG_BEG
459472
EPILOG_POP ebx
460473
EPILOG_END
@@ -996,19 +1009,29 @@ NESTED_ENTRY ThePreStub, _TEXT, NoHandler
9961009

9971010
mov esi, esp
9981011

1012+
// Compute padding size
1013+
lea ebx, [esp - 8]
1014+
and ebx, 15
1015+
// Adjust stack offset
1016+
sub esp, ebx
1017+
9991018
// EAX contains MethodDesc* from the precode. Push it here as argument
10001019
// for PreStubWorker
10011020
push eax
10021021

10031022
push esi
10041023

1024+
CHECK_STACK_ALIGNMENT
10051025
call C_FUNC(PreStubWorker)
10061026

10071027
// eax now contains replacement stub. PreStubWorker will never return
10081028
// NULL (it throws an exception if stub creation fails.)
10091029

10101030
// From here on, mustn't trash eax
10111031

1032+
// Restore stack pointer
1033+
mov esp, esi
1034+
10121035
STUB_EPILOG
10131036

10141037
// Tailcall target

0 commit comments

Comments
 (0)