Skip to content

Commit

Permalink
Improve obfuscation (#9608)
Browse files Browse the repository at this point in the history
- Implement new obfuscation method that obfuscates protected variables
  (with `--obfuscate=protected`) or only encrypted protected variables
  (with `--obfuscate=encrypted`) that replaces the old non-working
  method of obfuscating protected variables.
- Enable `--obfuscate=encrypted` automatically when the AST contains any
  encrypted classes.
- Add scopes to all equations and algorithms, to allow the obfuscation
  to look up names in annotations.
  • Loading branch information
perost committed Oct 27, 2022
1 parent 4647934 commit b363613
Show file tree
Hide file tree
Showing 22 changed files with 8,601 additions and 8,085 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Util/NBDifferentiate.mo
Expand Up @@ -1226,7 +1226,7 @@ public
(statements, diffArguments) := List.mapFold(alg.statements, differentiateStatement, diffArguments);
statements_flat := List.flatten(statements);
(inputs, outputs) := Algorithm.getInputsOutputs(statements_flat);
alg := Algorithm.ALGORITHM(statements_flat, inputs, outputs, alg.source);
alg := Algorithm.ALGORITHM(statements_flat, inputs, outputs, alg.scope, alg.source);
end differentiateAlgorithm;

function differentiateStatement
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFAlgorithm.mo
Expand Up @@ -53,6 +53,7 @@ public
list<Statement> statements;
list<ComponentRef> inputs;
list<ComponentRef> outputs;
InstNode scope;
DAE.ElementSource source;
end ALGORITHM;

Expand Down
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo
Expand Up @@ -505,9 +505,9 @@ protected
ty := Expression.typeOf(l);

if Type.isArray(ty) then
eq := Equation.ARRAY_EQUALITY(l, r, ty, DAE.emptyElementSource);
eq := Equation.ARRAY_EQUALITY(l, r, ty, InstNode.EMPTY_NODE(), DAE.emptyElementSource);
else
eq := Equation.EQUALITY(l, r, ty, DAE.emptyElementSource);
eq := Equation.EQUALITY(l, r, ty, InstNode.EMPTY_NODE(), DAE.emptyElementSource);
end if;

equations := eq :: equations;
Expand Down Expand Up @@ -574,7 +574,7 @@ protected
end while;

ty := Expression.typeOf(sum_exp);
eq := Equation.EQUALITY(sum_exp, Expression.makeZero(ty), ty, DAE.emptyElementSource);
eq := Equation.EQUALITY(sum_exp, Expression.makeZero(ty), ty, InstNode.EMPTY_NODE(), DAE.emptyElementSource);
equations := generateForLoop({eq}, iterators, ranges, equations);
end if;
end generateFlowEquation;
Expand Down Expand Up @@ -610,7 +610,7 @@ protected
// instead of creating an unnecessary for loop here.
body := Equation.replaceIteratorList(body, iterators[i], ranges[i]);
else
body := {Equation.FOR(iterators[i], SOME(ranges[i]), body, DAE.emptyElementSource)};
body := {Equation.FOR(iterators[i], SOME(ranges[i]), body, InstNode.EMPTY_NODE(), DAE.emptyElementSource)};
end if;
end for;

Expand Down
14 changes: 7 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFConnectEquations.mo
Expand Up @@ -261,7 +261,7 @@ protected
algorithm
source := ElementSource.mergeSources(lhsSource, rhsSource);
//source := ElementSource.addElementSourceConnect(source, (lhsCref, rhsCref));
equalityEq := Equation.makeCrefEquality(lhsCref, rhsCref, source);
equalityEq := Equation.makeCrefEquality(lhsCref, rhsCref, InstNode.EMPTY_NODE(), source);
end makeEqualityEquation;

function makeEqualityAssert
Expand Down Expand Up @@ -293,7 +293,7 @@ algorithm
exp := Expression.RELATION(lhs_exp, Operator.makeEqual(ty), rhs_exp);
end if;

equalityAssert := Equation.ASSERT(exp, EQ_ASSERT_STR, NFBuiltin.ASSERTIONLEVEL_ERROR, source);
equalityAssert := Equation.ASSERT(exp, EQ_ASSERT_STR, NFBuiltin.ASSERTIONLEVEL_ERROR, InstNode.EMPTY_NODE(), source);
end makeEqualityAssert;

//protected function shouldFlipPotentialEquation
Expand Down Expand Up @@ -340,7 +340,7 @@ algorithm
end for;
end if;

equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), c.ty, src)};
equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), c.ty, InstNode.EMPTY_NODE(), src)};
end generateFlowEquations;

function makeFlowExp
Expand Down Expand Up @@ -394,8 +394,8 @@ algorithm
e2 := makeInStreamCall(cref1);
src := ElementSource.mergeSources(src1, src2);
then
{Equation.EQUALITY(cref1, e1, Type.REAL(), src),
Equation.EQUALITY(cref2, e2, Type.REAL(), src)};
{Equation.EQUALITY(cref1, e1, Type.REAL(), InstNode.EMPTY_NODE(), src),
Equation.EQUALITY(cref2, e2, Type.REAL(), InstNode.EMPTY_NODE(), src)};

// One inside, one outside:
// cr1 = cr2;
Expand All @@ -404,7 +404,7 @@ algorithm
algorithm
src := ElementSource.mergeSources(src1, src2);
then
{Equation.makeCrefEquality(cr1, cr2, src)};
{Equation.makeCrefEquality(cr1, cr2, InstNode.EMPTY_NODE(), src)};

// The general case with N inside connectors and M outside:
else
Expand Down Expand Up @@ -433,7 +433,7 @@ algorithm
outside := removeStreamSetElement(e.name, outsideElements);
res := streamSumEquationExp(outside, insideElements, flowThreshold, variables);
src := ElementSource.addAdditionalComment(e.source, " equation generated from stream connection");
equations := Equation.EQUALITY(cref_exp, res, Type.REAL(), src) :: equations;
equations := Equation.EQUALITY(cref_exp, res, Type.REAL(), InstNode.EMPTY_NODE(), src) :: equations;
end for;
end streamEquationGeneral;

Expand Down
83 changes: 66 additions & 17 deletions OMCompiler/Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -149,84 +149,96 @@ public
Expression lhs "The left hand side expression.";
Expression rhs "The right hand side expression.";
Type ty;
InstNode scope;
DAE.ElementSource source;
end EQUALITY;

record ARRAY_EQUALITY
Expression lhs;
Expression rhs;
Type ty;
InstNode scope;
DAE.ElementSource source;
end ARRAY_EQUALITY;

record CONNECT
Expression lhs;
Expression rhs;
InstNode scope;
DAE.ElementSource source;
end CONNECT;

record FOR
InstNode iterator;
Option<Expression> range;
list<Equation> body "The body of the for loop.";
InstNode scope;
DAE.ElementSource source;
end FOR;

record IF
list<Branch> branches;
InstNode scope;
DAE.ElementSource source;
end IF;

record WHEN
list<Branch> branches;
InstNode scope;
DAE.ElementSource source;
end WHEN;

record ASSERT
Expression condition "The assert condition.";
Expression message "The message to display if the assert fails.";
Expression level "Error or warning";
InstNode scope;
DAE.ElementSource source;
end ASSERT;

record TERMINATE
Expression message "The message to display if the terminate triggers.";
InstNode scope;
DAE.ElementSource source;
end TERMINATE;

record REINIT
Expression cref "The variable to reinitialize.";
Expression reinitExp "The new value of the variable.";
InstNode scope;
DAE.ElementSource source;
end REINIT;

record NORETCALL
Expression exp;
InstNode scope;
DAE.ElementSource source;
end NORETCALL;

function makeEquality
input Expression lhs;
input Expression rhs;
input Type ty;
input InstNode scope;
input DAE.ElementSource src;
output Equation eq;
algorithm
eq := EQUALITY(lhs, rhs, ty, src);
eq := EQUALITY(lhs, rhs, ty, scope, src);
annotation(__OpenModelica_EarlyInline=true);
end makeEquality;

function makeCrefEquality
input ComponentRef lhsCref;
input ComponentRef rhsCref;
input InstNode scope;
input DAE.ElementSource src;
output Equation eq;
protected
Expression e1, e2;
algorithm
e1 := Expression.fromCref(lhsCref);
e2 := Expression.fromCref(rhsCref);
eq := makeEquality(e1, e2, Expression.typeOf(e1), src);
eq := makeEquality(e1, e2, Expression.typeOf(e1), scope, src);
end makeCrefEquality;

function makeBranch
Expand All @@ -241,10 +253,11 @@ public

function makeIf
input list<Branch> branches;
input InstNode scope;
input DAE.ElementSource src;
output Equation eq;
algorithm
eq := IF(branches, src);
eq := IF(branches, scope, src);
annotation(__OpenModelica_EarlyInline=true);
end makeIf;

Expand All @@ -266,6 +279,42 @@ public
end match;
end source;

function setSource
input DAE.ElementSource source;
input output Equation eq;
algorithm
() := match eq
case EQUALITY() algorithm eq.source := source; then ();
case ARRAY_EQUALITY() algorithm eq.source := source; then ();
case CONNECT() algorithm eq.source := source; then ();
case FOR() algorithm eq.source := source; then ();
case IF() algorithm eq.source := source; then ();
case WHEN() algorithm eq.source := source; then ();
case ASSERT() algorithm eq.source := source; then ();
case TERMINATE() algorithm eq.source := source; then ();
case REINIT() algorithm eq.source := source; then ();
case NORETCALL() algorithm eq.source := source; then ();
end match;
end setSource;

function scope
input Equation eq;
output InstNode scope;
algorithm
scope := match eq
case EQUALITY() then eq.scope;
case ARRAY_EQUALITY() then eq.scope;
case CONNECT() then eq.scope;
case FOR() then eq.scope;
case IF() then eq.scope;
case WHEN() then eq.scope;
case ASSERT() then eq.scope;
case TERMINATE() then eq.scope;
case REINIT() then eq.scope;
case NORETCALL() then eq.scope;
end match;
end scope;

function info
input Equation eq;
output SourceInfo info = ElementSource.getInfo(source(eq));
Expand Down Expand Up @@ -532,15 +581,15 @@ public
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else EQUALITY(e1, e2, eq.ty, eq.source);
then eq else EQUALITY(e1, e2, eq.ty, eq.scope, eq.source);

case ARRAY_EQUALITY()
algorithm
e1 := func(eq.lhs);
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else ARRAY_EQUALITY(e1, e2, eq.ty, eq.source);
then eq else ARRAY_EQUALITY(e1, e2, eq.ty, eq.scope, eq.source);

//case CREF_EQUALITY()
// algorithm
Expand All @@ -555,7 +604,7 @@ public
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else CONNECT(e1, e2, eq.source);
then eq else CONNECT(e1, e2, eq.scope, eq.source);

case FOR()
algorithm
Expand Down Expand Up @@ -583,27 +632,27 @@ public
e3 := func(eq.level);
then
if referenceEq(e1, eq.condition) and referenceEq(e2, eq.message) and
referenceEq(e3, eq.level) then eq else ASSERT(e1, e2, e3, eq.source);
referenceEq(e3, eq.level) then eq else ASSERT(e1, e2, e3, eq.scope, eq.source);

case TERMINATE()
algorithm
e1 := func(eq.message);
then
if referenceEq(e1, eq.message) then eq else TERMINATE(e1, eq.source);
if referenceEq(e1, eq.message) then eq else TERMINATE(e1, eq.scope, eq.source);

case REINIT()
algorithm
e1 := func(eq.cref);
e2 := func(eq.reinitExp);
then
if referenceEq(e1, eq.cref) and referenceEq(e2, eq.reinitExp) then
eq else REINIT(e1, e2, eq.source);
eq else REINIT(e1, e2, eq.scope, eq.source);

case NORETCALL()
algorithm
e1 := func(eq.exp);
then
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.source);
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.scope, eq.source);

else eq;
end match;
Expand All @@ -623,23 +672,23 @@ public
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else EQUALITY(e1, e2, eq.ty, eq.source);
then eq else EQUALITY(e1, e2, eq.ty, eq.scope, eq.source);

case ARRAY_EQUALITY()
algorithm
e1 := func(eq.lhs);
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else ARRAY_EQUALITY(e1, e2, eq.ty, eq.source);
then eq else ARRAY_EQUALITY(e1, e2, eq.ty, eq.scope, eq.source);

case CONNECT()
algorithm
e1 := func(eq.lhs);
e2 := func(eq.rhs);
then
if referenceEq(e1, eq.lhs) and referenceEq(e2, eq.rhs)
then eq else CONNECT(e1, e2, eq.source);
then eq else CONNECT(e1, e2, eq.scope, eq.source);

case FOR()
algorithm
Expand All @@ -666,27 +715,27 @@ public
e3 := func(eq.level);
then
if referenceEq(e1, eq.condition) and referenceEq(e2, eq.message) and
referenceEq(e3, eq.level) then eq else ASSERT(e1, e2, e3, eq.source);
referenceEq(e3, eq.level) then eq else ASSERT(e1, e2, e3, eq.scope, eq.source);

case TERMINATE()
algorithm
e1 := func(eq.message);
then
if referenceEq(e1, eq.message) then eq else TERMINATE(e1, eq.source);
if referenceEq(e1, eq.message) then eq else TERMINATE(e1, eq.scope, eq.source);

case REINIT()
algorithm
e1 := func(eq.cref);
e2 := func(eq.reinitExp);
then
if referenceEq(e1, eq.cref) and referenceEq(e2, eq.reinitExp) then
eq else REINIT(e1, e2, eq.source);
eq else REINIT(e1, e2, eq.scope, eq.source);

case NORETCALL()
algorithm
e1 := func(eq.exp);
then
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.source);
if referenceEq(e1, eq.exp) then eq else NORETCALL(e1, eq.scope, eq.source);

else eq;
end match;
Expand Down

0 comments on commit b363613

Please sign in to comment.