-
Notifications
You must be signed in to change notification settings - Fork 256
/
bufimagebuildtesting.go
237 lines (220 loc) · 6.85 KB
/
bufimagebuildtesting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufimagebuildtesting
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufimage"
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimagebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmodulebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleconfig"
"github.com/bufbuild/buf/private/bufpkg/buftesting"
"github.com/bufbuild/buf/private/pkg/command"
"github.com/bufbuild/buf/private/pkg/prototesting"
"github.com/bufbuild/buf/private/pkg/storage/storageos"
"github.com/bufbuild/buf/private/pkg/tmp"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/tools/txtar"
"google.golang.org/protobuf/types/descriptorpb"
)
// Fuzz is the entrypoint for the fuzzer.
// We use https://github.com/dvyukov/go-fuzz for fuzzing.
// Please follow the instructions
// in their README for help with running the fuzz targets.
func Fuzz(data []byte) int {
ctx := context.Background()
runner := command.NewRunner()
result, err := fuzz(ctx, runner, data)
if err != nil {
// data was invalid in some way
return -1
}
return result.panicOrN(ctx)
}
func fuzz(ctx context.Context, runner command.Runner, data []byte) (_ *fuzzResult, retErr error) {
dir, err := tmp.NewDir()
if err != nil {
return nil, err
}
defer func() {
retErr = multierr.Append(retErr, dir.Close())
}()
if err := untxtar(data, dir.AbsPath()); err != nil {
return nil, err
}
filePaths, err := buftesting.GetProtocFilePathsErr(ctx, dir.AbsPath(), 0)
if err != nil {
return nil, err
}
actualProtocFileDescriptorSet, protocErr := prototesting.GetProtocFileDescriptorSet(
ctx,
runner,
[]string{dir.AbsPath()},
filePaths,
false,
false,
)
image, bufAnnotations, bufErr := fuzzBuild(ctx, dir.AbsPath())
return newFuzzResult(
runner,
bufAnnotations,
bufErr,
protocErr,
actualProtocFileDescriptorSet,
image,
), nil
}
// fuzzBuild does a builder.Build for a fuzz test.
func fuzzBuild(ctx context.Context, dirPath string) (bufimage.Image, []bufanalysis.FileAnnotation, error) {
module, err := fuzzGetModule(ctx, dirPath)
if err != nil {
return nil, nil, err
}
builder := bufimagebuild.NewBuilder(zap.NewNop(), bufmodule.NewNopModuleReader())
opt := bufimagebuild.WithExcludeSourceCodeInfo()
return builder.Build(ctx, module, opt)
}
// fuzzGetModule gets the bufmodule.Module for a fuzz test.
func fuzzGetModule(ctx context.Context, dirPath string) (bufmodule.Module, error) {
storageosProvider := storageos.NewProvider(storageos.ProviderWithSymlinks())
readWriteBucket, err := storageosProvider.NewReadWriteBucket(
dirPath,
storageos.ReadWriteBucketWithSymlinksIfSupported(),
)
if err != nil {
return nil, err
}
config, err := bufmoduleconfig.NewConfigV1(bufmoduleconfig.ExternalConfigV1{})
if err != nil {
return nil, err
}
return bufmodulebuild.NewModuleBucketBuilder().BuildForBucket(
ctx,
readWriteBucket,
config,
)
}
// txtarParse is a wrapper around txtar.Parse that will turn panics into errors.
// This is necessary because of an issue where txtar.Parse can panic on invalid data. Because data is generated by the
// fuzzer, it will occasionally generate data that causes this panic.
// See https://github.com/golang/go/issues/47193
func txtarParse(data []byte) (_ *txtar.Archive, retErr error) {
defer func() {
if p := recover(); p != nil {
retErr = fmt.Errorf("panic from txtar.Parse: %v", p)
}
}()
return txtar.Parse(data), nil
}
// untxtar extracts txtar data to destDirPath.
func untxtar(data []byte, destDirPath string) error {
archive, err := txtarParse(data)
if err != nil {
return err
}
if len(archive.Files) == 0 {
return fmt.Errorf("txtar contains no files")
}
for _, file := range archive.Files {
dirPath := filepath.Dir(file.Name)
if dirPath != "" {
if err := os.MkdirAll(filepath.Join(destDirPath, dirPath), 0700); err != nil {
return err
}
}
if err := os.WriteFile(
filepath.Join(destDirPath, file.Name),
file.Data,
0600,
); err != nil {
return err
}
}
return nil
}
type fuzzResult struct {
runner command.Runner
bufAnnotations []bufanalysis.FileAnnotation
bufErr error
protocErr error
actualProtocFileDescriptorSet *descriptorpb.FileDescriptorSet
image bufimage.Image
}
func newFuzzResult(
runner command.Runner,
bufAnnotations []bufanalysis.FileAnnotation,
bufErr error,
protocErr error,
actualProtocFileDescriptorSet *descriptorpb.FileDescriptorSet,
image bufimage.Image,
) *fuzzResult {
return &fuzzResult{
runner: runner,
bufAnnotations: bufAnnotations,
bufErr: bufErr,
protocErr: protocErr,
actualProtocFileDescriptorSet: actualProtocFileDescriptorSet,
image: image,
}
}
// panicOrN panics if there is an error or returns the appropriate value for Fuzz to return.
func (f *fuzzResult) panicOrN(ctx context.Context) int {
if err := f.error(ctx); err != nil {
panic(err.Error())
}
// This will return 1 for valid protobufs and 0 for invalid in order to encourage the fuzzer to generate more
// realistic looking data.
if f.protocErr == nil {
return 1
}
return 0
}
// error returns an error that should cause Fuzz to panic.
func (f *fuzzResult) error(ctx context.Context) error {
if f.protocErr != nil {
if f.bufErr == nil && len(f.bufAnnotations) == 0 {
return fmt.Errorf("protoc has error but buf does not: %v", f.protocErr)
}
return nil
}
if f.bufErr != nil {
return fmt.Errorf("buf has error but protoc does not: %v", f.bufErr)
}
if len(f.bufAnnotations) > 0 {
return fmt.Errorf("buf has file annotations but protoc has no error: %v", f.bufAnnotations)
}
image := bufimage.ImageWithoutImports(f.image)
fileDescriptorSet := bufimage.ImageToFileDescriptorSet(image)
diff, err := prototesting.DiffFileDescriptorSetsJSON(
ctx,
f.runner,
fileDescriptorSet,
f.actualProtocFileDescriptorSet,
"buf",
"protoc",
)
if err != nil {
return fmt.Errorf("error diffing results: %v", err)
}
if strings.TrimSpace(diff) != "" {
return fmt.Errorf("protoc and buf have different results: %v", diff)
}
return nil
}