Skip to content

Commit

Permalink
fix ticket #6396
Browse files Browse the repository at this point in the history
- export all symbols from libOpenModelicaCompiler.dll
- on Windows/mingw, if the function pointer cannot be found, search all loaded dlls in the current process
- add more tests (ffi, meta) to sanity checking for the CI/MinGW label
  • Loading branch information
adrpo committed Feb 25, 2021
1 parent 6d8ee8c commit 997610d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .CI/common.groovy
Expand Up @@ -201,8 +201,16 @@ void buildOMC(CC, CXX, extraFlags, Boolean buildCpp, Boolean clean) {
echo rm -rf ./M* ./OMCppM*
echo cd ..
echo rm -rf .sanity-check
echo echo Testing some models from testsuite, ffi, meta
echo cd testsuite/flattening/libraries/biochem
echo ../../../rtest --return-with-error-code EnzMM.mos
echo cd \${MSYS_WORKSPACE}
echo cd testsuite/flattening/modelica/ffi
echo ../../../rtest --return-with-error-code ModelicaInternal_countLines.mos
echo ../../../rtest --return-with-error-code Integer1.mos
echo cd \${MSYS_WORKSPACE}
echo cd testsuite/metamodelica/meta
echo ../../rtest --return-with-error-code AlgPatternm.mos
echo echo Testing if we can compile in a path with spaces
echo cd \${MSYS_WORKSPACE}
echo mkdir -p ./path\\ with\\ space/
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/boot/Makefile.omdev.mingw
Expand Up @@ -47,7 +47,7 @@ LDFLAGS=-L./ $(LOMPARSE) $(LCOMPILERRUNTIME) -L"$(OMHOME)/lib/omc" \
-lzmq \
$(OMENCRYPTIONLIBS) \
$(LDFLAGS_CURL) \
$(EXTRA_LD_FLAGS)
$(EXTRA_LD_FLAGS) -Wl,--export-all-symbols

FMILIB = -L$(TOP_DIR)/3rdParty/FMIL/install/lib -lfmilib
CPPFLAGS=-I"$(OMHOME)/include/omc/c" -I../Util/ -DADD_METARECORD_DEFINITIONS=
Expand Down
76 changes: 52 additions & 24 deletions OMCompiler/Compiler/runtime/systemimpl.c
Expand Up @@ -79,6 +79,7 @@ typedef void* iconv_t;

#if defined(__MINGW32__) || defined(_MSC_VER)
#include <rpc.h>
#include <psapi.h>
#define getFunctionPointerFromDLL GetProcAddress
#define FreeLibraryFromHandle !FreeLibrary

Expand Down Expand Up @@ -1325,8 +1326,8 @@ static const char* SystemImpl__getUUIDStr(void)
{
static char uuidStr[37] = "8c4e810f-3df3-4a00-8276-176fa3c9f9e0";
#if defined(__MINGW32__) || defined(_MSC_VER)
unsigned char *tmp;
UUID uuid;
unsigned char *tmp = NULL;
UUID uuid = {0};
if (UuidCreate(&uuid) == RPC_S_OK)
UuidToString(&uuid, &tmp);
tmp[36] = '\0';
Expand Down Expand Up @@ -1372,7 +1373,9 @@ int SystemImpl__loadLibrary(const char *str, int relativePath, int printDebug)
}

h = LoadLibrary(libname);
} else {
} else { /* no library name, fetch current module handle */
/* set the libname */
strcpy(libname, "[own process]");
h = GetModuleHandle(0);
}

Expand All @@ -1394,16 +1397,6 @@ int SystemImpl__loadLibrary(const char *str, int relativePath, int printDebug)
return -1;
}

/* adrpo, pass the mmc_GC_state pointer from the current process!
mmc_GC_set_state_lib_function = (mmc_GC_function_set_gc_state)getFunctionPointerFromDLL(h, "mmc_GC_set_state");
if (mmc_GC_set_state_lib_function == NULL) {
fprintf(stderr, "Unable to get pointer for mmc_GC_set_state in %s!\n", libname);
fflush(stderr);
return -1;
}
mmc_GC_set_state_lib_function(mmc_GC_state);
*/

libIndex = alloc_ptr();
if (libIndex < 0) {
//fprintf(stderr, "Error loading library %s!\n", libname); fflush(stderr);
Expand Down Expand Up @@ -1450,16 +1443,6 @@ int SystemImpl__loadLibrary(const char *str, int relativePath, int printDebug)
return -1;
}

/* adrpo, pass the mmc_GC_state pointer from the current process!
mmc_GC_set_state_lib_function = (mmc_GC_function_set_gc_state)getFunctionPointerFromDLL(h, "mmc_GC_set_state");
if (mmc_GC_set_state_lib_function == NULL) {
fprintf(stderr, "Unable to get pointer for mmc_GC_set_state in %s!\n", libname);
fflush(stderr);
return -1;
}
mmc_GC_set_state_lib_function(mmc_GC_state);
*/

libIndex = alloc_ptr();
if (libIndex < 0) {
fprintf(stderr, "Error loading library %s!\n", libname); fflush(stderr);
Expand Down Expand Up @@ -1535,19 +1518,64 @@ int SystemImpl__lookupFunction(int libIndex, const char *str)
modelica_ptr_t lib = NULL, func = NULL;
function_t funcptr;
int funcIndex;
long lastError = 0;

lib = lookup_ptr(libIndex);

if (lib == NULL)
return -1;

funcptr = (int (*)(threadData_t*, type_description*, type_description*)) getFunctionPointerFromDLL(lib->data.lib, str);
lastError = GetLastError();

#if defined(__MINGW32__) || defined(_MSC_VER)
/* windows is special :)
* if we didn't find the function, search it in all the loaded DLLs as Linux does
*/
if (funcptr == NULL) {
HMODULE hMods[1024];
HMODULE hProcess = GetCurrentProcess();
DWORD cbNeeded;
unsigned int i;
char szModName[MAX_PATH];

/* see if the GetModuleHandle(0) is in the current process and an executable */
if ( GetModuleFileNameEx( hProcess, lib->data.lib, szModName, sizeof(szModName) / sizeof(char)) )
{
// if strcmp(szModName[strlen(szModName)-4], ".exe")
if ( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded) )
{
for ( i = 0; i < (cbNeeded / sizeof(modelica_integer)); i++ )
{
funcptr = (int (*)(threadData_t*, type_description*, type_description*)) getFunctionPointerFromDLL(hMods[i], str);
if (funcptr != NULL)
{
break;
}

/* // uncomment for debugging
{
if ( GetModuleFileNameEx( hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(char)) )
{
fprintf(stderr, "function %s in %s handle:[%p][%d] funcptr[%p] GetLastError:%d\n", str, szModName, hMods[i], i, funcptr, GetLastError()); fflush(NULL);
}
}
*/
}
}
}
}
#endif

if (funcptr == NULL) {
const char* err_toks[2];
char id_buf[11];
snprintf(id_buf, 11, "%lu", GetLastError());
snprintf(id_buf, 11, "%lu", lastError);
#if defined(__MINGW32__) || defined(_MSC_VER)
err_toks[0] = id_buf;
#else
err_toks[0] = dlerror();
#endif
err_toks[1] = str;
c_add_message(NULL, -1, ErrorType_runtime, ErrorLevel_error, gettext("Unable to find `%s': %s.\n"), err_toks, 2);
return -1;
Expand Down

0 comments on commit 997610d

Please sign in to comment.