use of verdict.vdm.vdm_model.ComponentImpl in project VERDICT by ge-high-assurance.
the class VDMInstrumentor method retrieve_component_and_channels.
protected void retrieve_component_and_channels(Model vdm_model, List<String> threats, boolean blame_assignment, boolean component_level) {
HashSet<ComponentType> vdm_components = new HashSet<ComponentType>();
HashSet<Connection> vdm_links = new HashSet<Connection>();
// Initialize Components with Empty Ports and Ignore
identifyEmptyOutputComponents();
// Initialize DataFlow for empty Implementations.
LustreProgram lt = vdm_model.getDataflowCode();
if (lt == null) {
lt = new LustreProgram();
vdm_model.setDataflowCode(lt);
}
if (threats.contains("LS")) {
System.out.println("Location Spoofing Instrumentation");
locationSpoofing(vdm_components);
}
if (threats.contains("LB")) {
System.out.println("Logic Bomb Instrumentation");
logicBomb(vdm_components);
}
if (threats.contains("SV")) {
System.out.println("Software Virus/malware/worm/trojan");
softwareVirus(vdm_components);
}
if (threats.contains("RI")) {
System.out.println("Remote Code Injection");
remoteCodeInjection(vdm_components);
}
if (threats.contains("OT")) {
System.out.println("Outsider Threat");
outsiderThreat(vdm_components);
}
if (threats.contains("IT")) {
System.out.println("Insider Threat");
insiderThreat(vdm_components);
}
if (threats.contains("HT")) {
System.out.println("Hardware Trojans");
hardwareTrojan(vdm_components);
}
if (threats.contains("NI")) {
System.out.println("Network Injection Instrumentation");
// Snooze links for component level blame assignment.
if (!component_level) {
networkInjection(vdm_links);
}
}
if (threats.contains("BN")) {
System.out.println("Benign");
vdm_components.clear();
vdm_links.clear();
}
// int component_index = 1;
// Removed Once component Implemtation assumption.
ComponentImpl componentImpl = retrieve_main_cmp_impl();
BlockImpl blockImpl = null;
if (componentImpl != null) {
blockImpl = componentImpl.getBlockImpl();
}
Map<String, HashSet<Connection>> components_map = new HashMap<String, HashSet<Connection>>();
if (vdm_components.size() > 0) {
for (ComponentType component : vdm_components) {
blockImpl = retrieve_block(component);
HashSet<Connection> vdm_cmp_links = instrument_component(component, blockImpl);
for (Connection link_con : vdm_cmp_links) {
// Check if connection contains Empty Component on Port Ends.
if (!ignoreMarkedLink(link_con)) {
// Check if Port is Probe Port
if (!isProbePort(link_con)) {
vdm_links.add(link_con);
}
}
}
components_map.put(component.getId(), vdm_cmp_links);
}
}
// Snoorzing probe ports and Empty output components
if (vdm_links.size() > 0) {
Iterator<Connection> it = vdm_links.iterator();
while (it.hasNext()) {
Connection con = it.next();
if (isProbePort(con)) {
it.remove();
} else if (ignoreMarkedLink(con)) {
it.remove();
}
}
}
HashSet<String> global_constants = new HashSet<String>();
Map<Connection, String> connections_map = new HashMap<Connection, String>();
if (vdm_links.size() > 0) {
for (Connection connection : vdm_links) {
// System.out.println("(" + connection_index++ + ") " +
// connection.getName());
// instrument_link(connection, blockImpl);
String cmpID = getComponentID(components_map, connection);
if (cmpID != null) {
// Find Block based on Connection
blockImpl = getBlockID(cmpID);
String constant = instrument_link(cmpID, connection, blockImpl);
global_constants.add(constant);
connections_map.put(connection, constant);
} else {
// Handle 'NI' as Special Case.
ConnectionEnd conDest = connection.getSource();
Port dest_port = conDest.getComponentPort();
if (dest_port != null) {
cmpID = dest_port.getId();
} else {
CompInstancePort compInstance = conDest.getSubcomponentPort();
ComponentInstance compInst = compInstance.getSubcomponent();
cmpID = compInst.getId();
}
blockImpl = retrieve_block(connection);
String constant = instrument_link(cmpID, connection, blockImpl);
global_constants.add(constant);
connections_map.put(connection, constant);
}
}
} else {
emptySelection = true;
}
// Declare Global Constants
for (String comp_id : global_constants) {
ConstantDeclaration global_comp_const = new ConstantDeclaration();
DataType global_comp_dataType = new DataType();
global_comp_dataType.setPlainType(PlainType.BOOL);
global_comp_const.setName(comp_id);
global_comp_const.setDataType(global_comp_dataType);
// Expression global_expr = new Expression();
// global_expr.setBoolLiteral(true);
// global_comp_const.setDefinition(global_expr);
vdm_model.getDataflowCode().getConstantDeclaration().add(global_comp_const);
// g_constants.add(global_comp_const);
}
Map<String, List<String>> connection_gps_comp_map = connection_gps_mapper(connections_map, components_map);
// Choosing Blame options
if (threats.contains("LS") && component_level) {
// Link Level Instrumentation varibales
dec_var_asmp_const(connection_gps_comp_map, blame_assignment, false);
} else if (threats.contains("LS") && !component_level) {
dec_var_asmp_const(connection_gps_comp_map, blame_assignment, true);
}
if (blame_assignment && component_level) {
Map<String, List<String>> connection_comp_map = connection_mapper(connections_map, components_map);
ComponentImpl compImpl = retrieve_main_cmp_impl();
// if (compImpl.getBlockImpl() == null) {
// compImpl = retrieve_block(compImpl);
// }
ContractSpec contractSpec = compImpl.getType().getContract();
for (String key : components_map.keySet()) {
String id = key.replace("::", "_double_colon_");
Expression wk_expr = new Expression();
wk_expr.setIdentifier(id);
Expression not_wkexpr = new Expression();
not_wkexpr.setNot(wk_expr);
// Adding weakly assume variables
ContractItem weakly_assume_item = new ContractItem();
weakly_assume_item.setName(id + " is not instrumented");
weakly_assume_item.setExpression(not_wkexpr);
// Checking connection before adding assumption
HashSet<Connection> empty_connection_check = components_map.get(key);
if (empty_connection_check.size() > 0) {
contractSpec.getWeaklyassume().add(weakly_assume_item);
}
}
dec_var_const(connection_comp_map);
} else if (blame_assignment && !component_level) {
ComponentImpl compImpl = retrieve_main_cmp_impl();
if (compImpl != null) {
// if (compImpl.getBlockImpl() == null) {
// compImpl = retrieve_block_impl(compImpl);
// }
ContractSpec contractSpec = compImpl.getType().getContract();
for (String key : global_constants) {
Expression wk_expr = new Expression();
wk_expr.setIdentifier(key);
Expression not_wkexpr = new Expression();
not_wkexpr.setNot(wk_expr);
// Adding weakly assume variables
ContractItem weakly_assume_item = new ContractItem();
weakly_assume_item.setName(link_name(key) + " is not instrumented");
weakly_assume_item.setExpression(not_wkexpr);
contractSpec.getWeaklyassume().add(weakly_assume_item);
}
}
}
}
use of verdict.vdm.vdm_model.ComponentImpl in project VERDICT by ge-high-assurance.
the class VDMInstrumentor method retrieve_block_impl.
protected ComponentImpl retrieve_block_impl(ComponentImpl compImpl) {
BlockImpl blockImpl = null;
String cmpID = compImpl.getType().getId();
for (ComponentImpl cImpl : vdm_model.getComponentImpl()) {
if (cImpl.getBlockImpl() != null) {
blockImpl = cImpl.getBlockImpl();
for (ComponentInstance cmpInstance : blockImpl.getSubcomponent()) {
ComponentImpl impl = cmpInstance.getImplementation();
ComponentType enumType = null;
if (impl != null) {
enumType = impl.getType();
} else {
enumType = cmpInstance.getSpecification();
}
if (cmpID.equals(enumType.getId())) {
// compImpl.getId());
return cImpl;
}
}
}
}
return compImpl;
}
use of verdict.vdm.vdm_model.ComponentImpl in project VERDICT by ge-high-assurance.
the class VDM2Lustre method visit.
// B) Component Implementation Translated into Lustre Node
public void visit(ComponentImpl componentImpl, Node node) {
NodeBody nodeBody = new NodeBody();
// Option 1) Block Implementation
// retrieve_block(componentImpl);
BlockImpl blockImpl = componentImpl.getBlockImpl();
// BlockImpl
if (blockImpl != null) {
ComponentType componentType = componentImpl.getType();
for (ComponentInstance componentInstance : blockImpl.getSubcomponent()) {
// replace to make naming compliant with lustre code
String id = componentInstance.getId();
id = id.replace(".", "_dot_");
id = id.replace("::", "_double_colon_");
componentInstance.setId(id);
String name = componentInstance.getName();
name = name.replace(".", "_dot_");
name = name.replace("::", "_double_colon_");
componentInstance.setName(name);
componentType = componentInstance.getSpecification();
ComponentImpl subcomponentImpl = componentInstance.getImplementation();
if (componentType == null && subcomponentImpl == null) {
System.out.println(componentInstance.getName() + " subcomponent is missing both a specification and an implemention.");
System.out.println("Please provide some specification or an implementation to continue.");
System.exit(-1);
}
// Option 1) Implementation
if (subcomponentImpl != null) {
componentType = subcomponentImpl.getType();
if (!this.marked_types.contains(componentType)) {
visit(componentType, nodeBody, componentInstance.getId(), true);
}
} else // Option 2) Specification
if (componentType != null) {
if (!this.marked_types.contains(componentType)) {
visit(componentType, nodeBody, componentInstance.getId(), false);
}
}
}
for (Connection connection : blockImpl.getConnection()) {
if (!ignoreConnection(connection)) {
visit(connection, nodeBody);
}
}
} else {
// Option 2) DataFlow Implementation / NodeBody
nodeBody = componentImpl.getDataflowImpl();
// node.setBody(nodeBody);
}
node.setBody(nodeBody);
}
use of verdict.vdm.vdm_model.ComponentImpl in project VERDICT by ge-high-assurance.
the class VDM2Lustre method visit.
// Connect -> NodeCall
public void visit(Connection connection, NodeBody nodeBody) {
// R.H.S
ConnectionEnd src = connection.getSource();
// Source Connection
Port src_component_port = src.getComponentPort();
// L.H.S.
ConnectionEnd dest = connection.getDestination();
// Destination Connection
Port dest_component_port = dest.getComponentPort();
if (dest_component_port != null) {
// Destination = Component (z3)
// Source = SubComponent (my_b:B _z2)
NodeEquation neq = new NodeEquation();
NodeEquationLHS eq_lhs = new NodeEquationLHS();
eq_lhs.getIdentifier().add(dest_component_port.getName());
neq.setLhs(eq_lhs);
CompInstancePort compInstancePort = src.getSubcomponentPort();
ComponentInstance componentInstance = compInstancePort.getSubcomponent();
src_component_port = compInstancePort.getPort();
String src_portID = src_component_port.getName();
Expression expr = new Expression();
List<VariableDeclaration> vars = nodeBody.getVariableDeclaration();
boolean match = false;
String id_expr = componentInstance.getId() + "_port_" + src_portID;
for (VariableDeclaration var : vars) {
if (var.getName().equals(id_expr)) {
match = true;
break;
}
}
if (match) {
expr.setIdentifier(id_expr);
} else {
String inst_cmp = "(.+)_instrumented";
Pattern inst_pattern = Pattern.compile(inst_cmp);
// System.out.println(src_portID);
Matcher m = inst_pattern.matcher(src_portID);
if (m.matches()) {
src_portID = m.group(1);
}
id_expr = componentInstance.getId() + "_port_" + src_portID;
expr.setIdentifier(id_expr);
// System.out.println(id_expr);
// System.out.println(">>>>>>>>>>>>>Identifiers: " +
// expr.getIdentifier());
}
neq.setRhs(expr);
nodeBody.getEquation().add(neq);
} else if (src_component_port != null) {
CompInstancePort compInstancePort = dest.getSubcomponentPort();
// X1
dest_component_port = compInstancePort.getPort();
// my_a1 : A
ComponentInstance componentInstance = compInstancePort.getSubcomponent();
String arg_value = src_component_port.getName();
// called node Identifier.
String called_node_ID = null;
ComponentType componentType = componentInstance.getSpecification();
ComponentImpl componentImpl = componentInstance.getImplementation();
if (componentType == null) {
componentType = componentImpl.getType();
called_node_ID = componentType.getName() + "_dot_Impl";
}
if (componentType != null && componentImpl != null) {
componentType = componentImpl.getType();
called_node_ID = componentType.getName() + "_dot_Impl";
String inst_cmp = "(.+)_instrumented";
Pattern inst_pattern = Pattern.compile(inst_cmp);
// System.out.println(arg_value);
Matcher m = inst_pattern.matcher(arg_value);
if (m.matches()) {
arg_value = m.group(1);
}
} else {
called_node_ID = componentType.getName();
arg_value = src_component_port.getName();
// System.out.println(arg_value);
}
for (Port port : componentType.getPort()) {
// MODE
PortMode port_mode = port.getMode();
if (port_mode == PortMode.OUT) {
// EQ L.H.S Variables *called Node return values
String expr_id = componentInstance.getId() + "_port_" + port.getName();
// System.out.print(">>>" + expr_id);
NodeEquation n_eq = getNodeEq(expr_id, nodeBody);
if (n_eq != null) {
Expression eq_rhs = n_eq.getRhs();
NodeCall node_called = eq_rhs.getCall();
String inst_cmp = "(.+)_Inst_.*";
Pattern inst_pattern = Pattern.compile(inst_cmp);
// String node_id = "";
// if (node_called != null) {
// node_id = node_called.getNodeId();
// }
// System.out.println(" = " + node_id + " (" + arg_value
// +
// ")");
Matcher m = inst_pattern.matcher(node_called.getNodeId());
IfThenElse ifelse = new IfThenElse();
Expression called_expr = new Expression();
// Condition
Expression gps_expr = new Expression();
if (m.matches()) {
// Instrumented component Instance ID
String component_id = m.group(1);
gps_expr.setIdentifier(component_id);
}
ifelse.setCondition(gps_expr);
// Then
ifelse.setThenBranch(called_expr);
// Else
Expression arg = new Expression();
arg.setIdentifier(src_component_port.getName());
ifelse.setElseBranch(arg);
// NodeCalled Expr
called_expr.setCall(node_called);
Expression instrumented_expr = new Expression();
instrumented_expr.setConditionalExpression(ifelse);
if (node_called != null) {
if (node_called.getNodeId().equals(called_node_ID)) {
for (Expression arg_expr : node_called.getArgument()) {
if (arg_expr.getIdentifier().equals(dest_component_port.getName())) {
arg_expr.setIdentifier(arg_value);
} else if (node_called.getArgument().size() == 1) {
arg_expr.setIdentifier(arg_value);
}
}
}
}
}
}
}
} else {
CompInstancePort compInstancePort = src.getSubcomponentPort();
ComponentInstance componentInstance = compInstancePort.getSubcomponent();
src_component_port = compInstancePort.getPort();
Expression arg_expr = new Expression();
// my_a1_y1
String src_portID = src_component_port.getName();
String componentInstanceID = componentInstance.getId();
arg_expr.setIdentifier(componentInstanceID + "_port_" + src_portID);
// called node Identifier.
String called_node_ID = null;
compInstancePort = dest.getSubcomponentPort();
componentInstance = compInstancePort.getSubcomponent();
dest_component_port = compInstancePort.getPort();
ComponentType componentType = componentInstance.getSpecification();
ComponentImpl componentImpl = componentInstance.getImplementation();
String old_portID = null;
if (componentType == null) {
componentType = componentImpl.getType();
called_node_ID = componentType.getName();
}
if (componentType != null && componentImpl != null) {
componentType = componentImpl.getType();
called_node_ID = componentType.getName() + "_dot_Impl";
String inst_cmp = "(.+)_instrumented";
Pattern inst_pattern = Pattern.compile(inst_cmp);
// System.out.print(src_portID + " ==> ");
Matcher m = inst_pattern.matcher(src_portID);
if (m.matches()) {
old_portID = src_portID;
src_portID = m.group(1);
arg_expr.setIdentifier(componentInstanceID + "_port_" + src_portID);
// System.out.println(old_portID + " -- " + src_portID);
}
// System.out.println(arg_expr.getIdentifier() + " <== " +
// src_portID);
} else {
called_node_ID = componentType.getName();
}
String node_arg = "(.+)_Inst_.*";
Pattern arg_pattern = Pattern.compile(node_arg);
Matcher m_arg = arg_pattern.matcher(called_node_ID);
if (!m_arg.matches() && old_portID != null) {
arg_expr.setIdentifier(componentInstanceID + "_port_" + old_portID);
// System.out.println("Node ID =>" + called_node_ID + "(" +
// arg_expr.getIdentifier() + ")");
}
// System.out.println(called_node_ID);
for (Port port : componentType.getPort()) {
// MODE
PortMode port_mode = port.getMode();
if (port_mode == PortMode.OUT) {
// EQ L.H.S Variables *called Node return values
String expr_id = componentInstance.getId() + "_port_" + port.getName();
NodeEquation n_eq = getNodeEq(expr_id, nodeBody);
if (n_eq != null) {
Expression eq_rhs = n_eq.getRhs();
NodeCall node_called = eq_rhs.getCall();
String inst_cmp = "(.+)_Inst_.*";
Pattern inst_pattern = Pattern.compile(inst_cmp);
String node_id = "";
if (node_called != null) {
node_id = node_called.getNodeId();
}
Matcher m = inst_pattern.matcher(node_id);
IfThenElse ifelse = new IfThenElse();
Expression called_expr = new Expression();
// Condition
Expression g_expr = new Expression();
if (m.matches()) {
// Instrumented component Instance ID
String component_id = m.group(1);
g_expr.setIdentifier(component_id);
}
ifelse.setCondition(g_expr);
// Then
ifelse.setThenBranch(called_expr);
// Else
// Expression arg = new Expression();
ifelse.setElseBranch(arg_expr);
// NodeCalled Expr
called_expr.setCall(node_called);
Expression instrumented_expr = new Expression();
instrumented_expr.setConditionalExpression(ifelse);
if (node_called != null) {
if (node_called.getNodeId().equals(called_node_ID)) {
for (Expression a_expr : node_called.getArgument()) {
if (a_expr.getIdentifier().equals(dest_component_port.getName())) {
a_expr.setIdentifier(arg_expr.getIdentifier());
} else if (node_called.getArgument().size() == 1) {
a_expr.setIdentifier(arg_expr.getIdentifier());
}
}
}
}
}
}
}
}
}
use of verdict.vdm.vdm_model.ComponentImpl in project VERDICT by ge-high-assurance.
the class VerdictTestInstrumentor method getTopLevelSystemType.
/**
* Note: presently only supports one top-level system. If there are multiple, prints a warning
* and chooses the first one.
*
* <p>Also note that instrumentation generates additional system types. But these are added
* after the one we want. In this case the warning is erroneous.
*
* @return the top-level system type in model
*/
private ComponentType getTopLevelSystemType() {
Set<ComponentType> subcomps = new HashSet<>();
for (ComponentImpl impl : model.getComponentImpl()) {
if (impl.getBlockImpl() != null) {
for (ComponentInstance comp : impl.getBlockImpl().getSubcomponent()) {
if (comp.getSpecification() != null) {
subcomps.add(comp.getSpecification());
} else if (comp.getImplementation() != null) {
subcomps.add(comp.getImplementation().getType());
} else {
throw new RuntimeException("ComponentInstance has neither specification nor implementation");
}
}
}
}
List<ComponentType> topLevels = new ArrayList<>();
for (ComponentType comp : model.getComponentType()) {
if (!subcomps.contains(comp)) {
topLevels.add(comp);
}
}
if (topLevels.isEmpty()) {
throw new RuntimeException("Verdict ATG error: No top-level component found");
}
if (topLevels.size() > 1) {
System.out.println("Verdict ATG Warning: Multiple top-level systems found, using first one (may be caused by instrumentation)");
}
return topLevels.get(0);
}
Aggregations