Raise exception when failing deserialization instead of returning false.

Fuzzer preprocessing should provide valid ssz.
This commit is contained in:
Nathaniel Jensen 2019-12-16 16:46:58 +11:00 committed by zah
parent dff61c2ea1
commit 0d764d87af
1 changed files with 7 additions and 4 deletions

View File

@ -13,6 +13,9 @@ type
AttestationInput = object
state: BeaconState
attestation: Attestation
# This and AssertionError are raised to indicate programming bugs
# Used as a wrapper to allow exception tracking to identify unexpected exceptions
FuzzCrashError* = object of Exception
# TODO: change ptr uint to ptr csize_t when available in newer Nim version.
proc copyState(state: BeaconState, output: ptr byte,
@ -32,13 +35,13 @@ proc copyState(state: BeaconState, output: ptr byte,
result = true
proc nfuzz_block(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[].} =
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError].} =
var data: BlockInput
try:
data = SSZ.decode(input, BlockInput)
except MalformedSszError, SszSizeMismatchError, RangeError:
return false
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.")
try:
result = state_transition(data.state, data.beaconBlock, flags = {})
@ -49,7 +52,7 @@ proc nfuzz_block(input: openArray[byte], output: ptr byte,
result = copyState(data.state, output, output_size)
proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
output_size: ptr uint): bool {.exportc, raises:[].} =
output_size: ptr uint): bool {.exportc, raises:[FuzzCrashError].} =
var
data: AttestationInput
cache = get_empty_per_epoch_cache()
@ -57,7 +60,7 @@ proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
try:
data = SSZ.decode(input, AttestationInput)
except MalformedSszError, SszSizeMismatchError, RangeError:
return false
raise newException(FuzzCrashError, "SSZ deserialisation failed, likely bug in preprocessing.")
try:
result = process_attestation(data.state, data.attestation,