Skip to content

Commit 899c232

Browse files
mertakmangopherbot
authored andcommitted
windows/mkwinsyscall: use syscall.SyscallN instead of syscall.Syscall{6,9,12,15}
Replace syscall.Syscall6, Syscall9, Syscall12, and Syscall15 with syscall.SyscallN for Go 1.18+. This simplifies system calls by allowing the exact number of arguments needed, eliminating zero padding and reducing potential errors. Updated TestSyscallXGeneration to TestSyscallNGeneration to verify correct SyscallN generation for different argument counts. Change-Id: Iaf01c7bddd7ad6a80ee462879e382b0066f35b4d Reviewed-on: https://go-review.googlesource.com/c/sys/+/691715 Reviewed-by: Quim Muntal <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 543f21a commit 899c232

File tree

4 files changed

+504
-540
lines changed

4 files changed

+504
-540
lines changed

windows/mkwinsyscall/mkwinsyscall.go

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import (
6262
"path/filepath"
6363
"runtime"
6464
"sort"
65-
"strconv"
6665
"strings"
6766
"text/template"
6867
)
@@ -543,47 +542,9 @@ func (f *Fn) ParamPrintList() string {
543542
return join(f.Params, func(p *Param) string { return fmt.Sprintf(`"%s=", %s, `, p.Name, p.Name) }, `", ", `)
544543
}
545544

546-
// ParamCount return number of syscall parameters for function f.
547-
func (f *Fn) ParamCount() int {
548-
n := 0
549-
for _, p := range f.Params {
550-
n += len(p.SyscallArgList())
551-
}
552-
return n
553-
}
554-
555-
// SyscallParamCount determines which version of Syscall/Syscall6/Syscall9/...
556-
// to use. It returns parameter count for correspondent SyscallX function.
557-
func (f *Fn) SyscallParamCount() int {
558-
n := f.ParamCount()
559-
switch {
560-
case n <= 3:
561-
return 3
562-
case n <= 6:
563-
return 6
564-
case n <= 9:
565-
return 9
566-
case n <= 12:
567-
return 12
568-
case n <= 15:
569-
return 15
570-
case n <= 42: // current SyscallN limit
571-
return n
572-
default:
573-
panic("too many arguments to system call")
574-
}
575-
}
576-
577-
// Syscall determines which SyscallX function to use for function f.
578-
func (f *Fn) Syscall() string {
579-
c := f.SyscallParamCount()
580-
if c == 3 {
581-
return syscalldot() + "Syscall"
582-
}
583-
if c > 15 {
584-
return syscalldot() + "SyscallN"
585-
}
586-
return syscalldot() + "Syscall" + strconv.Itoa(c)
545+
// SyscallN returns a string representing the SyscallN function.
546+
func (f *Fn) SyscallN() string {
547+
return syscalldot() + "SyscallN"
587548
}
588549

589550
// SyscallParamList returns source code for SyscallX parameters for function f.
@@ -592,9 +553,12 @@ func (f *Fn) SyscallParamList() string {
592553
for _, p := range f.Params {
593554
a = append(a, p.SyscallArgList()...)
594555
}
595-
for len(a) < f.SyscallParamCount() {
596-
a = append(a, "0")
556+
557+
// Check if the number exceeds the current SyscallN limit
558+
if len(a) > 42 {
559+
panic("too many arguments to system call")
597560
}
561+
598562
return strings.Join(a, ", ")
599563
}
600564

@@ -1015,7 +979,7 @@ func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
1015979
1016980
{{define "results"}}{{if .Rets.List}}{{.Rets.List}} {{end}}{{end}}
1017981
1018-
{{define "syscall"}}{{.Rets.SetReturnValuesCode}}{{.Syscall}}(proc{{.DLLFuncName}}.Addr(),{{if le .ParamCount 15}} {{.ParamCount}},{{end}} {{.SyscallParamList}}){{end}}
982+
{{define "syscall"}}{{.Rets.SetReturnValuesCode}}{{.SyscallN}}(proc{{.DLLFuncName}}.Addr(), {{.SyscallParamList}}){{end}}
1019983
1020984
{{define "tmpvarsreadback"}}{{range .Params}}{{if .TmpVarReadbackCode}}
1021985
{{.TmpVarReadbackCode}}{{end}}{{end}}{{end}}

windows/mkwinsyscall/mkwinsyscall_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ func TestDLLFilenameEscaping(t *testing.T) {
5050
}
5151
}
5252

53-
func TestSyscallXGeneration(t *testing.T) {
53+
func TestSyscallNGeneration(t *testing.T) {
5454
tests := []struct {
5555
name string
5656
wantsysfunc string
5757
sig string
5858
}{
5959
{
6060
name: "syscall with 2 params",
61-
wantsysfunc: "syscall.Syscall",
61+
wantsysfunc: "syscall.SyscallN",
6262
sig: "Example(a1 *uint16, a2 *uint16) = ",
6363
},
6464
{
6565
name: "syscall with 6 params",
66-
wantsysfunc: "syscall.Syscall6",
66+
wantsysfunc: "syscall.SyscallN",
6767
sig: "Example(a1 *uint, a2 *uint, a3 *uint, a4 *uint, a5 *uint, a6 *uint) = ",
6868
},
6969
{
7070
name: "syscall with 15 params",
71-
wantsysfunc: "syscall.Syscall15",
71+
wantsysfunc: "syscall.SyscallN",
7272
sig: strings.ReplaceAll(`Example(a1 *uint, a2 *uint, a3 *uint, a4 *uint, a5 *uint, a6 *uint,
7373
a7 *uint, a8 *uint, a9 *uint, a10 *uint, a11 *uint, a12 *uint,
7474
a13 *uint, a14 *uint, a15 *uint) = `, "\n", ""),

windows/registry/zsyscall_windows.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)