fuzz.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. Copyright 2014 Google Inc. All rights reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fuzz
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "reflect"
  18. "regexp"
  19. "time"
  20. )
  21. // fuzzFuncMap is a map from a type to a fuzzFunc that handles that type.
  22. type fuzzFuncMap map[reflect.Type]reflect.Value
  23. // Fuzzer knows how to fill any object with random fields.
  24. type Fuzzer struct {
  25. fuzzFuncs fuzzFuncMap
  26. defaultFuzzFuncs fuzzFuncMap
  27. r *rand.Rand
  28. nilChance float64
  29. minElements int
  30. maxElements int
  31. maxDepth int
  32. skipFieldPatterns []*regexp.Regexp
  33. }
  34. // New returns a new Fuzzer. Customize your Fuzzer further by calling Funcs,
  35. // RandSource, NilChance, or NumElements in any order.
  36. func New() *Fuzzer {
  37. return NewWithSeed(time.Now().UnixNano())
  38. }
  39. func NewWithSeed(seed int64) *Fuzzer {
  40. f := &Fuzzer{
  41. defaultFuzzFuncs: fuzzFuncMap{
  42. reflect.TypeOf(&time.Time{}): reflect.ValueOf(fuzzTime),
  43. },
  44. fuzzFuncs: fuzzFuncMap{},
  45. r: rand.New(rand.NewSource(seed)),
  46. nilChance: .2,
  47. minElements: 1,
  48. maxElements: 10,
  49. maxDepth: 100,
  50. }
  51. return f
  52. }
  53. // Funcs adds each entry in fuzzFuncs as a custom fuzzing function.
  54. //
  55. // Each entry in fuzzFuncs must be a function taking two parameters.
  56. // The first parameter must be a pointer or map. It is the variable that
  57. // function will fill with random data. The second parameter must be a
  58. // fuzz.Continue, which will provide a source of randomness and a way
  59. // to automatically continue fuzzing smaller pieces of the first parameter.
  60. //
  61. // These functions are called sensibly, e.g., if you wanted custom string
  62. // fuzzing, the function `func(s *string, c fuzz.Continue)` would get
  63. // called and passed the address of strings. Maps and pointers will always
  64. // be made/new'd for you, ignoring the NilChange option. For slices, it
  65. // doesn't make much sense to pre-create them--Fuzzer doesn't know how
  66. // long you want your slice--so take a pointer to a slice, and make it
  67. // yourself. (If you don't want your map/pointer type pre-made, take a
  68. // pointer to it, and make it yourself.) See the examples for a range of
  69. // custom functions.
  70. func (f *Fuzzer) Funcs(fuzzFuncs ...interface{}) *Fuzzer {
  71. for i := range fuzzFuncs {
  72. v := reflect.ValueOf(fuzzFuncs[i])
  73. if v.Kind() != reflect.Func {
  74. panic("Need only funcs!")
  75. }
  76. t := v.Type()
  77. if t.NumIn() != 2 || t.NumOut() != 0 {
  78. panic("Need 2 in and 0 out params!")
  79. }
  80. argT := t.In(0)
  81. switch argT.Kind() {
  82. case reflect.Ptr, reflect.Map:
  83. default:
  84. panic("fuzzFunc must take pointer or map type")
  85. }
  86. if t.In(1) != reflect.TypeOf(Continue{}) {
  87. panic("fuzzFunc's second parameter must be type fuzz.Continue")
  88. }
  89. f.fuzzFuncs[argT] = v
  90. }
  91. return f
  92. }
  93. // RandSource causes f to get values from the given source of randomness.
  94. // Use if you want deterministic fuzzing.
  95. func (f *Fuzzer) RandSource(s rand.Source) *Fuzzer {
  96. f.r = rand.New(s)
  97. return f
  98. }
  99. // NilChance sets the probability of creating a nil pointer, map, or slice to
  100. // 'p'. 'p' should be between 0 (no nils) and 1 (all nils), inclusive.
  101. func (f *Fuzzer) NilChance(p float64) *Fuzzer {
  102. if p < 0 || p > 1 {
  103. panic("p should be between 0 and 1, inclusive.")
  104. }
  105. f.nilChance = p
  106. return f
  107. }
  108. // NumElements sets the minimum and maximum number of elements that will be
  109. // added to a non-nil map or slice.
  110. func (f *Fuzzer) NumElements(atLeast, atMost int) *Fuzzer {
  111. if atLeast > atMost {
  112. panic("atLeast must be <= atMost")
  113. }
  114. if atLeast < 0 {
  115. panic("atLeast must be >= 0")
  116. }
  117. f.minElements = atLeast
  118. f.maxElements = atMost
  119. return f
  120. }
  121. func (f *Fuzzer) genElementCount() int {
  122. if f.minElements == f.maxElements {
  123. return f.minElements
  124. }
  125. return f.minElements + f.r.Intn(f.maxElements-f.minElements+1)
  126. }
  127. func (f *Fuzzer) genShouldFill() bool {
  128. return f.r.Float64() > f.nilChance
  129. }
  130. // MaxDepth sets the maximum number of recursive fuzz calls that will be made
  131. // before stopping. This includes struct members, pointers, and map and slice
  132. // elements.
  133. func (f *Fuzzer) MaxDepth(d int) *Fuzzer {
  134. f.maxDepth = d
  135. return f
  136. }
  137. // Skip fields which match the supplied pattern. Call this multiple times if needed
  138. // This is useful to skip XXX_ fields generated by protobuf
  139. func (f *Fuzzer) SkipFieldsWithPattern(pattern *regexp.Regexp) *Fuzzer {
  140. f.skipFieldPatterns = append(f.skipFieldPatterns, pattern)
  141. return f
  142. }
  143. // Fuzz recursively fills all of obj's fields with something random. First
  144. // this tries to find a custom fuzz function (see Funcs). If there is no
  145. // custom function this tests whether the object implements fuzz.Interface and,
  146. // if so, calls Fuzz on it to fuzz itself. If that fails, this will see if
  147. // there is a default fuzz function provided by this package. If all of that
  148. // fails, this will generate random values for all primitive fields and then
  149. // recurse for all non-primitives.
  150. //
  151. // This is safe for cyclic or tree-like structs, up to a limit. Use the
  152. // MaxDepth method to adjust how deep you need it to recurse.
  153. //
  154. // obj must be a pointer. Only exported (public) fields can be set (thanks,
  155. // golang :/ ) Intended for tests, so will panic on bad input or unimplemented
  156. // fields.
  157. func (f *Fuzzer) Fuzz(obj interface{}) {
  158. v := reflect.ValueOf(obj)
  159. if v.Kind() != reflect.Ptr {
  160. panic("needed ptr!")
  161. }
  162. v = v.Elem()
  163. f.fuzzWithContext(v, 0)
  164. }
  165. // FuzzNoCustom is just like Fuzz, except that any custom fuzz function for
  166. // obj's type will not be called and obj will not be tested for fuzz.Interface
  167. // conformance. This applies only to obj and not other instances of obj's
  168. // type.
  169. // Not safe for cyclic or tree-like structs!
  170. // obj must be a pointer. Only exported (public) fields can be set (thanks, golang :/ )
  171. // Intended for tests, so will panic on bad input or unimplemented fields.
  172. func (f *Fuzzer) FuzzNoCustom(obj interface{}) {
  173. v := reflect.ValueOf(obj)
  174. if v.Kind() != reflect.Ptr {
  175. panic("needed ptr!")
  176. }
  177. v = v.Elem()
  178. f.fuzzWithContext(v, flagNoCustomFuzz)
  179. }
  180. const (
  181. // Do not try to find a custom fuzz function. Does not apply recursively.
  182. flagNoCustomFuzz uint64 = 1 << iota
  183. )
  184. func (f *Fuzzer) fuzzWithContext(v reflect.Value, flags uint64) {
  185. fc := &fuzzerContext{fuzzer: f}
  186. fc.doFuzz(v, flags)
  187. }
  188. // fuzzerContext carries context about a single fuzzing run, which lets Fuzzer
  189. // be thread-safe.
  190. type fuzzerContext struct {
  191. fuzzer *Fuzzer
  192. curDepth int
  193. }
  194. func (fc *fuzzerContext) doFuzz(v reflect.Value, flags uint64) {
  195. if fc.curDepth >= fc.fuzzer.maxDepth {
  196. return
  197. }
  198. fc.curDepth++
  199. defer func() { fc.curDepth-- }()
  200. if !v.CanSet() {
  201. return
  202. }
  203. if flags&flagNoCustomFuzz == 0 {
  204. // Check for both pointer and non-pointer custom functions.
  205. if v.CanAddr() && fc.tryCustom(v.Addr()) {
  206. return
  207. }
  208. if fc.tryCustom(v) {
  209. return
  210. }
  211. }
  212. if fn, ok := fillFuncMap[v.Kind()]; ok {
  213. fn(v, fc.fuzzer.r)
  214. return
  215. }
  216. switch v.Kind() {
  217. case reflect.Map:
  218. if fc.fuzzer.genShouldFill() {
  219. v.Set(reflect.MakeMap(v.Type()))
  220. n := fc.fuzzer.genElementCount()
  221. for i := 0; i < n; i++ {
  222. key := reflect.New(v.Type().Key()).Elem()
  223. fc.doFuzz(key, 0)
  224. val := reflect.New(v.Type().Elem()).Elem()
  225. fc.doFuzz(val, 0)
  226. v.SetMapIndex(key, val)
  227. }
  228. return
  229. }
  230. v.Set(reflect.Zero(v.Type()))
  231. case reflect.Ptr:
  232. if fc.fuzzer.genShouldFill() {
  233. v.Set(reflect.New(v.Type().Elem()))
  234. fc.doFuzz(v.Elem(), 0)
  235. return
  236. }
  237. v.Set(reflect.Zero(v.Type()))
  238. case reflect.Slice:
  239. if fc.fuzzer.genShouldFill() {
  240. n := fc.fuzzer.genElementCount()
  241. v.Set(reflect.MakeSlice(v.Type(), n, n))
  242. for i := 0; i < n; i++ {
  243. fc.doFuzz(v.Index(i), 0)
  244. }
  245. return
  246. }
  247. v.Set(reflect.Zero(v.Type()))
  248. case reflect.Array:
  249. if fc.fuzzer.genShouldFill() {
  250. n := v.Len()
  251. for i := 0; i < n; i++ {
  252. fc.doFuzz(v.Index(i), 0)
  253. }
  254. return
  255. }
  256. v.Set(reflect.Zero(v.Type()))
  257. case reflect.Struct:
  258. for i := 0; i < v.NumField(); i++ {
  259. skipField := false
  260. fieldName := v.Type().Field(i).Name
  261. for _, pattern := range fc.fuzzer.skipFieldPatterns {
  262. if pattern.MatchString(fieldName) {
  263. skipField = true
  264. break
  265. }
  266. }
  267. if !skipField {
  268. fc.doFuzz(v.Field(i), 0)
  269. }
  270. }
  271. case reflect.Chan:
  272. fallthrough
  273. case reflect.Func:
  274. fallthrough
  275. case reflect.Interface:
  276. fallthrough
  277. default:
  278. panic(fmt.Sprintf("Can't handle %#v", v.Interface()))
  279. }
  280. }
  281. // tryCustom searches for custom handlers, and returns true iff it finds a match
  282. // and successfully randomizes v.
  283. func (fc *fuzzerContext) tryCustom(v reflect.Value) bool {
  284. // First: see if we have a fuzz function for it.
  285. doCustom, ok := fc.fuzzer.fuzzFuncs[v.Type()]
  286. if !ok {
  287. // Second: see if it can fuzz itself.
  288. if v.CanInterface() {
  289. intf := v.Interface()
  290. if fuzzable, ok := intf.(Interface); ok {
  291. fuzzable.Fuzz(Continue{fc: fc, Rand: fc.fuzzer.r})
  292. return true
  293. }
  294. }
  295. // Finally: see if there is a default fuzz function.
  296. doCustom, ok = fc.fuzzer.defaultFuzzFuncs[v.Type()]
  297. if !ok {
  298. return false
  299. }
  300. }
  301. switch v.Kind() {
  302. case reflect.Ptr:
  303. if v.IsNil() {
  304. if !v.CanSet() {
  305. return false
  306. }
  307. v.Set(reflect.New(v.Type().Elem()))
  308. }
  309. case reflect.Map:
  310. if v.IsNil() {
  311. if !v.CanSet() {
  312. return false
  313. }
  314. v.Set(reflect.MakeMap(v.Type()))
  315. }
  316. default:
  317. return false
  318. }
  319. doCustom.Call([]reflect.Value{v, reflect.ValueOf(Continue{
  320. fc: fc,
  321. Rand: fc.fuzzer.r,
  322. })})
  323. return true
  324. }
  325. // Interface represents an object that knows how to fuzz itself. Any time we
  326. // find a type that implements this interface we will delegate the act of
  327. // fuzzing itself.
  328. type Interface interface {
  329. Fuzz(c Continue)
  330. }
  331. // Continue can be passed to custom fuzzing functions to allow them to use
  332. // the correct source of randomness and to continue fuzzing their members.
  333. type Continue struct {
  334. fc *fuzzerContext
  335. // For convenience, Continue implements rand.Rand via embedding.
  336. // Use this for generating any randomness if you want your fuzzing
  337. // to be repeatable for a given seed.
  338. *rand.Rand
  339. }
  340. // Fuzz continues fuzzing obj. obj must be a pointer.
  341. func (c Continue) Fuzz(obj interface{}) {
  342. v := reflect.ValueOf(obj)
  343. if v.Kind() != reflect.Ptr {
  344. panic("needed ptr!")
  345. }
  346. v = v.Elem()
  347. c.fc.doFuzz(v, 0)
  348. }
  349. // FuzzNoCustom continues fuzzing obj, except that any custom fuzz function for
  350. // obj's type will not be called and obj will not be tested for fuzz.Interface
  351. // conformance. This applies only to obj and not other instances of obj's
  352. // type.
  353. func (c Continue) FuzzNoCustom(obj interface{}) {
  354. v := reflect.ValueOf(obj)
  355. if v.Kind() != reflect.Ptr {
  356. panic("needed ptr!")
  357. }
  358. v = v.Elem()
  359. c.fc.doFuzz(v, flagNoCustomFuzz)
  360. }
  361. // RandString makes a random string up to 20 characters long. The returned string
  362. // may include a variety of (valid) UTF-8 encodings.
  363. func (c Continue) RandString() string {
  364. return randString(c.Rand)
  365. }
  366. // RandUint64 makes random 64 bit numbers.
  367. // Weirdly, rand doesn't have a function that gives you 64 random bits.
  368. func (c Continue) RandUint64() uint64 {
  369. return randUint64(c.Rand)
  370. }
  371. // RandBool returns true or false randomly.
  372. func (c Continue) RandBool() bool {
  373. return randBool(c.Rand)
  374. }
  375. func fuzzInt(v reflect.Value, r *rand.Rand) {
  376. v.SetInt(int64(randUint64(r)))
  377. }
  378. func fuzzUint(v reflect.Value, r *rand.Rand) {
  379. v.SetUint(randUint64(r))
  380. }
  381. func fuzzTime(t *time.Time, c Continue) {
  382. var sec, nsec int64
  383. // Allow for about 1000 years of random time values, which keeps things
  384. // like JSON parsing reasonably happy.
  385. sec = c.Rand.Int63n(1000 * 365 * 24 * 60 * 60)
  386. c.Fuzz(&nsec)
  387. *t = time.Unix(sec, nsec)
  388. }
  389. var fillFuncMap = map[reflect.Kind]func(reflect.Value, *rand.Rand){
  390. reflect.Bool: func(v reflect.Value, r *rand.Rand) {
  391. v.SetBool(randBool(r))
  392. },
  393. reflect.Int: fuzzInt,
  394. reflect.Int8: fuzzInt,
  395. reflect.Int16: fuzzInt,
  396. reflect.Int32: fuzzInt,
  397. reflect.Int64: fuzzInt,
  398. reflect.Uint: fuzzUint,
  399. reflect.Uint8: fuzzUint,
  400. reflect.Uint16: fuzzUint,
  401. reflect.Uint32: fuzzUint,
  402. reflect.Uint64: fuzzUint,
  403. reflect.Uintptr: fuzzUint,
  404. reflect.Float32: func(v reflect.Value, r *rand.Rand) {
  405. v.SetFloat(float64(r.Float32()))
  406. },
  407. reflect.Float64: func(v reflect.Value, r *rand.Rand) {
  408. v.SetFloat(r.Float64())
  409. },
  410. reflect.Complex64: func(v reflect.Value, r *rand.Rand) {
  411. panic("unimplemented")
  412. },
  413. reflect.Complex128: func(v reflect.Value, r *rand.Rand) {
  414. panic("unimplemented")
  415. },
  416. reflect.String: func(v reflect.Value, r *rand.Rand) {
  417. v.SetString(randString(r))
  418. },
  419. reflect.UnsafePointer: func(v reflect.Value, r *rand.Rand) {
  420. panic("unimplemented")
  421. },
  422. }
  423. // randBool returns true or false randomly.
  424. func randBool(r *rand.Rand) bool {
  425. if r.Int()&1 == 1 {
  426. return true
  427. }
  428. return false
  429. }
  430. type charRange struct {
  431. first, last rune
  432. }
  433. // choose returns a random unicode character from the given range, using the
  434. // given randomness source.
  435. func (r *charRange) choose(rand *rand.Rand) rune {
  436. count := int64(r.last - r.first)
  437. return r.first + rune(rand.Int63n(count))
  438. }
  439. var unicodeRanges = []charRange{
  440. {' ', '~'}, // ASCII characters
  441. {'\u00a0', '\u02af'}, // Multi-byte encoded characters
  442. {'\u4e00', '\u9fff'}, // Common CJK (even longer encodings)
  443. }
  444. // randString makes a random string up to 20 characters long. The returned string
  445. // may include a variety of (valid) UTF-8 encodings.
  446. func randString(r *rand.Rand) string {
  447. n := r.Intn(20)
  448. runes := make([]rune, n)
  449. for i := range runes {
  450. runes[i] = unicodeRanges[r.Intn(len(unicodeRanges))].choose(r)
  451. }
  452. return string(runes)
  453. }
  454. // randUint64 makes random 64 bit numbers.
  455. // Weirdly, rand doesn't have a function that gives you 64 random bits.
  456. func randUint64(r *rand.Rand) uint64 {
  457. return uint64(r.Uint32())<<32 | uint64(r.Uint32())
  458. }