Skip to content

Commit 013311c

Browse files
authored
Don't print C-comments when obfuscating (#9936)
- Hide more occurrences of variable names in comments. - Also skip printing equation information when obfuscating. - See #9119 - Continuation of #9672 and #9880
1 parent e001fb0 commit 013311c

File tree

4 files changed

+67
-49
lines changed

4 files changed

+67
-49
lines changed

OMCompiler/Compiler/Template/CodegenCFunctions.tpl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,14 +4045,14 @@ template crefToOMSICStr(ComponentRef cref, HashTableCrefSimVar.HashTable hashTab
40454045
case v as SIMVAR(index=-2) then
40464046
match cref2simvar(componentRef, getSimCode())
40474047
case v as SIMVAR(__) then
4048-
let c_comment = CodegenUtil.crefCComment(v)
4048+
let c_comment = CodegenUtil.crefCCommentWithVariability(v)
40494049
let index = getValueReference(v, getSimCode(), false)
40504050
<<
4051-
this_function->pre_vars-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%> /* TODO: Check why pre variable <%CodegenUtil.escapeCComments(CodegenUtil.crefStrNoUnderscore(v.name))%> is not in local hash table! */
4051+
this_function->pre_vars-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%> /* TODO: Check why pre variable <%CodegenUtil.crefCComment(v, CodegenUtil.crefStrNoUnderscore(v.name))%> is not in local hash table! */
40524052
>>
40534053
end match
40544054
case v as SIMVAR(__) then
4055-
let c_comment = CodegenUtil.crefCComment(v)
4055+
let c_comment = CodegenUtil.crefCCommentWithVariability(v)
40564056
let index = getValueReference(v, getSimCode(), false)
40574057
<<
40584058
this_function->pre_vars-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%>
@@ -4066,7 +4066,7 @@ template crefToOMSICStr(ComponentRef cref, HashTableCrefSimVar.HashTable hashTab
40664066
match cref2simvar(cref, getSimCode())
40674067
case v as SIMVAR(__) then
40684068
let index = getValueReference(v, getSimCode(), false)
4069-
let c_comment = CodegenUtil.crefCComment(v)
4069+
let c_comment = CodegenUtil.crefCCommentWithVariability(v)
40704070
<<
40714071
model_vars_and_params-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%>
40724072
>>
@@ -4076,13 +4076,13 @@ template crefToOMSICStr(ComponentRef cref, HashTableCrefSimVar.HashTable hashTab
40764076
case v as SIMVAR(varKind=JAC_VAR(__))
40774077
case v as SIMVAR(varKind=JAC_DIFF_VAR(__))
40784078
case v as SIMVAR(varKind=SEED_VAR(__)) then
4079-
let c_comment = CodegenUtil.crefCComment(v)
4079+
let c_comment = CodegenUtil.crefCCommentWithVariability(v)
40804080
<<
40814081
this_function->local_vars-><%crefTypeOMSIC(name)%>[<%v.index%>]<%c_comment%>
40824082
>>
40834083

40844084
case v as SIMVAR(__) then
4085-
let c_comment = CodegenUtil.crefCComment(v)
4085+
let c_comment = CodegenUtil.crefCCommentWithVariability(v)
40864086
let index = getValueReference(v, getSimCode(), false)
40874087
<<
40884088
this_function->function_vars-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%>
@@ -4750,9 +4750,9 @@ template jacCrefs(ComponentRef cr, Context context, Integer ix)
47504750
match context
47514751
case JACOBIAN_CONTEXT(jacHT=SOME(jacHT)) then
47524752
match simVarFromHT(cr, jacHT)
4753-
case v as SIMVAR(varKind=BackendDAE.JAC_VAR()) then 'jacobian->resultVars[<%index%>]<%crefCComment(v)%>'
4754-
case v as SIMVAR(varKind=BackendDAE.JAC_DIFF_VAR()) then 'jacobian->tmpVars[<%index%>]<%crefCComment(v)%>'
4755-
case v as SIMVAR(varKind=BackendDAE.SEED_VAR()) then 'jacobian->seedVars[<%index%>]<%crefCComment(v)%>'
4753+
case v as SIMVAR(varKind=BackendDAE.JAC_VAR()) then 'jacobian->resultVars[<%index%>]<%crefCCommentWithVariability(v)%>'
4754+
case v as SIMVAR(varKind=BackendDAE.JAC_DIFF_VAR()) then 'jacobian->tmpVars[<%index%>]<%crefCCommentWithVariability(v)%>'
4755+
case v as SIMVAR(varKind=BackendDAE.SEED_VAR()) then 'jacobian->seedVars[<%index%>]<%crefCCommentWithVariability(v)%>'
47564756
case SIMVAR(index=-2) then crefOld(cr, ix)
47574757
end jacCrefs;
47584758

@@ -4866,11 +4866,11 @@ template crefToCStr(ComponentRef cr, Integer ix, Boolean isPre, Boolean isStart,
48664866
case SIMVAR(aliasvar=ALIAS(varName=varName)) then crefToCStr(varName, ix, isPre, isStart, &sub)
48674867
case SIMVAR(aliasvar=NEGATEDALIAS(varName=varName), type_=T_BOOL()) then '!(<%crefToCStr(varName, ix, isPre, isStart, &sub)%>)'
48684868
case SIMVAR(aliasvar=NEGATEDALIAS(varName=varName)) then '-(<%crefToCStr(varName, ix, isPre, isStart, &sub)%>)'
4869-
case v as SIMVAR(varKind=JAC_VAR()) then '(parentJacobian->resultVars[<%index%>])<%&sub%><%crefCComment(v)%>'
4870-
case v as SIMVAR(varKind=JAC_DIFF_VAR()) then '(parentJacobian->tmpVars[<%index%>])<%&sub%><%crefCComment(v)%>'
4871-
case v as SIMVAR(varKind=SEED_VAR()) then '(parentJacobian->seedVars[<%index%>])<%&sub%><%crefCComment(v)%>'
4872-
case v as SIMVAR(varKind=DAE_RESIDUAL_VAR()) then '(data->simulationInfo->daeModeData->residualVars[<%index%>])<%&sub%><%crefCComment(v)%>'
4873-
case v as SIMVAR(varKind=DAE_AUX_VAR()) then '(data->simulationInfo->daeModeData->auxiliaryVars[<%index%>])<%&sub%><%crefCComment(v)%>'
4869+
case v as SIMVAR(varKind=JAC_VAR()) then '(parentJacobian->resultVars[<%index%>])<%&sub%><%crefCCommentWithVariability(v)%>'
4870+
case v as SIMVAR(varKind=JAC_DIFF_VAR()) then '(parentJacobian->tmpVars[<%index%>])<%&sub%><%crefCCommentWithVariability(v)%>'
4871+
case v as SIMVAR(varKind=SEED_VAR()) then '(parentJacobian->seedVars[<%index%>])<%&sub%><%crefCCommentWithVariability(v)%>'
4872+
case v as SIMVAR(varKind=DAE_RESIDUAL_VAR()) then '(data->simulationInfo->daeModeData->residualVars[<%index%>])<%&sub%><%crefCCommentWithVariability(v)%>'
4873+
case v as SIMVAR(varKind=DAE_AUX_VAR()) then '(data->simulationInfo->daeModeData->auxiliaryVars[<%index%>])<%&sub%><%crefCCommentWithVariability(v)%>'
48744874
case SIMVAR(index=-2) then
48754875
(let s = (if isPre then crefNonSimVar(crefPrefixPre(cr)) else crefNonSimVar(cr))
48764876
if intEq(ix,0) then s
@@ -7854,7 +7854,7 @@ template varArrayNameValues(SimVar var, Integer ix, Boolean isPre, Boolean isSta
78547854
case SIMVAR(varKind=EXTOBJ()) then
78557855
"ERROR: Not implemented in varArrayNameValues"
78567856
case SIMVAR(__) then
7857-
let c_comment = CodegenUtil.crefCComment(var)
7857+
let c_comment = CodegenUtil.crefCCommentWithVariability(var)
78587858
<<
78597859
<%if isStart then '<%varAttributes(var, &sub)%>.start'
78607860
else if isPre then '(<%arr%>this_function->pre_vars-><%crefTypeOMSIC(name)%>[<%index%>]<%c_comment%>)<%&sub%>'
@@ -7866,11 +7866,11 @@ template varArrayNameValues(SimVar var, Integer ix, Boolean isPre, Boolean isSta
78667866
match var
78677867
case SIMVAR(varKind=PARAM())
78687868
case SIMVAR(varKind=OPT_TGRID()) then
7869-
'(<%arr%>data->simulationInfo-><%crefShortType(name)%>Parameter[<%index%>]<%crefCComment(var)%>)<%&sub%>'
7869+
'(<%arr%>data->simulationInfo-><%crefShortType(name)%>Parameter[<%index%>]<%crefCCommentWithVariability(var)%>)<%&sub%>'
78707870
case SIMVAR(varKind=EXTOBJ()) then
78717871
'(<%arr%>data->simulationInfo->extObjs[<%index%>])<%&sub%>'
78727872
case SIMVAR(__) then
7873-
let c_comment = CodegenUtil.crefCComment(var)
7873+
let c_comment = CodegenUtil.crefCCommentWithVariability(var)
78747874
'<%if isStart then '<%varAttributes(var, &sub)%>.start'
78757875
else if isPre then '(<%arr%>data->simulationInfo-><%crefShortType(name)%>VarsPre[<%index%>]<%c_comment%>)<%&sub%>'
78767876
else '(<%arr%>data->localData[<%ix%>]-><%crefShortType(name)%>Vars[<%index%>]<%c_comment%>)<%sub%>'%>'
@@ -7889,7 +7889,7 @@ template crefVarInfo(ComponentRef cr)
78897889
::=
78907890
match cref2simvar(cr, getSimCode())
78917891
case var as SIMVAR(__) then
7892-
'data->modelData-><%varArrayName(var)%>Data[<%index%>].info /* <%escapeCComments(crefStrNoUnderscore(name))%> */'
7892+
'data->modelData-><%varArrayName(var)%>Data[<%index%>].info /* <%crefCComment(var, crefStrNoUnderscore(name))%> */'
78937893
end crefVarInfo;
78947894

78957895
template varAttributes(SimVar var, Text &sub)
@@ -7898,7 +7898,7 @@ template varAttributes(SimVar var, Text &sub)
78987898
match var
78997899
case SIMVAR(index=-1) then crefAttributes(name) // input variable? pass subs!!!
79007900
case SIMVAR(__) then
7901-
'(<%arr%>data->modelData-><%varArrayName(var)%>Data[<%index%>]<%crefCComment(var)%>)<%sub%>.attribute '
7901+
'(<%arr%>data->modelData-><%varArrayName(var)%>Data[<%index%>]<%crefCCommentWithVariability(var)%>)<%sub%>.attribute '
79027902
end varAttributes;
79037903

79047904
template crefAttributes(ComponentRef cr)
@@ -7907,7 +7907,7 @@ template crefAttributes(ComponentRef cr)
79077907
case var as SIMVAR(index=-1, varKind=JAC_VAR()) then "dummyREAL_ATTRIBUTE"
79087908
case var as SIMVAR(__) then
79097909
if intLt(index,0) then error(sourceInfo(), 'varAttributes got negative index=<%index%> for <%crefStr(name)%>') else
7910-
'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%escapeCComments(crefStrNoUnderscore(name))%> */'
7910+
'data->modelData-><%varArrayName(var)%>Data[<%index%>].attribute /* <%crefCComment(var, crefStrNoUnderscore(name))%> */'
79117911
end crefAttributes;
79127912

79137913
template typeCastContext(Context context, Type ty)

OMCompiler/Compiler/Template/CodegenOMSI_common.tpl

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ template generateEquationsCode (SimCode simCode, String FileNamePrefix)
6969

7070
/* generate file for algebraic systems in simulation problem */
7171

72-
72+
7373
let modelNamePrefix = modelNameOMSIC
74-
7574

76-
75+
76+
7777
let content = generateOmsiFunctionCode(simulation, modelNamePrefix,"","sim_eqns")
7878
let () = textFile(content, fullPathPrefix+"/"+fileNamePrefix+"_sim_eqns.c")
79-
79+
8080

8181

8282
let content = generateOmsiFunctionCode(initialization, modelNamePrefix,"", "init_eqns")
8383
let () = textFile(content, fullPathPrefix+"/"+fileNamePrefix+"_init_eqns.c")
84-
84+
8585

8686
<<>>
8787
end generateEquationsCode;
@@ -103,14 +103,14 @@ template generateOmsiFunctionCode(OMSIFunction omsiFunction, String FileNamePref
103103

104104
// generate header file
105105
let &functionPrototypes +='omsi_status <%FileNamePrefix%>_<%omsiName%>_allEqns(omsi_function_t* simulation, omsi_values* model_vars_and_params, void* data);<%\n%>'
106-
106+
107107

108108
let headerFileName = '<%fileNamePrefix%>_<%omsiName%>'
109-
110-
109+
110+
111111
let headerFileContent = generateCodeHeader(FileNamePrefix, &includes, headerFileName, &functionPrototypes)
112112
let () = textFile(headerFileContent, fullPathPrefix+"/"+headerFileName+".h")
113-
113+
114114

115115

116116
match omsiFunction
@@ -132,7 +132,7 @@ template generateOmsiFunctionCode(OMSIFunction omsiFunction, String FileNamePref
132132
133133
/* Equations evaluation */
134134
omsi_status <%FileNamePrefix%>_<%omsiName%>_allEqns(omsi_function_t* <%omsiName%>, omsi_values* model_vars_and_params, void* data){
135-
135+
136136
137137
/* Variables */
138138
omsi_status status, new_status;
@@ -581,27 +581,24 @@ template generateOmsiIndexTypeInitialization (list<SimVar> variables, String Str
581581
"OMSI_TYPE_UNKNOWN"
582582
)
583583

584-
let &stringName = buffer""
584+
let &stringName = buffer ""
585585
let &stringIndex = buffer ""
586-
let &stringVarKind = buffer ""
587586
let _ = (match variable
588587
case var as SIMVAR(varKind=JAC_VAR(__))
589588
case var as SIMVAR(varKind=JAC_DIFF_VAR(__))
590589
case var as SIMVAR(varKind=SEED_VAR(__)) then
591-
let &stringName += '<%CodegenUtil.escapeCComments(CodegenUtil.crefStrNoUnderscore(var.name))%>'
590+
let &stringName += '<%CodegenUtil.crefCCommentWithVariability(var)%>'
592591
let &stringIndex += var.index
593-
let &stringVarKind += CodegenUtil.variabilityString(var.varKind)
594592
<<>>
595593
case var as SIMVAR(__) then
596-
let &stringName += '<%CodegenUtil.escapeCComments(CodegenUtil.crefStrNoUnderscore(var.name))%>'
594+
let &stringName += '<%CodegenUtil.crefCCommentWithVariability(var)%>'
597595
let &stringIndex += getValueReference(var, getSimCode(), false)
598-
let &stringVarKind += CodegenUtil.variabilityString(var.varKind)
599596
<<>>
600597
)
601598

602599
<<
603600
<%omsiFuncName%>[<%i0%>].type = <%stringType%>;
604-
<%omsiFuncName%>[<%i0%>].index = <%stringIndex%>; /* <%stringName%> <%stringVarKind%> */
601+
<%omsiFuncName%>[<%i0%>].index = <%stringIndex%>; <%stringName%>
605602
>>
606603
;separator="\n")
607604

@@ -637,10 +634,10 @@ template generateInitalizationOMSIFunction (OMSIFunction omsiFunction, String fu
637634

638635
<<
639636

640-
637+
641638
omsi_status <%FileNamePrefix%>_<%omsiName%>_instantiate_<%functionName%>_OMSIFunc (omsi_function_t* omsi_function) {
642-
643-
639+
640+
644641
645642
filtered_base_logger(global_logCategories, log_all, omsi_ok,
646643
"fmi2Instantiate: Instantiate omsi_function <%functionName%>.");

OMCompiler/Compiler/Template/CodegenUtil.tpl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ template replaceDotAndUnderscore(String str)
6565
end replaceDotAndUnderscore;
6666

6767
template getGeneralTarget(String str)
68-
"Replace _ with __ and dot in identifiers with _"
6968
::=
7069
match str
7170
case "msvc10"
@@ -160,16 +159,29 @@ template subscriptStr(Subscript subscript)
160159
else "UNKNOWN_SUBSCRIPT"
161160
end subscriptStr;
162161

163-
164162
/*********************** Comments ************************/
165163

166164
template escapeCComments(String stringWithCComments)
167165
"escape the C comments inside a string, replaces them with /* */->(* *)"
168166
::= '<%System.stringReplace(System.stringReplace(stringWithCComments, "/*", "(*"), "*/", "*)")%>'
169167
end escapeCComments;
170168

171-
template crefCComment(SimVar v)
169+
template crefCComment(SimVar v, String vName)
172170
"write the C comment for a cref, if it is not to be obfuscated"
171+
::=
172+
match v
173+
case SIMVAR(isProtected = true) then
174+
if stringEq(getConfigString(OBFUSCATE), "none")
175+
then '<%escapeCComments(vName)%>'
176+
else 'OBFUSCATED'
177+
case SIMVAR(__) then
178+
if not stringEq(getConfigString(OBFUSCATE), "full")
179+
then '<%escapeCComments(vName)%>'
180+
else 'OBFUSCATED'
181+
end crefCComment;
182+
183+
template crefCCommentWithVariability(SimVar v)
184+
"write the C comment for a cref with variability"
173185
::=
174186
match v
175187
case SIMVAR(isProtected = true) then
@@ -178,11 +190,10 @@ template crefCComment(SimVar v)
178190
case SIMVAR(__) then
179191
if not stringEq(getConfigString(OBFUSCATE), "full")
180192
then ' /* <%escapeCComments(crefStrNoUnderscore(name))%> <%variabilityString(varKind)%> */'
181-
end crefCComment;
193+
end crefCCommentWithVariability;
182194

183195
/*********************************************************/
184196

185-
186197
template initDefaultValXml(DAE.Type type_)
187198
::=
188199
match type_

OMCompiler/Compiler/Template/CodegenUtilSimulation.tpl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ template equationIndexAlternativeTearing(SimEqSystem eq)
112112
end equationIndexAlternativeTearing;
113113

114114
template dumpEqs(list<SimEqSystem> eqs)
115+
::= match getConfigString(OBFUSCATE)
116+
case "none" then dumpEqsWork(eqs)
117+
end dumpEqs;
118+
119+
template dumpEqsWork(list<SimEqSystem> eqs)
115120
::= eqs |> eq hasindex i0 =>
116121
match eq
117122
case e as SES_RESIDUAL(__) then
@@ -275,10 +280,10 @@ template dumpEqs(list<SimEqSystem> eqs)
275280
<<
276281
unknown equation
277282
>>
278-
end dumpEqs;
283+
end dumpEqsWork;
279284

280285

281-
template dumpAlgSystemOps (Option<DerivativeMatrix> derivativeMatrix)
286+
template dumpAlgSystemOps(Option<DerivativeMatrix> derivativeMatrix)
282287
"dumps description of eqations of algebraic system.
283288
Helper function for dumpEqs."
284289
::=
@@ -298,7 +303,7 @@ template dumpAlgSystemOps (Option<DerivativeMatrix> derivativeMatrix)
298303
>>
299304
end dumpAlgSystemOps;
300305

301-
template dumpAlgSystemColumn (OMSIFunction column ,Text &columnBuffer, Text &varsBuffer)
306+
template dumpAlgSystemColumn(OMSIFunction column ,Text &columnBuffer, Text &varsBuffer)
302307
"dumps equation description for one OMSIFunction"
303308
::=
304309

@@ -363,6 +368,11 @@ template dumpWhenOps(list<BackendDAE.WhenOperator> whenOps)
363368
end dumpWhenOps;
364369

365370
template dumpEqsAlternativeTearing(list<SimEqSystem> eqs)
371+
::= match getConfigString(OBFUSCATE)
372+
case "none" then dumpEqsAlternativeTearingWork(eqs)
373+
end dumpEqsAlternativeTearing;
374+
375+
template dumpEqsAlternativeTearingWork(list<SimEqSystem> eqs)
366376
::= eqs |> eq hasindex i0 =>
367377
match eq
368378
case e as SES_LINEAR(alternativeTearing=SOME(at as LINEARSYSTEM(__))) then
@@ -416,7 +426,7 @@ template dumpEqsAlternativeTearing(list<SimEqSystem> eqs)
416426
<<
417427
unknown equation
418428
>>
419-
end dumpEqsAlternativeTearing;
429+
end dumpEqsAlternativeTearingWork;
420430

421431
annotation(__OpenModelica_Interface="backend");
422432
end CodegenUtilSimulation;

0 commit comments

Comments
 (0)