ThummeTo / FMI.jl

FMI.jl is a free-to-use software library for the Julia programming language which integrates FMI (fmi-standard.org): load or create, parameterize, differentiate and simulate FMUs seamlessly inside the Julia programming language!
MIT License
86 stars 24 forks source link

Segmentation fault after assert #212

Open AnHeuermann opened 11 months ago

AnHeuermann commented 11 months ago

Issue

I added regression tests for OpenModelica and they are testing some OpenModelica FMUs with FMI.jl.

For model Modelica.Fluid.Examples.PumpingSystem FMI.jl is crashing when simulating the generated FMU and an assert is encountered.

OMSimulator is triggering the assert as well, but finishes the simulation with success.

How to reproduce

using FMI
using OMJulia

omc = OMJulia.OMCSession()
mkpath("temp")
OMJulia.API.cd(omc, "temp")

OMJulia.API.loadModel(omc, "Modelica"; priorityVersion = ["4.0.0"], requireExactVersion = true)
fmuPath = OMJulia.API.buildModelFMU(omc, "Modelica.Fluid.Examples.PumpingSystem")
OMJulia.quit(omc)

#fmuPath = joinpath("temp","Modelica.Fluid.Examples.PumpingSystem.fmu")
fmu = FMI.fmiLoad(fmuPath)
solution = FMI.fmiSimulate(fmu; showProgress=false)
LOG_ASSERT        | warning | [C:/Users/USERNAME/AppData/Roaming/.openmodelica/libraries/Modelica 4.0.0+maint.om/Fluid/Interfaces.mo:1027:9-1032:39:writable]
|                 | |       | The following assertion has been violated at time 0.000000
|                 | |       | ((pipe.flowModel.m_flows[1] >= 0.0 and pipe.flowModel.m_flows[1] <= 1e5)) --> "Variable violating min/max constraint: 0.0 <= pipe.flowModel.m_flows[1] <= 1e5, has value: -141.424"

Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x7fffa3977f7f -- setjmpex at C:\WINDOWS\System32\msvcrt.dll (unknown line)
in expression starting at D:\path\to\test.jl:14
setjmpex at C:\WINDOWS\System32\msvcrt.dll (unknown line)
omc_assert_fmi at D:/path/to/temp/FMU/sources/fmi-export\fmu2_model_interface.c.inc:228

Windows FMU with debug symbols (remove the .zip extension): Modelica.Fluid.Examples.PumpingSystem.fmu.zip

Error log: error.log

Versions 'n stuff

AnHeuermann commented 11 months ago

I'm not sure what exactly the issue is, but I guess it has something to do with the long jump function used when throwing an assert in the C code of the FMU:

static void omc_assert_fmi(threadData_t *threadData, FILE_INFO info, const char *msg, ...)
{
  va_list args;
  va_start(args, msg);
  omc_assert_fmi_common(threadData, fmi2Error, LOG_STATUSERROR, info, msg, args);
  va_end(args);
  MMC_THROW_INTERNAL(); // <---- EXCEPTION_ACCESS_VIOLATION
}
#define MMC_THROW_INTERNAL() {longjmp(*threadData->mmc_jumper,1);}

The issue happens on Windows and Linux, so I rule out some compatibility issues between the different C runtimes on Windows.

On Ubuntu the call stack looks like:

libc.so.6!__libc_siglongjmp(struct __jmp_buf_tag * env, int val) (longjmp.c:32)
Modelica_Fluid_Examples_PumpingSystem.so!omc_assert_fmi(threadData_t * threadData, FILE_INFO info, const char * msg) (\path\to\temp\FMU\sources\fmi-export\fmu2_model_interface.c.inc:228)
Modelica_Fluid_Examples_PumpingSystem.so!omc_Modelica_Media_Water_IF97__Utilities_BaseIF97_Basic_tsat(threadData_t * threadData, modelica_real _p) (\path\to\temp\FMU\sources\Modelica_Fluid_Examples_PumpingSystem_functions.c:13630)
[...]