funcs.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2023 The go-fuzz-headers Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package gofuzzheaders
  15. import (
  16. "fmt"
  17. "reflect"
  18. )
  19. type Continue struct {
  20. F *ConsumeFuzzer
  21. }
  22. func (f *ConsumeFuzzer) AddFuncs(fuzzFuncs []interface{}) {
  23. for i := range fuzzFuncs {
  24. v := reflect.ValueOf(fuzzFuncs[i])
  25. if v.Kind() != reflect.Func {
  26. panic("Need only funcs!")
  27. }
  28. t := v.Type()
  29. if t.NumIn() != 2 || t.NumOut() != 1 {
  30. fmt.Println(t.NumIn(), t.NumOut())
  31. panic("Need 2 in and 1 out params. In must be the type. Out must be an error")
  32. }
  33. argT := t.In(0)
  34. switch argT.Kind() {
  35. case reflect.Ptr, reflect.Map:
  36. default:
  37. panic("fuzzFunc must take pointer or map type")
  38. }
  39. if t.In(1) != reflect.TypeOf(Continue{}) {
  40. panic("fuzzFunc's second parameter must be type Continue")
  41. }
  42. f.Funcs[argT] = v
  43. }
  44. }
  45. func (f *ConsumeFuzzer) GenerateWithCustom(targetStruct interface{}) error {
  46. e := reflect.ValueOf(targetStruct).Elem()
  47. return f.fuzzStruct(e, true)
  48. }
  49. func (c Continue) GenerateStruct(targetStruct interface{}) error {
  50. return c.F.GenerateStruct(targetStruct)
  51. }
  52. func (c Continue) GenerateStructWithCustom(targetStruct interface{}) error {
  53. return c.F.GenerateWithCustom(targetStruct)
  54. }