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 15:49:58 +01:00
parent 83cfaa298f
commit cec591ef54

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
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:
mkwinsyscall [flags] [path ...]
@@ -342,6 +345,7 @@ type Fn struct {
Params []*Param
Rets *Rets
PrintTrace bool
MaybeAbsent bool
dllname string
dllfuncname string
src string
@@ -469,6 +473,10 @@ func newFn(s string) (*Fn, error) {
default:
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
}
@@ -899,7 +907,7 @@ func {{.Name}}({{.ParamList}}) {{template "results" .}}{
{{define "funcbody"}}
func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
{{template "tmpvars" .}} {{template "syscall" .}} {{template "tmpvarsreadback" .}}
{{template "maybeabsent" .}} {{template "tmpvars" .}} {{template "syscall" .}} {{template "tmpvarsreadback" .}}
{{template "seterror" .}}{{template "printtrace" .}} return
}
{{end}}
@@ -907,6 +915,10 @@ func {{.HelperName}}({{.HelperParamList}}) {{template "results" .}}{
{{define "helpertmpvars"}}{{range .Params}}{{if .TmpVarHelperCode}} {{.TmpVarHelperCode}}
{{end}}{{end}}{{end}}
{{define "maybeabsent"}}{{if .MaybeAbsent}}err = proc{{.DLLFuncName}}.Find()
if err != nil { return }
{{end}}{{end}}
{{define "tmpvars"}}{{range .Params}}{{if .TmpVarCode}} {{.TmpVarCode}}
{{end}}{{end}}{{end}}