From 475533d3b92495973b048ff22251d85c330e77aa Mon Sep 17 00:00:00 2001 From: Heinz Riener Date: Thu, 10 Dec 2020 16:24:59 +0100 Subject: [PATCH 1/2] Network fuzz tester. --- .../algorithms/network_fuzz_tester.hpp | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 include/mockturtle/algorithms/network_fuzz_tester.hpp diff --git a/include/mockturtle/algorithms/network_fuzz_tester.hpp b/include/mockturtle/algorithms/network_fuzz_tester.hpp new file mode 100644 index 000000000..3a0635d7a --- /dev/null +++ b/include/mockturtle/algorithms/network_fuzz_tester.hpp @@ -0,0 +1,135 @@ +/* mockturtle: C++ logic network library + * Copyright (C) 2018-2020 EPFL + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +/*! + \file network_fuzz_tester.hpp + \brief Network fuzz tester + + \author Heinz Riener +*/ + +#include +#include +#include +#include +#include + +namespace mockturtle +{ + +struct fuzz_tester_params +{ + uint64_t num_pis{4u}; + uint64_t num_gates{10u}; + std::string filename{"fuzz_test.v"}; + + /* number of networks to test: nullopt means infinity */ + std::optional num_iterations{std::nullopt}; +}; /* fuzz_tester_params */ + +/*! \brief Network fuzz tester + * + * Runs an algorithm on many small ramdon logic networks. Fuzz + * testing is often useful to detect potential segmentation faults in + * new implementations. The generated benchmarks are saved first in a + * file. If a segmentation fault occurs, the file can be used to + * reproduce and debug the problem. + * + * Usage: + * #include + * #include + * #include + * #include + * #include + * + * auto opt = [&]( aig_network aig ) -> bool { + * resubstitution_params ps; + * resubstitution_stats st; + * aig_resubstitution( aig, ps, &st ); + * aig = cleanup_dangling( aig ); + * return true; + * }; + * + * fuzz_tester_params ps; + * ps.num_iterations = 100; + * auto gen = default_random_aig_generator(); + * network_fuzz_tester fuzzer( gen, ps ); + * fuzzer.run( opt ); +*/ +template +class network_fuzz_tester +{ +public: + explicit network_fuzz_tester( NetworkGenerator& gen, fuzz_tester_params const ps = {} ) + : gen( gen ) + , ps( ps ) + {} + + template + void run( Fn&& fn ) + { + uint64_t counter{0}; + while ( !ps.num_iterations || counter < ps.num_iterations ) + { + auto ntk = gen.generate( ps.num_pis, ps.num_gates, std::random_device{}() ); + fmt::print( "[i] create network #{}: I/O = {}/{} gates = {} nodes = {}\n", + counter++, ntk.num_pis(), ntk.num_pos(), ntk.num_gates(), ntk.size() ); + + fmt::print( "[i] write network `{}`\n", ps.filename ); + write_verilog( ntk, ps.filename ); + + /* run optimization algorithm */ + if ( !fn( ntk ) ) + { + return; + } + } + } + + template + void rerun_on_benchmark( Fn&& fn ) + { + /* read benchmark from a file */ + Ntk ntk; + fmt::print( "[i] read network `{}`\n", ps.filename ); + if ( lorina::read_verilog( ps.filename, verilog_reader( ntk ) ) != lorina::return_code::success ) + { + fmt::print( "[e] could not read benchmark `{}`\n", ps.filename ); + return; + } + + fmt::print( "[i] network: I/O = {}/{} gates = {} nodes = {}\n", + ntk.num_pis(), ntk.num_pos(), ntk.num_gates(), ntk.size() ); + + /* run optimization algorithm */ + fn( ntk ); + } + +private: + NetworkGenerator& gen; + fuzz_tester_params const ps; +}; /* network_fuzz_tester */ + +} /* namespace mockturtle */ From c273d9cf5c3833d2bbbd3ac612f77f73448efddb Mon Sep 17 00:00:00 2001 From: Heinz Riener Date: Thu, 10 Dec 2020 16:28:28 +0100 Subject: [PATCH 2/2] changelog. --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 872c62eb9..37a75cf4e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -32,6 +32,7 @@ v0.2 (not yet released) - Check functional equivalence (`circuit_validator`) `#346 `_ - Restructured resubstitution framework (`resubstitution`), simulation-guided resubstitution (`sim_resub`) `#373 `_ - Functional reduction (`functional_reduction`) `#380 `_ + - Network fuzz testing (`network_fuzz_tester`) `#408 `_ * Views: - Assign names to signals and outputs (`names_view`) `#181 `_ `#184 `_ - Creates a CNF while creating a network (`cnf_view`) `#274 `_