strtabs demo mutator

This commit is contained in:
Antonis Geralis 2022-09-07 15:16:06 +03:00
parent 4662b4215a
commit a0e4a1c6ed
5 changed files with 26 additions and 4 deletions

View File

@ -154,8 +154,8 @@ This is only necessary for destructor-based types. `mutate`, `default` and `==`
- Don't `echo` in a fuzz target as it slows down execution speed.
- Prefer `-d:danger|release` for maximum performance.
- Once you have a crash you can recompile with `-d:debug` and pass the crashing test case as parameter.
- With this line `when defined(dumpFuzzInput): debugEcho(x)` in a target and `-d:dumpFuzzInput`, observe the crashing input.
- You could compile without sanitizers, AddressSanitizer slows down by 2x, but it's not recommended.
- Use `debugEcho(x)` in a target to print the crashing input.
- You could compile without sanitizers, AddressSanitizer slows down programs by ~2x, but it's not recommended.
### What's not supported

View File

@ -52,7 +52,7 @@ when isMainModule:
proc default(_: typedesc[HtmlNode]): HtmlNode =
HtmlNode(tag: text, s: "")
func fuzzTarget(x: HtmlNode) =
proc fuzzTarget(x: HtmlNode) =
when defined(dumpFuzzInput): debugEcho(x)
# Here you could feed `$x` to htmlparser.parseHtml and make sure it doesn't crash.
#var errors: seq[string] = @[]

21
examples/fuzz_strtabs.nim Normal file
View File

@ -0,0 +1,21 @@
import drchaos/mutator, std/[strtabs, random]
proc mutate(data: var StringTableRef; sizeIncreaseHint: int; enforceChanges: bool; r: var Rand) =
var value: seq[tuple[key, value: string]]
for p in pairs(data):
value.add p
repeatMutateInplace(mutateSeq(value, tmp, 2, sizeIncreaseHint, r))
clear(data)
for key, val in value.items:
data[key] = val
proc mutate(value: var string; sizeIncreaseHint: int; enforceChanges: bool; r: var Rand) =
repeatMutate(mutateUtf8String(move value, 4, sizeIncreaseHint, r))
proc default(_: typedesc[StringTableRef]): StringTableRef =
newStringTable(modeCaseSensitive)
func fuzzTarget(x: StringTableRef) =
doAssert x != {"key1": "val1", "key2": "val2"}.newStringTable
defaultMutator(fuzzTarget)

View File

@ -1,5 +1,6 @@
# WARNING: This mutator crashes for OrderedTable and it's too slow with Table.
# TODO: split into files and make it compile again.
# Use https://nim-lang.org/docs/importutils.html
import random
include std/tables

View File

@ -21,6 +21,6 @@ func fuzzTarget(x: ContentNode) =
ContentNode(kind: Text, textStr: "mychild"),
ContentNode(kind: Br)
])
doAssert x != data
doAssert x != x
defaultMutator(fuzzTarget)