Skip to content

Commit abe09ec

Browse files
committed
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 <[email protected]>
1 parent 6e5cb29 commit abe09ec

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

common/node_id.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id)
5656
fromwire(cursor, max, &id->k, sizeof(id->k));
5757
}
5858

59+
bool dev_towire_allow_invalid_node_id = false;
60+
5961
void towire_node_id(u8 **pptr, const struct node_id *id)
6062
{
61-
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
6263
/* Cheap sanity check. For wire fuzzing, we only care about correct
6364
* encoding of node IDs and not whether the IDs are valid, so we disable
6465
* this check while fuzzing. */
65-
assert(id->k[0] == 0x2 || id->k[0] == 0x3);
66-
#endif
66+
if (!dev_towire_allow_invalid_node_id)
67+
assert(id->k[0] == 0x2 || id->k[0] == 0x3);
68+
6769
towire(pptr, id->k, sizeof(id->k));
6870
}
6971

common/node_id.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ static inline int node_id_idx(const struct node_id *id1,
4848
void towire_node_id(u8 **pptr, const struct node_id *id);
4949
void fromwire_node_id(const u8 **cursor, size_t *max, struct node_id *id);
5050

51+
/* Fuzzer creates these, so sets this flag to allow it */
52+
extern bool dev_towire_allow_invalid_node_id;
53+
5154
/* Hash table functions for node ids */
5255
static inline const struct node_id *node_id_keyof(const struct node_id *id)
5356
{

tests/fuzz/wire.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static u8 *prefix_arr(const u8 *data, size_t size, u16 prefix)
2121
}
2222

2323
/* The init function used by all fuzz-wire-* targets. */
24-
void init(int *argc, char ***argv) { common_setup("fuzzer"); }
24+
void init(int *argc, char ***argv) { common_setup("fuzzer"); dev_towire_allow_invalid_node_id = true; }
2525

2626
/* Test that decoding arbitrary data does not crash. Then, if the data was
2727
* successfully decoded, test that encoding and decoding the message does not

0 commit comments

Comments
 (0)