Skip to content

Commit fe2b719

Browse files
committed
[x64] Fix stack manipulation in EmitHostToGuestThunk
To access the stack at adresses higher than the stack pointer, it must be moved down (subtracted) before the accesses. If not, the stack pointer still points to the previous stack frame, corrupting it. This fixes a crash on Linux when compiled in release mode with clang.
1 parent 528da1e commit fe2b719

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/xenia/cpu/backend/x64/x64_backend.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ HostToGuestThunk X64ThunkEmitter::EmitHostToGuestThunk() {
434434
code_offsets.prolog = getSize();
435435

436436
// rsp + 0 = return address
437+
sub(rsp, stack_size);
437438
mov(qword[rsp + offsetof(StackLayout::Thunk, arg_temp[2])], GetNativeReg(2));
438439
mov(qword[rsp + offsetof(StackLayout::Thunk, arg_temp[1])], GetNativeReg(1));
439440
mov(qword[rsp + offsetof(StackLayout::Thunk, arg_temp[0])], GetNativeReg(0));
440-
sub(rsp, stack_size);
441441

442442
code_offsets.prolog_stack_alloc = getSize();
443443
code_offsets.body = getSize();
@@ -454,10 +454,10 @@ HostToGuestThunk X64ThunkEmitter::EmitHostToGuestThunk() {
454454

455455
code_offsets.epilog = getSize();
456456

457-
add(rsp, stack_size);
458457
mov(GetNativeReg(0), qword[rsp + offsetof(StackLayout::Thunk, arg_temp[0])]);
459458
mov(GetNativeReg(1), qword[rsp + offsetof(StackLayout::Thunk, arg_temp[1])]);
460459
mov(GetNativeReg(2), qword[rsp + offsetof(StackLayout::Thunk, arg_temp[2])]);
460+
add(rsp, stack_size);
461461
ret();
462462

463463
code_offsets.tail = getSize();

0 commit comments

Comments
 (0)