-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from emsec/feature/boolean-influence
Boolean Influence Plugin
- Loading branch information
Showing
29 changed files
with
1,328 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
option(PL_BOOLEAN_INFLUENCE "PL_BOOLEAN_INFLUENCE" OFF) | ||
if(PL_BOOLEAN_INFLUENCE OR BUILD_ALL_PLUGINS) | ||
file(GLOB_RECURSE BOOLEAN_INFLUENCE_INC ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h) | ||
file(GLOB_RECURSE BOOLEAN_INFLUENCE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) | ||
file(GLOB_RECURSE BOOLEAN_INFLUENCE_PYTHON_SRC ${CMAKE_CURRENT_SOURCE_DIR}/python/*.cpp) | ||
|
||
hal_add_plugin(boolean_influence | ||
SHARED | ||
HEADER ${BOOLEAN_INFLUENCE_INC} | ||
SOURCES ${BOOLEAN_INFLUENCE_SRC} ${BOOLEAN_INFLUENCE_PYTHON_SRC} | ||
LINK_LIBRARIES PUBLIC ${Z3_LIBRARIES} z3_utils OpenMP::OpenMP_CXX | ||
PYDOC SPHINX_DOC_INDEX_FILE ${CMAKE_CURRENT_SOURCE_DIR}/documentation/boolean_influence.rst | ||
) | ||
endif() |
5 changes: 5 additions & 0 deletions
5
plugins/boolean_influence/documentation/boolean_influence.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Boolean Influence | ||
========================== | ||
|
||
.. autoclass:: boolean_influence.BooleanInfluencePlugin | ||
:members: |
32 changes: 32 additions & 0 deletions
32
plugins/boolean_influence/include/boolean_influence/plugin_boolean_influence.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include "hal_core/plugin_system/plugin_interface_base.h" | ||
|
||
#include "hal_core/netlist/net.h" | ||
#include "hal_core/netlist/gate.h" | ||
|
||
namespace hal | ||
{ | ||
class PLUGIN_API BooleanInfluencePlugin : public BasePluginInterface | ||
{ | ||
public: | ||
|
||
std::string get_name() const override; | ||
std::string get_version() const override; | ||
|
||
void initialize() override; | ||
|
||
/** | ||
* Generates the function of the dataport net of the given flip-flop. | ||
* Afterwards the generated function gets translated from a z3::expr to efficent c code, compiled, executed and evalated.G | ||
* | ||
* @param[in] gate - Pointer to the flip-flop which data input net is used to build the boolean function. | ||
* @returns A mapping of the gates that appear in the function of the data net to their boolean influence in said function. | ||
*/ | ||
std::map<Net*, double> get_boolean_influences_of_gate(const Gate* gate); | ||
|
||
private: | ||
std::vector<Gate*> extract_function_gates(const Gate* start, const std::string& pin); | ||
void add_inputs(Gate* gate, std::unordered_set<Gate*>& gates); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "pybind11/operators.h" | ||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" | ||
#include "pybind11/stl_bind.h" | ||
#include "hal_core/python_bindings/python_bindings.h" | ||
|
||
#include "boolean_influence/plugin_boolean_influence.h" | ||
|
||
namespace py = pybind11; | ||
|
||
namespace hal | ||
{ | ||
|
||
// the name in PYBIND11_MODULE/PYBIND11_PLUGIN *MUST* match the filename of the output library (without extension), | ||
// otherwise you will get "ImportError: dynamic module does not define module export function" when importing the module | ||
|
||
#ifdef PYBIND11_MODULE | ||
PYBIND11_MODULE(boolean_influence, m) | ||
{ | ||
m.doc() = "hal BooleanInfluencePlugin python bindings"; | ||
#else | ||
PYBIND11_PLUGIN(boolean_influence) | ||
{ | ||
py::module m("boolean_influence", "hal BooleanInfluencePlugin python bindings"); | ||
#endif // ifdef PYBIND11_MODULE | ||
|
||
py::class_<BooleanInfluencePlugin, RawPtrWrapper<BooleanInfluencePlugin>, BasePluginInterface>(m, "BooleanInfluencePlugin") | ||
.def_property_readonly("name", &BooleanInfluencePlugin::get_name) | ||
.def("get_name", &BooleanInfluencePlugin::get_name) | ||
.def_property_readonly("version", &BooleanInfluencePlugin::get_version) | ||
.def("get_version", &BooleanInfluencePlugin::get_version) | ||
.def("get_boolean_influences_of_gate", &BooleanInfluencePlugin::get_boolean_influences_of_gate, py::arg("gate"), R"( | ||
Generates the function of the dataport net of the given flip-flop. | ||
Afterwards the generated function gets translated from a z3::expr to efficent c code, compiled, executed and evalated. | ||
:param halPy.Gate gate: The flip-flop which data input net is used to build the boolean function.. | ||
:returns: A mapping of the gates that appear in the function of the data net to their boolean influence in said function. | ||
:rtype: dict | ||
)"); | ||
; | ||
|
||
#ifndef PYBIND11_MODULE | ||
return m.ptr(); | ||
#endif // PYBIND11_MODULE | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
plugins/boolean_influence/src/plugin_boolean_influence.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "boolean_influence/plugin_boolean_influence.h" | ||
|
||
#include "z3_utils/include/plugin_z3_utils.h" | ||
|
||
namespace hal | ||
{ | ||
extern std::unique_ptr<BasePluginInterface> create_plugin_instance() | ||
{ | ||
return std::make_unique<BooleanInfluencePlugin>(); | ||
} | ||
|
||
std::string BooleanInfluencePlugin::get_name() const | ||
{ | ||
return std::string("boolean_influence"); | ||
} | ||
|
||
std::string BooleanInfluencePlugin::get_version() const | ||
{ | ||
return std::string("0.1"); | ||
} | ||
|
||
void BooleanInfluencePlugin::initialize() | ||
{ | ||
} | ||
|
||
std::map<Net*, double> BooleanInfluencePlugin::get_boolean_influences_of_gate(const Gate* gate) | ||
{ | ||
if (gate->get_type()->get_base_type() != hal::GateType::BaseType::ff) | ||
{ | ||
log_error("boolean_influence", "Can only handle flip flops but found gate type {}.", gate->get_type()->get_name()); | ||
return {}; | ||
} | ||
|
||
// Check for the data port pin | ||
std::unordered_set<std::string> d_ports = gate->get_type()->get_pins_of_type(GateType::PinType::data); | ||
if (d_ports.size() != 1) | ||
{ | ||
log_error("boolean_influence", "Can only handle flip flops with exactly 1 data port, but found {}.", d_ports.size()); | ||
return {}; | ||
} | ||
std::string data_pin = *d_ports.begin(); | ||
|
||
// Extract all gates in front of the data port and iterate backwards until another flip flop is found. | ||
std::vector<Gate*> function_gates = extract_function_gates(gate, data_pin); | ||
|
||
// Generate function for the data port | ||
z3_utils::SubgraphFunctionGenerator g; | ||
|
||
std::unique_ptr ctx = std::make_unique<z3::context>(); | ||
std::unique_ptr func = std::make_unique<z3::expr>(*ctx); | ||
|
||
std::unordered_set<u32> net_ids; | ||
g.get_subgraph_z3_function(gate->get_fan_in_net(data_pin), function_gates, *ctx, *func, net_ids); | ||
|
||
z3_utils::z3Wrapper func_wrapper = z3_utils::z3Wrapper(std::move(ctx), std::move(func)); | ||
|
||
// Generate boolean influence | ||
std::unordered_map<u32, double> net_ids_to_inf = func_wrapper.get_boolean_influence(); | ||
|
||
// translate net_ids back to nets | ||
std::map<Net*, double> nets_to_inf; | ||
|
||
Netlist* nl = gate->get_netlist(); | ||
for (const auto& [net_id, inf] : net_ids_to_inf) | ||
{ | ||
Net* net = nl->get_net_by_id(net_id); | ||
|
||
if (net->get_sources().size() != 1) | ||
{ | ||
log_error("boolean_influence", "Net ({}) has either multiple or 0 sources ({})", net->get_id(), net->get_sources().size()); | ||
return {}; | ||
} | ||
|
||
nets_to_inf.insert({net, inf}); | ||
} | ||
|
||
return nets_to_inf; | ||
} | ||
|
||
std::vector<Gate*> BooleanInfluencePlugin::extract_function_gates(const Gate* start, const std::string& pin) | ||
{ | ||
std::unordered_set<Gate*> function_gates; | ||
|
||
add_inputs(start->get_predecessor(pin)->get_gate(), function_gates); | ||
|
||
return {function_gates.begin(), function_gates.end()}; | ||
} | ||
|
||
void BooleanInfluencePlugin::add_inputs(Gate* gate, std::unordered_set<Gate*>& gates) | ||
{ | ||
if (gate->is_vcc_gate() || gate->is_gnd_gate() || gate->get_type()->get_base_type() == hal::GateType::BaseType::ff || gate->get_type()->get_name().find("RAM") != std::string::npos) | ||
{ | ||
return; | ||
} | ||
|
||
gates.insert(gate); | ||
for (const auto& pre : gate->get_predecessors()) | ||
{ | ||
if (pre->get_gate() && gates.find(pre->get_gate()) == gates.end()) | ||
{ | ||
add_inputs(pre->get_gate(), gates); | ||
} | ||
} | ||
return; | ||
} | ||
} // namespace hal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from hal_plugins import boolean_influence | ||
|
||
pl = hal_py.plugin_manager.get_plugin_instance("boolean_influence") | ||
|
||
g = netlist.get_gate_by_id(1547) | ||
inf = pl.get_boolean_influences_of_gate(g) | ||
|
||
print(inf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.