modelon-community/fmi-library

The segmentation fault occurs in the fmi2_SI_base_unit_exp_to_string function.

Closed this issue · 10 comments

When using the fmi2_import_instantiate function, a segmentation fault occurs within the fmi2_SI_base_unit_exp_to_string function.

Can you provide us with some more information to reproduce the issue?

this is information
const fmi2_string_t instanceName = "MyModelInstance";
const fmi2_type_t fmuType = fmi2_cosimulation;
const fmi2_string_t fmuResourceLocation = NULL;
const fmi2_boolean_t visible = fmi2_false;

    jm_status_enu_t result = fmi2_import_instantiate(fmu2, instanceName, fmuType, fmuResourceLocation, visible);

The above implementation encountered a segmentation fault at the fmi2_SI_base_unit_exp_to_string function when debugging the program at the fmi2_import_instantiate step.

It is possible to share more information, e.g., a full backtrace from the segmentation fault?

It seems very odd that fmi2_SI_base_unit_exp_to_string would cause a segfault inside a call of fmi2_import_instantiate, since there is no direct connection, unless via callbacks.

After using the latest version of fmilibrary, when I use the fmi2_import_instantiate function, the parameter fmi2_import_t* fmu has a value. However, when it runs to the fmi2_component_t fmi2_capi_instantiate(fmi2_capi_t* fmu, fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuGUID, fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible, fmi2_boolean_t loggingOn) function, the value of fmu becomes 0x0.

Can you provide a full call sequence to provide context on what you found thus far? I cannot reliably follow what you are doing with the information provided.

One possible option that I am seeing is that your fmu returned null on the fmi2Instantiate call.

step1:use fmi2_import_instantiate(this step fmu have value)
const fmi2_string_t instanceName = "MyModelInstance";
const fmi2_type_t fmuType = fmi2_cosimulation;
const fmi2_string_t fmuResourceLocation = NULL;
const fmi2_boolean_t visible = fmi2_false;

    jm_status_enu_t result = fmi2_import_instantiate(fmu2, instanceName, fmuType, fmuResourceLocation, visible);

step2: in fmi2_import_instantiate(this step fmu have value)
jm_status_enu_t fmi2_import_instantiate(fmi2_import_t* fmu,
fmi2_string_t instanceName, fmi2_type_t fmuType,
fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible) {
fmi2_string_t fmuGUID = fmi2_import_get_GUID(fmu);
fmi2_boolean_t loggingOn = (fmu->callbacks->log_level > jm_log_level_nothing);
fmi2_component_t c;
if(!fmuResourceLocation)
fmuResourceLocation = fmu->resourceLocation;
c = fmi2_capi_instantiate(fmu -> capi, instanceName, fmuType, fmuGUID,
fmuResourceLocation, visible, loggingOn);
if (c == NULL) {
return jm_status_error;
} else {
return jm_status_success;
}
}
step3:in fmi2_capi_instantiate(this step fmu is 0x0)
fmi2_component_t fmi2_capi_instantiate(fmi2_capi_t* fmu,
fmi2_string_t instanceName, fmi2_type_t fmuType, fmi2_string_t fmuGUID,
fmi2_string_t fmuResourceLocation, fmi2_boolean_t visible,
fmi2_boolean_t loggingOn)
{
return fmu->c = fmu->fmi2Instantiate(instanceName, fmuType, fmuGUID,
fmuResourceLocation, &fmu->callBackFunctions, visible, loggingOn);
}
in this function fmu is 0x0;

What are the steps before this?

I'm assuming fmu2 = fmi2_import_parse_xml(...); but it sounds like you might be missing a call to fmi2_import_create_dllfmu?

You can also check fmi2_import_cs_test.c for reference on all the steps involved in loading a FMI2 CS FMU.

before is
step1:
fmi_import_context_t * context = fmi_import_allocate_context(&callbacks);
step2:
fmi_version_enu_t version = fmi_import_get_fmi_version(context, Filename, tempPath);
step3:
fmi2_import_t * fmu2 = fmi2_import_parse_xml(context, tempPath, NULL);

You seem to be missing an fmi2_import_create_dllfmu call, e.g., fmi2_import_create_dllfmu(fmu, fmi2_fmu_kind_cs, NULL) after fmi2_import_parse_xml(...) and before fmi2_import_instantiate(...).

Can you try and report back?

Edit: If this is the issue, there is a fix incoming: #119

With that fix, you should get an error rather than a segmentation fault.

I solved the problem by using fmi2_import_create_dllfmu. Thank you for your help.