Skip to content

Commit 3fc0c5f

Browse files
hajimehoshiclaude
andcommitted
all: align the stack for C calls on linux/386 (#558)
The i386 System V ABI requires the stack pointer to be 16-byte aligned at a call, and compilers rely on it for aligned SSE stack accesses such as movaps, which fault otherwise. Two linux/386 trampolines broke this. syscallX is entered on an aligned stack from runtime.asmcgocall, but its register saves and fixed frame left ESP at 12 mod 16 at the call. linux/386 uses this path with Cgo enabled too. call5 in fakecgo, which calls pthread_create, malloc, setenv and others, did not realign at all, so its alignment depended on the calling Go frame. Save SP in the callee-saved BP and align SP with ANDL before placing the arguments in both trampolines, as call5 already does on amd64. Add a test that calls a naked C function returning the caller's stack pointer modulo 16 at the call. It runs on linux/386 and on amd64, where the alignment in syscallX depends on its frame size. Also rename the FP operands in the 386 setg_trampoline and call5 to match their Go declarations, which fixes go vet's asmdecl reports. The generated code is unchanged. Closes #458 Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9eba627 commit 3fc0c5f

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎func_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ func TestRegisterLibFunc_Bool(t *testing.T) {
181181
}
182182
}
183183

184+
func TestRegisterFunc_StackAlignment(t *testing.T) {
185+
if runtime.GOARCH != "amd64" && (runtime.GOOS != "linux" || runtime.GOARCH != "386") {
186+
t.Skip("stack alignment is checked only on amd64 and linux/386")
187+
}
188+
libFileName := filepath.Join(t.TempDir(), "abitest.so")
189+
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "abitest", "abi_test.c")); err != nil {
190+
t.Fatal(err)
191+
}
192+
lib, err := load.OpenLibrary(libFileName)
193+
if err != nil {
194+
t.Fatal(err)
195+
}
196+
defer func() {
197+
if err := load.CloseLibrary(lib); err != nil {
198+
t.Error(err)
199+
}
200+
}()
201+
var fn func() uint32
202+
purego.RegisterLibFunc(&fn, lib, "stack_misalignment")
203+
if got := fn(); got != 0 {
204+
t.Errorf("stack pointer modulo 16 at the C call: got %d, want 0", got)
205+
}
206+
}
207+
184208
func TestABI(t *testing.T) {
185209
libFileName := filepath.Join(t.TempDir(), "abitest.so")
186210
t.Logf("Build %v", libFileName)

‎internal/fakecgo/trampolines_386.s‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEXT x_cgo_bindm_trampoline(SB), NOSPLIT, $0
7272
// func setg_trampoline(setg uintptr, g uintptr)
7373
// This is called from Go, so args are at normal FP positions
7474
TEXT ·setg_trampoline(SB), NOSPLIT, $4-8
75-
MOVL g+4(FP), AX
75+
MOVL G+4(FP), AX
7676
MOVL setg+0(FP), BX
7777

7878
// setg expects g in 0(SP)
@@ -102,20 +102,29 @@ TEXT threadentry_trampoline(SB), NOSPLIT, $24-4
102102
MOVL 20(SP), BP
103103
RET
104104

105-
TEXT ·call5(SB), NOSPLIT, $24-28
105+
TEXT ·call5(SB), NOSPLIT, $0-28
106106
MOVL fn+0(FP), AX
107107
MOVL a1+4(FP), BX
108108
MOVL a2+8(FP), CX
109109
MOVL a3+12(FP), DX
110110
MOVL a4+16(FP), SI
111111
MOVL a5+20(FP), DI
112112

113-
// Place arguments on local stack frame for C calling convention
113+
PUSHL BP // save BP
114+
MOVL SP, BP // save SP inside BP bc BP is callee-saved
115+
SUBL $20, SP // allocate space for the C arguments
116+
ANDL $~15, SP // align on 16 bytes as the i386 System V ABI requires
117+
118+
// Place arguments on the aligned stack for C calling convention
114119
MOVL BX, 0(SP) // a1
115120
MOVL CX, 4(SP) // a2
116121
MOVL DX, 8(SP) // a3
117122
MOVL SI, 12(SP) // a4
118123
MOVL DI, 16(SP) // a5
119124
CALL AX
120-
MOVL AX, r1+24(FP)
125+
126+
MOVL BP, SP // get SP back
127+
POPL BP // restore BP
128+
129+
MOVL AX, ret+24(FP)
121130
RET

‎sys_386.s‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ TEXT syscallX(SB), NOSPLIT|NOFRAME, $0-0
4545

4646
// Allocate stack space for C function arguments
4747
// i386 SysV: all 32 args on stack = 32 * 4 = 128 bytes
48-
// Plus 16 bytes for alignment and local storage
48+
// Plus local storage for the fn and args pointers
49+
// The i386 System V ABI requires SP to be 16-byte aligned at the call.
50+
// Align it here regardless of the caller's alignment, and keep the
51+
// unaligned SP in BP, which is callee-saved, to restore it after the call.
52+
MOVL SP, BP
4953
SUBL $STACK_SIZE, SP
54+
ANDL $~15, SP
5055
MOVL BX, PTR_ADDRESS(SP) // save args pointer
5156

5257
// Load function pointer
@@ -137,7 +142,7 @@ TEXT syscallX(SB), NOSPLIT|NOFRAME, $0-0
137142
FMOVDP F0, syscallArgs_f1(BX)
138143

139144
// Clean up stack
140-
ADDL $STACK_SIZE, SP
145+
MOVL BP, SP
141146

142147
// Restore callee-saved registers
143148
POPL DI

‎testdata/abitest/abi_test.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,21 @@ AddFunc return_func_ptr(void) {
211211
AddFunc return_null_func_ptr(void) {
212212
return NULL;
213213
}
214+
215+
#if defined(__i386__) || defined(__x86_64__)
216+
// stack_misalignment returns the caller's stack pointer modulo 16 at the call.
217+
// The i386 System V and x86-64 ABIs require it to be 0.
218+
__attribute__((naked)) uint32_t stack_misalignment(void) {
219+
#if defined(__i386__)
220+
__asm__(
221+
"leal 4(%esp), %eax\n\t"
222+
"andl $15, %eax\n\t"
223+
"ret");
224+
#else
225+
__asm__(
226+
"leaq 8(%rsp), %rax\n\t"
227+
"andl $15, %eax\n\t"
228+
"ret");
229+
#endif
230+
}
231+
#endif

0 commit comments

Comments
 (0)