Opened 9 years ago

Closed 7 years ago

#3599 closed defect (fixed)

fmiInitialize crash - stack alignment assumption incorrect

Reported by: michael.kobierski@… Owned by: Adrian Pop
Priority: high Milestone: 1.11.0
Component: FMI Version:
Keywords: Cc: Adrian Pop

Description

The version of MinGW shipped with OpenModelica 1.9.3 contains gcc version 4.4.0, which causes problems when SIMD instructions are generated in functions which can be called by an external application with different stack alignment constraints.

Specifically, for some models, calling a function in a (MinGW compiled) FMU dll from a MSVC compiled executable results in a crash inside fmiInitialize. The offending function:

int getAnalyticalJacobianLapack(DATA* data, threadData_t *threadData, double* jac, int sysNumber)
{
  int i,j,k,l,ii,currentSys = sysNumber;
  LINEAR_SYSTEM_DATA* systemData = &(((DATA*)data)->simulationInfo.linearSystemData[currentSys]);

  const int index = systemData->jacobianIndex;

  memset(jac, 0, (systemData->size)*(systemData->size)*sizeof(double));

  for(i=0; i < data->simulationInfo.analyticJacobians[index].sparsePattern.maxColors; i++)
  {



    /* activate seed variable for the corresponding color */
    for(ii=0; ii < data->simulationInfo.analyticJacobians[index].sizeCols; ii++)
      if(data->simulationInfo.analyticJacobians[index].sparsePattern.colorCols[ii]-1 == i)
        data->simulationInfo.analyticJacobians[index].seedVars[ii] = 1;

    // CRASH OCCURS JUST BEFORE THIS CALL
    ((systemData->analyticalJacobianColumn))(data, threadData);
/*
Disassembly of call:
movsd       xmm2,mmword ptr ds:[6B78F6C8h]  
movsd       xmm1,mmword ptr ds:[6B78F6D0h]  
...
mov         edx,dword ptr [ebp+0Ch]  
mov         dword ptr [esp+4],edx  
mov         ecx,dword ptr [ebp+8]  
mov         dword ptr [esp],ecx  
movapd      xmmword ptr [ebp-68h],xmm1 <-- This instruction assumes 16 byte alignment
movsd       mmword ptr [ebp-58h],xmm2  
mov         edi,dword ptr [ebp-40h]  
call        dword ptr [edi+10h]  
*/

...
}

The reason: GCC assumes 16 byte alignment of the stack pointer upon entry to a new frame, but MSVC only aligns to 4 bytes. Thus, the crash will not always occur, but will depend on how your local variables are organized in the calling (MSVC) code. Super obscure.

According to this article, the bug has been fixed in versions of GCC 4.5 and greater.

http://eigen.tuxfamily.org/dox/group__TopicWrongStackAlignment.html

Change History (9)

comment:1 by michael.kobierski@…, 9 years ago

Component: UnknownFMI
Owner: changed from somebody to Adeel Asghar

comment:2 by michael.kobierski@…, 9 years ago

Hello, to follow up, is there a solution for this defect?

comment:3 by Adeel Asghar, 9 years ago

Cc: Adrian Pop added
Owner: changed from Adeel Asghar to Willi Braun
Status: newassigned

comment:4 by Willi Braun, 9 years ago

As far as I understand that Bug was in OM 1.9.3, because of using gcc 4.4, but in the current versions we switch to newer gcc so this Bug is not really relevant any more.
Or do we just need to change the line

data->simulationInfo.analyticJacobians[index].seedVars[ii] = 1;

to

data->simulationInfo.analyticJacobians[index].seedVars[ii] = 1.0;
}}}.

comment:5 by Adrian Pop, 9 years ago

We will update the gcc inside OpenModelica windows installation from gcc 4.4 to gcc 5.2 very soon. That will hopefully get rid of the issue.

comment:6 by Adrian Pop, 9 years ago

Owner: changed from Willi Braun to Adrian Pop
Status: assignedaccepted

comment:7 by michael.kobierski@…, 8 years ago

Hello, just to follow up, I have noticed that v1.9.6 still uses GCC 4.4. Can I ask an rough estimate for when an updated version will be packaged?

comment:8 by Adrian Pop, 8 years ago

You can use the nightly builds already:
https://build.openmodelica.org/omc/builds/windows/nightly-builds/
note: if you want 32 bit FMUs install 32 bit, if you want 64 bit FMUs install 64 bit

comment:9 by Martin Sjölund, 7 years ago

Milestone: Future1.11.0
Resolution: fixed
Status: acceptedclosed

I assume this was fixed in 1.11.0 and will close this. Re-open if the issue remains.

Note: See TracTickets for help on using tickets.