Adress review feedback

This commit is contained in:
kdeme 2019-10-01 12:00:20 +02:00 committed by zah
parent 91862ce65b
commit 035b7eda24
4 changed files with 13 additions and 19 deletions

View File

@ -44,7 +44,7 @@ type
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}" let compileCmd = &"""nim c {defaultFlags} {aflOptions} {target}"""
exec compileCmd exec compileCmd
proc aflExec*(target: string, inputDir: string, resultsDir: string, proc aflExec*(target: string, inputDir: string, resultsDir: string,
@ -57,14 +57,14 @@ proc aflExec*(target: string, inputDir: string, resultsDir: string,
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} -o {resultsDir} -M fuzzer01 -- ./{target}" fuzzCmd = &"""afl-fuzz -i {inputDir} -o {resultsDir} -M fuzzer01 -- ./{target}"""
else: else:
fuzzCmd = &"afl-fuzz -i - -o {resultsDir} -M fuzzer01 -- ./{target}" fuzzCmd = &"""afl-fuzz -i - -o {resultsDir} -M fuzzer01 -- ./{target}"""
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}" let compileCmd = &"""nim c {defaultFlags} {libFuzzerOptions} {target}"""
exec compileCmd exec compileCmd
proc libFuzzerExec*(target: string, corpusDir: string) = proc libFuzzerExec*(target: string, corpusDir: string) =
@ -72,7 +72,7 @@ proc libFuzzerExec*(target: string, corpusDir: string) =
# libFuzzer is OK when starting with empty corpus dir # libFuzzer is OK when starting with empty corpus dir
mkDir(corpusDir) mkDir(corpusDir)
exec &"./{target} {corpusDir}" exec &"""./{target} {corpusDir}"""
proc getDir*(path: string): string = proc getDir*(path: string): string =
# TODO: This is not platform friendly at all. # TODO: This is not platform friendly at all.

View File

@ -1,4 +1,4 @@
import streams, posix, strutils, chronicles, macros import streams, posix, strutils, chronicles, macros, stew/ranges/ptr_arith
template fuzz(body) = template fuzz(body) =
# For code we want to fuzz, SIGSEGV is needed on unwanted exceptions. # For code we want to fuzz, SIGSEGV is needed on unwanted exceptions.
@ -27,9 +27,6 @@ proc readStdin*(): seq[byte] =
proc NimMain() {.importc: "NimMain".} proc NimMain() {.importc: "NimMain".}
template `+`*[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) +% off * sizeof(p[]))
template test*(body: untyped): untyped = template test*(body: untyped): untyped =
when defined(standalone): when defined(standalone):
var payload {.inject.} = readStdin() var payload {.inject.} = readStdin()
@ -38,12 +35,8 @@ template test*(body: untyped): untyped =
else: else:
proc fuzzerCall(data: ptr byte, len: csize): proc fuzzerCall(data: ptr byte, len: csize):
cint {.exportc: "LLVMFuzzerTestOneInput".} = cint {.exportc: "LLVMFuzzerTestOneInput".} =
var payload {.inject.} : seq[byte] template payload(): auto =
if len > 0: makeOpenArray(data, len)
# TODO: something better to get this data in the seq?
newSeq(payload, len)
for i in 0..<len:
payload[i] = (data + i)[]
`body` `body`

View File

@ -6,7 +6,7 @@ init:
test: test:
try: try:
var rlp = rlpFromBytes(payload.toRange) var rlp = rlpFromBytes(@payload.toRange)
discard rlp.inspect() discard rlp.inspect()
except RlpError: except RlpError:
debug "Inspect failed", err = getCurrentExceptionMsg() debug "Inspect failed", err = getCurrentExceptionMsg()

View File

@ -33,8 +33,9 @@ template asyncTest*(name, body: untyped) =
proc scenario {.async.} = body proc scenario {.async.} = body
waitFor scenario() waitFor scenario()
proc packData*(payload: seq[byte], pk: PrivateKey): seq[byte] = proc packData*(payload: openArray[byte], pk: PrivateKey): seq[byte] =
let let
payloadSeq = @payload
signature = @(pk.signMessage(payload).getRaw()) signature = @(pk.signMessage(payload).getRaw())
msgHash = keccak256.digest(signature & payload) msgHash = keccak256.digest(signature & payloadSeq)
result = @(msgHash.data) & signature & payload result = @(msgHash.data) & signature & payloadSeq