diff --git a/CMakeLists.txt b/CMakeLists.txt index 97c8cd4..2f891c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -386,6 +386,9 @@ if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN) message(FATAL_ERROR "catkin_add_gtest NOT FOUND") endif() + # Plugin giả cho ABI guard test: chỉ build cùng test, không install. + add_action_core_plugin(action_core_test_stale_abi_handler test/plugins/stale_abi_action_handler.cpp) + set(ACTION_CORE_TESTS action_registry_test noop_action_handler_test diff --git a/include/action_core/action_abi.h b/include/action_core/action_abi.h new file mode 100644 index 0000000..8f1b81b --- /dev/null +++ b/include/action_core/action_abi.h @@ -0,0 +1,48 @@ +/********************************************************************* + * + * Software License Agreement (BSD License) + * + * action_core — phiên bản ABI của biên plugin và macro export cho plugin .so. + * + * Author: DuongTD + *********************************************************************/ +#ifndef ACTION_CORE_ACTION_ABI_H_ +#define ACTION_CORE_ACTION_ABI_H_ + +#include + +namespace action_core +{ + +/** + * Phiên bản ABI của `ActionHandler` (vtable + layout struct đi qua biên .so). + * + * TĂNG mỗi lần đổi chữ ký/virtual của `ActionHandler`, `ActionTick`, `ActionContext`. Plugin build + * với header cũ nạp vào runtime mới thì lời gọi ảo nhảy vào slot sai — crash lúc chạy, không có + * compile error nào bắt được (boost::dll = dlsym + reinterpret_cast). `ActionRegistry` tra symbol + * `_abi` TRƯỚC factory và từ chối nạp khi lệch. + * + * 1 — trước 2026-09-11 (không có symbol; registry coi là "không khai"). + * 2 — 2026-09-11: thêm virtual `cancelNeedsTicks()`, `pause()`, `resume()`; `ActionStatus::kCancelled`. + */ +constexpr int kActionAbiVersion = 2; + +/// Hàm được export kèm mỗi plugin qua @ref ACTION_CORE_EXPORT_HANDLER; trả @ref kActionAbiVersion. +inline int actionAbiVersion() +{ + return kActionAbiVersion; +} + +} // namespace action_core + +/** + * @brief Export một handler cho boost::dll kèm phiên bản ABI. + * + * Thay cho `BOOST_DLL_ALIAS(Factory, Symbol)` thuần: sinh thêm symbol `_abi` trả + * @ref action_core::kActionAbiVersion để registry kiểm trước khi gọi factory. + */ +#define ACTION_CORE_EXPORT_HANDLER(factory, symbol) \ + BOOST_DLL_ALIAS(factory, symbol) \ + BOOST_DLL_ALIAS(action_core::actionAbiVersion, symbol##_abi) + +#endif // ACTION_CORE_ACTION_ABI_H_ diff --git a/include/action_core/action_handler.h b/include/action_core/action_handler.h index d7bcceb..c8ebba4 100644 --- a/include/action_core/action_handler.h +++ b/include/action_core/action_handler.h @@ -79,8 +79,40 @@ public: /// @brief Một control cycle. Chỉ được gọi sau khi @ref start trả true. virtual ActionTick update(const robot::Time& now) = 0; - /// @brief Yêu cầu dừng an toàn. Handler phải đưa thiết bị về trạng thái an toàn, không treo. + /** + * @brief Yêu cầu dừng an toàn. Handler phải đưa thiết bị về trạng thái an toàn, không treo. + * + * Hai kiểu handler (2026-09-11): + * - @ref cancelNeedsTicks trả false (mặc định; perception, no-op): cancel() làm xong việc ngay, + * caller quên handler, không tick nữa. + * - trả true (thiết bị: nâng kệ, sạc): cancel() chỉ RA LỆNH dừng; caller tiếp tục gọi + * @ref update cho tới khi handler trả `kCancelled` (hoặc kSucceeded/kFailed). Không được block + * trong cancel() — nó chạy trên control thread. + */ virtual void cancel() = 0; + + /// @brief Sau cancel() handler còn cần được tick tới khi trả terminal không. Xem @ref cancel. + virtual bool cancelNeedsTicks() const + { + return false; + } + + /** + * @brief Tạm dừng (2026-09-11). KHÔNG dừng thiết bị — pause là đóng băng, không huỷ. + * + * Caller ngừng gọi update() cho tới khi @ref resume. Handler có timeout riêng PHẢI ghi mốc để + * resume() dịch `started_at_`: nếu không, 20 s đứng chờ bị tính vào timeout dò 15 s và tick đầu + * sau resume là kFailed → ABORTED oan (lỗi có thật, xem move_base2/docs/PLAN_LIFECYCLE…). + * Default no-op — handler không có đồng hồ thì không cần gì. + */ + virtual void pause(const robot::Time& /*now*/) + { + } + + /// @brief Tiếp tục sau @ref pause: cộng quãng dừng vào mốc timeout. Default no-op. + virtual void resume(const robot::Time& /*now*/) + { + } }; } // namespace action_core diff --git a/include/action_core/action_types.h b/include/action_core/action_types.h index 990cd95..017eb04 100644 --- a/include/action_core/action_types.h +++ b/include/action_core/action_types.h @@ -18,15 +18,17 @@ namespace action_core * @enum ActionStatus * @brief Tiến trình của action đang chạy, trả về mỗi control cycle. * - * Không có `kCancelled`: huỷ đi qua @ref ActionHandler::cancel và kết thúc lượt chạy ngay tại đó, - * caller không tick tiếp nên không có kết quả nào để mang trạng thái đó. Cùng lý do mà - * `recovery_core::RecoveryStatus::kCancelled` không bao giờ vượt ranh giới port. + * `kCancelled` (2026-09-11): chỉ handler khai @ref ActionHandler::cancelNeedsTicks mới trả nó — + * sau @ref ActionHandler::cancel handler tiếp tục được tick để đưa thiết bị về vị trí an toàn rồi + * báo `kCancelled`; caller coi đó là terminal. Handler không cần tick sau cancel (5 handler hiện + * có: perception/no-op) không bao giờ trả giá trị này. */ enum class ActionStatus { kRunning, ///< Chưa xong, caller phải tiếp tục gọi update(). kSucceeded, ///< Thiết bị đã hoàn tất thao tác. - kFailed ///< Không thực hiện được, hoặc quá thời gian của chính handler. + kFailed, ///< Không thực hiện được, hoặc quá thời gian của chính handler. + kCancelled ///< Đã dừng an toàn sau cancel() (handler có cancelNeedsTicks). Terminal. }; const char* toString(ActionStatus status); diff --git a/plugins/frame_sampler_action_handler.cpp b/plugins/frame_sampler_action_handler.cpp index b6f1269..0669c82 100644 --- a/plugins/frame_sampler_action_handler.cpp +++ b/plugins/frame_sampler_action_handler.cpp @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include @@ -296,6 +296,7 @@ public: bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override { started_at_ = now; + paused_ = false; action_id_ = action.actionId; resetRun(); simulated_goal_published_ = false; @@ -411,6 +412,27 @@ public: return tick; } + /** + * @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào timeout. + */ + void pause(const robot::Time& now) override + { + if (!paused_) + { + paused_ = true; + paused_at_ = now; + } + } + + void resume(const robot::Time& now) override + { + if (paused_) + { + started_at_ += (now - paused_at_); + paused_ = false; + } + } + void cancel() override { // Không có thiết bị nào để dừng; chỉ bỏ mẫu để lượt sau không thừa hưởng dữ liệu cũ. @@ -606,6 +628,8 @@ private: PoseSampleFilter filter_; robot::Time started_at_; + robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_. + bool paused_ = false; std::string action_id_; std::string frame_; bool simulated_goal_published_ = false; @@ -620,4 +644,4 @@ private: } // namespace action_core -BOOST_DLL_ALIAS(action_core::FrameSamplerActionHandler::create, FrameSamplerActionHandler) +ACTION_CORE_EXPORT_HANDLER(action_core::FrameSamplerActionHandler::create, FrameSamplerActionHandler) diff --git a/plugins/log_report_action_handler.cpp b/plugins/log_report_action_handler.cpp index 197b51f..555c69a 100644 --- a/plugins/log_report_action_handler.cpp +++ b/plugins/log_report_action_handler.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include namespace action_core @@ -115,4 +115,4 @@ private: } // namespace action_core -BOOST_DLL_ALIAS(action_core::LogReportActionHandler::create, LogReportActionHandler) +ACTION_CORE_EXPORT_HANDLER(action_core::LogReportActionHandler::create, LogReportActionHandler) diff --git a/plugins/noop_action_handler.cpp b/plugins/noop_action_handler.cpp index 519a80e..fe14262 100644 --- a/plugins/noop_action_handler.cpp +++ b/plugins/noop_action_handler.cpp @@ -14,7 +14,7 @@ #include #include -#include +#include #include namespace action_core @@ -113,6 +113,7 @@ public: bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override { started_at_ = now; + paused_ = false; action_id_ = action.actionId; robot::log_info("[action_core] '%s': starting action '%s' (id '%s'), duration %.3f s.", @@ -154,6 +155,27 @@ public: return tick; } + /** + * @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào duration/timeout. + */ + void pause(const robot::Time& now) override + { + if (!paused_) + { + paused_ = true; + paused_at_ = now; + } + } + + void resume(const robot::Time& now) override + { + if (paused_) + { + started_at_ += (now - paused_at_); + paused_ = false; + } + } + void cancel() override { // Không có thiết bị nào để đưa về trạng thái an toàn. Handler thật phải làm việc đó ở đây. @@ -169,9 +191,11 @@ private: bool hang_ = false; ///< true = không bao giờ hoàn tất (fault injection) robot::Time started_at_; + robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_. + bool paused_ = false; std::string action_id_; }; } // namespace action_core -BOOST_DLL_ALIAS(action_core::NoopActionHandler::create, NoopActionHandler) +ACTION_CORE_EXPORT_HANDLER(action_core::NoopActionHandler::create, NoopActionHandler) diff --git a/plugins/wait_action_handler.cpp b/plugins/wait_action_handler.cpp index 9f34942..13ec312 100644 --- a/plugins/wait_action_handler.cpp +++ b/plugins/wait_action_handler.cpp @@ -16,7 +16,7 @@ #include #include -#include +#include #include namespace action_core @@ -99,6 +99,7 @@ public: { action_id_ = action.actionId; started_at_ = now; + paused_ = false; if (!readDuration(action, duration_)) { @@ -130,6 +131,27 @@ public: return tick; } + /** + * @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào timeout. + */ + void pause(const robot::Time& now) override + { + if (!paused_) + { + paused_ = true; + paused_at_ = now; + } + } + + void resume(const robot::Time& now) override + { + if (paused_) + { + started_at_ += (now - paused_at_); + paused_ = false; + } + } + void cancel() override { // Không có thiết bị nào để đưa về trạng thái an toàn — chờ thì dừng chờ là xong. @@ -186,10 +208,12 @@ private: double max_duration_ = kDefaultMaxDuration; ///< [s] robot::Time started_at_; + robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_. + bool paused_ = false; double duration_ = 0.0; ///< [s] của action đang chạy std::string action_id_; }; } // namespace action_core -BOOST_DLL_ALIAS(action_core::WaitActionHandler::create, WaitActionHandler) +ACTION_CORE_EXPORT_HANDLER(action_core::WaitActionHandler::create, WaitActionHandler) diff --git a/src/action_registry.cpp b/src/action_registry.cpp index 8972867..78f049a 100644 --- a/src/action_registry.cpp +++ b/src/action_registry.cpp @@ -8,6 +8,8 @@ *********************************************************************/ #include +#include + #include #include @@ -112,6 +114,34 @@ bool ActionRegistry::loadOne(const std::string& name, const std::string& type, } std::function factory; + + // ABI guard (2026-09-11): plugin build với header ActionHandler cũ có vtable ngắn hơn; nạp vào + // runtime mới thì lời gọi virtual mới (pause/resume/cancelNeedsTicks) nhảy vào byte rác — crash + // đúng lúc người vận hành bấm pause. Tra `_abi` trước; thiếu hoặc lệch thì từ chối, log LỖI; + // mission mang actionType đó sẽ bị từ chối ở start() thay vì crash cả tiến trình. + try + { + const auto abi = boost::dll::import_alias(library_path, type + "_abi", + boost::dll::load_mode::append_decorations); + const int plugin_abi = abi(); + if (plugin_abi != kActionAbiVersion) + { + robot::log_error("[action_core] ActionRegistry: '%s' from '%s' was built for ABI %d but this " + "runtime is ABI %d — refusing to load it. Rebuild the plugin against the " + "current action_core headers.", + type.c_str(), library_path.c_str(), plugin_abi, kActionAbiVersion); + return false; + } + } + catch (const std::exception& ex) + { + robot::log_error("[action_core] ActionRegistry: '%s' from '%s' exports no '%s_abi' symbol " + "(built before 2026-09-11 or not using ACTION_CORE_EXPORT_HANDLER) — refusing " + "to load it: %s", + type.c_str(), library_path.c_str(), type.c_str(), ex.what()); + return false; + } + try { factory = boost::dll::import_alias( diff --git a/src/action_types.cpp b/src/action_types.cpp index 165b382..37ac00a 100644 --- a/src/action_types.cpp +++ b/src/action_types.cpp @@ -21,6 +21,8 @@ const char* toString(ActionStatus status) return "SUCCEEDED"; case ActionStatus::kFailed: return "FAILED"; + case ActionStatus::kCancelled: + return "CANCELLED"; } return "UNKNOWN"; } diff --git a/test/action_registry_test.cpp b/test/action_registry_test.cpp index 42d60bd..f64a88f 100644 --- a/test/action_registry_test.cpp +++ b/test/action_registry_test.cpp @@ -198,6 +198,24 @@ TEST(ActionRegistry, ReportsMissingLibraryPath) EXPECT_EQ(registry.size(), 0u); } +TEST(ActionRegistry, RefusesAPluginBuiltForAnotherAbiVersion) +{ + // 2026-09-11: header ActionHandler thêm virtual; plugin build với header cũ có vtable ngắn hơn và + // sẽ crash ở lời gọi pause(). Registry tra `_abi` trước factory và từ chối khi lệch. + robot::NodeHandle nh; + ActionRegistry registry; + EXPECT_FALSE(registry.loadFromConfig(nh, "actions_stale_abi")); + EXPECT_EQ(registry.size(), 0u) << "a plugin with a mismatching _abi must not be registered"; +} + +TEST(ActionRegistry, RefusesAPluginWithoutAnAbiSymbol) +{ + robot::NodeHandle nh; + ActionRegistry registry; + EXPECT_FALSE(registry.loadFromConfig(nh, "actions_no_abi")); + EXPECT_EQ(registry.size(), 0u) << "a plugin built before the ABI guard must not be registered"; +} + // ================================================================================================ // Contract của registerHandler // ================================================================================================ diff --git a/test/config/action_handlers_params.yaml b/test/config/action_handlers_params.yaml index e2b9af7..c1f60cd 100644 --- a/test/config/action_handlers_params.yaml +++ b/test/config/action_handlers_params.yaml @@ -49,6 +49,15 @@ actions_missing_library: actions_empty: something_else: 1 +# ABI guard (2026-09-11): plugin giả export `_abi` = 1 (lệch) và một alias không có `_abi`. +# Registry phải từ chối cả hai, log lỗi, không crash. +actions_stale_abi: + handlers: + - {name: stale, type: StaleAbiActionHandler} +actions_no_abi: + handlers: + - {name: ancient, type: NoAbiActionHandler} + # Bộ handler default, mỗi cái một namespace riêng để test nạp độc lập. actions_wait: handlers: @@ -193,6 +202,10 @@ actions_detect_simulated: NoopActionHandler: library_path: libaction_core_noop_action_handler +StaleAbiActionHandler: + library_path: libaction_core_test_stale_abi_handler +NoAbiActionHandler: + library_path: libaction_core_test_stale_abi_handler FrameSamplerActionHandler: library_path: libaction_core_frame_sampler_action_handler diff --git a/test/plugins/stale_abi_action_handler.cpp b/test/plugins/stale_abi_action_handler.cpp new file mode 100644 index 0000000..2a027a0 --- /dev/null +++ b/test/plugins/stale_abi_action_handler.cpp @@ -0,0 +1,72 @@ +/********************************************************************* + * + * Software License Agreement (BSD License) + * + * action_core — plugin GIẢ mô phỏng một handler build với header cũ: export `_abi` = 1 (và một + * symbol không có `_abi` nào). Chỉ để test ABI guard của ActionRegistry; không cài đặt. + * + * Author: DuongTD + *********************************************************************/ +#include +#include +#include + +#include + +#include + +namespace action_core +{ +namespace testing +{ + +/// Handler tối thiểu, hợp lệ về mặt interface — registry phải từ chối nó CHỈ vì phiên bản ABI. +class StaleAbiActionHandler final : public ActionHandler +{ +public: + static ActionHandler::Ptr create() + { + return std::make_shared(); + } + + bool configure(const std::string&, const ActionContext&, robot::NodeHandle&) override + { + return true; + } + + std::vector supportedActionTypes() const override + { + return {"stale"}; + } + + bool start(const robot_protocol_msgs::Action&, const robot::Time&) override + { + return true; + } + + ActionTick update(const robot::Time&) override + { + ActionTick tick; + tick.status = ActionStatus::kSucceeded; + return tick; + } + + void cancel() override + { + } +}; + +/// Phiên bản ABI "cũ" — lệch với kActionAbiVersion của runtime. +int staleAbiVersion() +{ + return 1; +} + +} // namespace testing +} // namespace action_core + +// Alias 1: có _abi nhưng lệch phiên bản. +BOOST_DLL_ALIAS(action_core::testing::StaleAbiActionHandler::create, StaleAbiActionHandler) +BOOST_DLL_ALIAS(action_core::testing::staleAbiVersion, StaleAbiActionHandler_abi) +// Alias 2: không có _abi (plugin build trước 2026-09-11). +BOOST_DLL_ALIAS(action_core::testing::StaleAbiActionHandler::create, NoAbiActionHandler)