use of verdict.vdm.vdm_model.PortMode 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.PortMode in project VERDICT by ge-high-assurance.
the class VDM2Lustre method visit.
public void visit(ComponentType componentType, NodeBody nodeBody, String componentInstanceID, boolean impl_type) {
// Rename componentType
String cmpName = componentType.getName();
cmpName = cmpName.replace(".", "_dot_");
cmpName = cmpName.replace("::", "_double_colon_");
componentType.setName(cmpName);
// Node Equation
NodeEquation node_eq = new NodeEquation();
// Return variables
NodeEquationLHS eq_lhs = new NodeEquationLHS();
// Node Call
Expression node = new Expression();
NodeCall nodeCall = new NodeCall();
if (impl_type) {
nodeCall.setNodeId(componentType.getName() + "_dot_Impl");
} else {
nodeCall.setNodeId(componentType.getName());
}
// Port
for (Port port : componentType.getPort()) {
// //Event Port Definition
// for (Port port : componentType.getPort()) {
// visit(port);
// }
// MODE
PortMode port_mode = port.getMode();
if (port_mode == PortMode.IN) {
Expression arg = new Expression();
arg.setIdentifier(port.getName());
nodeCall.getArgument().add(arg);
}
if (port_mode == PortMode.OUT) {
VariableDeclaration var = new VariableDeclaration();
// Node Name
String output_name = port.getName();
String var_name = componentInstanceID + "_port_" + output_name;
var.setName(var_name);
// Node DataType
DataType dataType = port.getType();
var.setDataType(dataType);
nodeBody.getVariableDeclaration().add(var);
eq_lhs.getIdentifier().add(var_name);
}
}
// Ignore node call that do not have output
if (eq_lhs.getIdentifier() != null) {
node.setCall(nodeCall);
node_eq.setRhs(node);
node_eq.setLhs(eq_lhs);
nodeBody.getEquation().add(node_eq);
}
}
use of verdict.vdm.vdm_model.PortMode in project VERDICT by ge-high-assurance.
the class Instrumentor method softwareVirus.
// SV:
// - Select components c in the model M such that:
// c.ComponentType = 'Software' v c.ComponentType = 'Hybrid' & c.Manufacturer =
// 'ThirdParty'
// & \exists ch\in M. p\in InputPort(c). ch = p.channel & ch.Connectin-Type =
// Remote
@Override
public void softwareVirus(HashSet<ComponentType> vdm_components) {
try {
HashSet<String> components = new HashSet<String>();
HashSet<String> svComponentTypeSet = new HashSet<String>(Arrays.asList("software", "swhwhybrid", "swhumanhybrid", "hybrid"));
BlockImpl blockImpl = null;
for (ComponentImpl componentImpl : vdm_model.getComponentImpl()) {
blockImpl = componentImpl.getBlockImpl();
// BlockImpl
if (blockImpl != null) {
ComponentType componentType = componentImpl.getType();
for (ComponentInstance componentInstance : blockImpl.getSubcomponent()) {
componentType = componentInstance.getSpecification();
ComponentImpl subcomponentImpl = componentInstance.getImplementation();
// Option 1) Specification
if (componentType != null) {
} else // Option 2) Implementation
if (subcomponentImpl != null) {
componentType = subcomponentImpl.getType();
}
List<GenericAttribute> attributeList = componentInstance.getAttribute();
GenericAttribute componentKindAttribute = getAttributeByName(attributeList, "ComponentType", componentInstance.getName());
GenericAttribute staticCodeAnalysisAttribute = getAttributeByName(attributeList, "StaticCodeAnalysis", componentInstance.getName());
GenericAttribute inputValidationAttribute = getAttributeByName(attributeList, "InputValidation", componentInstance.getName());
GenericAttribute memoryProtectionAttribute = getAttributeByName(attributeList, "MemoryProtection", componentInstance.getName());
GenericAttribute secureBootAttribute = getAttributeByName(attributeList, "SecureBoot", componentInstance.getName());
String componentKind = componentKindAttribute.getValue().toString().toLowerCase();
int staticCodeAnalysis = Integer.parseInt(staticCodeAnalysisAttribute.getValue().toString());
int inputValidation = Integer.parseInt(inputValidationAttribute.getValue().toString());
int memoryProtection = Integer.parseInt(memoryProtectionAttribute.getValue().toString());
int secureBoot = Integer.parseInt(secureBootAttribute.getValue().toString());
if (svComponentTypeSet.contains(componentKind.toLowerCase()) && (staticCodeAnalysis == 0 || inputValidation == 0 || memoryProtection == 0 || secureBoot == 0)) {
Boolean hasEligibleIncomingChannels = false;
for (Port port : componentType.getPort()) {
PortMode mode = port.getMode();
if (mode == PortMode.IN) {
for (Connection connection : blockImpl.getConnection()) {
if (connection.getDestination().getSubcomponentPort() != null) {
if (connection.getDestination().getSubcomponentPort().getPort() == port) {
Boolean scInsideTrustedBoundary;
String scComponentKind;
String scPedigree;
int scStrongCryptoAlgorithms;
int scSupplyChainSecurity;
int scTamperProtection;
if (connection.getSource().getSubcomponentPort() != null) {
ComponentInstance sourceComponent = connection.getSource().getSubcomponentPort().getSubcomponent();
List<GenericAttribute> sourceComponentAttributeList = sourceComponent.getAttribute();
GenericAttribute sourceComponentInsideTrustedBoundaryAttribute = getAttributeByName(sourceComponentAttributeList, "InsideTrustedBoundary", sourceComponent.getName());
GenericAttribute sourceComponentComponentKindAttribute = getAttributeByName(sourceComponentAttributeList, "ComponentType", sourceComponent.getName());
GenericAttribute sourceComponentPedigreeAttribute = getAttributeByName(sourceComponentAttributeList, "Pedigree", sourceComponent.getName());
GenericAttribute sourceComponentStrongCryptoAlgorithmsAttribute = getAttributeByName(sourceComponentAttributeList, "StrongCryptoAlgorithms", sourceComponent.getName());
GenericAttribute sourceComponentSupplyChainSecurityAttribute = getAttributeByName(sourceComponentAttributeList, "SupplyChainSecurity", sourceComponent.getName());
GenericAttribute sourceComponentTamperProtectionAttribute = getAttributeByName(sourceComponentAttributeList, "TamperProtection", sourceComponent.getName());
scInsideTrustedBoundary = Boolean.parseBoolean(sourceComponentInsideTrustedBoundaryAttribute.getValue().toString());
scComponentKind = sourceComponentComponentKindAttribute.getValue().toString().toLowerCase();
scPedigree = sourceComponentPedigreeAttribute.getValue().toString().toLowerCase();
scStrongCryptoAlgorithms = Integer.parseInt(sourceComponentStrongCryptoAlgorithmsAttribute.getValue().toString());
scSupplyChainSecurity = Integer.parseInt(sourceComponentSupplyChainSecurityAttribute.getValue().toString());
scTamperProtection = Integer.parseInt(sourceComponentTamperProtectionAttribute.getValue().toString());
} else {
scInsideTrustedBoundary = true;
scComponentKind = "";
scPedigree = "";
scStrongCryptoAlgorithms = -1;
scSupplyChainSecurity = -1;
scTamperProtection = -1;
}
List<GenericAttribute> connectionAttributeList = connection.getAttribute();
GenericAttribute connectionTypeAttribute = getAttributeByName(connectionAttributeList, "ConnectionType", connection.getName());
GenericAttribute deviceAuthenticationAttribute = getAttributeByName(connectionAttributeList, "DeviceAuthentication", connection.getName());
GenericAttribute sessionAuthenticityAttribute = getAttributeByName(connectionAttributeList, "SessionAuthenticity", connection.getName());
String connectionType = connectionTypeAttribute.getValue().toString().toLowerCase();
int deviceAuthentication = Integer.parseInt(deviceAuthenticationAttribute.getValue().toString());
int sessionAuthenticity = Integer.parseInt(sessionAuthenticityAttribute.getValue().toString());
if ((!scInsideTrustedBoundary || connectionType.equalsIgnoreCase("untrusted")) && !scComponentKind.equalsIgnoreCase("hardware") && ((scPedigree.equalsIgnoreCase("cots") || (scPedigree.equalsIgnoreCase("sourced") && scSupplyChainSecurity == 0 && scTamperProtection == 0)) || ((deviceAuthentication == 0 && sessionAuthenticity == 0) || scStrongCryptoAlgorithms == 0))) {
hasEligibleIncomingChannels = true;
}
break;
}
}
}
}
if (hasEligibleIncomingChannels) {
break;
}
}
if (hasEligibleIncomingChannels) {
vdm_components.add(componentType);
components.add(componentType.getId());
}
}
}
}
}
this.attack_cmp_link_map.put("SV", components);
} catch (CRVException e) {
System.out.println("\tCRV Error " + e.getCode() + " " + e.getMessage());
}
}
use of verdict.vdm.vdm_model.PortMode in project VERDICT by ge-high-assurance.
the class Instrumentor method instrument_component.
// Instrument Link for all outgoing edges
@Override
public HashSet<Connection> instrument_component(ComponentType component, BlockImpl blockImpl) {
HashSet<Connection> vdm_links = new HashSet<Connection>();
HashSet<String> links = new HashSet<String>();
for (Port port : component.getPort()) {
PortMode mode = port.getMode();
if (mode == PortMode.OUT) {
// for (Connection connection : blockImpl.getConnection()) {
// links.add(connection.getName());
// links.add(port.getName());
// links.addAll(get_ports(connection));
// }
// links.add(port.getName());
}
{
// instrument_link(port, blockImpl);
if (blockImpl != null) {
for (Connection connection : blockImpl.getConnection()) {
if (retrieve_links(component, connection, port)) {
vdm_links.add(connection);
links.add(connection.getName());
// links.add(get_ports(vdm_links));
}
// links.addAll(get_ports(connection));
}
} else {
}
}
}
String attack_type = getThreatID(component.getId());
if (this.attack_cmp_link_map.containsKey(attack_type)) {
HashSet<String> cmp_links = this.attack_cmp_link_map.get(attack_type);
for (Connection con : vdm_links) {
if (!isProbePort(con)) {
cmp_links.addAll(get_ports(con));
}
}
}
// System.out.println(links);
return vdm_links;
}
use of verdict.vdm.vdm_model.PortMode in project VERDICT by ge-high-assurance.
the class VDM2Lustre method visit.
public void visit(Port port, Node node) {
PortMode port_mode = port.getMode();
NodeParameter node_parameter = new NodeParameter();
// Node Name
String port_name = port.getName();
node_parameter.setName(port_name);
// Node DataType
DataType dataType = port.getType();
// Change Null DataType to Integer Default Type
if (dataType == null) {
port.setType(defaultType());
}
node_parameter.setDataType(dataType);
// Node Input Parameter
if (port_mode == PortMode.IN) {
node.getInputParameter().add(node_parameter);
} else // Node OutputParameter
if (port_mode == PortMode.OUT) {
node.getOutputParameter().add(node_parameter);
// if(node.isIsImported() != null) {
String inst_cmp = "(.+)_Inst_.*";
Pattern inst_pattern = Pattern.compile(inst_cmp);
Matcher m = inst_pattern.matcher(node.getName());
if (m.matches()) {
node_parameter.setName(node_parameter.getName() + "_instrumented");
}
}
}
Aggregations