Skip to content

Commit

Permalink
runtime: support nil libcall.args when calling stdcall on Windows
Browse files Browse the repository at this point in the history
Having to pass a dummy pointer to the libcall.args field is a bit
annoying. This change allows nil to be passed instead.

windows/arm and windows/arm64 already support nil libcall.args.

Change-Id: I07a2bdb7d1f76b13d125397ff5177337c43536a3
Reviewed-on: https://go-review.googlesource.com/c/go/+/526016
Reviewed-by: Ian Lance Taylor <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
  • Loading branch information
qmuntal committed Sep 7, 2023
1 parent 3a124de commit 1d538f1
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/runtime/os_windows.go
Original file line number Diff line number Diff line change
@@ -985,7 +985,7 @@ func stdcall(fn stdFunction) uintptr {
func stdcall0(fn stdFunction) uintptr {
mp := getg().m
mp.libcall.n = 0
mp.libcall.args = uintptr(noescape(unsafe.Pointer(&fn))) // it's unused but must be non-nil, otherwise crashes
mp.libcall.args = 0
return stdcall(fn)
}

10 changes: 8 additions & 2 deletions src/runtime/sys_windows_386.s
Original file line number Diff line number Diff line change
@@ -14,13 +14,18 @@
// void runtime·asmstdcall(void *c);
TEXT runtime·asmstdcall(SB),NOSPLIT,$0
MOVL fn+0(FP), BX
MOVL SP, BP // save stack pointer

// SetLastError(0).
MOVL $0, 0x34(FS)

MOVL libcall_n(BX), CX

// Fast version, do not store args on the stack.
CMPL CX, $0
JE docall

// Copy args to the stack.
MOVL SP, BP
MOVL libcall_n(BX), CX // words
MOVL CX, AX
SALL $2, AX
SUBL AX, SP // room for args
@@ -29,6 +34,7 @@ TEXT runtime·asmstdcall(SB),NOSPLIT,$0
CLD
REP; MOVSL

docall:
// Call stdcall or cdecl function.
// DI SI BP BX are preserved, SP is not
CALL libcall_fn(BX)
6 changes: 6 additions & 0 deletions src/runtime/sys_windows_amd64.s
Original file line number Diff line number Diff line change
@@ -29,6 +29,11 @@ TEXT runtime·asmstdcall(SB),NOSPLIT,$16

SUBQ $(const_maxArgs*8), SP // room for args

// Fast version, do not store args on the stack nor
// load them into registers.
CMPL CX, $0
JE docall

// Fast version, do not store args on the stack.
CMPL CX, $4
JLE loadregs
@@ -59,6 +64,7 @@ loadregs:
MOVQ R8, X2
MOVQ R9, X3

docall:
// Call stdcall function.
CALL AX

0 comments on commit 1d538f1

Please sign in to comment.