Skip to content

Commit

Permalink
windows/mkwinsyscall: use syscall.SyscallN instead of syscall.Syscall…
Browse files Browse the repository at this point in the history
…{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: Icd6662b591d7548e367b88f34243f5529e177eab
GitHub-Last-Rev: c06fca1
GitHub-Pull-Request: #219
Reviewed-on: https://go-review.googlesource.com/c/sys/+/614082
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Alex Brainman <[email protected]>
Reviewed-by: Quim Muntal <[email protected]>
  • Loading branch information
mertakman authored and gopherbot committed Oct 21, 2024
1 parent a57fdb8 commit adbb8bb
Show file tree
Hide file tree
Showing 4 changed files with 496 additions and 532 deletions.
54 changes: 9 additions & 45 deletions windows/mkwinsyscall/mkwinsyscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import (
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"text/template"
)
Expand Down Expand Up @@ -543,47 +542,9 @@ func (f *Fn) ParamPrintList() string {
return join(f.Params, func(p *Param) string { return fmt.Sprintf(`"%s=", %s, `, p.Name, p.Name) }, `", ", `)
}

// ParamCount return number of syscall parameters for function f.
func (f *Fn) ParamCount() int {
n := 0
for _, p := range f.Params {
n += len(p.SyscallArgList())
}
return n
}

// SyscallParamCount determines which version of Syscall/Syscall6/Syscall9/...
// to use. It returns parameter count for correspondent SyscallX function.
func (f *Fn) SyscallParamCount() int {
n := f.ParamCount()
switch {
case n <= 3:
return 3
case n <= 6:
return 6
case n <= 9:
return 9
case n <= 12:
return 12
case n <= 15:
return 15
case n <= 42: // current SyscallN limit
return n
default:
panic("too many arguments to system call")
}
}

// Syscall determines which SyscallX function to use for function f.
func (f *Fn) Syscall() string {
c := f.SyscallParamCount()
if c == 3 {
return syscalldot() + "Syscall"
}
if c > 15 {
return syscalldot() + "SyscallN"
}
return syscalldot() + "Syscall" + strconv.Itoa(c)
// SyscallN returns a string representing the SyscallN function.
func (f *Fn) SyscallN() string {
return syscalldot() + "SyscallN"
}

// SyscallParamList returns source code for SyscallX parameters for function f.
Expand All @@ -592,9 +553,12 @@ func (f *Fn) SyscallParamList() string {
for _, p := range f.Params {
a = append(a, p.SyscallArgList()...)
}
for len(a) < f.SyscallParamCount() {
a = append(a, "0")

// Check if the number exceeds the current SyscallN limit
if len(a) > 42 {
panic("too many arguments to system call")
}

return strings.Join(a, ", ")
}

Expand Down Expand Up @@ -1015,7 +979,7 @@ func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
{{define "results"}}{{if .Rets.List}}{{.Rets.List}} {{end}}{{end}}
{{define "syscall"}}{{.Rets.SetReturnValuesCode}}{{.Syscall}}(proc{{.DLLFuncName}}.Addr(),{{if le .ParamCount 15}} {{.ParamCount}},{{end}} {{.SyscallParamList}}){{end}}
{{define "syscall"}}{{.Rets.SetReturnValuesCode}}{{.SyscallN}}(proc{{.DLLFuncName}}.Addr(), {{.SyscallParamList}}){{end}}
{{define "tmpvarsreadback"}}{{range .Params}}{{if .TmpVarReadbackCode}}
{{.TmpVarReadbackCode}}{{end}}{{end}}{{end}}
Expand Down
8 changes: 4 additions & 4 deletions windows/mkwinsyscall/mkwinsyscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ func TestDLLFilenameEscaping(t *testing.T) {
}
}

func TestSyscallXGeneration(t *testing.T) {
func TestSyscallNGeneration(t *testing.T) {
tests := []struct {
name string
wantsysfunc string
sig string
}{
{
name: "syscall with 2 params",
wantsysfunc: "syscall.Syscall",
wantsysfunc: "syscall.SyscallN",
sig: "Example(a1 *uint16, a2 *uint16) = ",
},
{
name: "syscall with 6 params",
wantsysfunc: "syscall.Syscall6",
wantsysfunc: "syscall.SyscallN",
sig: "Example(a1 *uint, a2 *uint, a3 *uint, a4 *uint, a5 *uint, a6 *uint) = ",
},
{
name: "syscall with 15 params",
wantsysfunc: "syscall.Syscall15",
wantsysfunc: "syscall.SyscallN",
sig: strings.ReplaceAll(`Example(a1 *uint, a2 *uint, a3 *uint, a4 *uint, a5 *uint, a6 *uint,
a7 *uint, a8 *uint, a9 *uint, a10 *uint, a11 *uint, a12 *uint,
a13 *uint, a14 *uint, a15 *uint) = `, "\n", ""),
Expand Down
16 changes: 8 additions & 8 deletions windows/registry/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit adbb8bb

Please sign in to comment.