﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
3599	fmiInitialize crash - stack alignment assumption incorrect	michael.kobierski@…	Adrian Pop	"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
"	defect	closed	high	1.11.0	FMI		fixed		Adrian Pop
