windows: add potentially absent function marker to mkwinsyscall

Some functions that x/sys/windows will eventually have won't be
available on older versions of Windows, and in general we can expect
Microsoft to continue to add functions to newer builds of Windows
10. Therefore, we introduces a new notation for marking functions that
might not exist, letting the caller handle the situation without a
panic.

Change-Id: Ia66bf4aab601357198872c5cd29b6ca7c3bc6969
Reviewed-on: https://go-review.googlesource.com/c/sys/+/269938
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld
2020-11-13 23:29:40 +00:00
parent 83cfaa298f
commit cec591ef54
+13 -1
View File
@@ -31,6 +31,9 @@ like func declarations if //sys is replaced by func, but:
//sys LoadLibrary(libname string) (handle uint32, err error) [failretval==-1] = LoadLibraryA //sys LoadLibrary(libname string) (handle uint32, err error) [failretval==-1] = LoadLibraryA
and is [failretval==0] by default. and is [failretval==0] by default.
* If the function name ends in a "?", then the function not existing is non-
fatal, and an error will be returned instead of panicking.
Usage: Usage:
mkwinsyscall [flags] [path ...] mkwinsyscall [flags] [path ...]
@@ -342,6 +345,7 @@ type Fn struct {
Params []*Param Params []*Param
Rets *Rets Rets *Rets
PrintTrace bool PrintTrace bool
MaybeAbsent bool
dllname string dllname string
dllfuncname string dllfuncname string
src string src string
@@ -469,6 +473,10 @@ func newFn(s string) (*Fn, error) {
default: default:
return nil, errors.New("Could not extract dll name from \"" + f.src + "\"") return nil, errors.New("Could not extract dll name from \"" + f.src + "\"")
} }
if n := f.dllfuncname; strings.HasSuffix(n, "?") {
f.dllfuncname = n[:len(n)-1]
f.MaybeAbsent = true
}
return f, nil return f, nil
} }
@@ -899,7 +907,7 @@ func {{.Name}}({{.ParamList}}) {{template "results" .}}{
{{define "funcbody"}} {{define "funcbody"}}
func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{ func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
{{template "tmpvars" .}} {{template "syscall" .}} {{template "tmpvarsreadback" .}} {{template "maybeabsent" .}} {{template "tmpvars" .}} {{template "syscall" .}} {{template "tmpvarsreadback" .}}
{{template "seterror" .}}{{template "printtrace" .}} return {{template "seterror" .}}{{template "printtrace" .}} return
} }
{{end}} {{end}}
@@ -907,6 +915,10 @@ func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
{{define "helpertmpvars"}}{{range .Params}}{{if .TmpVarHelperCode}} {{.TmpVarHelperCode}} {{define "helpertmpvars"}}{{range .Params}}{{if .TmpVarHelperCode}} {{.TmpVarHelperCode}}
{{end}}{{end}}{{end}} {{end}}{{end}}{{end}}
{{define "maybeabsent"}}{{if .MaybeAbsent}}err = proc{{.DLLFuncName}}.Find()
if err != nil { return }
{{end}}{{end}}
{{define "tmpvars"}}{{range .Params}}{{if .TmpVarCode}} {{.TmpVarCode}} {{define "tmpvars"}}{{range .Params}}{{if .TmpVarCode}} {{.TmpVarCode}}
{{end}}{{end}}{{end}} {{end}}{{end}}{{end}}