|
| 1 | +// Copyright 2020 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package errorstest |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + "unicode" |
| 16 | +) |
| 17 | + |
| 18 | +// A manually modified object file could pass unexpected characters |
| 19 | +// into the files generated by cgo. |
| 20 | + |
| 21 | +const magicInput = "abcdefghijklmnopqrstuvwxyz0123" |
| 22 | +const magicReplace = "\n//go:cgo_ldflag \"-badflag\"\n//" |
| 23 | + |
| 24 | +const cSymbol = "BadSymbol" + magicInput + "Name" |
| 25 | +const cDefSource = "int " + cSymbol + " = 1;" |
| 26 | +const cRefSource = "extern int " + cSymbol + "; int F() { return " + cSymbol + "; }" |
| 27 | + |
| 28 | +// goSource is the source code for the trivial Go file we use. |
| 29 | +// We will replace TMPDIR with the temporary directory name. |
| 30 | +const goSource = ` |
| 31 | +package main |
| 32 | +
|
| 33 | +// #cgo LDFLAGS: TMPDIR/cbad.o TMPDIR/cbad.so |
| 34 | +// extern int F(); |
| 35 | +import "C" |
| 36 | +
|
| 37 | +func main() { |
| 38 | + println(C.F()) |
| 39 | +} |
| 40 | +` |
| 41 | + |
| 42 | +func TestBadSymbol(t *testing.T) { |
| 43 | + dir := t.TempDir() |
| 44 | + |
| 45 | + mkdir := func(base string) string { |
| 46 | + ret := filepath.Join(dir, base) |
| 47 | + if err := os.Mkdir(ret, 0755); err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + return ret |
| 51 | + } |
| 52 | + |
| 53 | + cdir := mkdir("c") |
| 54 | + godir := mkdir("go") |
| 55 | + |
| 56 | + makeFile := func(mdir, base, source string) string { |
| 57 | + ret := filepath.Join(mdir, base) |
| 58 | + if err := ioutil.WriteFile(ret, []byte(source), 0644); err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + return ret |
| 62 | + } |
| 63 | + |
| 64 | + cDefFile := makeFile(cdir, "cdef.c", cDefSource) |
| 65 | + cRefFile := makeFile(cdir, "cref.c", cRefSource) |
| 66 | + |
| 67 | + ccCmd := cCompilerCmd(t) |
| 68 | + |
| 69 | + cCompile := func(arg, base, src string) string { |
| 70 | + out := filepath.Join(cdir, base) |
| 71 | + run := append(ccCmd, arg, "-o", out, src) |
| 72 | + output, err := exec.Command(run[0], run[1:]...).CombinedOutput() |
| 73 | + if err != nil { |
| 74 | + t.Log(run) |
| 75 | + t.Logf("%s", output) |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + if err := os.Remove(src); err != nil { |
| 79 | + t.Fatal(err) |
| 80 | + } |
| 81 | + return out |
| 82 | + } |
| 83 | + |
| 84 | + // Build a shared library that defines a symbol whose name |
| 85 | + // contains magicInput. |
| 86 | + |
| 87 | + cShared := cCompile("-shared", "c.so", cDefFile) |
| 88 | + |
| 89 | + // Build an object file that refers to the symbol whose name |
| 90 | + // contains magicInput. |
| 91 | + |
| 92 | + cObj := cCompile("-c", "c.o", cRefFile) |
| 93 | + |
| 94 | + // Rewrite the shared library and the object file, replacing |
| 95 | + // magicInput with magicReplace. This will have the effect of |
| 96 | + // introducing a symbol whose name looks like a cgo command. |
| 97 | + // The cgo tool will use that name when it generates the |
| 98 | + // _cgo_import.go file, thus smuggling a magic //go:cgo_ldflag |
| 99 | + // pragma into a Go file. We used to not check the pragmas in |
| 100 | + // _cgo_import.go. |
| 101 | + |
| 102 | + rewrite := func(from, to string) { |
| 103 | + obj, err := ioutil.ReadFile(from) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + if bytes.Count(obj, []byte(magicInput)) == 0 { |
| 109 | + t.Fatalf("%s: did not find magic string", from) |
| 110 | + } |
| 111 | + |
| 112 | + if len(magicInput) != len(magicReplace) { |
| 113 | + t.Fatalf("internal test error: different magic lengths: %d != %d", len(magicInput), len(magicReplace)) |
| 114 | + } |
| 115 | + |
| 116 | + obj = bytes.ReplaceAll(obj, []byte(magicInput), []byte(magicReplace)) |
| 117 | + |
| 118 | + if err := ioutil.WriteFile(to, obj, 0644); err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + cBadShared := filepath.Join(godir, "cbad.so") |
| 124 | + rewrite(cShared, cBadShared) |
| 125 | + |
| 126 | + cBadObj := filepath.Join(godir, "cbad.o") |
| 127 | + rewrite(cObj, cBadObj) |
| 128 | + |
| 129 | + goSourceBadObject := strings.ReplaceAll(goSource, "TMPDIR", godir) |
| 130 | + makeFile(godir, "go.go", goSourceBadObject) |
| 131 | + |
| 132 | + makeFile(godir, "go.mod", "module badsym") |
| 133 | + |
| 134 | + // Try to build our little package. |
| 135 | + cmd := exec.Command("go", "build", "-ldflags=-v") |
| 136 | + cmd.Dir = godir |
| 137 | + output, err := cmd.CombinedOutput() |
| 138 | + |
| 139 | + // The build should fail, but we want it to fail because we |
| 140 | + // detected the error, not because we passed a bad flag to the |
| 141 | + // C linker. |
| 142 | + |
| 143 | + if err == nil { |
| 144 | + t.Errorf("go build succeeded unexpectedly") |
| 145 | + } |
| 146 | + |
| 147 | + t.Logf("%s", output) |
| 148 | + |
| 149 | + for _, line := range bytes.Split(output, []byte("\n")) { |
| 150 | + if bytes.Contains(line, []byte("dynamic symbol")) && bytes.Contains(line, []byte("contains unsupported character")) { |
| 151 | + // This is the error from cgo. |
| 152 | + continue |
| 153 | + } |
| 154 | + |
| 155 | + // We passed -ldflags=-v to see the external linker invocation, |
| 156 | + // which should not include -badflag. |
| 157 | + if bytes.Contains(line, []byte("-badflag")) { |
| 158 | + t.Error("output should not mention -badflag") |
| 159 | + } |
| 160 | + |
| 161 | + // Also check for compiler errors, just in case. |
| 162 | + // GCC says "unrecognized command line option". |
| 163 | + // clang says "unknown argument". |
| 164 | + if bytes.Contains(line, []byte("unrecognized")) || bytes.Contains(output, []byte("unknown")) { |
| 165 | + t.Error("problem should have been caught before invoking C linker") |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func cCompilerCmd(t *testing.T) []string { |
| 171 | + cc := []string{goEnv(t, "CC")} |
| 172 | + |
| 173 | + out := goEnv(t, "GOGCCFLAGS") |
| 174 | + quote := '\000' |
| 175 | + start := 0 |
| 176 | + lastSpace := true |
| 177 | + backslash := false |
| 178 | + s := string(out) |
| 179 | + for i, c := range s { |
| 180 | + if quote == '\000' && unicode.IsSpace(c) { |
| 181 | + if !lastSpace { |
| 182 | + cc = append(cc, s[start:i]) |
| 183 | + lastSpace = true |
| 184 | + } |
| 185 | + } else { |
| 186 | + if lastSpace { |
| 187 | + start = i |
| 188 | + lastSpace = false |
| 189 | + } |
| 190 | + if quote == '\000' && !backslash && (c == '"' || c == '\'') { |
| 191 | + quote = c |
| 192 | + backslash = false |
| 193 | + } else if !backslash && quote == c { |
| 194 | + quote = '\000' |
| 195 | + } else if (quote == '\000' || quote == '"') && !backslash && c == '\\' { |
| 196 | + backslash = true |
| 197 | + } else { |
| 198 | + backslash = false |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + if !lastSpace { |
| 203 | + cc = append(cc, s[start:]) |
| 204 | + } |
| 205 | + return cc |
| 206 | +} |
| 207 | + |
| 208 | +func goEnv(t *testing.T, key string) string { |
| 209 | + out, err := exec.Command("go", "env", key).CombinedOutput() |
| 210 | + if err != nil { |
| 211 | + t.Logf("go env %s\n", key) |
| 212 | + t.Logf("%s", out) |
| 213 | + t.Fatal(err) |
| 214 | + } |
| 215 | + return strings.TrimSpace(string(out)) |
| 216 | +} |
0 commit comments