From 2263d6037f8ef6b8d5bef14117ad1a17c32a20b9 Mon Sep 17 00:00:00 2001 From: Emil Taylor Bye Date: Fri, 26 Dec 2025 16:04:51 +0100 Subject: [PATCH] Fix closures using FFI_REGISTER ABI (#949) * Fix closures using FFI_REGISTER ABI ffi_closure_REGISTER had the positions of the closure and return address switched, resulting in a segfault when calling a closure created with the FFI_REGISTER ABI. * Fix comment and remove redundant assembly * After fixing closures using FFI_REGISTER ABI, the comments were incorrect, not reflecting the value that was actually loaded into eax and ecx. * Additionally, ecx was loaded with the data at esp + closure_FS only to write it back to esp + closure_FS. Both commands were removed as they end up not having any effect as ecx is overwritten during FFI_CLOSURE_COPY_TRAMP_DATA. --- src/x86/sysv.S | 4 +--- src/x86/sysv_intel.S | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/x86/sysv.S b/src/x86/sysv.S index c7a0fb51..cfcd103d 100644 --- a/src/x86/sysv.S +++ b/src/x86/sysv.S @@ -458,9 +458,7 @@ L(UW24): L(UW25): # cfi_def_cfa_offset(closure_FS + 4) FFI_CLOSURE_SAVE_REGS - movl closure_FS-4(%esp), %ecx /* load retaddr */ - movl closure_FS(%esp), %eax /* load closure */ - movl %ecx, closure_FS(%esp) /* move retaddr */ + movl closure_FS-4(%esp), %eax /* load closure */ jmp L(do_closure_REGISTER) L(UW26): # cfi_endproc diff --git a/src/x86/sysv_intel.S b/src/x86/sysv_intel.S index cccdf7d7..d9c63961 100644 --- a/src/x86/sysv_intel.S +++ b/src/x86/sysv_intel.S @@ -394,9 +394,7 @@ L(UW24): L(UW25): /* cfi_def_cfa_offset(closure_FS + 4) */ FFI_CLOSURE_SAVE_REGS - mov ecx, [esp+closure_FS-4] /* load retaddr */ - mov eax, [esp+closure_FS] /* load closure */ - mov [esp+closure_FS], ecx /* move retaddr */ + mov eax, [esp+closure_FS-4] /* load closure */ jmp L(do_closure_REGISTER) L(UW26): /* cfi_endproc */