src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

entries.go (1805B)


      1 // Copyright 2021 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 vulncheck
      6 
      7 import (
      8 	"strings"
      9 
     10 	"golang.org/x/tools/go/ssa"
     11 )
     12 
     13 // entryPoints returns functions of topPackages considered entry
     14 // points of govulncheck analysis: main, inits, and exported methods
     15 // and functions.
     16 //
     17 // TODO(https://go.dev/issue/57221): currently, entry functions
     18 // that are generics are not considered an entry point.
     19 func entryPoints(topPackages []*ssa.Package) []*ssa.Function {
     20 	var entries []*ssa.Function
     21 	for _, pkg := range topPackages {
     22 		if pkg.Pkg.Name() == "main" {
     23 			// for "main" packages the only valid entry points are the "main"
     24 			// function and any "init#" functions, even if there are other
     25 			// exported functions or types. similarly to isEntry it should be
     26 			// safe to ignore the validity of the main or init# signatures,
     27 			// since the compiler will reject malformed definitions,
     28 			// and the init function is synthetic
     29 			entries = append(entries, memberFuncs(pkg.Members["main"], pkg.Prog)...)
     30 			for name, member := range pkg.Members {
     31 				if strings.HasPrefix(name, "init#") || name == "init" {
     32 					entries = append(entries, memberFuncs(member, pkg.Prog)...)
     33 				}
     34 			}
     35 			continue
     36 		}
     37 		for _, member := range pkg.Members {
     38 			for _, f := range memberFuncs(member, pkg.Prog) {
     39 				if isEntry(f) {
     40 					entries = append(entries, f)
     41 				}
     42 			}
     43 		}
     44 	}
     45 	return entries
     46 }
     47 
     48 func isEntry(f *ssa.Function) bool {
     49 	// it should be safe to ignore checking that the signature of the "init" function
     50 	// is valid, since it is synthetic
     51 	if f.Name() == "init" && f.Synthetic == "package initializer" {
     52 		return true
     53 	}
     54 
     55 	return f.Synthetic == "" && f.Object() != nil && f.Object().Exported()
     56 }