Skip to content

Commit

Permalink
Restrict the API based on the access annotation of the encrypted class
Browse files Browse the repository at this point in the history
Show error message when user can't access contents

Belonging to [master]:
  - OpenModelica/OMCompiler#2498
  • Loading branch information
adeas31 authored and OpenModelica-Hudson committed Jun 13, 2018
1 parent 91cd77d commit 1f361bb
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2466,7 +2466,7 @@ end buildModelFMU;
function buildEncryptedPackage
input TypeName className "the class that should encrypted";
output Boolean success;
output String commandOutput "Output of the packagetool execuable";
output String commandOutput "Output of the packagetool executable";
external "builtin";
annotation(preferredView="text");
end buildEncryptedPackage;
Expand Down
14 changes: 10 additions & 4 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -316,7 +316,7 @@ algorithm
// see https://trac.openmodelica.org/OpenModelica/ticket/2422
// prio = if_(stringEq(prio,""), "default", prio);
mp := System.realpath(dir + "/../") + System.groupDelimiter() + Settings.getModelicaPath(Config.getRunningTestsuite());
(outProgram,true) := loadModel((Absyn.IDENT(cname),{prio},true)::{}, mp, p, true, true, checkUses, true, false);
(outProgram,true) := loadModel((Absyn.IDENT(cname),{prio},true)::{}, mp, p, true, true, checkUses, true, filename == "package.moc");
return;
end if;
outProgram := Parser.parse(name,encoding);
Expand Down Expand Up @@ -551,7 +551,7 @@ algorithm
array<list<Integer>> m,mt;
Values.Value ret_val,simValue,value,v,cvar,cvar2,v1,v2,v3,gcStatRec;
Absyn.ComponentRef cr,cr_1;
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status;
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status, access;
list<Integer> is;
list<String> vars_1,args,strings,strs,strs1,strs2,visvars,postOptModStrings,postOptModStringsOrg,mps,files,dirs;
Real timeTotal,timeSimulation,timeStamp,val,x1,x2,y1,y2,r,r1,r2,linearizeTime,curveWidth,offset,offset1,offset2,scaleFactor,scaleFactor1,scaleFactor2;
Expand Down Expand Up @@ -635,8 +635,14 @@ algorithm

case (cache,_,"setSourceFile",{Values.CODE(Absyn.C_TYPENAME(path)),Values.STRING(str)},_)
equation
(b,p) = Interactive.setSourceFile(path, str, SymbolTable.getAbsyn());
SymbolTable.setAbsyn(p);
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., The class is not encrypted.
(b,p) = Interactive.setSourceFile(path, str, SymbolTable.getAbsyn());
SymbolTable.setAbsyn(p);
else
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
b = false;
end if;
then
(cache,Values.BOOL(b));

Expand Down
139 changes: 57 additions & 82 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -702,7 +702,7 @@ algorithm
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;
Values.Value ret_val,simValue,value,v,cvar,cvar2,v1,v2,v3;
Absyn.ComponentRef cr,cr_1;
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status;
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status, access;
Option<Integer> fmiContext, fmiInstance, fmiModelVariablesInstance; /* void* implementation: DO NOT UNBOX THE POINTER AS THAT MIGHT CHANGE IT. Just treat this as an opaque type. */
Integer fmiLogLevel, direction;
list<Integer> is;
Expand Down Expand Up @@ -1858,24 +1858,37 @@ algorithm
case (cache,_,"saveModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},_)
algorithm
b := false;
absynClass := Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());
str := Dump.unparseStr(Absyn.PROGRAM({absynClass},Absyn.TOP()),true);
try
System.writeFile(filename, str);
b := true;
Values.ENUM_LITERAL(index=access) := Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., The class is not encrypted.
absynClass := Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());
str := Dump.unparseStr(Absyn.PROGRAM({absynClass},Absyn.TOP()),true);
try
System.writeFile(filename, str);
b := true;
else
Error.addMessage(Error.WRITING_FILE_ERROR, {filename});
end try;
else
Error.addMessage(Error.WRITING_FILE_ERROR, {filename});
end try;
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
b := false;
end if;
then
(cache,Values.BOOL(true));
(cache,Values.BOOL(b));

case (cache,_,"save",{Values.CODE(Absyn.C_TYPENAME(className))},_)
equation
(newp,filename) = Interactive.getContainedClassAndFile(className, SymbolTable.getAbsyn());
str = Dump.unparseStr(newp);
System.writeFile(filename, str);
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(className, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., The class is not encrypted.
(newp,filename) = Interactive.getContainedClassAndFile(className, SymbolTable.getAbsyn());
str = Dump.unparseStr(newp);
System.writeFile(filename, str);
b = true;
else
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
b = false;
end if;
then
(cache,Values.BOOL(true));
(cache,Values.BOOL(b));

case (cache,_,"save",{Values.CODE(Absyn.C_TYPENAME(_))},_)
then (cache,Values.BOOL(false));
Expand All @@ -1897,17 +1910,30 @@ algorithm
case (cache,_,"saveTotalModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath)),
Values.BOOL(b1), Values.BOOL(b2)},_)
equation
saveTotalModel(filename, classpath, b1, b2);
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., Access.documentation
saveTotalModel(filename, classpath, b1, b2);
b = true;
else
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
b = false;
end if;
then
(cache, Values.BOOL(true));
(cache, Values.BOOL(b));

case (cache,_,"saveTotalModel",{Values.STRING(_),Values.CODE(Absyn.C_TYPENAME(_)),
Values.BOOL(_), Values.BOOL(_)},_)
then (cache, Values.BOOL(false));

case (cache,_,"getDocumentationAnnotation",{Values.CODE(Absyn.C_TYPENAME(classpath))},_)
equation
((str1,str2,str3)) = Interactive.getNamedAnnotation(classpath, SymbolTable.getAbsyn(), Absyn.IDENT("Documentation"), SOME(("","","")),Interactive.getDocumentationAnnotationString);
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
if (access >= 3) then // i.e., Access.documentation
((str1,str2,str3)) = Interactive.getNamedAnnotation(classpath, SymbolTable.getAbsyn(), Absyn.IDENT("Documentation"), SOME(("","","")),Interactive.getDocumentationAnnotationString);
else
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
((str1,str2,str3)) = ("", "", "");
end if;
then
(cache,ValuesUtil.makeArray({Values.STRING(str1),Values.STRING(str2),Values.STRING(str3)}));

Expand Down Expand Up @@ -2578,15 +2604,27 @@ algorithm
case (cache,_,"getConnectionCount",{Values.CODE(Absyn.C_TYPENAME(path))},_)
equation
absynClass = Interactive.getPathedClassInProgram(path, SymbolTable.getAbsyn());
n = listLength(Interactive.getConnections(absynClass));
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
if (access >= 4) then // i.e., Access.diagram
n = listLength(Interactive.getConnections(absynClass));
else
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
n = 0;
end if;
then
(cache,Values.INTEGER(n));

case (cache,_,"getConnectionCount",_,_) then (cache,Values.INTEGER(0));

case (cache,_,"getNthConnection",{Values.CODE(Absyn.C_TYPENAME(path)),Values.INTEGER(n)},_)
equation
vals = Interactive.getNthConnection(Absyn.pathToCref(path), SymbolTable.getAbsyn(), n);
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
if (access >= 4) then // i.e., Access.diagram
vals = Interactive.getNthConnection(Absyn.pathToCref(path), SymbolTable.getAbsyn(), n);
else
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
vals = {};
end if;
then
(cache,ValuesUtil.makeArray(vals));

Expand Down Expand Up @@ -7063,69 +7101,6 @@ algorithm
end matchcontinue;
end getDymolaStateAnnotationModStr;

protected function getAccessAnnotation
"Returns the Protection(access=) annotation of a class.
This is annotated with the annotation:
annotation(Protection(access = Access.documentation)); in the class definition"
input Absyn.Path className;
input Absyn.Program p;
output String access;
algorithm
access := match(className,p)
local
String accessStr;
case(_,_)
equation
accessStr = Interactive.getNamedAnnotation(className, p, Absyn.IDENT("Protection"), SOME(""), getAccessAnnotationString);
then
accessStr;
else "";
end match;
end getAccessAnnotation;

protected function getAccessAnnotationString
"Extractor function for getAccessAnnotation"
input Option<Absyn.Modification> mod;
output String access;
algorithm
access := match (mod)
local
list<Absyn.ElementArg> arglst;

case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
then getAccessAnnotationString2(arglst);

end match;
end getAccessAnnotationString;

protected function getAccessAnnotationString2
"Extractor function for getAccessAnnotation"
input list<Absyn.ElementArg> eltArgs;
output String access;
algorithm
access := match eltArgs
local
list<Absyn.ElementArg> xs;
Absyn.ComponentRef cref;
String name;
Absyn.Info info;

case ({}) then "";

case (Absyn.MODIFICATION(path = Absyn.IDENT(name="access"),
modification = SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.CREF(cref)))))::_)
equation
name = Dump.printComponentRefStr(cref);
then name;

case (_::xs)
equation
name = getAccessAnnotationString2(xs);
then name;

end match;
end getAccessAnnotationString2;

protected function getClassInformation
"author: PA
Returns all the possible class information.
Expand Down Expand Up @@ -7161,7 +7136,7 @@ algorithm
version := CevalScript.getPackageVersion(path, p);
Absyn.STRING(preferredView) := Interactive.getNamedAnnotation(path, p, Absyn.IDENT("preferredView"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
isState := getDymolaStateAnnotation(path, p);
access := getAccessAnnotation(path, p);
access := Interactive.getAccessAnnotation(path, p);
res_1 := Values.TUPLE({
Values.STRING(res),
Values.STRING(cmt),
Expand Down

0 comments on commit 1f361bb

Please sign in to comment.