From cec591ef54eecb509f406ffb172c2c0cf3219c96 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 13 Nov 2020 15:49:58 +0100 Subject: [PATCH] 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 TryBot-Result: Go Bot Reviewed-by: Alex Brainman Reviewed-by: Brad Fitzpatrick Trust: Jason A. Donenfeld --- windows/mkwinsyscall/mkwinsyscall.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/windows/mkwinsyscall/mkwinsyscall.go b/windows/mkwinsyscall/mkwinsyscall.go index 75b5bd2a..0f2f645e 100644 --- a/windows/mkwinsyscall/mkwinsyscall.go +++ b/windows/mkwinsyscall/mkwinsyscall.go @@ -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}}