diff --git a/README.md b/README.md index 76b7d5d..d49cd82 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ be overloaded. Which is especially useful when `nil` for `ref` is not an accepta ### Needed config -Add these flags to either `.nims`, `config.nims`, `nim.cfg` or directly pass to the compiler: +Add these flags to either `.nims`, `config.nims`, `nim.cfg` or directly pass to the nim compiler: ```nim --cc: clang diff --git a/drchaos/mutator.nim b/drchaos/mutator.nim index ee06d9a..4f3c3d8 100644 --- a/drchaos/mutator.nim +++ b/drchaos/mutator.nim @@ -562,10 +562,11 @@ proc myMutator*[T](x: var T; sizeIncreaseHint: Natural; r: var Rand) {.nimcall.} runPostProcessor(x, MaxInitializeDepth, r) template initializeImpl*() = - proc NimMain() {.importc: "NimMain".} + when not defined(fuzzerStandalone): + proc NimMain() {.importc: "NimMain".} - proc LLVMFuzzerInitialize(): cint {.exportc.} = - NimMain() + proc LLVMFuzzerInitialize(): cint {.exportc.} = + NimMain() template mutatorImpl*(target, mutator, typ: untyped) = mixin default @@ -627,13 +628,16 @@ template mutatorImpl*(target, mutator, typ: untyped) = when compileOption("exceptions", "goto"): {.emit: "nimTestErrorFlag();".} - proc LLVMFuzzerCustomMutator(data: ptr UncheckedArray[byte]; len, maxLen: int; - seed: int64): int {.exportc.} = - try: - result = customMutatorImpl(toOpenArray(data, 0, len-1), maxLen, seed) - finally: - when compileOption("exceptions", "goto"): - {.emit: "nimTestErrorFlag();".} + when defined(fuzzerStandalone): + include standalone + else: + proc LLVMFuzzerCustomMutator(data: ptr UncheckedArray[byte]; len, maxLen: int; + seed: int64): int {.exportc.} = + try: + result = customMutatorImpl(toOpenArray(data, 0, len-1), maxLen, seed) + finally: + when compileOption("exceptions", "goto"): + {.emit: "nimTestErrorFlag();".} proc commonImpl(target, mutator: NimNode): NimNode = let typ = getImpl(target).params[^1][1] diff --git a/drchaos/standalone.nim b/drchaos/standalone.nim new file mode 100644 index 0000000..b0029dd --- /dev/null +++ b/drchaos/standalone.nim @@ -0,0 +1,12 @@ +import std/[os, strformat, strutils] + +proc standaloneFuzzTarget = + stderr.write &"StandaloneFuzzTarget: running {paramCount()} inputs\n" + #discard initialize() + for i in 1..paramCount(): + stderr.write &"Running: {paramStr(i)}\n" + var buf = readFile(paramStr(i)) + discard LLVMFuzzerTestOneInput(cast[ptr UncheckedArray[byte]](cstring(buf)), buf.len) + stderr.write &"Done: {paramStr(i)}: ({formatSize(buf.len)})\n" + +standaloneFuzzTarget()