Skip to content

Commit

Permalink
Fix memory corruption in ZeroMQ init
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2144
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Feb 1, 2018
1 parent 8b177b9 commit 38212aa
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Compiler/Main/Main.mo
Expand Up @@ -628,7 +628,7 @@ protected
Boolean b;
String str,replystr;
algorithm
zmqSocket := ZeroMQ.initialize();
zmqSocket := ZeroMQ.initialize(Flags.getConfigString(Flags.ZEROMQ_FILE_SUFFIX));
false := valueEq(SOME(0), zmqSocket);
while true loop
str := ZeroMQ.handleRequest(zmqSocket);
Expand Down
9 changes: 0 additions & 9 deletions Compiler/Util/Flags.mo
Expand Up @@ -2307,15 +2307,6 @@ algorithm
then
();

// The zeroMQ file suffix needs to be sent to the C runtime.
case (_, _)
equation
true = configFlagsIsEqualIndex(inFlag, ZEROMQ_FILE_SUFFIX);
STRING_FLAG(data = zeroMQFileSuffix) = inValue;
ZeroMQ.setFileSuffix(zeroMQFileSuffix);
then
();

else ();
end matchcontinue;
end applySideEffects;
Expand Down
9 changes: 2 additions & 7 deletions Compiler/Util/ZeroMQ.mo
Expand Up @@ -39,16 +39,11 @@ encapsulated package ZeroMQ
Used in interactive mode if omc is started with -d=interactiveZMQ
Implemented in ./runtime/zeromqimpl.c"

public function setFileSuffix
input String fileSuffix;

external "C" ZeroMQ_setFileSuffix(fileSuffix) annotation(Library = "omcruntime");
end setFileSuffix;

public function initialize
input String fileSuffix="";
output Option<Integer> zmqSocket;

external "C" zmqSocket = ZeroMQ_initialize() annotation(Library = "omcruntime");
external "C" zmqSocket = ZeroMQ_initialize(fileSuffix) annotation(Library = "omcruntime");
end initialize;

public function handleRequest
Expand Down
34 changes: 5 additions & 29 deletions Compiler/runtime/zeromqimpl.c
Expand Up @@ -38,21 +38,8 @@
#include "settingsimpl.h"

char* zeroMQFilePath = 0;
char* zeroMQFileSuffix = 0;

void ZeroMQ_setFileSuffix(const char *fileSuffix)
{
if (strlen(fileSuffix) == 0) return;
if (zeroMQFileSuffix) free(zeroMQFileSuffix);

if (*fileSuffix) {
zeroMQFileSuffix = strdup(fileSuffix);
} else {
zeroMQFileSuffix = NULL;
}
}

void* ZeroMQ_initialize()
void* ZeroMQ_initialize(const char *zeroMQFileSuffix)
{
// Create a pointer for storing the ZeroMQ socket
void *mmcZmqSocket = mmc_mk_some(0);
Expand All @@ -71,22 +58,12 @@ void* ZeroMQ_initialize()
// create the file path
const char* tempPath = SettingsImpl__getTempDirectoryPath();
#if defined(__MINGW32__) || defined(_MSC_VER)
if (zeroMQFileSuffix != NULL) {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port.") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port.%s", tempPath, zeroMQFileSuffix);
} else {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port") + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port", tempPath);
}
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.port") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.port%s", tempPath, zeroMQFileSuffix);
#else
char *tmp_user = getenv("USER");
if (zeroMQFileSuffix != NULL) {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + (tmp_user ? strlen(tmp_user) : strlen("nobody")) + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port.%s", tempPath, tmp_user ? tmp_user : "nobody", zeroMQFileSuffix);
} else {
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica.") + (tmp_user ? strlen(tmp_user) : strlen("nobody")) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port", tempPath, tmp_user ? tmp_user : "nobody");
}
zeroMQFilePath = (char*)malloc(strlen(tempPath) + strlen("/openmodelica..port.") + strlen(tmp_user ? tmp_user : "nobody") + strlen(zeroMQFileSuffix) + 1);
sprintf(zeroMQFilePath, "%s/openmodelica.%s.port.%s", tempPath, tmp_user ? tmp_user : "nobody", zeroMQFileSuffix);
#endif
// Create the file with port number
FILE *fp;
Expand Down Expand Up @@ -143,7 +120,6 @@ void ZeroMQ_close(void *mmcZmqSocket)
remove(zeroMQFilePath);
free(zeroMQFilePath);
}
if (zeroMQFileSuffix) free(zeroMQFileSuffix);
// Convert the void* to ZeroMQ Socket
intptr_t zmqSocket = (intptr_t)MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(mmcZmqSocket),1));
// close the ZeroMQ socket
Expand Down

0 comments on commit 38212aa

Please sign in to comment.