]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] Revert "[dev.fuzz] testing: convert seed corpus values where possible"
authorKatie Hockman <katie@golang.org>
Thu, 16 Sep 2021 14:03:04 +0000 (10:03 -0400)
committerKatie Hockman <katie@golang.org>
Mon, 20 Sep 2021 17:18:57 +0000 (17:18 +0000)
This reverts commit 413c125da38990720744c0d98ab65c0d5b1602da.

Reason for revert: Giving this more thought, we've decided that
converting types under the hood may cause unexpected behavior to
users. This is a feature that can always be added after more
consideration has been done, but is not something that can be
removed due to the backwards compatibility promise.

Updates golang/go#45593

Change-Id: I79bab24979d7e4c294e6cb6455d4c7729d6a0efb
Reviewed-on: https://go-review.googlesource.com/c/go/+/350251
Trust: Katie Hockman <katie@golang.org>
Trust: Joe Tsai <joetsai@digital-static.net>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
src/cmd/go/testdata/script/test_fuzz.txt
src/internal/fuzz/fuzz.go
src/testing/fuzz.go

index 3f825c1bc3d535c6092f476ef56868b3f6edb394..b1a02f46ebc37caa523c199afddb710d2dcaa239 100644 (file)
@@ -131,23 +131,11 @@ stdout FAIL
 ! stdout ^ok
 stdout FAIL
 
-# Test that converting compatible value from f.Add successful runs cleanly.
-go test -run FuzzConvertType fuzz_add_test.go
-stdout ^ok
-! stdout FAIL
-
-# Test that converting incompatible value from f.Add fails.
-! go test -run FuzzConvertIncompatibleType fuzz_add_test.go
+# Test that the wrong type given with f.Add will fail.
+! go test -run FuzzWrongType fuzz_add_test.go
 ! stdout ^ok
 stdout FAIL
 
-# Test that converts value which would lose precision from f.Add.
-# Consider making a test like this fail, as it may have unexpected
-# consequences for the developer.
-go test -v -run FuzzConvertLosePrecision fuzz_add_test.go
-stdout ok
-! stdout FAIL
-
 # Test fatal with testdata seed corpus
 ! go test -run FuzzFail corpustesting/fuzz_testdata_corpus_test.go
 ! stdout ^ok
@@ -391,19 +379,9 @@ func FuzzAddDifferentType(f *testing.F) {
     f.Fuzz(func(*testing.T, []byte) {})
 }
 
-func FuzzConvertIncompatibleType(f *testing.F) {
-    f.Add("abcde")
-    f.Fuzz(func(*testing.T, int64) {})
-}
-
-func FuzzConvertLosePrecision(f *testing.F) {
-    f.Add(-1)
-    f.Fuzz(func(*testing.T, uint) {})
-}
-
-func FuzzConvertType(f *testing.F) {
-    f.Add(1, "hello")
-    f.Fuzz(func(*testing.T, uint, []byte) {})
+func FuzzWrongType(f *testing.F) {
+    f.Add("hello")
+    f.Fuzz(func(*testing.T, []byte) {})
 }
 
 -- corpustesting/fuzz_testdata_corpus_test.go --
index 99cf39e10051d3fcd1ae1998ac6c9505cb23684a..2cd7ebb47285870e026ca61f76dd521484673a36 100644 (file)
@@ -942,47 +942,19 @@ func readCorpusData(data []byte, types []reflect.Type) ([]interface{}, error) {
 }
 
 // CheckCorpus verifies that the types in vals match the expected types
-// provided. If not, attempt to convert them. If that's not possible, return an
-// error.
+// provided.
 func CheckCorpus(vals []interface{}, types []reflect.Type) error {
        if len(vals) != len(types) {
-               return fmt.Errorf("wrong number of values in corpus file: %d, want %d", len(vals), len(types))
+               return fmt.Errorf("wrong number of values in corpus entry: %d, want %d", len(vals), len(types))
        }
        for i := range types {
-               orig := reflect.ValueOf(vals[i])
-               origType := orig.Type()
-               wantType := types[i]
-               if origType == wantType {
-                       continue // already the same type
-               }
-               // Attempt to convert the corpus value to the expected type
-               if !origType.ConvertibleTo(wantType) {
-                       return fmt.Errorf("cannot convert %v to %v", origType, wantType)
-               }
-               convertedVal, ok := convertToType(orig, wantType)
-               if !ok {
-                       return fmt.Errorf("error converting %v to %v", origType, wantType)
+               if reflect.TypeOf(vals[i]) != types[i] {
+                       return fmt.Errorf("mismatched types in corpus entry: %v, want %v", vals, types)
                }
-               // TODO: Check that the value didn't change.
-               // e.g. val went from int64(-1) -> uint(0) -> int64(0) which should fail
-
-               // Updates vals to use the newly converted value of the expected type.
-               vals[i] = convertedVal.Interface()
        }
        return nil
 }
 
-func convertToType(orig reflect.Value, t reflect.Type) (converted reflect.Value, ok bool) {
-       // Convert might panic even if ConvertibleTo returns true, so catch
-       // that panic and return false.
-       defer func() {
-               if r := recover(); r != nil {
-                       ok = false
-               }
-       }()
-       return orig.Convert(t), true
-}
-
 // writeToCorpus atomically writes the given bytes to a new file in testdata.
 // If the directory does not exist, it will create one. If the file already
 // exists, writeToCorpus will not rewrite it. writeToCorpus returns the
index 3a1b0bdeaa0db92ae998a807c40e1e99ab59f567..57ea41803915d06a5869daf703d00941ea8815a3 100644 (file)
@@ -250,7 +250,7 @@ func (f *F) TempDir() string {
 
 // Add will add the arguments to the seed corpus for the fuzz target. This will
 // be a no-op if called after or within the Fuzz function. The args must match
-// or be convertible to those in the Fuzz function.
+// those in the Fuzz function.
 func (f *F) Add(args ...interface{}) {
        var values []interface{}
        for i := range args {