Skip to content

Commit

Permalink
Only strip comments from inaccessible variables (#11487)
Browse files Browse the repository at this point in the history
- Use the Protection annotation to determine whether to strip comments
  from variables or not in FlatModel.obfuscate.
  • Loading branch information
perost committed Nov 1, 2023
1 parent d230b4a commit f0d7b74
Show file tree
Hide file tree
Showing 7 changed files with 2,947 additions and 2,792 deletions.
16 changes: 9 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -707,10 +707,6 @@ public
addObfuscatedVariable(v, only_encrypted, obfuscation_map);
end for;
if UnorderedMap.isEmpty(obfuscation_map) then
return;
end if;
flatModel.variables := list(obfuscateVariable(v, obfuscation_map) for v in flatModel.variables);
flatModel := mapEquations(flatModel, function obfuscateEquation(obfuscationMap = obfuscation_map));
flatModel := mapAlgorithms(flatModel, function obfuscateAlgorithm(obfuscationMap = obfuscation_map));
Expand Down Expand Up @@ -740,7 +736,8 @@ public
input ObfuscationMap obfuscationMap;
algorithm
var.name := obfuscateCref(var.name, obfuscationMap);
var.comment := obfuscateCommentOpt(var.comment, ComponentRef.node(var.name), obfuscationMap);
var.comment := obfuscateCommentOpt(var.comment, ComponentRef.node(var.name),
obfuscationMap, stripComment = not Variable.isAccessible(var));
var := Variable.mapExpShallow(var, function obfuscateExp(obfuscationMap = obfuscationMap));
end obfuscateVariable;
Expand Down Expand Up @@ -839,18 +836,23 @@ public
input output Option<SCode.Comment> comment;
input InstNode scope;
input ObfuscationMap obfuscationMap;
input Boolean stripComment = true;
algorithm
comment := Util.applyOption(comment,
function obfuscateComment(scope = scope, obfuscationMap = obfuscationMap));
function obfuscateComment(scope = scope, obfuscationMap = obfuscationMap, stripComment = stripComment));
end obfuscateCommentOpt;
function obfuscateComment
input output SCode.Comment comment;
input InstNode scope;
input ObfuscationMap obfuscationMap;
input Boolean stripComment = true;
algorithm
comment.annotation_ := obfuscateAnnotationOpt(comment.annotation_, scope, obfuscationMap);
comment.comment := NONE();
if stripComment then
comment.comment := NONE();
end if;
end obfuscateComment;
function obfuscateAnnotationOpt
Expand Down
28 changes: 28 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -44,6 +44,7 @@ import Pointer;
import Error;
import Prefixes = NFPrefixes;
import Visibility = NFPrefixes.Visibility;
import AccessLevel = NFPrefixes.AccessLevel;
import NFModifier.Modifier;
import SCodeDump;
import DAE;
Expand Down Expand Up @@ -2086,6 +2087,33 @@ uniontype InstNode
InstNodeType.TOP_SCOPE(generatedInners = inners) := nodeType(InstNode.topScope(node));
UnorderedMap.clear(inners);
end clearGeneratedInners;

function getAccessLevel
input InstNode node;
output Option<AccessLevel> access = NONE();
protected
InstNode scope;
SCode.Mod access_mod;
Option<Absyn.Exp> access_exp;
algorithm
scope := classScope(parent(resolveInner(node)));

while isClass(scope) loop
access_mod := SCodeUtil.lookupElementAnnotation(definition(scope), "Protection");
access_mod := SCodeUtil.lookupModInMod("access", access_mod);
access_exp := SCodeUtil.getModifierBinding(access_mod);

if isSome(access_exp) then
access := Prefixes.accessLevelFromAbsyn(Util.getOption(access_exp));

if isSome(access) then
return;
end if;
end if;

scope := parent(scope);
end while;
end getAccessLevel;
end InstNode;

annotation(__OpenModelica_Interface="frontend");
Expand Down
36 changes: 36 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFPrefixes.mo
Expand Up @@ -302,6 +302,17 @@ type Visibility = enumeration(
PROTECTED
);

type AccessLevel = enumeration(
HIDE,
ICON,
DOCUMENTATION,
DIAGRAM,
NON_PACKAGE_TEXT,
NON_PACKAGE_DUPLICATE,
PACKAGE_TEXT,
PACKAGE_DUPLICATE
);

uniontype Replaceable
record REPLACEABLE
Option<InstNode> constrainingClass;
Expand Down Expand Up @@ -706,5 +717,30 @@ algorithm
fail();
end printPrefixError;

function accessLevelFromAbsyn
input Absyn.Exp exp;
output Option<AccessLevel> access;
protected
String name;
algorithm
access := match exp
case Absyn.Exp.CREF(componentRef = Absyn.ComponentRef.CREF_QUAL(name = "Access",
componentRef = Absyn.ComponentRef.CREF_IDENT(name = name)))
then match name
case "hide" then SOME(AccessLevel.HIDE);
case "icon" then SOME(AccessLevel.ICON);
case "documentation" then SOME(AccessLevel.DOCUMENTATION);
case "diagram" then SOME(AccessLevel.DIAGRAM);
case "nonPackageText" then SOME(AccessLevel.NON_PACKAGE_TEXT);
case "nonPackageDuplicate" then SOME(AccessLevel.NON_PACKAGE_DUPLICATE);
case "packageText" then SOME(AccessLevel.PACKAGE_TEXT);
case "packageDuplicate" then SOME(AccessLevel.PACKAGE_DUPLICATE);
else NONE();
end match;

else NONE();
end match;
end accessLevelFromAbsyn;

annotation(__OpenModelica_Interface="frontend");
end NFPrefixes;
25 changes: 25 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -40,6 +40,7 @@ encapsulated uniontype NFVariable
import NFPrefixes.Variability;
import NFPrefixes.ConnectorType;
import NFPrefixes.Direction;
import NFPrefixes.AccessLevel;
import Type = NFType;
import BackendExtension = NFBackendExtension;
import NFBackendExtension.BackendInfo;
Expand Down Expand Up @@ -289,6 +290,30 @@ public
output Boolean isEncrypted = Util.endsWith(variable.info.fileName, ".moc");
end isEncrypted;

function isAccessible
input Variable variable;
output Boolean isAccessible;
protected
Option<AccessLevel> oaccess;
AccessLevel access;
algorithm
oaccess := InstNode.getAccessLevel(ComponentRef.node(variable.name));

if isSome(oaccess) then
SOME(access) := oaccess;
else
access := if isEncrypted(variable) then AccessLevel.DOCUMENTATION else AccessLevel.PACKAGE_DUPLICATE;
end if;

if access < AccessLevel.ICON then
isAccessible := false;
elseif access < AccessLevel.NON_PACKAGE_TEXT then
isAccessible := not isProtected(variable);
else
isAccessible := true;
end if;
end isAccessible;

function lookupTypeAttribute
input String name;
input Variable var;
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/interactive-API/Makefile
Expand Up @@ -75,6 +75,7 @@ MoveClass2.mos \
MoveClass.mos \
Obfuscation1.mos \
Obfuscation2.mos \
Obfuscation3.mos \
ProtectedHandlingBug2917.mos \
ReadOnlyPkg.mos \
refactorGraphAnn1.mos \
Expand Down

0 comments on commit f0d7b74

Please sign in to comment.