github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/compiler/meta.go (about)

     1  // Copyright 2022 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package compiler
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	"github.com/google/syzkaller/pkg/ast"
    10  	"github.com/google/syzkaller/sys/targets"
    11  )
    12  
    13  type Meta struct {
    14  	NoExtract bool
    15  	Arches    map[string]bool
    16  }
    17  
    18  func (meta *Meta) SupportsArch(arch string) bool {
    19  	return len(meta.Arches) == 0 || meta.Arches[arch]
    20  }
    21  
    22  func FileList(desc *ast.Description, OS string, eh ast.ErrorHandler) map[string]Meta {
    23  	// Use any target for this OS.
    24  	for _, target := range targets.List[OS] {
    25  		return createCompiler(desc, target, eh).fileList()
    26  	}
    27  	return nil
    28  }
    29  
    30  func (comp *compiler) fileList() map[string]Meta {
    31  	files := make(map[string]Meta)
    32  	for _, n := range comp.desc.Nodes {
    33  		pos, _, _ := n.Info()
    34  		file := filepath.Base(pos.File)
    35  		if file == ast.BuiltinFile {
    36  			continue
    37  		}
    38  		meta := files[file]
    39  		switch n := n.(type) {
    40  		case *ast.Meta:
    41  			errors0 := comp.errors
    42  			comp.checkTypeImpl(checkCtx{}, n.Value, metaTypes[n.Value.Ident], 0)
    43  			if errors0 != comp.errors {
    44  				break
    45  			}
    46  			switch n.Value.Ident {
    47  			case metaNoExtract.Names[0]:
    48  				meta.NoExtract = true
    49  			case metaArches.Names[0]:
    50  				meta.Arches = make(map[string]bool)
    51  				for _, arg := range n.Value.Args {
    52  					meta.Arches[arg.String] = true
    53  				}
    54  			}
    55  		}
    56  		files[file] = meta
    57  	}
    58  	if comp.errors != 0 {
    59  		return nil
    60  	}
    61  	return files
    62  }
    63  
    64  var metaTypes = map[string]*typeDesc{
    65  	metaNoExtract.Names[0]: metaNoExtract,
    66  	metaArches.Names[0]:    metaArches,
    67  }
    68  
    69  var metaNoExtract = &typeDesc{
    70  	Names:     []string{"noextract"},
    71  	CantBeOpt: true,
    72  }
    73  
    74  var metaArches = &typeDesc{
    75  	Names:     []string{"arches"},
    76  	CantBeOpt: true,
    77  	OptArgs:   8,
    78  	Args:      []namedArg{metaArch, metaArch, metaArch, metaArch, metaArch, metaArch, metaArch, metaArch},
    79  }
    80  
    81  var metaArch = namedArg{Name: "arch", Type: &typeArg{
    82  	Kind: kindString,
    83  	Check: func(comp *compiler, t *ast.Type) {
    84  		if targets.List[comp.target.OS][t.String] == nil {
    85  			comp.error(t.Pos, "unknown arch %v", t.String)
    86  		}
    87  	},
    88  }}