syscall: rewrite handle inheritance test to use C rather than Powershell

In CL 327210, we disabled this test on arm platforms, because the
powershell shipped with those systems isn't native, which means it'd
refuse to load native DLLs. This commit rewrites the test to simply not
use Powershell, and instead compiles a trivial C program that tests for
the same thing. Reverting CL 316269 makes this test fail, as desired,
while applying it makes this test succeed.

Fixes #46701

Change-Id: If39612c57bf74c63adf58e2c49b5cb739b461fe5
Reviewed-on: https://go-review.googlesource.com/c/go/+/327969
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Trust: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
This commit is contained in:
Jason A. Donenfeld 2021-06-15 01:55:50 +02:00
parent cf4e3e3d3b
commit 4061d3463b

View file

@ -10,7 +10,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"testing" "testing"
@ -80,9 +79,6 @@ func TestTOKEN_ALL_ACCESS(t *testing.T) {
func TestStdioAreInheritable(t *testing.T) { func TestStdioAreInheritable(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
testenv.MustHaveExecPath(t, "gcc") testenv.MustHaveExecPath(t, "gcc")
if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" {
t.Skip("Powershell is not native on ARM; see golang.org/issues/46701")
}
tmpdir := t.TempDir() tmpdir := t.TempDir()
@ -114,18 +110,28 @@ func main() {}
t.Fatalf("failed to build go library: %s\n%s", err, out) t.Fatalf("failed to build go library: %s\n%s", err, out)
} }
// run powershell script // build c exe
psscript := fmt.Sprintf(` const exetext = `
hostname; #include <stdlib.h>
$signature = " [DllImport("%q")] public static extern void HelloWorld(); "; #include <windows.h>
Add-Type -MemberDefinition $signature -Name World -Namespace Hello; int main(int argc, char *argv[])
[Hello.World]::HelloWorld(); {
hostname; system("hostname");
`, dll) ((void(*)(void))GetProcAddress(LoadLibraryA(%q), "HelloWorld"))();
psscript = strings.ReplaceAll(psscript, "\n", "") system("hostname");
out, err = exec.Command("powershell", "-Command", psscript).CombinedOutput() return 0;
}
`
exe := filepath.Join(tmpdir, "helloworld.exe")
cmd = exec.Command("gcc", "-o", exe, "-xc", "-")
cmd.Stdin = strings.NewReader(fmt.Sprintf(exetext, dll))
out, err = testenv.CleanCmdEnv(cmd).CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("Powershell command failed: %v: %v", err, string(out)) t.Fatalf("failed to build c executable: %s\n%s", err, out)
}
out, err = exec.Command(exe).CombinedOutput()
if err != nil {
t.Fatalf("c program execution failed: %v: %v", err, string(out))
} }
hostname, err := os.Hostname() hostname, err := os.Hostname()
@ -137,6 +143,6 @@ hostname;
have = strings.ReplaceAll(have, "\r", "") have = strings.ReplaceAll(have, "\r", "")
want := fmt.Sprintf("%sHello World%s", hostname, hostname) want := fmt.Sprintf("%sHello World%s", hostname, hostname)
if have != want { if have != want {
t.Fatalf("Powershell command output is wrong: got %q, want %q", have, want) t.Fatalf("c program output is wrong: got %q, want %q", have, want)
} }
} }