Skip to content

Commit

Permalink
windows/svc: align stack in servicemain before calling syscall
Browse files Browse the repository at this point in the history
I noticed that we call syscall in servicemain without aligning stack.
That is against Windows rules, so align the stack as required.

I tried running this code with specifically non-aligned stack (I aligned
stack, and then subtracted 1 from SP) on my Windows 10 to test this
change. But it makes no difference on my Windows 10 PC - I built and run
golang.org/x/sys/windows/svc/example, and it runs successfully
regardless of stack alignment. But alignment might make difference on
other computers.

Maybe fixes golang/go#40160

Change-Id: I351f7f730fba4aa6dc409a79de4ad737b4a0a7d4
Reviewed-on: https://go-review.googlesource.com/c/sys/+/246317
Run-TryBot: Alex Brainman <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
alexbrainman committed Aug 2, 2020
1 parent 3e129f6 commit 4b90ce9
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions windows/svc/sys_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@

// func servicemain(argc uint32, argv **uint16)
TEXT ·servicemain(SB),7,$0
MOVQ SP, AX
ANDQ $~15, SP // alignment as per Windows requirement
SUBQ $48, SP // room for SP and 4 args as per Windows requirement
// plus one extra word to keep stack 16 bytes aligned
MOVQ AX, 32(SP)

MOVL CX, ·sArgc(SB)
MOVQ DX, ·sArgv(SB)

SUBQ $32, SP // stack for the first 4 syscall params

MOVQ ·sName(SB), CX
MOVQ $·servicectlhandler(SB), DX
// BUG(pastarmovj): Figure out a way to pass in context in R8.
Expand All @@ -32,7 +36,7 @@ TEXT ·servicemain(SB),7,$0
CALL AX

exit:
ADDQ $32, SP
MOVQ 32(SP), SP
RET

// I do not know why, but this seems to be the only way to call
Expand Down

0 comments on commit 4b90ce9

Please sign in to comment.