Skip to content

Commit 355bbcb

Browse files
committed
Merge bitcoin/bitcoin#28066: fuzz: Generate process_message targets individually
fa6245d fuzz: Generate process_message targets individually (MarcoFalke) fa1471e refactor: Remove duplicate allNetMessageTypesVec (MarcoFalke) Pull request description: Now that `LIMIT_TO_MESSAGE_TYPE` is a runtime setting after commit 927b001, it shouldn't hurt to also generate each message type individually. Something similar was done for the `rpc` target in commit cf4da5e. ACKs for top commit: stickies-v: re-crACK fa6245d brunoerg: reACK fa6245d Tree-SHA512: 8f3ec71bab89781f10820a0e027fcde8949f3333eb19a30315aaad6f90f5167028113cea255b2d60b700da817c7eaac20b7b4c92f931052d7f5c2f148d33aa5a
2 parents 04afe55 + fa6245d commit 355bbcb

File tree

2 files changed

+74
-50
lines changed

2 files changed

+74
-50
lines changed

src/protocol.cpp

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,47 @@
1212
static std::atomic<bool> g_initial_block_download_completed(false);
1313

1414
namespace NetMsgType {
15-
const char *VERSION="version";
16-
const char *VERACK="verack";
17-
const char *ADDR="addr";
18-
const char *ADDRV2="addrv2";
19-
const char *SENDADDRV2="sendaddrv2";
20-
const char *INV="inv";
21-
const char *GETDATA="getdata";
22-
const char *MERKLEBLOCK="merkleblock";
23-
const char *GETBLOCKS="getblocks";
24-
const char *GETHEADERS="getheaders";
25-
const char *TX="tx";
26-
const char *HEADERS="headers";
27-
const char *BLOCK="block";
28-
const char *GETADDR="getaddr";
29-
const char *MEMPOOL="mempool";
30-
const char *PING="ping";
31-
const char *PONG="pong";
32-
const char *NOTFOUND="notfound";
33-
const char *FILTERLOAD="filterload";
34-
const char *FILTERADD="filteradd";
35-
const char *FILTERCLEAR="filterclear";
36-
const char *SENDHEADERS="sendheaders";
37-
const char *FEEFILTER="feefilter";
38-
const char *SENDCMPCT="sendcmpct";
39-
const char *CMPCTBLOCK="cmpctblock";
40-
const char *GETBLOCKTXN="getblocktxn";
41-
const char *BLOCKTXN="blocktxn";
42-
const char *GETCFILTERS="getcfilters";
43-
const char *CFILTER="cfilter";
44-
const char *GETCFHEADERS="getcfheaders";
45-
const char *CFHEADERS="cfheaders";
46-
const char *GETCFCHECKPT="getcfcheckpt";
47-
const char *CFCHECKPT="cfcheckpt";
48-
const char *WTXIDRELAY="wtxidrelay";
49-
const char *SENDTXRCNCL="sendtxrcncl";
15+
const char* VERSION = "version";
16+
const char* VERACK = "verack";
17+
const char* ADDR = "addr";
18+
const char* ADDRV2 = "addrv2";
19+
const char* SENDADDRV2 = "sendaddrv2";
20+
const char* INV = "inv";
21+
const char* GETDATA = "getdata";
22+
const char* MERKLEBLOCK = "merkleblock";
23+
const char* GETBLOCKS = "getblocks";
24+
const char* GETHEADERS = "getheaders";
25+
const char* TX = "tx";
26+
const char* HEADERS = "headers";
27+
const char* BLOCK = "block";
28+
const char* GETADDR = "getaddr";
29+
const char* MEMPOOL = "mempool";
30+
const char* PING = "ping";
31+
const char* PONG = "pong";
32+
const char* NOTFOUND = "notfound";
33+
const char* FILTERLOAD = "filterload";
34+
const char* FILTERADD = "filteradd";
35+
const char* FILTERCLEAR = "filterclear";
36+
const char* SENDHEADERS = "sendheaders";
37+
const char* FEEFILTER = "feefilter";
38+
const char* SENDCMPCT = "sendcmpct";
39+
const char* CMPCTBLOCK = "cmpctblock";
40+
const char* GETBLOCKTXN = "getblocktxn";
41+
const char* BLOCKTXN = "blocktxn";
42+
const char* GETCFILTERS = "getcfilters";
43+
const char* CFILTER = "cfilter";
44+
const char* GETCFHEADERS = "getcfheaders";
45+
const char* CFHEADERS = "cfheaders";
46+
const char* GETCFCHECKPT = "getcfcheckpt";
47+
const char* CFCHECKPT = "cfcheckpt";
48+
const char* WTXIDRELAY = "wtxidrelay";
49+
const char* SENDTXRCNCL = "sendtxrcncl";
5050
} // namespace NetMsgType
5151

5252
/** All known message types. Keep this in the same order as the list of
5353
* messages above and in protocol.h.
5454
*/
55-
const static std::string allNetMessageTypes[] = {
55+
const static std::vector<std::string> g_all_net_message_types{
5656
NetMsgType::VERSION,
5757
NetMsgType::VERACK,
5858
NetMsgType::ADDR,
@@ -89,7 +89,6 @@ const static std::string allNetMessageTypes[] = {
8989
NetMsgType::WTXIDRELAY,
9090
NetMsgType::SENDTXRCNCL,
9191
};
92-
const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
9392

9493
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
9594
{
@@ -182,7 +181,7 @@ std::string CInv::ToString() const
182181

183182
const std::vector<std::string> &getAllNetMessageTypes()
184183
{
185-
return allNetMessageTypesVec;
184+
return g_all_net_message_types;
186185
}
187186

188187
/**

test/fuzz/test_runner.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,52 @@ def main():
193193
)
194194

195195

196-
def generate_corpus(*, fuzz_pool, src_dir, build_dir, corpus_dir, targets):
197-
"""Generates new corpus.
196+
def transform_process_message_target(targets, src_dir):
197+
"""Add a target per process message, and also keep ("process_message", {}) to allow for
198+
cross-pollination, or unlimited search"""
199+
200+
p2p_msg_target = "process_message"
201+
if (p2p_msg_target, {}) in targets:
202+
lines = subprocess.run(
203+
["git", "grep", "--function-context", "g_all_net_message_types{", src_dir / "src" / "protocol.cpp"],
204+
check=True,
205+
stdout=subprocess.PIPE,
206+
text=True,
207+
).stdout.splitlines()
208+
lines = [l.split("::", 1)[1].split(",")[0].lower() for l in lines if l.startswith("src/protocol.cpp- NetMsgType::")]
209+
assert len(lines)
210+
targets += [(p2p_msg_target, {"LIMIT_TO_MESSAGE_TYPE": m}) for m in lines]
211+
return targets
212+
213+
214+
def transform_rpc_target(targets, src_dir):
215+
"""Add a target per RPC command, and also keep ("rpc", {}) to allow for cross-pollination,
216+
or unlimited search"""
198217

199-
Run {targets} without input, and outputs the generated corpus to
200-
{corpus_dir}.
201-
"""
202-
logging.info("Generating corpus to {}".format(corpus_dir))
203218
rpc_target = "rpc"
204-
has_rpc = rpc_target in targets
205-
if has_rpc:
206-
targets.remove(rpc_target)
207-
targets = [(t, {}) for t in targets]
208-
if has_rpc:
219+
if (rpc_target, {}) in targets:
209220
lines = subprocess.run(
210-
["git", "grep", "--function-context", "RPC_COMMANDS_SAFE_FOR_FUZZING{", os.path.join(src_dir, "src", "test", "fuzz", "rpc.cpp")],
221+
["git", "grep", "--function-context", "RPC_COMMANDS_SAFE_FOR_FUZZING{", src_dir / "src" / "test" / "fuzz" / "rpc.cpp"],
211222
check=True,
212223
stdout=subprocess.PIPE,
213224
text=True,
214225
).stdout.splitlines()
215226
lines = [l.split("\"", 1)[1].split("\"")[0] for l in lines if l.startswith("src/test/fuzz/rpc.cpp- \"")]
227+
assert len(lines)
216228
targets += [(rpc_target, {"LIMIT_TO_RPC_COMMAND": r}) for r in lines]
229+
return targets
230+
231+
232+
def generate_corpus(*, fuzz_pool, src_dir, build_dir, corpus_dir, targets):
233+
"""Generates new corpus.
234+
235+
Run {targets} without input, and outputs the generated corpus to
236+
{corpus_dir}.
237+
"""
238+
logging.info("Generating corpus to {}".format(corpus_dir))
239+
targets = [(t, {}) for t in targets] # expand to add dictionary for target-specific env variables
240+
targets = transform_process_message_target(targets, Path(src_dir))
241+
targets = transform_rpc_target(targets, Path(src_dir))
217242

218243
def job(command, t, t_env):
219244
logging.debug(f"Running '{command}'")

0 commit comments

Comments
 (0)