From ddd5cc1a737281f52f7acbda545623b7c3e8435c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 16 Nov 2023 15:53:23 -0500 Subject: [PATCH] avoid segfault on command errors in force style unit tests and print error mesage instead --- unittest/force-styles/test_angle_style.cpp | 19 ++++++++++++++-- unittest/force-styles/test_bond_style.cpp | 19 ++++++++++++++-- unittest/force-styles/test_dihedral_style.cpp | 19 ++++++++++++++-- unittest/force-styles/test_improper_style.cpp | 19 ++++++++++++++-- unittest/force-styles/test_pair_style.cpp | 22 ++++++++++++++++--- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/unittest/force-styles/test_angle_style.cpp b/unittest/force-styles/test_angle_style.cpp index bd0e3d8859..3476ae8dde 100644 --- a/unittest/force-styles/test_angle_style.cpp +++ b/unittest/force-styles/test_angle_style.cpp @@ -26,6 +26,7 @@ #include "angle.h" #include "atom.h" #include "compute.h" +#include "exceptions.h" #include "fmt/format.h" #include "force.h" #include "info.h" @@ -59,7 +60,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) delete lmp; } -LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newton = true) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true) { LAMMPS *lmp; @@ -90,7 +91,21 @@ LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newto // utility lambdas to improve readability auto command = [&](const std::string &line) { - lmp->input->one(line); + try { + lmp->input->one(line); + } catch (LAMMPSAbortException &ae) { + fprintf(stderr, "LAMMPS Error: %s\n", ae.what()); + exit(2); + } catch (LAMMPSException &e) { + fprintf(stderr, "LAMMPS Error: %s\n", e.what()); + exit(3); + } catch (fmt::format_error &fe) { + fprintf(stderr, "fmt::format_error: %s\n", fe.what()); + exit(4); + } catch (std::exception &e) { + fprintf(stderr, "General exception: %s\n", e.what()); + exit(5); + } }; auto parse_input_script = [&](const std::string &filename) { lmp->input->file(filename.c_str()); diff --git a/unittest/force-styles/test_bond_style.cpp b/unittest/force-styles/test_bond_style.cpp index aa99f41f8d..f7ecd835b0 100644 --- a/unittest/force-styles/test_bond_style.cpp +++ b/unittest/force-styles/test_bond_style.cpp @@ -26,6 +26,7 @@ #include "atom.h" #include "bond.h" #include "compute.h" +#include "exceptions.h" #include "fmt/format.h" #include "force.h" #include "info.h" @@ -59,7 +60,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) delete lmp; } -LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newton = true) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true) { LAMMPS *lmp; @@ -90,7 +91,21 @@ LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newto // utility lambdas to improve readability auto command = [&](const std::string &line) { - lmp->input->one(line); + try { + lmp->input->one(line); + } catch (LAMMPSAbortException &ae) { + fprintf(stderr, "LAMMPS Error: %s\n", ae.what()); + exit(2); + } catch (LAMMPSException &e) { + fprintf(stderr, "LAMMPS Error: %s\n", e.what()); + exit(3); + } catch (fmt::format_error &fe) { + fprintf(stderr, "fmt::format_error: %s\n", fe.what()); + exit(4); + } catch (std::exception &e) { + fprintf(stderr, "General exception: %s\n", e.what()); + exit(5); + } }; auto parse_input_script = [&](const std::string &filename) { lmp->input->file(filename.c_str()); diff --git a/unittest/force-styles/test_dihedral_style.cpp b/unittest/force-styles/test_dihedral_style.cpp index 25690fc33d..662d63909d 100644 --- a/unittest/force-styles/test_dihedral_style.cpp +++ b/unittest/force-styles/test_dihedral_style.cpp @@ -26,6 +26,7 @@ #include "atom.h" #include "compute.h" #include "dihedral.h" +#include "exceptions.h" #include "fmt/format.h" #include "force.h" #include "info.h" @@ -59,7 +60,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) delete lmp; } -LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newton = true) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true) { LAMMPS *lmp = new LAMMPS(args, MPI_COMM_WORLD); @@ -88,7 +89,21 @@ LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newto // utility lambdas to improve readability auto command = [&](const std::string &line) { - lmp->input->one(line); + try { + lmp->input->one(line); + } catch (LAMMPSAbortException &ae) { + fprintf(stderr, "LAMMPS Error: %s\n", ae.what()); + exit(2); + } catch (LAMMPSException &e) { + fprintf(stderr, "LAMMPS Error: %s\n", e.what()); + exit(3); + } catch (fmt::format_error &fe) { + fprintf(stderr, "fmt::format_error: %s\n", fe.what()); + exit(4); + } catch (std::exception &e) { + fprintf(stderr, "General exception: %s\n", e.what()); + exit(5); + } }; auto parse_input_script = [&](const std::string &filename) { lmp->input->file(filename.c_str()); diff --git a/unittest/force-styles/test_improper_style.cpp b/unittest/force-styles/test_improper_style.cpp index b4096df868..dc1b846b5a 100644 --- a/unittest/force-styles/test_improper_style.cpp +++ b/unittest/force-styles/test_improper_style.cpp @@ -25,6 +25,7 @@ #include "atom.h" #include "compute.h" +#include "exceptions.h" #include "fmt/format.h" #include "force.h" #include "improper.h" @@ -59,7 +60,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) delete lmp; } -LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newton = true) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true) { LAMMPS *lmp; @@ -90,7 +91,21 @@ LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newto // utility lambdas to improve readability auto command = [&](const std::string &line) { - lmp->input->one(line); + try { + lmp->input->one(line); + } catch (LAMMPSAbortException &ae) { + fprintf(stderr, "LAMMPS Error: %s\n", ae.what()); + exit(2); + } catch (LAMMPSException &e) { + fprintf(stderr, "LAMMPS Error: %s\n", e.what()); + exit(3); + } catch (fmt::format_error &fe) { + fprintf(stderr, "fmt::format_error: %s\n", fe.what()); + exit(4); + } catch (std::exception &e) { + fprintf(stderr, "General exception: %s\n", e.what()); + exit(5); + } }; auto parse_input_script = [&](const std::string &filename) { lmp->input->file(filename.c_str()); diff --git a/unittest/force-styles/test_pair_style.cpp b/unittest/force-styles/test_pair_style.cpp index 8ad2ce9aaa..9db9c7ac8b 100644 --- a/unittest/force-styles/test_pair_style.cpp +++ b/unittest/force-styles/test_pair_style.cpp @@ -26,6 +26,7 @@ #include "atom.h" #include "compute.h" #include "domain.h" +#include "exceptions.h" #include "force.h" #include "info.h" #include "input.h" @@ -61,7 +62,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) delete lmp; } -LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newton = true) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true) { LAMMPS *lmp; @@ -92,8 +93,23 @@ LAMMPS *init_lammps(LAMMPS::argv & args, const TestConfig &cfg, const bool newto // utility lambdas to improve readability auto command = [&](const std::string &line) { - lmp->input->one(line); + try { + lmp->input->one(line); + } catch (LAMMPSAbortException &ae) { + fprintf(stderr, "LAMMPS Error: %s\n", ae.what()); + exit(2); + } catch (LAMMPSException &e) { + fprintf(stderr, "LAMMPS Error: %s\n", e.what()); + exit(3); + } catch (fmt::format_error &fe) { + fprintf(stderr, "fmt::format_error: %s\n", fe.what()); + exit(4); + } catch (std::exception &e) { + fprintf(stderr, "General exception: %s\n", e.what()); + exit(5); + } }; + auto parse_input_script = [&](const std::string &filename) { lmp->input->file(filename.c_str()); }; @@ -760,7 +776,7 @@ TEST(PairStyle, gpu) "screen", "-nocite", "-sf", "gpu"}; LAMMPS::argv args_noneigh = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite", "-sf", "gpu", "-pk", "gpu", "0", "neigh", "no"}; - LAMMPS::argv args = args_neigh; + LAMMPS::argv args = args_neigh; // cannot use GPU neighbor list with hybrid pair style (yet) if (test_config.pair_style.substr(0, 6) == "hybrid") {