Skip to content

[Code scan] Broadcast MD restart bool flags with the correct MPI datatype #7547

Description

@njzjz

This issue is a result of a Codex global repository scan.

FIRE::restart() and MSST::restart() store the restart-file status in a C++ bool ok, then broadcast it as one MPI_INT. On receivers, MPI writes an integer-sized payload into a bool-sized object, which can corrupt adjacent stack state and is undefined behavior.

FIRE restart:

void FIRE::restart(const std::string& global_readin_dir)
{
bool ok = true;
if (!my_rank)
{
std::stringstream ssc;
ssc << global_readin_dir << "Restart_md.txt";
std::ifstream file(ssc.str().c_str());
if (!file)
{
ok = false;
}
if (ok)
{
file >> step_rst_ >> md_tfirst >> alpha >> negative_count >> dt_max >> md_dt;
file.close();
}
}
#ifdef __MPI
MPI_Bcast(&ok, 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif

MSST restart:

void MSST::restart(const std::string& global_readin_dir)
{
bool ok = true;
if (!my_rank)
{
std::stringstream ssc;
ssc << global_readin_dir << "Restart_md.txt";
std::ifstream file(ssc.str().c_str());
if (!file)
{
ok = false;
}
if (ok)
{
file >> step_rst_ >> md_tfirst >> omega[mdp.msst_direction] >> e0 >> v0 >> p0 >> lag_pos;
file.close();
}
}
#ifdef __MPI
MPI_Bcast(&ok, 1, MPI_INT, 0, MPI_COMM_WORLD);

Relevant code:

bool ok = true;
...
MPI_Bcast(&ok, 1, MPI_INT, 0, MPI_COMM_WORLD);

Suggested fix:

Broadcast with MPI_C_BOOL when available, or broadcast an int ok_int and convert explicitly to/from bool. This also makes the code consistent with the actual object size on all ranks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions