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

Commit 5163543

Browse files
adrpoOpenModelica-Hudson
authored andcommitted
fix crashes in omccAPI if threadData is allocated in the heap
- hide threadData again - set threadData and then copy it after each call Belonging to [master]: - #2216
1 parent 69b2c90 commit 5163543

File tree

6 files changed

+104
-133
lines changed

6 files changed

+104
-133
lines changed

SimulationRuntime/c/meta/meta_modelica_data.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ void mmc_catch_dummy_fn();
250250
#define MMC_ELSE() } else { threadData->mmc_jumper = old_jumper;
251251

252252
#define MMC_TRY_TOP() { threadData_t threadDataOnStack = {0}, *oldThreadData = (threadData_t*)pthread_getspecific(mmc_thread_data_key),*threadData = &threadDataOnStack; pthread_setspecific(mmc_thread_data_key,threadData); pthread_mutex_init(&threadData->parentMutex,NULL); mmc_init_stackoverflow(threadData); MMC_TRY_INTERNAL(mmc_jumper) threadData->mmc_stack_overflow_jumper = threadData->mmc_jumper; /* Let the default stack overflow handler be the top-level handler */
253+
254+
#define MMC_TRY_TOP_SET(X) { threadData_t threadDataOnStack = *((threadData_t*)X), *oldThreadData = (threadData_t*)pthread_getspecific(mmc_thread_data_key),*threadData = &threadDataOnStack; pthread_setspecific(mmc_thread_data_key,threadData); pthread_mutex_init(&threadData->parentMutex,NULL); mmc_init_stackoverflow(threadData); MMC_TRY_INTERNAL(mmc_jumper) threadData->mmc_stack_overflow_jumper = threadData->mmc_jumper; /* Let the default stack overflow handler be the top-level handler */
255+
253256
#define MMC_TRY_TOP_INTERNAL() { threadData_t *oldThreadData = (threadData_t*)pthread_getspecific(mmc_thread_data_key); pthread_setspecific(mmc_thread_data_key,threadData); pthread_mutex_init(&threadData->parentMutex,NULL); mmc_init_stackoverflow(threadData); MMC_TRY_INTERNAL(mmc_jumper) threadData->mmc_stack_overflow_jumper = threadData->mmc_jumper;
254257
#define MMC_CATCH_TOP(X) pthread_setspecific(mmc_thread_data_key,oldThreadData); } else {pthread_setspecific(mmc_thread_data_key,oldThreadData);X;}}}
255258

SimulationRuntime/cpp/Core/ReduceDAE/com/ModelicaCompiler.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _model(model)
3333
std::cout << "Intialize OMC, use gcc" << std::endl;
3434

3535

36-
status = InitOMC(&_omcPtr,"gcc",omhome, 1);
36+
status = InitOMC(&_omcPtr,"gcc",omhome);
3737
if(status > 0)
3838
{
3939
std::cout << "..ok" << std::endl;
@@ -43,7 +43,7 @@ _model(model)
4343
{
4444
std::cout << "..failed" << std::endl;
4545
char* errorMsg = 0;
46-
status = GetError(&_omcPtr, &errorMsg);
46+
status = GetError(_omcPtr, &errorMsg);
4747
string errorMsgStr(errorMsg);
4848
throw std::runtime_error("error to intialize OMC: " + errorMsgStr);
4949
}
@@ -52,7 +52,7 @@ _model(model)
5252

5353
ModelicaCompiler::~ModelicaCompiler(void)
5454
{
55-
55+
FreeOMC(_omcPtr);
5656
}
5757

5858
void ModelicaCompiler::loadFile(bool load_file)
@@ -65,7 +65,7 @@ void ModelicaCompiler::loadFile(bool load_file)
6565
{
6666
//command="loadModel(Modelica)";
6767
command="loadModel(Modelica,{\"3.2.2\"})";
68-
status=SendCommand(&_omcPtr,command.c_str(),&result);
68+
status=SendCommand(_omcPtr,command.c_str(),&result);
6969
if(status>0)
7070
cout<<"load Modelica Library : "<< result<<std::endl;
7171
else
@@ -82,7 +82,7 @@ void ModelicaCompiler::loadFile(bool load_file)
8282
{
8383
//command="loadModel(Modelica)";
8484
command="loadModel(Modelica,{\"3.2.2\"})";
85-
status=SendCommand(&_omcPtr,command.c_str(),&result);
85+
status=SendCommand(_omcPtr,command.c_str(),&result);
8686

8787
if(status>0)
8888
cout<<"load Modelica Library : "<< result<<std::endl;
@@ -101,7 +101,7 @@ void ModelicaCompiler::loadFile()
101101
char* result =0;
102102

103103
command="loadModel(Modelica,{\"3.2.2\"})";
104-
status = SendCommand(&_omcPtr, command.c_str(), &result);
104+
status = SendCommand(_omcPtr, command.c_str(), &result);
105105

106106
if(status>0)
107107
cout<<"load Modelica Library : "<< result<<std::endl;
@@ -131,7 +131,7 @@ void ModelicaCompiler::loadFile()
131131

132132
cout << "PackageName : " << str << std::endl;
133133

134-
status=LoadFile(&_omcPtr,fileToLoad);
134+
status=LoadFile(_omcPtr,fileToLoad);
135135
//status=LoadFile(_omcPtr,"package.mo");
136136

137137
if(status>0)
@@ -141,7 +141,7 @@ void ModelicaCompiler::loadFile()
141141

142142
std::cout << "failed to load "<<str << std::endl;
143143
char* errorMsg=0;
144-
status = GetError(&_omcPtr,&errorMsg);
144+
status = GetError(_omcPtr,&errorMsg);
145145
string errorMsgStr(errorMsg);
146146
throw std::runtime_error("error while loading file: " + str+" " + errorMsgStr);
147147
}
@@ -169,15 +169,15 @@ void ModelicaCompiler::generateLabeledSimCode(string reduction_method)
169169
//command.append(s);
170170
command.append(")");
171171
cout << command << std::endl;
172-
int status = SendCommand(&_omcPtr, command.c_str(), &result);
172+
int status = SendCommand(_omcPtr, command.c_str(), &result);
173173
if(status>0)
174174
cout<<"generated labeled simcode for: "<<_model<<" "<< result<<std::endl;
175175
else
176176
{
177177

178178
std::cout << "..failed" << std::endl;
179179
char* errorMsg=0;
180-
status = GetError(&_omcPtr, &errorMsg);
180+
status = GetError(_omcPtr, &errorMsg);
181181

182182
throw std::runtime_error("error while executing " + command+ " with error " + errorMsg);
183183
}
@@ -192,7 +192,7 @@ void ModelicaCompiler::generateReferenceSolution()
192192
command.append(_model);
193193
command.append(_model);
194194
command.append(")");
195-
int status = SendCommand(&_omcPtr, command.c_str(), &result);
195+
int status = SendCommand(_omcPtr, command.c_str(), &result);
196196

197197
if(status>0)
198198
cout<<"generated reference solution for: "<<_model<<" "<< result<<std::endl;
@@ -201,7 +201,7 @@ void ModelicaCompiler::generateReferenceSolution()
201201

202202
std::cout << "..failed" << std::endl;
203203
char* errorMsg = 0;
204-
status = GetError(&_omcPtr, &errorMsg);
204+
status = GetError(_omcPtr, &errorMsg);
205205

206206
throw std::runtime_error("error while executing " + command+ " with error " + errorMsg);
207207
}
@@ -243,15 +243,15 @@ void ModelicaCompiler::reduceTerms(std::vector<unsigned int>& labels, double sta
243243
//command.append(")");
244244

245245
cout<<command<<std::endl;
246-
status = SendCommand(&_omcPtr, command.c_str(), &result);
246+
status = SendCommand(_omcPtr, command.c_str(), &result);
247247
if(status>0)
248248
cout<<"reduceTerms for: "<<_model<<" "<< result<<std::endl;
249249
else
250250
{
251251

252252
std::cout << "..failed" << std::endl;
253253
char* errorMsg = 0;
254-
status = GetError(&_omcPtr, &errorMsg);
254+
status = GetError(_omcPtr, &errorMsg);
255255

256256
throw std::runtime_error("error while executing " + command+ " with error " + errorMsg);
257257
}
@@ -271,15 +271,15 @@ void ModelicaCompiler::reduceTerms(std::vector<unsigned int>& labels, double sta
271271
command.append(",startTime=0.0,stopTime = 20.0)");
272272
273273
cout<<command<<std::endl;
274-
status=SendCommand(&_omcPtr,command.c_str(),&result);
274+
status=SendCommand(_omcPtr,command.c_str(),&result);
275275
if(status>0)
276276
cout<<"simulation for reduced model: "<<_model<<" "<< result<<std::endl;
277277
else
278278
{
279279
280280
std::cout << "simulation for reduced model failed" << std::endl;
281281
char* errorMsg=0;
282-
status = GetError(&_omcPtr, &errorMsg);
282+
status = GetError(_omcPtr, &errorMsg);
283283
284284
throw std::runtime_error("error while executing " + command+ " with error " + errorMsg);
285285
}

SimulationRuntime/cpp/Include/Core/ReduceDAE/com/ModelicaCompiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class ModelicaCompiler
1717
string _model;
1818
string _model_path;
1919
string _file_name;
20-
OMCData _omcPtr;
20+
OMCData* _omcPtr;
2121
bool _load_package;
2222
};

SimulationRuntime/cpp/omcCAPI/include/OMC.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,18 @@
66

77
#include "OMCAPI.h"
88

9-
/**
10-
Complete definition for OMCData
11-
*/
12-
OMC_DLL typedef struct OMCData
13-
{
14-
OMCData(void *threadData);
15-
~OMCData();
16-
void *threadData;
17-
} data;
18-
19-
209
extern "C"
2110
{
11+
OMC_DLL typedef struct OMCData data;
12+
2213
void OMC_DLL InitMetaOMC();
2314
/**
2415
* \brief Allocates and initializes OpenModelica compiler(omc) instance
2516
* \param [in] compiler name of c++ compiler (gcc for mingw compiler, msvc10 for Visual Studio 2010 compiler,msvc12 for Visual Studio 2012 compiler ...)
2617
* \param [out] omcPtr pointer to allocated omc instance
2718
* \return returns a status flag
2819
*/
29-
int OMC_DLL InitOMC(data* omcData, const char* compiler, const char* openModelicaHome, int initThreadData = 1);
20+
int OMC_DLL InitOMC(data** omcDataPtr, const char* compiler, const char* openModelicaHome);
3021
/**
3122
* \brief returns the version of the OpenModelica compiler (omc) instance
3223
* \param [in] omcPtr Pointer to omc instance

0 commit comments

Comments
 (0)