Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 43c6d44

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Add a few stackoverflow checks to the backend
Also fixes a stack overflow in SimCodeUtil.replaceCrefWithStartValue, which recursed on a start-value expression being the variable itself (replace x by x by x by ... x).
1 parent f79568b commit 43c6d44

File tree

4 files changed

+101
-54
lines changed

4 files changed

+101
-54
lines changed

Compiler/BackEnd/BackendDAECreate.mo

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,45 @@ public import BackendDAE;
4242
public import DAE;
4343
public import FCore;
4444

45-
protected import BackendDAEUtil;
46-
protected import BackendDump;
47-
protected import BackendEquation;
48-
protected import BackendVariable;
49-
protected import BackendVarTransform;
50-
protected import BaseHashTable;
51-
protected import CheckModel;
52-
protected import ComponentReference;
53-
protected import Config;
54-
protected import ClassInf;
55-
protected import DAEDump;
56-
protected import DAEUtil;
57-
protected import Debug;
58-
protected import ElementSource;
59-
protected import Error;
60-
protected import Expression;
61-
protected import ExpressionDump;
62-
protected import ExpressionSimplify;
63-
protected import ExpressionSolve;
64-
protected import Flags;
65-
protected import Global;
66-
protected import HashTableExpToExp;
67-
protected import HashTableExpToIndex;
68-
protected import HashTable;
69-
protected import HashTableCrToExpSourceTpl;
70-
protected import Inline;
71-
protected import List;
72-
protected import ExecStat.execStat;
73-
protected import SCode;
74-
protected import System;
75-
protected import Types;
76-
protected import Util;
77-
protected import VarTransform;
78-
protected import Vectorization;
79-
protected import ZeroCrossings;
45+
protected
46+
47+
import BackendDAEUtil;
48+
import BackendDump;
49+
import BackendEquation;
50+
import BackendVariable;
51+
import BackendVarTransform;
52+
import BaseHashTable;
53+
import CheckModel;
54+
import ComponentReference;
55+
import Config;
56+
import ClassInf;
57+
import DAEDump;
58+
import DAEUtil;
59+
import Debug;
60+
import ElementSource;
61+
import Error;
62+
import ErrorExt;
63+
import Expression;
64+
import ExpressionDump;
65+
import ExpressionSimplify;
66+
import ExpressionSolve;
67+
import Flags;
68+
import Global;
69+
import HashTableExpToExp;
70+
import HashTableExpToIndex;
71+
import HashTable;
72+
import HashTableCrToExpSourceTpl;
73+
import Inline;
74+
import List;
75+
import ExecStat.execStat;
76+
import SCode;
77+
import StackOverflow;
78+
import System;
79+
import Types;
80+
import Util;
81+
import VarTransform;
82+
import Vectorization;
83+
import ZeroCrossings;
8084

8185
protected type Functiontuple = tuple<Option<DAE.FunctionTree>,list<DAE.InlineType>>;
8286

@@ -111,8 +115,11 @@ protected
111115
DAE.FunctionTree functionTree;
112116
list<BackendDAE.TimeEvent> timeEvents;
113117
String neqStr,nvarStr;
114-
Integer varSize, eqnSize;
118+
Integer varSize, eqnSize, numCheckpoints;
115119
algorithm
120+
numCheckpoints:=ErrorExt.getNumCheckpoints();
121+
try
122+
StackOverflow.clearStacktraceMessages();
116123
// reset dumped file sequence number
117124
System.tmpTickResetIndex(0, Global.backendDAE_fileSequence);
118125
System.tmpTickResetIndex(1, Global.backendDAE_cseIndex);
@@ -173,6 +180,15 @@ algorithm
173180

174181
Error.assertionOrAddSourceMessage(not Flags.isSet(Flags.DUMP_BACKENDDAE_INFO),Error.BACKENDDAEINFO_LOWER,{neqStr,nvarStr},Absyn.dummyInfo);
175182
execStat("Generate backend data structure");
183+
return;
184+
else
185+
setGlobalRoot(Global.stackoverFlowIndex, NONE());
186+
ErrorExt.rollbackNumCheckpoints(ErrorExt.getNumCheckpoints()-numCheckpoints);
187+
Error.addInternalError("Stack overflow in "+getInstanceName()+"...\n"+stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
188+
/* Do not fail or we can loop too much */
189+
StackOverflow.clearStacktraceMessages();
190+
end try annotation(__OpenModelica_stackOverflowCheckpoint=true);
191+
fail();
176192
end lower;
177193

178194
protected function getExternalObjectAlias "Checks equations if there is an alias equation for external objects.

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import DumpGraphML;
8080
import DynamicOptimization;
8181
import ElementSource;
8282
import Error;
83+
import ErrorExt;
8384
import EvaluateFunctions;
8485
import EvaluateParameter;
8586
import ExecStat.execStat;
@@ -103,6 +104,7 @@ import RemoveSimpleEquations;
103104
import ResolveLoops;
104105
import SCode;
105106
import Sorting;
107+
import StackOverflow;
106108
import StateMachineFeatures;
107109
import SymbolicImplicitSolver;
108110
import SymbolicJacobian;
@@ -6801,7 +6803,11 @@ protected
68016803
tuple<BackendDAEFunc.matchingAlgorithmFunc, String> matchingAlgorithm;
68026804
BackendDAE.InlineData inlineData;
68036805
BackendDAE.Variables globalKnownVars;
6806+
Integer numCheckpoints;
68046807
algorithm
6808+
numCheckpoints:=ErrorExt.getNumCheckpoints();
6809+
try
6810+
StackOverflow.clearStacktraceMessages();
68056811
preOptModules := getPreOptModules(strPreOptModules);
68066812
postOptModules := getPostOptModules(strPostOptModules);
68076813
matchingAlgorithm := getMatchingAlgorithm(strmatchingAlgorithm);
@@ -6881,6 +6887,15 @@ algorithm
68816887
BackendDump.dumpLoops(outSimDAE);
68826888
end if;
68836889
checkBackendDAEWithErrorMsg(outSimDAE);
6890+
return;
6891+
else
6892+
setGlobalRoot(Global.stackoverFlowIndex, NONE());
6893+
ErrorExt.rollbackNumCheckpoints(ErrorExt.getNumCheckpoints()-numCheckpoints);
6894+
Error.addInternalError("Stack overflow in "+getInstanceName()+"...\n"+stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
6895+
/* Do not fail or we can loop too much */
6896+
StackOverflow.clearStacktraceMessages();
6897+
end try annotation(__OpenModelica_stackOverflowCheckpoint=true);
6898+
fail();
68846899
end getSolvedSystem;
68856900

68866901
public function preOptimizeBackendDAE "

Compiler/SimCode/SimCodeMain.mo

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import SimCode;
5656
protected
5757
import AvlSetString;
5858
import BackendDAECreate;
59+
import BackendDump;
5960
import Builtin;
6061
import ClockIndexes;
6162
import CevalScriptBackend;
@@ -77,6 +78,7 @@ import DAEUtil;
7778
import Debug;
7879
import Error;
7980
import ErrorExt;
81+
import ExecStat;
8082
import Flags;
8183
import FMI;
8284
import GC;
@@ -87,11 +89,10 @@ import TaskSystemDump;
8789
import SerializeInitXML;
8890
import Serializer;
8991
import SimCodeUtil;
92+
import StackOverflow;
9093
import StringUtil;
9194
import System;
9295
import Util;
93-
import BackendDump;
94-
import ExecStat;
9596

9697
public function createSimulationSettings
9798
input Real startTime;
@@ -420,7 +421,11 @@ protected
420421
Absyn.ComponentRef a_cref;
421422
tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
422423
list<tuple<String, String>> program;
424+
Integer numCheckpoints;
423425
algorithm
426+
numCheckpoints:=ErrorExt.getNumCheckpoints();
427+
try
428+
StackOverflow.clearStacktraceMessages();
424429
if Flags.isSet(Flags.GRAPHML) then
425430
HpcOmTaskGraph.dumpTaskGraph(inBackendDAE, filenamePrefix);
426431
end if;
@@ -442,6 +447,15 @@ algorithm
442447
callTargetTemplates(simCode, Config.simCodeTarget());
443448
timeTemplates := System.realtimeTock(ClockIndexes.RT_CLOCK_TEMPLATES);
444449
ExecStat.execStat("Templates");
450+
return;
451+
else
452+
setGlobalRoot(Global.stackoverFlowIndex, NONE());
453+
ErrorExt.rollbackNumCheckpoints(ErrorExt.getNumCheckpoints()-numCheckpoints);
454+
Error.addInternalError("Stack overflow in "+getInstanceName()+"...\n"+stringDelimitList(StackOverflow.readableStacktraceMessages(), "\n"), sourceInfo());
455+
/* Do not fail or we can loop too much */
456+
StackOverflow.clearStacktraceMessages();
457+
end try annotation(__OpenModelica_stackOverflowCheckpoint=true);
458+
fail();
445459
end generateModelCode;
446460

447461
protected function createSimCode "
@@ -900,11 +914,11 @@ algorithm
900914
list<BackendDAE.Var> allPrimaryParameters "already sorted";
901915
Real fsize;
902916

903-
case (cache, graph, _, (st as GlobalScript.SYMBOLTABLE(ast=p)), filenameprefix, _, _, _) equation
917+
case (cache, graph, _, (st as GlobalScript.SYMBOLTABLE(ast=p)), filenameprefix, _, _, _) algorithm
904918
// calculate stuff that we need to create SimCode data structure
905919
System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND);
906920
ExecStat.execStatReset();
907-
(cache, graph, dae, st) = CevalScriptBackend.runFrontEnd(cache, graph, className, st, false);
921+
(cache, graph, dae, st) := CevalScriptBackend.runFrontEnd(cache, graph, className, st, false);
908922
ExecStat.execStat("FrontEnd");
909923

910924
if Flags.isSet(Flags.SERIALIZED_SIZE) then
@@ -915,26 +929,26 @@ algorithm
915929
ExecStat.execStat("Serialize FrontEnd");
916930
end if;
917931

918-
timeFrontend = System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);
932+
timeFrontend := System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);
919933

920934
System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND);
921-
dae = DAEUtil.transformationsBeforeBackend(cache, graph, dae);
935+
dae := DAEUtil.transformationsBeforeBackend(cache, graph, dae);
922936
ExecStat.execStat("Transformations before backend");
923937

924938
if Flags.isSet(Flags.SERIALIZED_SIZE) then
925939
serializeNotify(dae, filenameprefix, "dae2");
926940
ExecStat.execStat("Serialize DAE (2)");
927941
end if;
928942

929-
generateFunctions = Flags.set(Flags.GEN, false);
943+
generateFunctions := Flags.set(Flags.GEN, false);
930944
// We should not need to lookup constants and classes in the backend,
931945
// so let's free up the old graph and just make it the initial environment.
932946
if not Flags.isSet(Flags.BACKEND_KEEP_ENV_GRAPH) then
933-
(cache,graph) = Builtin.initialGraph(cache);
947+
(cache,graph) := Builtin.initialGraph(cache);
934948
end if;
935949

936-
description = DAEUtil.daeDescription(dae);
937-
dlow = BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));
950+
description := DAEUtil.daeDescription(dae);
951+
dlow := BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));
938952

939953
GC.free(dae);
940954

@@ -944,8 +958,8 @@ algorithm
944958
end if;
945959

946960
//BackendDump.printBackendDAE(dlow);
947-
(dlow, initDAE, inlineData, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters) = BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix);
948-
timeBackend = System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
961+
(dlow, initDAE, inlineData, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters) := BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix);
962+
timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
949963

950964
if Flags.isSet(Flags.SERIALIZED_SIZE) then
951965
serializeNotify(dlow, filenameprefix, "simDAE");
@@ -957,10 +971,9 @@ algorithm
957971
ExecStat.execStat("Serialize solved system");
958972
end if;
959973

960-
(libs, file_dir, timeSimCode, timeTemplates) =
961-
generateModelCode(dlow, initDAE, inlineData, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters, p, className, filenameprefix, inSimSettingsOpt, args);
974+
(libs, file_dir, timeSimCode, timeTemplates) := generateModelCode(dlow, initDAE, inlineData, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters, p, className, filenameprefix, inSimSettingsOpt, args);
962975

963-
resultValues = {("timeTemplates", Values.REAL(timeTemplates)),
976+
resultValues := {("timeTemplates", Values.REAL(timeTemplates)),
964977
("timeSimCode", Values.REAL(timeSimCode)),
965978
("timeBackend", Values.REAL(timeBackend)),
966979
("timeFrontend", Values.REAL(timeFrontend))};

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6965,7 +6965,7 @@ algorithm
69656965
end match;
69666966
end artificialVarKind;
69676967

6968-
protected function replaceCrefWithStartValue"replaces a cref with its constant start value. Only if the BackendDAE.Varkind is not artificial in order to avoid guess-start values
6968+
protected function replaceCrefWithStartValue "replaces a cref with its constant start value. Only if the BackendDAE.Varkind is not artificial in order to avoid guess-start values
69696969
Waurich 2015-01"
69706970
input DAE.Exp expIn;
69716971
input BackendDAE.Variables varsIn;
@@ -6994,14 +6994,17 @@ algorithm
69946994
// print("VAR: "+BackendDump.varString(var)+" -->");
69956995
if BackendVariable.varHasBindExp(var) /*and Expression.isConst(BackendVariable.varBindExp(var))*/ then
69966996
exp = BackendVariable.varBindExp(var);
6997-
exp = replaceCrefWithStartValue(exp,varsIn);
6997+
if 0 <> Expression.compare(exp, expIn) then
6998+
// Or should this be an error? Replacing the start-value by itself...
6999+
exp = replaceCrefWithStartValue(exp,varsIn);
7000+
end if;
69987001
elseif BackendVariable.varHasStartValue(var) then
69997002
exp = BackendVariable.varStartValue(var);
70007003
else
70017004
exp = expIn;
70027005
end if;
70037006
//print(" has START:"+ ExpressionDump.printExpStr(exp)+"\n");
7004-
true = Expression.isConst(exp);
7007+
exp = if Expression.isConst(exp) then exp else expIn;
70057008
then (exp,varsIn);
70067009

70077010
case(DAE.CALL(path=Absyn.IDENT("sample"), expLst=expLst),_)

0 commit comments

Comments
 (0)