prog: validate deserialized programs

The optimization change removed validation too aggressively.
We do need program validation during deserialization,
because we can get bad programs from corpus or hub.
Restore program validation after deserialization.
This commit is contained in:
Dmitry Vyukov 2017-01-24 10:51:38 +01:00
parent 40c6a8ebf5
commit 40723a067e
6 changed files with 23 additions and 13 deletions

View File

@ -15,8 +15,10 @@ func (p *Prog) Clone() *Prog {
}
p1.Calls = append(p1.Calls, c1)
}
if err := p1.validate(); err != nil {
panic(err)
if debug {
if err := p1.validate(); err != nil {
panic(err)
}
}
return p1
}

View File

@ -27,8 +27,10 @@ func (p *Prog) String() string {
}
func (p *Prog) Serialize() []byte {
if err := p.validate(); err != nil {
panic("serializing invalid program")
if debug {
if err := p.validate(); err != nil {
panic("serializing invalid program")
}
}
buf := new(bytes.Buffer)
vars := make(map[*Arg]int)
@ -173,6 +175,9 @@ func Deserialize(data []byte) (prog *Prog, err error) {
if err := p.Err(); err != nil {
return nil, err
}
// This validation is done even in non-debug mode because deserialization
// procedure does not catch all bugs (e.g. mismatched types).
// And we can receive bad programs from corpus and hub.
if err := prog.validate(); err != nil {
return nil, err
}

View File

@ -35,8 +35,10 @@ const (
// SerializeForExec serializes program p for execution by process pid into the provided buffer.
// If the provided buffer is too small for the program an error is returned.
func (p *Prog) SerializeForExec(buffer []byte, pid int) error {
if err := p.validate(); err != nil {
panic(fmt.Errorf("serializing invalid program: %v", err))
if debug {
if err := p.validate(); err != nil {
panic(fmt.Errorf("serializing invalid program: %v", err))
}
}
var instrSeq uintptr
w := &execContext{

View File

@ -20,8 +20,10 @@ func Generate(rs rand.Source, ncalls int, ct *ChoiceTable) *Prog {
p.Calls = append(p.Calls, c)
}
}
if err := p.validate(); err != nil {
panic(err)
if debug {
if err := p.validate(); err != nil {
panic(err)
}
}
return p
}

View File

@ -233,8 +233,10 @@ func (p *Prog) Mutate(rs rand.Source, ncalls int, ct *ChoiceTable, corpus []*Pro
for _, c := range p.Calls {
sanitizeCall(c)
}
if err := p.validate(); err != nil {
panic(err)
if debug {
if err := p.validate(); err != nil {
panic(err)
}
}
}

View File

@ -17,9 +17,6 @@ type validCtx struct {
}
func (p *Prog) validate() error {
if !debug {
return nil
}
ctx := &validCtx{make(map[*Arg]bool), make(map[*Arg]*Arg)}
for _, c := range p.Calls {
if err := c.validate(ctx); err != nil {