@@ -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