code-intelligence.com/cifuzz@v0.40.0/internal/bundler/archive/metadata.go (about)

     1  package archive
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/pkg/errors"
     7  	"gopkg.in/yaml.v3"
     8  )
     9  
    10  // MetadataFileName is the name of the meta information yaml file within an artifact archive.
    11  const MetadataFileName = "bundle.yaml"
    12  
    13  // Metadata defines meta information for artifacts contained within a fuzzing artifact archive.
    14  type Metadata struct {
    15  	*RunEnvironment `yaml:"run_environment"`
    16  	CodeRevision    *CodeRevision `yaml:"code_revision,omitempty"`
    17  	Fuzzers         []*Fuzzer     `yaml:"fuzzers"`
    18  }
    19  
    20  // Fuzzer specifies the type and locations of fuzzers contained in the archive.
    21  type Fuzzer struct {
    22  	Target    string `yaml:"target,omitempty"`
    23  	Name      string `yaml:"name,omitempty"`
    24  	Path      string `yaml:"path"`
    25  	Engine    string `yaml:"engine"`
    26  	Sanitizer string `yaml:"sanitizer,omitempty"`
    27  	// The different YAML field name is *not* a typo: For historical reasons, the "build_dir" field is supposed to
    28  	// include the root directory of the *source* rather than the build tree of the project. Rather than expose all
    29  	// cifuzz devs to this inconsistency, we keep it in the serialization logic.
    30  	ProjectDir    string        `yaml:"build_dir"`
    31  	Dictionary    string        `yaml:"dictionary,omitempty"`
    32  	Seeds         string        `yaml:"seeds,omitempty"`
    33  	LibraryPaths  []string      `yaml:"library_paths,omitempty"`
    34  	RuntimePaths  []string      `yaml:"runtime_paths,omitempty"`
    35  	EngineOptions EngineOptions `yaml:"engine_options,omitempty"`
    36  	MaxRunTime    uint          `yaml:"max_run_time,omitempty"`
    37  }
    38  
    39  // RunEnvironment specifies the environment in which the fuzzers are to be run.
    40  type RunEnvironment struct {
    41  	// The docker image and tag to be used: eg. debian:stable
    42  	Docker string
    43  }
    44  
    45  type CodeRevision struct {
    46  	Git *GitRevision `yaml:"git,omitempty"`
    47  }
    48  
    49  type GitRevision struct {
    50  	Commit string `yaml:"commit,omitempty"`
    51  	Branch string `yaml:"branch,omitempty"`
    52  }
    53  
    54  type EngineOptions struct {
    55  	Flags []string `yaml:"flags,omitempty"`
    56  	Env   []string `yaml:"env,omitempty"`
    57  }
    58  
    59  func (a *Metadata) ToYaml() ([]byte, error) {
    60  	out, err := yaml.Marshal(a)
    61  	if err != nil {
    62  		return nil, errors.Wrapf(err, "failed to marshal metadata to YAML: %+v", *a)
    63  	}
    64  
    65  	return out, nil
    66  }
    67  
    68  func (a *Metadata) FromYaml(data []byte) error {
    69  	err := yaml.Unmarshal(data, a)
    70  	if err != nil {
    71  		return errors.Wrapf(err, "failed to unmarshal metadata from YAML:\n%s", string(data))
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  func MetadataFromPath(path string) (*Metadata, error) {
    78  	metadataFile, err := os.ReadFile(path)
    79  	if err != nil {
    80  		return nil, errors.WithStack(err)
    81  	}
    82  
    83  	metadata := &Metadata{}
    84  	err = metadata.FromYaml(metadataFile)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return metadata, nil
    90  }