diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2024-09-11 16:04:43 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2024-09-11 14:55:22 +0000 |
| commit | 7308f9d5a4da8ea4e2a9a0748ceb0921333c11f4 (patch) | |
| tree | 3c4c0d6bdfd082e089a5514c0c06e7eabec864f9 /prog | |
| parent | fd0ad8a3f899365059471f5fdefdc73082ce0777 (diff) | |
prog: allow deeper nesting of conditional fields patching
There is a totally valid situation when we could be recursively patching
conditional fields: if by changing a field's value we insert new
resource constructor calls. It's a bug to skip conditional field
patching for them.
Allow up to 2 nested patchConditionalFields() calls and panic if there
happen to be more.
Add a test that reproduces the situation described above.
Diffstat (limited to 'prog')
| -rw-r--r-- | prog/expr.go | 13 | ||||
| -rw-r--r-- | prog/expr_test.go | 22 | ||||
| -rw-r--r-- | prog/rand.go | 8 |
3 files changed, 34 insertions, 9 deletions
diff --git a/prog/expr.go b/prog/expr.go index cfe6eb70b..e9446ab2c 100644 --- a/prog/expr.go +++ b/prog/expr.go @@ -67,11 +67,14 @@ func makeArgFinder(t *Target, c *Call, unionArg *UnionArg, parents parentStack) } func (r *randGen) patchConditionalFields(c *Call, s *state) (extra []*Call, changed bool) { - if r.inPatchConditional { - return nil, false - } - r.inPatchConditional = true - defer func() { r.inPatchConditional = false }() + if r.patchConditionalDepth > 1 { + // Some nested patchConditionalFields() calls are fine as we could trigger a resource + // constructor via generateArg(). But since nested createResource() calls are prohibited, + // patchConditionalFields() should never be nested more than 2 times. + panic("third nested patchConditionalFields call") + } + r.patchConditionalDepth++ + defer func() { r.patchConditionalDepth-- }() var extraCalls []*Call var anyPatched bool diff --git a/prog/expr_test.go b/prog/expr_test.go index aaae6a74a..74818ea55 100644 --- a/prog/expr_test.go +++ b/prog/expr_test.go @@ -280,3 +280,25 @@ func TestConditionalUnionFields(t *testing.T) { assert.Greater(t, zeroU2, 0) assert.Greater(t, nonzeroU2, 0) } + +func TestNestedConditionalCall(t *testing.T) { + // Ensure that we reach different combinations of conditional fields. + target, rs, _ := initRandomTargetTest(t, "test", "64") + ct := target.DefaultChoiceTable() + r := newRand(target, rs) + + for i := 0; i < 100; i++ { + for _, name := range []string{"test$conditional_struct_nested", "test$conditional_struct_nested2"} { + s := newState(target, ct, nil) + calls := r.generateParticularCall(s, target.SyscallMap[name]) + p := &Prog{ + Target: target, + Calls: calls, + } + err := p.checkConditions() + if err != nil { + t.Fatal(err) + } + } + } +} diff --git a/prog/rand.go b/prog/rand.go index b15871b8a..760f7a904 100644 --- a/prog/rand.go +++ b/prog/rand.go @@ -25,10 +25,10 @@ const ( type randGen struct { *rand.Rand - target *Target - inGenerateResource bool - inPatchConditional bool - recDepth map[string]int + target *Target + inGenerateResource bool + patchConditionalDepth int + recDepth map[string]int } func newRand(target *Target, rs rand.Source) *randGen { |
