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:
Rusty Russell
2025-10-22 19:44:28 +10:30
parent 6e5cb299dd
commit abe09ec03e
3 changed files with 9 additions and 4 deletions

View File

@@ -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)); 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) 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 /* 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 * encoding of node IDs and not whether the IDs are valid, so we disable
* this check while fuzzing. */ * this check while fuzzing. */
if (!dev_towire_allow_invalid_node_id)
assert(id->k[0] == 0x2 || id->k[0] == 0x3); assert(id->k[0] == 0x2 || id->k[0] == 0x3);
#endif
towire(pptr, id->k, sizeof(id->k)); towire(pptr, id->k, sizeof(id->k));
} }

View File

@@ -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 towire_node_id(u8 **pptr, const struct node_id *id);
void fromwire_node_id(const u8 **cursor, size_t *max, 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 */ /* Hash table functions for node ids */
static inline const struct node_id *node_id_keyof(const struct node_id *id) static inline const struct node_id *node_id_keyof(const struct node_id *id)
{ {

View File

@@ -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. */ /* 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 /* Test that decoding arbitrary data does not crash. Then, if the data was
* successfully decoded, test that encoding and decoding the message does not * successfully decoded, test that encoding and decoding the message does not