]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/noder/noder.go
all: implement wasmimport directive
[gostls13.git] / src / cmd / compile / internal / noder / noder.go
1 // Copyright 2016 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 noder
6
7 import (
8         "errors"
9         "fmt"
10         "internal/buildcfg"
11         "os"
12         "path/filepath"
13         "runtime"
14         "strconv"
15         "strings"
16         "unicode"
17         "unicode/utf8"
18
19         "cmd/compile/internal/base"
20         "cmd/compile/internal/ir"
21         "cmd/compile/internal/syntax"
22         "cmd/compile/internal/typecheck"
23         "cmd/compile/internal/types"
24         "cmd/internal/objabi"
25 )
26
27 func LoadPackage(filenames []string) {
28         base.Timer.Start("fe", "parse")
29
30         // Limit the number of simultaneously open files.
31         sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
32
33         noders := make([]*noder, len(filenames))
34         for i := range noders {
35                 p := noder{
36                         err: make(chan syntax.Error),
37                 }
38                 noders[i] = &p
39         }
40
41         // Move the entire syntax processing logic into a separate goroutine to avoid blocking on the "sem".
42         go func() {
43                 for i, filename := range filenames {
44                         filename := filename
45                         p := noders[i]
46                         sem <- struct{}{}
47                         go func() {
48                                 defer func() { <-sem }()
49                                 defer close(p.err)
50                                 fbase := syntax.NewFileBase(filename)
51
52                                 f, err := os.Open(filename)
53                                 if err != nil {
54                                         p.error(syntax.Error{Msg: err.Error()})
55                                         return
56                                 }
57                                 defer f.Close()
58
59                                 p.file, _ = syntax.Parse(fbase, f, p.error, p.pragma, syntax.CheckBranches) // errors are tracked via p.error
60                         }()
61                 }
62         }()
63
64         var lines uint
65         var m posMap
66         for _, p := range noders {
67                 for e := range p.err {
68                         base.ErrorfAt(m.makeXPos(e.Pos), "%s", e.Msg)
69                 }
70                 if p.file == nil {
71                         base.ErrorExit()
72                 }
73                 lines += p.file.EOF.Line()
74         }
75         base.Timer.AddEvent(int64(lines), "lines")
76
77         unified(m, noders)
78 }
79
80 // trimFilename returns the "trimmed" filename of b, which is the
81 // absolute filename after applying -trimpath processing. This
82 // filename form is suitable for use in object files and export data.
83 //
84 // If b's filename has already been trimmed (i.e., because it was read
85 // in from an imported package's export data), then the filename is
86 // returned unchanged.
87 func trimFilename(b *syntax.PosBase) string {
88         filename := b.Filename()
89         if !b.Trimmed() {
90                 dir := ""
91                 if b.IsFileBase() {
92                         dir = base.Ctxt.Pathname
93                 }
94                 filename = objabi.AbsFile(dir, filename, base.Flag.TrimPath)
95         }
96         return filename
97 }
98
99 // noder transforms package syntax's AST into a Node tree.
100 type noder struct {
101         file       *syntax.File
102         linknames  []linkname
103         pragcgobuf [][]string
104         err        chan syntax.Error
105 }
106
107 // linkname records a //go:linkname directive.
108 type linkname struct {
109         pos    syntax.Pos
110         local  string
111         remote string
112 }
113
114 var unOps = [...]ir.Op{
115         syntax.Recv: ir.ORECV,
116         syntax.Mul:  ir.ODEREF,
117         syntax.And:  ir.OADDR,
118
119         syntax.Not: ir.ONOT,
120         syntax.Xor: ir.OBITNOT,
121         syntax.Add: ir.OPLUS,
122         syntax.Sub: ir.ONEG,
123 }
124
125 var binOps = [...]ir.Op{
126         syntax.OrOr:   ir.OOROR,
127         syntax.AndAnd: ir.OANDAND,
128
129         syntax.Eql: ir.OEQ,
130         syntax.Neq: ir.ONE,
131         syntax.Lss: ir.OLT,
132         syntax.Leq: ir.OLE,
133         syntax.Gtr: ir.OGT,
134         syntax.Geq: ir.OGE,
135
136         syntax.Add: ir.OADD,
137         syntax.Sub: ir.OSUB,
138         syntax.Or:  ir.OOR,
139         syntax.Xor: ir.OXOR,
140
141         syntax.Mul:    ir.OMUL,
142         syntax.Div:    ir.ODIV,
143         syntax.Rem:    ir.OMOD,
144         syntax.And:    ir.OAND,
145         syntax.AndNot: ir.OANDNOT,
146         syntax.Shl:    ir.OLSH,
147         syntax.Shr:    ir.ORSH,
148 }
149
150 // error is called concurrently if files are parsed concurrently.
151 func (p *noder) error(err error) {
152         p.err <- err.(syntax.Error)
153 }
154
155 // pragmas that are allowed in the std lib, but don't have
156 // a syntax.Pragma value (see lex.go) associated with them.
157 var allowedStdPragmas = map[string]bool{
158         "go:cgo_export_static":  true,
159         "go:cgo_export_dynamic": true,
160         "go:cgo_import_static":  true,
161         "go:cgo_import_dynamic": true,
162         "go:cgo_ldflag":         true,
163         "go:cgo_dynamic_linker": true,
164         "go:embed":              true,
165         "go:generate":           true,
166 }
167
168 // *pragmas is the value stored in a syntax.pragmas during parsing.
169 type pragmas struct {
170         Flag       ir.PragmaFlag // collected bits
171         Pos        []pragmaPos   // position of each individual flag
172         Embeds     []pragmaEmbed
173         WasmImport *WasmImport
174 }
175
176 // WasmImport stores metadata associated with the //go:wasmimport pragma
177 type WasmImport struct {
178         Pos    syntax.Pos
179         Module string
180         Name   string
181 }
182
183 type pragmaPos struct {
184         Flag ir.PragmaFlag
185         Pos  syntax.Pos
186 }
187
188 type pragmaEmbed struct {
189         Pos      syntax.Pos
190         Patterns []string
191 }
192
193 func (p *noder) checkUnusedDuringParse(pragma *pragmas) {
194         for _, pos := range pragma.Pos {
195                 if pos.Flag&pragma.Flag != 0 {
196                         p.error(syntax.Error{Pos: pos.Pos, Msg: "misplaced compiler directive"})
197                 }
198         }
199         if len(pragma.Embeds) > 0 {
200                 for _, e := range pragma.Embeds {
201                         p.error(syntax.Error{Pos: e.Pos, Msg: "misplaced go:embed directive"})
202                 }
203         }
204         if pragma.WasmImport != nil {
205                 p.error(syntax.Error{Pos: pragma.WasmImport.Pos, Msg: "misplaced go:wasmimport directive"})
206         }
207 }
208
209 // pragma is called concurrently if files are parsed concurrently.
210 func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.Pragma) syntax.Pragma {
211         pragma, _ := old.(*pragmas)
212         if pragma == nil {
213                 pragma = new(pragmas)
214         }
215
216         if text == "" {
217                 // unused pragma; only called with old != nil.
218                 p.checkUnusedDuringParse(pragma)
219                 return nil
220         }
221
222         if strings.HasPrefix(text, "line ") {
223                 // line directives are handled by syntax package
224                 panic("unreachable")
225         }
226
227         if !blankLine {
228                 // directive must be on line by itself
229                 p.error(syntax.Error{Pos: pos, Msg: "misplaced compiler directive"})
230                 return pragma
231         }
232
233         switch {
234         case strings.HasPrefix(text, "go:wasmimport "):
235                 f := strings.Fields(text)
236                 if len(f) != 3 {
237                         p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmimport importmodule importname"})
238                         break
239                 }
240                 if !base.Flag.CompilingRuntime && base.Ctxt.Pkgpath != "syscall/js" && base.Ctxt.Pkgpath != "syscall/js_test" {
241                         p.error(syntax.Error{Pos: pos, Msg: "//go:wasmimport directive cannot be used outside of runtime or syscall/js"})
242                         break
243                 }
244
245                 if buildcfg.GOARCH == "wasm" {
246                         // Only actually use them if we're compiling to WASM though.
247                         pragma.WasmImport = &WasmImport{
248                                 Pos:    pos,
249                                 Module: f[1],
250                                 Name:   f[2],
251                         }
252                 }
253         case strings.HasPrefix(text, "go:linkname "):
254                 f := strings.Fields(text)
255                 if !(2 <= len(f) && len(f) <= 3) {
256                         p.error(syntax.Error{Pos: pos, Msg: "usage: //go:linkname localname [linkname]"})
257                         break
258                 }
259                 // The second argument is optional. If omitted, we use
260                 // the default object symbol name for this and
261                 // linkname only serves to mark this symbol as
262                 // something that may be referenced via the object
263                 // symbol name from another package.
264                 var target string
265                 if len(f) == 3 {
266                         target = f[2]
267                 } else if base.Ctxt.Pkgpath != "" {
268                         // Use the default object symbol name if the
269                         // user didn't provide one.
270                         target = objabi.PathToPrefix(base.Ctxt.Pkgpath) + "." + f[1]
271                 } else {
272                         p.error(syntax.Error{Pos: pos, Msg: "//go:linkname requires linkname argument or -p compiler flag"})
273                         break
274                 }
275                 p.linknames = append(p.linknames, linkname{pos, f[1], target})
276
277         case text == "go:embed", strings.HasPrefix(text, "go:embed "):
278                 args, err := parseGoEmbed(text[len("go:embed"):])
279                 if err != nil {
280                         p.error(syntax.Error{Pos: pos, Msg: err.Error()})
281                 }
282                 if len(args) == 0 {
283                         p.error(syntax.Error{Pos: pos, Msg: "usage: //go:embed pattern..."})
284                         break
285                 }
286                 pragma.Embeds = append(pragma.Embeds, pragmaEmbed{pos, args})
287
288         case strings.HasPrefix(text, "go:cgo_import_dynamic "):
289                 // This is permitted for general use because Solaris
290                 // code relies on it in golang.org/x/sys/unix and others.
291                 fields := pragmaFields(text)
292                 if len(fields) >= 4 {
293                         lib := strings.Trim(fields[3], `"`)
294                         if lib != "" && !safeArg(lib) && !isCgoGeneratedFile(pos) {
295                                 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid library name %q in cgo_import_dynamic directive", lib)})
296                         }
297                         p.pragcgo(pos, text)
298                         pragma.Flag |= pragmaFlag("go:cgo_import_dynamic")
299                         break
300                 }
301                 fallthrough
302         case strings.HasPrefix(text, "go:cgo_"):
303                 // For security, we disallow //go:cgo_* directives other
304                 // than cgo_import_dynamic outside cgo-generated files.
305                 // Exception: they are allowed in the standard library, for runtime and syscall.
306                 if !isCgoGeneratedFile(pos) && !base.Flag.Std {
307                         p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
308                 }
309                 p.pragcgo(pos, text)
310                 fallthrough // because of //go:cgo_unsafe_args
311         default:
312                 verb := text
313                 if i := strings.Index(text, " "); i >= 0 {
314                         verb = verb[:i]
315                 }
316                 flag := pragmaFlag(verb)
317                 const runtimePragmas = ir.Systemstack | ir.Nowritebarrier | ir.Nowritebarrierrec | ir.Yeswritebarrierrec
318                 if !base.Flag.CompilingRuntime && flag&runtimePragmas != 0 {
319                         p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in runtime", verb)})
320                 }
321                 if flag == ir.UintptrKeepAlive && !base.Flag.Std {
322                         p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is only allowed in the standard library", verb)})
323                 }
324                 if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
325                         p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
326                 }
327                 pragma.Flag |= flag
328                 pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
329         }
330
331         return pragma
332 }
333
334 // isCgoGeneratedFile reports whether pos is in a file
335 // generated by cgo, which is to say a file with name
336 // beginning with "_cgo_". Such files are allowed to
337 // contain cgo directives, and for security reasons
338 // (primarily misuse of linker flags), other files are not.
339 // See golang.org/issue/23672.
340 func isCgoGeneratedFile(pos syntax.Pos) bool {
341         return strings.HasPrefix(filepath.Base(trimFilename(pos.Base())), "_cgo_")
342 }
343
344 // safeArg reports whether arg is a "safe" command-line argument,
345 // meaning that when it appears in a command-line, it probably
346 // doesn't have some special meaning other than its own name.
347 // This is copied from SafeArg in cmd/go/internal/load/pkg.go.
348 func safeArg(name string) bool {
349         if name == "" {
350                 return false
351         }
352         c := name[0]
353         return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
354 }
355
356 // parseGoEmbed parses the text following "//go:embed" to extract the glob patterns.
357 // It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
358 // go/build/read.go also processes these strings and contains similar logic.
359 func parseGoEmbed(args string) ([]string, error) {
360         var list []string
361         for args = strings.TrimSpace(args); args != ""; args = strings.TrimSpace(args) {
362                 var path string
363         Switch:
364                 switch args[0] {
365                 default:
366                         i := len(args)
367                         for j, c := range args {
368                                 if unicode.IsSpace(c) {
369                                         i = j
370                                         break
371                                 }
372                         }
373                         path = args[:i]
374                         args = args[i:]
375
376                 case '`':
377                         i := strings.Index(args[1:], "`")
378                         if i < 0 {
379                                 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
380                         }
381                         path = args[1 : 1+i]
382                         args = args[1+i+1:]
383
384                 case '"':
385                         i := 1
386                         for ; i < len(args); i++ {
387                                 if args[i] == '\\' {
388                                         i++
389                                         continue
390                                 }
391                                 if args[i] == '"' {
392                                         q, err := strconv.Unquote(args[:i+1])
393                                         if err != nil {
394                                                 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args[:i+1])
395                                         }
396                                         path = q
397                                         args = args[i+1:]
398                                         break Switch
399                                 }
400                         }
401                         if i >= len(args) {
402                                 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
403                         }
404                 }
405
406                 if args != "" {
407                         r, _ := utf8.DecodeRuneInString(args)
408                         if !unicode.IsSpace(r) {
409                                 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
410                         }
411                 }
412                 list = append(list, path)
413         }
414         return list, nil
415 }
416
417 // A function named init is a special case.
418 // It is called by the initialization before main is run.
419 // To make it unique within a package and also uncallable,
420 // the name, normally "pkg.init", is altered to "pkg.init.0".
421 var renameinitgen int
422
423 func Renameinit() *types.Sym {
424         s := typecheck.LookupNum("init.", renameinitgen)
425         renameinitgen++
426         return s
427 }
428
429 func checkEmbed(decl *syntax.VarDecl, haveEmbed, withinFunc bool) error {
430         switch {
431         case !haveEmbed:
432                 return errors.New("go:embed only allowed in Go files that import \"embed\"")
433         case len(decl.NameList) > 1:
434                 return errors.New("go:embed cannot apply to multiple vars")
435         case decl.Values != nil:
436                 return errors.New("go:embed cannot apply to var with initializer")
437         case decl.Type == nil:
438                 // Should not happen, since Values == nil now.
439                 return errors.New("go:embed cannot apply to var without type")
440         case withinFunc:
441                 return errors.New("go:embed cannot apply to var inside func")
442         case !types.AllowsGoVersion(1, 16):
443                 return fmt.Errorf("go:embed requires go1.16 or later (-lang was set to %s; check go.mod)", base.Flag.Lang)
444
445         default:
446                 return nil
447         }
448 }