common/node_id: runtime assertion override, not separate compile time for fuzzing.
This makes it trivial to run the fuzz tests as unit tests in non-fuzzing mode. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -56,14 +56,16 @@ void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id)
|
||||
fromwire(cursor, max, &id->k, sizeof(id->k));
|
||||
}
|
||||
|
||||
bool dev_towire_allow_invalid_node_id = false;
|
||||
|
||||
void towire_node_id(u8 **pptr, const struct node_id *id)
|
||||
{
|
||||
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
/* Cheap sanity check. For wire fuzzing, we only care about correct
|
||||
* encoding of node IDs and not whether the IDs are valid, so we disable
|
||||
* this check while fuzzing. */
|
||||
if (!dev_towire_allow_invalid_node_id)
|
||||
assert(id->k[0] == 0x2 || id->k[0] == 0x3);
|
||||
#endif
|
||||
|
||||
towire(pptr, id->k, sizeof(id->k));
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,9 @@ static inline int node_id_idx(const struct node_id *id1,
|
||||
void towire_node_id(u8 **pptr, const struct node_id *id);
|
||||
void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id);
|
||||
|
||||
/* Fuzzer creates these, so sets this flag to allow it */
|
||||
extern bool dev_towire_allow_invalid_node_id;
|
||||
|
||||
/* Hash table functions for node ids */
|
||||
static inline const struct node_id *node_id_keyof(const struct node_id *id)
|
||||
{
|
||||
|
||||
@@ -21,7 +21,7 @@ static u8 *prefix_arr(const u8 *data, size_t size, u16 prefix)
|
||||
}
|
||||
|
||||
/* The init function used by all fuzz-wire-* targets. */
|
||||
void init(int *argc, char ***argv) { common_setup("fuzzer"); }
|
||||
void init(int *argc, char ***argv) { common_setup("fuzzer"); dev_towire_allow_invalid_node_id = true; }
|
||||
|
||||
/* Test that decoding arbitrary data does not crash. Then, if the data was
|
||||
* successfully decoded, test that encoding and decoding the message does not
|
||||
|
||||
Reference in New Issue
Block a user