Skip to content

Commit 1f361bb

Browse files
adeas31OpenModelica-Hudson
authored andcommitted
Restrict the API based on the access annotation of the encrypted class
Show error message when user can't access contents Belonging to [master]: - OpenModelica/OMCompiler#2498
1 parent 91cd77d commit 1f361bb

File tree

6 files changed

+241
-116
lines changed

6 files changed

+241
-116
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ end buildModelFMU;
24662466
function buildEncryptedPackage
24672467
input TypeName className "the class that should encrypted";
24682468
output Boolean success;
2469-
output String commandOutput "Output of the packagetool execuable";
2469+
output String commandOutput "Output of the packagetool executable";
24702470
external "builtin";
24712471
annotation(preferredView="text");
24722472
end buildEncryptedPackage;

Compiler/Script/CevalScript.mo

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ algorithm
316316
// see https://trac.openmodelica.org/OpenModelica/ticket/2422
317317
// prio = if_(stringEq(prio,""), "default", prio);
318318
mp := System.realpath(dir + "/../") + System.groupDelimiter() + Settings.getModelicaPath(Config.getRunningTestsuite());
319-
(outProgram,true) := loadModel((Absyn.IDENT(cname),{prio},true)::{}, mp, p, true, true, checkUses, true, false);
319+
(outProgram,true) := loadModel((Absyn.IDENT(cname),{prio},true)::{}, mp, p, true, true, checkUses, true, filename == "package.moc");
320320
return;
321321
end if;
322322
outProgram := Parser.parse(name,encoding);
@@ -551,7 +551,7 @@ algorithm
551551
array<list<Integer>> m,mt;
552552
Values.Value ret_val,simValue,value,v,cvar,cvar2,v1,v2,v3,gcStatRec;
553553
Absyn.ComponentRef cr,cr_1;
554-
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status;
554+
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status, access;
555555
list<Integer> is;
556556
list<String> vars_1,args,strings,strs,strs1,strs2,visvars,postOptModStrings,postOptModStringsOrg,mps,files,dirs;
557557
Real timeTotal,timeSimulation,timeStamp,val,x1,x2,y1,y2,r,r1,r2,linearizeTime,curveWidth,offset,offset1,offset2,scaleFactor,scaleFactor1,scaleFactor2;
@@ -635,8 +635,14 @@ algorithm
635635

636636
case (cache,_,"setSourceFile",{Values.CODE(Absyn.C_TYPENAME(path)),Values.STRING(str)},_)
637637
equation
638-
(b,p) = Interactive.setSourceFile(path, str, SymbolTable.getAbsyn());
639-
SymbolTable.setAbsyn(p);
638+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
639+
if (access >= 9) then // i.e., The class is not encrypted.
640+
(b,p) = Interactive.setSourceFile(path, str, SymbolTable.getAbsyn());
641+
SymbolTable.setAbsyn(p);
642+
else
643+
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
644+
b = false;
645+
end if;
640646
then
641647
(cache,Values.BOOL(b));
642648

Compiler/Script/CevalScriptBackend.mo

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ algorithm
702702
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;
703703
Values.Value ret_val,simValue,value,v,cvar,cvar2,v1,v2,v3;
704704
Absyn.ComponentRef cr,cr_1;
705-
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status;
705+
Integer size,resI,i,i1,i2,i3,n,curveStyle,numberOfIntervals, status, access;
706706
Option<Integer> fmiContext, fmiInstance, fmiModelVariablesInstance; /* void* implementation: DO NOT UNBOX THE POINTER AS THAT MIGHT CHANGE IT. Just treat this as an opaque type. */
707707
Integer fmiLogLevel, direction;
708708
list<Integer> is;
@@ -1858,24 +1858,37 @@ algorithm
18581858
case (cache,_,"saveModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))},_)
18591859
algorithm
18601860
b := false;
1861-
absynClass := Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());
1862-
str := Dump.unparseStr(Absyn.PROGRAM({absynClass},Absyn.TOP()),true);
1863-
try
1864-
System.writeFile(filename, str);
1865-
b := true;
1861+
Values.ENUM_LITERAL(index=access) := Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
1862+
if (access >= 9) then // i.e., The class is not encrypted.
1863+
absynClass := Interactive.getPathedClassInProgram(classpath, SymbolTable.getAbsyn());
1864+
str := Dump.unparseStr(Absyn.PROGRAM({absynClass},Absyn.TOP()),true);
1865+
try
1866+
System.writeFile(filename, str);
1867+
b := true;
1868+
else
1869+
Error.addMessage(Error.WRITING_FILE_ERROR, {filename});
1870+
end try;
18661871
else
1867-
Error.addMessage(Error.WRITING_FILE_ERROR, {filename});
1868-
end try;
1872+
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
1873+
b := false;
1874+
end if;
18691875
then
1870-
(cache,Values.BOOL(true));
1876+
(cache,Values.BOOL(b));
18711877

18721878
case (cache,_,"save",{Values.CODE(Absyn.C_TYPENAME(className))},_)
18731879
equation
1874-
(newp,filename) = Interactive.getContainedClassAndFile(className, SymbolTable.getAbsyn());
1875-
str = Dump.unparseStr(newp);
1876-
System.writeFile(filename, str);
1880+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(className, SymbolTable.getAbsyn());
1881+
if (access >= 9) then // i.e., The class is not encrypted.
1882+
(newp,filename) = Interactive.getContainedClassAndFile(className, SymbolTable.getAbsyn());
1883+
str = Dump.unparseStr(newp);
1884+
System.writeFile(filename, str);
1885+
b = true;
1886+
else
1887+
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
1888+
b = false;
1889+
end if;
18771890
then
1878-
(cache,Values.BOOL(true));
1891+
(cache,Values.BOOL(b));
18791892

18801893
case (cache,_,"save",{Values.CODE(Absyn.C_TYPENAME(_))},_)
18811894
then (cache,Values.BOOL(false));
@@ -1897,17 +1910,30 @@ algorithm
18971910
case (cache,_,"saveTotalModel",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath)),
18981911
Values.BOOL(b1), Values.BOOL(b2)},_)
18991912
equation
1900-
saveTotalModel(filename, classpath, b1, b2);
1913+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
1914+
if (access >= 9) then // i.e., Access.documentation
1915+
saveTotalModel(filename, classpath, b1, b2);
1916+
b = true;
1917+
else
1918+
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
1919+
b = false;
1920+
end if;
19011921
then
1902-
(cache, Values.BOOL(true));
1922+
(cache, Values.BOOL(b));
19031923

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

19081928
case (cache,_,"getDocumentationAnnotation",{Values.CODE(Absyn.C_TYPENAME(classpath))},_)
19091929
equation
1910-
((str1,str2,str3)) = Interactive.getNamedAnnotation(classpath, SymbolTable.getAbsyn(), Absyn.IDENT("Documentation"), SOME(("","","")),Interactive.getDocumentationAnnotationString);
1930+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
1931+
if (access >= 3) then // i.e., Access.documentation
1932+
((str1,str2,str3)) = Interactive.getNamedAnnotation(classpath, SymbolTable.getAbsyn(), Absyn.IDENT("Documentation"), SOME(("","","")),Interactive.getDocumentationAnnotationString);
1933+
else
1934+
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
1935+
((str1,str2,str3)) = ("", "", "");
1936+
end if;
19111937
then
19121938
(cache,ValuesUtil.makeArray({Values.STRING(str1),Values.STRING(str2),Values.STRING(str3)}));
19131939

@@ -2578,15 +2604,27 @@ algorithm
25782604
case (cache,_,"getConnectionCount",{Values.CODE(Absyn.C_TYPENAME(path))},_)
25792605
equation
25802606
absynClass = Interactive.getPathedClassInProgram(path, SymbolTable.getAbsyn());
2581-
n = listLength(Interactive.getConnections(absynClass));
2607+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
2608+
if (access >= 4) then // i.e., Access.diagram
2609+
n = listLength(Interactive.getConnections(absynClass));
2610+
else
2611+
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
2612+
n = 0;
2613+
end if;
25822614
then
25832615
(cache,Values.INTEGER(n));
25842616

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

25872619
case (cache,_,"getNthConnection",{Values.CODE(Absyn.C_TYPENAME(path)),Values.INTEGER(n)},_)
25882620
equation
2589-
vals = Interactive.getNthConnection(Absyn.pathToCref(path), SymbolTable.getAbsyn(), n);
2621+
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(path, SymbolTable.getAbsyn());
2622+
if (access >= 4) then // i.e., Access.diagram
2623+
vals = Interactive.getNthConnection(Absyn.pathToCref(path), SymbolTable.getAbsyn(), n);
2624+
else
2625+
Error.addMessage(Error.ACCESS_ENCRYPTED_PROTECTED_CONTENTS, {});
2626+
vals = {};
2627+
end if;
25902628
then
25912629
(cache,ValuesUtil.makeArray(vals));
25922630

@@ -7063,69 +7101,6 @@ algorithm
70637101
end matchcontinue;
70647102
end getDymolaStateAnnotationModStr;
70657103

7066-
protected function getAccessAnnotation
7067-
"Returns the Protection(access=) annotation of a class.
7068-
This is annotated with the annotation:
7069-
annotation(Protection(access = Access.documentation)); in the class definition"
7070-
input Absyn.Path className;
7071-
input Absyn.Program p;
7072-
output String access;
7073-
algorithm
7074-
access := match(className,p)
7075-
local
7076-
String accessStr;
7077-
case(_,_)
7078-
equation
7079-
accessStr = Interactive.getNamedAnnotation(className, p, Absyn.IDENT("Protection"), SOME(""), getAccessAnnotationString);
7080-
then
7081-
accessStr;
7082-
else "";
7083-
end match;
7084-
end getAccessAnnotation;
7085-
7086-
protected function getAccessAnnotationString
7087-
"Extractor function for getAccessAnnotation"
7088-
input Option<Absyn.Modification> mod;
7089-
output String access;
7090-
algorithm
7091-
access := match (mod)
7092-
local
7093-
list<Absyn.ElementArg> arglst;
7094-
7095-
case (SOME(Absyn.CLASSMOD(elementArgLst = arglst)))
7096-
then getAccessAnnotationString2(arglst);
7097-
7098-
end match;
7099-
end getAccessAnnotationString;
7100-
7101-
protected function getAccessAnnotationString2
7102-
"Extractor function for getAccessAnnotation"
7103-
input list<Absyn.ElementArg> eltArgs;
7104-
output String access;
7105-
algorithm
7106-
access := match eltArgs
7107-
local
7108-
list<Absyn.ElementArg> xs;
7109-
Absyn.ComponentRef cref;
7110-
String name;
7111-
Absyn.Info info;
7112-
7113-
case ({}) then "";
7114-
7115-
case (Absyn.MODIFICATION(path = Absyn.IDENT(name="access"),
7116-
modification = SOME(Absyn.CLASSMOD(eqMod=Absyn.EQMOD(exp=Absyn.CREF(cref)))))::_)
7117-
equation
7118-
name = Dump.printComponentRefStr(cref);
7119-
then name;
7120-
7121-
case (_::xs)
7122-
equation
7123-
name = getAccessAnnotationString2(xs);
7124-
then name;
7125-
7126-
end match;
7127-
end getAccessAnnotationString2;
7128-
71297104
protected function getClassInformation
71307105
"author: PA
71317106
Returns all the possible class information.
@@ -7161,7 +7136,7 @@ algorithm
71617136
version := CevalScript.getPackageVersion(path, p);
71627137
Absyn.STRING(preferredView) := Interactive.getNamedAnnotation(path, p, Absyn.IDENT("preferredView"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
71637138
isState := getDymolaStateAnnotation(path, p);
7164-
access := getAccessAnnotation(path, p);
7139+
access := Interactive.getAccessAnnotation(path, p);
71657140
res_1 := Values.TUPLE({
71667141
Values.STRING(res),
71677142
Values.STRING(cmt),

0 commit comments

Comments
 (0)