From b6712946c1b46eb629fb010e65e5b3735f94d171 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 7 Apr 2016 15:12:32 +1000 Subject: [PATCH] runtime: make mksyscall_windows.go flags do what they say they do The -systemdll and -xsys flags generate broken code in some situations (see issue for details). Fix all that. This CL only fixes bugs in existing code, but I have more changes comming: golang.org/x/sys/windows is not the only package that uses mksyscall_windows.go. golang.org/x/exp/shiny and github.com/derekparker/delve do too. I also have few personal packages that use mksyscall_windows.go. None of those packages are aware of new -xsys flag. I would like to change mksyscall_windows.go, so external packages do not need to use -xsys flag. I would love to get rid of -xsys flag altogether, but I don't see how it is possible. So I will, probably, replace -xsys with a flag that means opposite to -xsys, and use new flag everywhere in standard libraries. Flag name suggestions are welcome. -systemdll flag makes users code more "secure". I would like to make -systemdll behaviour a default for all mksyscall_windows.go users. We use that already in standard library. If we think "secure" is important, we should encourage it in all users code. If mksyscall_windows.go user insist on using old code, provide -use_old_loaddll (need good name here) flag for that. So -systemdll flag will be replaced with -use_old_loaddll. Fixes #15167 Change-Id: I516369507867358ba1b66aabe00a17a7b477016e Reviewed-on: https://go-review.googlesource.com/21645 Reviewed-by: Brad Fitzpatrick --- src/syscall/mksyscall_windows.go | 47 ++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/syscall/mksyscall_windows.go b/src/syscall/mksyscall_windows.go index a6cef6fca7..a066307309 100644 --- a/src/syscall/mksyscall_windows.go +++ b/src/syscall/mksyscall_windows.go @@ -617,9 +617,6 @@ func ParseFiles(fs []string) (*Source, error) { "unsafe", }, } - if *systemDLL { - src.Import("internal/syscall/windows/sysdll") - } for _, file := range fs { if err := src.ParseFile(file); err != nil { return nil, err @@ -691,8 +688,29 @@ func (src *Source) ParseFile(path string) error { // Generate output source file from a source set src. func (src *Source) Generate(w io.Writer) error { - if *sysRepo && packageName != "windows" { - src.Import("golang.org/x/sys/windows") + const ( + pkgStd = iota // any package in std library + pkgXSysWindows // x/sys/windows package + pkgOther + ) + var pkgtype int + switch { + case !*sysRepo: + pkgtype = pkgStd + case packageName == "windows": + // TODO: this needs better logic than just using package name + pkgtype = pkgXSysWindows + default: + pkgtype = pkgOther + } + if *systemDLL { + switch pkgtype { + case pkgStd: + src.Import("internal/syscall/windows/sysdll") + case pkgXSysWindows: + default: + src.Import("golang.org/x/sys/windows") + } } if packageName != "syscall" { src.Import("syscall") @@ -702,18 +720,17 @@ func (src *Source) Generate(w io.Writer) error { "syscalldot": syscalldot, "newlazydll": func(dll string) string { arg := "\"" + dll + ".dll\"" - if *systemDLL { - arg = "sysdll.Add(" + arg + ")" - } - if *sysRepo { - if packageName == "windows" { - return "NewLazySystemDLL(" + arg + ")" - } else { - return "windows.NewLazySystemDLL(" + arg + ")" - } - } else { + if !*systemDLL { return syscalldot() + "NewLazyDLL(" + arg + ")" } + switch pkgtype { + case pkgStd: + return syscalldot() + "NewLazyDLL(sysdll.Add(" + arg + "))" + case pkgXSysWindows: + return "NewLazySystemDLL(" + arg + ")" + default: + return "windows.NewLazySystemDLL(" + arg + ")" + } }, } t := template.Must(template.New("main").Funcs(funcMap).Parse(srcTemplate)) -- 2.50.0