2019-11-27 20:43:02 +00:00
|
|
|
import
|
|
|
|
endians, stew/ptrops, stew/ranges/ptr_arith,
|
|
|
|
../beacon_chain/[ssz, state_transition],
|
2020-01-08 01:03:33 +00:00
|
|
|
../beacon_chain/spec/[datatypes, helpers, digest, validator, beaconstate,
|
|
|
|
state_transition_block],
|
|
|
|
# Required for deserialisation of ValidatorSig in Attestation due to
|
2019-11-27 20:43:02 +00:00
|
|
|
# https://github.com/nim-lang/Nim/issues/11225
|
2019-12-29 23:43:18 +00:00
|
|
|
../beacon_chain/spec/crypto,
|
|
|
|
../beacon_chain/extras
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
BlockInput = object
|
|
|
|
state: BeaconState
|
|
|
|
beaconBlock: BeaconBlock
|
2019-12-17 04:50:47 +00:00
|
|
|
BlockHeaderInput = BlockInput
|
|
|
|
AttesterSlashingInput = object
|
|
|
|
state: BeaconState
|
|
|
|
attesterSlashing: AttesterSlashing
|
2019-11-27 20:43:02 +00:00
|
|
|
AttestationInput = object
|
|
|
|
state: BeaconState
|
|
|
|
attestation: Attestation
|
2019-12-16 05:46:58 +00:00
|
|
|
# This and AssertionError are raised to indicate programming bugs
|
2020-01-08 01:03:33 +00:00
|
|
|
# A wrapper to allow exception tracking to identify unexpected exceptions
|
2019-12-17 03:01:30 +00:00
|
|
|
FuzzCrashError = object of Exception
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
# TODO: change ptr uint to ptr csize_t when available in newer Nim version.
|
2019-11-27 20:43:02 +00:00
|
|
|
proc copyState(state: BeaconState, output: ptr byte,
|
2020-01-08 01:03:33 +00:00
|
|
|
output_size: ptr uint): bool {.raises: [FuzzCrashError, Defect].} =
|
2019-11-28 22:01:12 +00:00
|
|
|
var resultState: seq[byte]
|
|
|
|
|
|
|
|
try:
|
|
|
|
resultState = SSZ.encode(state)
|
2019-12-17 03:01:30 +00:00
|
|
|
except IOError as e:
|
2020-01-08 00:54:21 +00:00
|
|
|
# Shouldn't occur as the writer isn't a file
|
2019-12-17 03:01:30 +00:00
|
|
|
raise newException(FuzzCrashError, "Unexpected failure to serialize.", e)
|
|
|
|
|
|
|
|
if unlikely(resultState.len.uint > output_size[]):
|
2020-01-08 01:03:33 +00:00
|
|
|
let msg = (
|
|
|
|
"Not enough output buffer provided to nimbus harness. Provided: " &
|
|
|
|
$(output_size[]) &
|
|
|
|
"Required: " &
|
|
|
|
$resultState.len.uint
|
|
|
|
)
|
|
|
|
raise newException(FuzzCrashError, msg)
|
2019-12-17 03:01:30 +00:00
|
|
|
output_size[] = resultState.len.uint
|
|
|
|
# TODO: improvement might be to write directly to buffer with OutputStream
|
|
|
|
# and SszWriter (but then need to ensure length doesn't overflow)
|
|
|
|
copyMem(output, unsafeAddr resultState[0], output_size[])
|
|
|
|
result = true
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
proc nfuzz_attestation(input: openArray[byte], output: ptr byte,
|
2020-01-08 01:03:33 +00:00
|
|
|
output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2019-12-17 04:50:47 +00:00
|
|
|
var
|
|
|
|
data: AttestationInput
|
|
|
|
cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = SSZ.decode(input, AttestationInput)
|
|
|
|
except MalformedSszError, SszSizeMismatchError:
|
|
|
|
let e = getCurrentException()
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"SSZ deserialisation failed, likely bug in preprocessing.",
|
|
|
|
e,
|
|
|
|
)
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = process_attestation(data.state, data.attestation,
|
2019-12-29 23:43:18 +00:00
|
|
|
{skipValidation}, cache)
|
2020-01-08 00:54:21 +00:00
|
|
|
except ValueError as e:
|
|
|
|
# These exceptions are expected to be raised by chronicles logging:
|
|
|
|
# See status-im/nim-chronicles#60
|
|
|
|
# TODO remove this when resolved
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Unexpected (logging?) error in attestation processing",
|
|
|
|
e
|
|
|
|
)
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
result = copyState(data.state, output, output_size)
|
|
|
|
|
|
|
|
|
|
|
|
proc nfuzz_attester_slashing(input: openArray[byte], output: ptr byte,
|
2020-01-08 01:03:33 +00:00
|
|
|
output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2019-12-17 04:50:47 +00:00
|
|
|
var
|
|
|
|
data: AttesterSlashingInput
|
|
|
|
cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = SSZ.decode(input, AttesterSlashingInput)
|
|
|
|
except MalformedSszError, SszSizeMismatchError:
|
|
|
|
let e = getCurrentException()
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"SSZ deserialisation failed, likely bug in preprocessing.",
|
|
|
|
e,
|
|
|
|
)
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = process_attester_slashing(data.state, data.attesterSlashing, cache)
|
2020-01-08 00:54:21 +00:00
|
|
|
except ValueError as e:
|
|
|
|
# TODO remove when status-im/nim-chronicles#60 is resolved
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Unexpected (logging?) error in attester slashing",
|
|
|
|
e,
|
|
|
|
)
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
result = copyState(data.state, output, output_size)
|
|
|
|
|
|
|
|
|
2019-11-27 20:43:02 +00:00
|
|
|
proc nfuzz_block(input: openArray[byte], output: ptr byte,
|
2020-01-08 01:03:33 +00:00
|
|
|
output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2019-11-27 20:43:02 +00:00
|
|
|
var data: BlockInput
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = SSZ.decode(input, BlockInput)
|
2019-12-17 03:01:30 +00:00
|
|
|
except MalformedSszError, SszSizeMismatchError:
|
|
|
|
let e = getCurrentException()
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"SSZ deserialisation failed, likely bug in preprocessing.",
|
|
|
|
e,
|
|
|
|
)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
try:
|
2019-12-30 02:50:59 +00:00
|
|
|
result = state_transition(data.state, data.beaconBlock, {})
|
2020-01-08 00:54:21 +00:00
|
|
|
except IOError, ValueError:
|
|
|
|
# TODO remove when status-im/nim-chronicles#60 is resolved
|
|
|
|
let e = getCurrentException()
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Unexpected (logging?) error in state transition",
|
|
|
|
e,
|
|
|
|
)
|
2019-12-17 03:01:30 +00:00
|
|
|
except Exception as e:
|
|
|
|
# TODO why an Exception?
|
2019-12-17 04:50:47 +00:00
|
|
|
# Lots of vendor code looks like it might raise a bare exception type
|
2020-01-08 00:54:21 +00:00
|
|
|
raise newException(FuzzCrashError, "Unexpected Exception in state transition", e)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
result = copyState(data.state, output, output_size)
|
|
|
|
|
2019-12-17 04:50:47 +00:00
|
|
|
|
|
|
|
proc nfuzz_block_header(input: openArray[byte], output: ptr byte,
|
2020-01-08 01:03:33 +00:00
|
|
|
output_size: ptr uint): bool {.exportc, raises: [FuzzCrashError, Defect].} =
|
2019-11-27 20:43:02 +00:00
|
|
|
var
|
2019-12-17 04:50:47 +00:00
|
|
|
data: BlockHeaderInput
|
2019-11-27 20:43:02 +00:00
|
|
|
cache = get_empty_per_epoch_cache()
|
|
|
|
|
|
|
|
try:
|
2019-12-17 04:50:47 +00:00
|
|
|
data = SSZ.decode(input, BlockHeaderInput)
|
2019-12-17 03:01:30 +00:00
|
|
|
except MalformedSszError, SszSizeMismatchError:
|
|
|
|
let e = getCurrentException()
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"SSZ deserialisation failed, likely bug in preprocessing.",
|
|
|
|
e,
|
|
|
|
)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
try:
|
2019-12-30 02:50:59 +00:00
|
|
|
# TODO disable bls
|
|
|
|
result = process_block_header(data.state, data.beaconBlock, {}, cache)
|
2020-01-08 00:54:21 +00:00
|
|
|
except IOError, ValueError:
|
|
|
|
let e = getCurrentException()
|
|
|
|
# TODO remove when status-im/nim-chronicles#60 is resolved
|
2020-01-08 01:03:33 +00:00
|
|
|
raise newException(
|
|
|
|
FuzzCrashError,
|
|
|
|
"Unexpected IOError in block header processing",
|
|
|
|
e,
|
|
|
|
)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
result = copyState(data.state, output, output_size)
|
|
|
|
|
2019-12-17 04:50:47 +00:00
|
|
|
|
2019-11-27 20:43:02 +00:00
|
|
|
# Note: Could also accept raw input pointer and access list_size + seed here.
|
|
|
|
# However, list_size needs to be known also outside this proc to allocate output.
|
|
|
|
# TODO: rework to copy immediatly in an uint8 openArray, considering we have to
|
|
|
|
# go over the list anyhow?
|
2019-11-28 22:01:12 +00:00
|
|
|
proc nfuzz_shuffle(input_seed: ptr byte, output: var openArray[uint64]): bool
|
2020-01-08 01:03:33 +00:00
|
|
|
{.exportc, raises: [Defect].} =
|
2019-11-27 20:43:02 +00:00
|
|
|
var seed: Eth2Digest
|
2019-11-28 22:01:12 +00:00
|
|
|
# Should be OK as max 2 bytes are passed by the framework.
|
|
|
|
let list_size = output.len.uint64
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
copyMem(addr(seed.data), input_seed, sizeof(seed.data))
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2019-11-28 22:01:12 +00:00
|
|
|
var shuffled_seq: seq[ValidatorIndex]
|
2019-12-17 03:01:30 +00:00
|
|
|
shuffled_seq = get_shuffled_seq(seed, list_size)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
2020-01-08 01:03:33 +00:00
|
|
|
doAssert(
|
|
|
|
list_size == shuffled_seq.len.uint64,
|
|
|
|
"Shuffled list should be of requested size."
|
|
|
|
)
|
2019-11-27 20:43:02 +00:00
|
|
|
|
|
|
|
for i in 0..<list_size:
|
|
|
|
# ValidatorIndex is currently wrongly uint32 so we copy this 1 by 1,
|
|
|
|
# assumes passed output is zeroed.
|
|
|
|
copyMem(offset(addr output, i.int), shuffled_seq[i.int].unsafeAddr,
|
|
|
|
sizeof(ValidatorIndex))
|
2019-11-28 22:01:12 +00:00
|
|
|
|
|
|
|
result = true
|