Can use ospaths apparently

This commit is contained in:
kdeme 2019-10-02 13:01:50 +02:00 committed by zah
parent 94dee80aeb
commit 2a0793ce60
1 changed files with 15 additions and 36 deletions

View File

@ -1,4 +1,4 @@
import strformat, strutils import strformat, ospaths
# Dependencies: # Dependencies:
# - afl fuzzing: afl and gcc or clang/llvm # - afl fuzzing: afl and gcc or clang/llvm
@ -42,75 +42,54 @@ type
clang = aflClang, clang = aflClang,
clangFast = aflClangFast clangFast = aflClangFast
proc quote(s: string): string {.noSideEffect.} =
## Copy of quoteShellPosix from os module
const safeUnixChars = {'%', '+', '-', '.', '/', '_', ':', '=', '@',
'0'..'9', 'A'..'Z', 'a'..'z'}
if s.len == 0:
return "''"
let safe = s.allCharsInSet(safeUnixChars)
if safe:
return s
else:
return "'" & s.replace("'", "'\"'\"'") & "'"
proc aflCompile*(target: string, c: Compiler) = proc aflCompile*(target: string, c: Compiler) =
let aflOptions = &"-d:standalone -d:noSignalHandler {$c}" let aflOptions = &"-d:standalone -d:noSignalHandler {$c}"
let compileCmd = &"nim c {defaultFlags} {aflOptions} {target.quote()}" let compileCmd = &"nim c {defaultFlags} {aflOptions} {target.quoteShell()}"
exec compileCmd exec compileCmd
proc aflExec*(target: string, inputDir: string, resultsDir: string, proc aflExec*(target: string, inputDir: string, resultsDir: string,
cleanStart = false) = cleanStart = false) =
let exe = target.addFileExt(ExeExt)
if not dirExists(inputDir): if not dirExists(inputDir):
# create a input dir with one 0 file for afl # create a input dir with one 0 file for afl
mkDir(inputDir) mkDir(inputDir)
# TODO: improve
withDir inputDir: exec "echo '0' > test" withDir inputDir: exec "echo '0' > test"
var fuzzCmd: string var fuzzCmd: string
# if there is an output dir already, continue fuzzing from previous run # if there is an output dir already, continue fuzzing from previous run
if (not dirExists(resultsDir)) or cleanStart: if (not dirExists(resultsDir)) or cleanStart:
fuzzCmd = &"afl-fuzz -i {inputDir.quote()} -o {resultsDir.quote()} -M fuzzer01 -- ./{target.quote()}" fuzzCmd = &"afl-fuzz -i {inputDir.quoteShell()} -o {resultsDir.quoteShell()} -M fuzzer01 -- {exe.quoteShell()}"
else: else:
fuzzCmd = &"afl-fuzz -i - -o {resultsDir.quote()} -M fuzzer01 -- ./{target.quote()}" fuzzCmd = &"afl-fuzz -i - -o {resultsDir.quoteShell()} -M fuzzer01 -- {exe.quoteShell()}"
exec fuzzCmd exec fuzzCmd
proc libFuzzerCompile*(target: string) = proc libFuzzerCompile*(target: string) =
let libFuzzerOptions = &"--noMain {libFuzzerClang}" let libFuzzerOptions = &"--noMain {libFuzzerClang}"
let compileCmd = &"nim c {defaultFlags} {libFuzzerOptions} {target.quote()}" let compileCmd = &"nim c {defaultFlags} {libFuzzerOptions} {target.quoteShell()}"
exec compileCmd exec compileCmd
proc libFuzzerExec*(target: string, corpusDir: string) = proc libFuzzerExec*(target: string, corpusDir: string) =
let exe = target.addFileExt(ExeExt)
if not dirExists(corpusDir): if not dirExists(corpusDir):
# libFuzzer is OK when starting with empty corpus dir # libFuzzer is OK when starting with empty corpus dir
mkDir(corpusDir) mkDir(corpusDir)
exec &"./{target.quote()} {corpusDir.quote()}" exec &"{exe.quoteShell()} {corpusDir.quoteShell()}"
proc getDir*(path: string): string =
# TODO: This is not platform friendly at all.
let splitFile = path.rsplit("/", 1)
result = splitFile[0]
proc getTarget*(path: string): string =
# TODO: error handling
result = path
result.removeSuffix(".nim")
proc runFuzzer*(targetPath: string, fuzzer: Fuzzer) = proc runFuzzer*(targetPath: string, fuzzer: Fuzzer) =
let let (path, target, ext) = splitFile(targetPath)
path = getDir(targetPath)
target = getTarget(targetPath)
case fuzzer case fuzzer
of afl: of afl:
aflCompile(targetPath, gcc) aflCompile(targetPath, gcc)
aflExec(target, path & "/input", path & "/results") aflExec(path & DirSep & target,
path & DirSep & "input",
path & DirSep & "results")
of libFuzzer: of libFuzzer:
libFuzzerCompile(targetPath) libFuzzerCompile(targetPath)
# Note: Lets not mix afl input with libFuzzer corpus default. This can have # Note: Lets not mix afl input with libFuzzer corpus default. This can have
# consequences on speed for afl. Better to look into merging afl results & # consequences on speed for afl. Better to look into merging afl results &
# libFuzzer corpus. # libFuzzer corpus.
libFuzzerExec(target, path & "/corpus") libFuzzerExec(path & DirSep & target, path & DirSep & "corpus")