github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/prog/generation.go (about)

     1  // Copyright 2015 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 prog
     5  
     6  import (
     7  	"math/rand"
     8  )
     9  
    10  // Generate generates a random program with ncalls calls.
    11  // ct contains a set of allowed syscalls, if nil all syscalls are used.
    12  func (target *Target) Generate(rs rand.Source, ncalls int, ct *ChoiceTable) *Prog {
    13  	p := &Prog{
    14  		Target: target,
    15  	}
    16  	r := newRand(target, rs)
    17  	s := newState(target, ct, nil)
    18  	for len(p.Calls) < ncalls {
    19  		calls := r.generateCall(s, p, len(p.Calls))
    20  		for _, c := range calls {
    21  			s.analyze(c)
    22  			p.Calls = append(p.Calls, c)
    23  		}
    24  	}
    25  	// For the last generated call we could get additional calls that create
    26  	// resources and overflow ncalls. Remove some of these calls.
    27  	// The resources in the last call will be replaced with the default values,
    28  	// which is exactly what we want.
    29  	for len(p.Calls) > ncalls {
    30  		p.RemoveCall(ncalls - 1)
    31  	}
    32  	p.sanitizeFix()
    33  	p.debugValidate()
    34  	return p
    35  }