MCPcopy Create free account
hub / github.com/ElementsProject/lightning / LLVMFuzzerCustomMutator

Function LLVMFuzzerCustomMutator

tests/fuzz/fuzz-codex32.c:43–99  ·  view source on GitHub ↗

Custom mutator with structure-aware and byte-level mutations */

Source from the content-addressed store, hash-verified

41
42/* Custom mutator with structure-aware and byte-level mutations */
43size_t LLVMFuzzerCustomMutator(uint8_t *fuzz_data, size_t size,
44 size_t max_size, unsigned int seed)
45{
46 srand(seed);
47 char *str = to_string(tmpctx, fuzz_data, size);
48 char *fail;
49 struct codex32 *parts = codex32_decode(tmpctx, NULL, str, &fail);
50
51 /* If valid, try structure-aware mutation */
52 if (parts) {
53 /* Mutate a random component */
54 switch(rand() % 3) {
55 case 0: /* Mutate threshold to any value */
56 parts->threshold = rand();
57 break;
58
59 case 1: /* Mutate ID arbitrarily */
60 {
61 size_t id_len = sizeof(parts->id) - 1;
62 LLVMFuzzerMutate((u8 *)parts->id, id_len, id_len);
63 parts->id[id_len] = '\0';
64 }
65 break;
66
67 case 2: /* Mutate payload */
68 {
69 size_t old_size = tal_bytelen(parts->payload);
70 tal_resize(&parts->payload, max_size);
71 size_t new_size = LLVMFuzzerMutate((u8 *)parts->payload, old_size, max_size);
72 tal_resize(&parts->payload, new_size);
73 }
74 break;
75 }
76
77 /* Always mutate the HRP to ensure it's valid for encoding. */
78 char *new_hrp = tal_arr(parts, char, 3);
79 new_hrp[0] = valid_hrp_chars[rand() % strlen(valid_hrp_chars)];
80 new_hrp[1] = valid_hrp_chars[rand() % strlen(valid_hrp_chars)];
81 new_hrp[2] = '\0';
82 parts->hrp = new_hrp;
83
84 char *reencoded;
85 const char *err = codex32_secret_encode(tmpctx, parts->hrp, parts->id,
86 parts->threshold, parts->payload,
87 tal_bytelen(parts->payload), &reencoded);
88 if (!err) {
89 size_t len = tal_bytelen(reencoded) - 1;
90 if (len <= max_size) {
91 memcpy(fuzz_data, reencoded, len);
92 return len;
93 }
94 }
95 }
96
97 /* Fallback: byte-level mutation */
98 return LLVMFuzzerMutate(fuzz_data, size, max_size);
99}
100

Callers

nothing calls this directly

Calls 5

codex32_decodeFunction · 0.85
LLVMFuzzerMutateFunction · 0.85
tal_bytelenFunction · 0.85
codex32_secret_encodeFunction · 0.85
to_stringFunction · 0.70

Tested by

no test coverage detected