Commit b6712946 authored by Alex Brainman's avatar Alex Brainman

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/21645Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 9628e6fd
...@@ -617,9 +617,6 @@ func ParseFiles(fs []string) (*Source, error) { ...@@ -617,9 +617,6 @@ func ParseFiles(fs []string) (*Source, error) {
"unsafe", "unsafe",
}, },
} }
if *systemDLL {
src.Import("internal/syscall/windows/sysdll")
}
for _, file := range fs { for _, file := range fs {
if err := src.ParseFile(file); err != nil { if err := src.ParseFile(file); err != nil {
return nil, err return nil, err
...@@ -691,9 +688,30 @@ func (src *Source) ParseFile(path string) error { ...@@ -691,9 +688,30 @@ func (src *Source) ParseFile(path string) error {
// Generate output source file from a source set src. // Generate output source file from a source set src.
func (src *Source) Generate(w io.Writer) error { func (src *Source) Generate(w io.Writer) error {
if *sysRepo && packageName != "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") src.Import("golang.org/x/sys/windows")
} }
}
if packageName != "syscall" { if packageName != "syscall" {
src.Import("syscall") src.Import("syscall")
} }
...@@ -702,18 +720,17 @@ func (src *Source) Generate(w io.Writer) error { ...@@ -702,18 +720,17 @@ func (src *Source) Generate(w io.Writer) error {
"syscalldot": syscalldot, "syscalldot": syscalldot,
"newlazydll": func(dll string) string { "newlazydll": func(dll string) string {
arg := "\"" + dll + ".dll\"" arg := "\"" + dll + ".dll\""
if *systemDLL { if !*systemDLL {
arg = "sysdll.Add(" + arg + ")" return syscalldot() + "NewLazyDLL(" + arg + ")"
} }
if *sysRepo { switch pkgtype {
if packageName == "windows" { case pkgStd:
return syscalldot() + "NewLazyDLL(sysdll.Add(" + arg + "))"
case pkgXSysWindows:
return "NewLazySystemDLL(" + arg + ")" return "NewLazySystemDLL(" + arg + ")"
} else { default:
return "windows.NewLazySystemDLL(" + arg + ")" return "windows.NewLazySystemDLL(" + arg + ")"
} }
} else {
return syscalldot() + "NewLazyDLL(" + arg + ")"
}
}, },
} }
t := template.Must(template.New("main").Funcs(funcMap).Parse(srcTemplate)) t := template.Must(template.New("main").Funcs(funcMap).Parse(srcTemplate))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment