Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid possible UB in Clock jump callbacks #954

Merged
merged 4 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rclcpp/include/rclcpp/clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define RCLCPP__CLOCK_HPP_

#include <functional>
#include<memory>

#include "rclcpp/macros.hpp"
#include "rclcpp/time.hpp"
Expand Down Expand Up @@ -133,7 +134,7 @@ class Clock
void * user_data);

/// Internal storage backed by rcl
rcl_clock_t rcl_clock_;
std::shared_ptr<rcl_clock_t> rcl_clock_;
friend TimeSource; /// Allow TimeSource to access the rcl_clock_ datatype.
rcl_allocator_t allocator_;
};
Expand Down
55 changes: 33 additions & 22 deletions rclcpp/src/rclcpp/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "rclcpp/clock.hpp"

#include<memory>

#include "rclcpp/exceptions.hpp"

#include "rcutils/logging_macros.h"
Expand All @@ -30,29 +32,36 @@ JumpHandler::JumpHandler(
notice_threshold(threshold)
{}

Clock::Clock(rcl_clock_type_t clock_type)
static
void
rcl_clock_deleter(rcl_clock_t * rcl_clock)
{
allocator_ = rcl_get_default_allocator();
auto ret = rcl_clock_init(clock_type, &rcl_clock_, &allocator_);
auto ret = rcl_clock_fini(rcl_clock);
if (ret != RCL_RET_OK) {
exceptions::throw_from_rcl_error(ret, "could not get current time stamp");
RCUTILS_LOG_ERROR("Failed to fini rcl clock.");
}
delete rcl_clock;
}

Clock::~Clock()
Clock::Clock(rcl_clock_type_t clock_type)
{
auto ret = rcl_clock_fini(&rcl_clock_);
allocator_ = rcl_get_default_allocator();
auto clock_unique_ptr = std::make_unique<rcl_clock_t>();
clalancette marked this conversation as resolved.
Show resolved Hide resolved
rcl_clock_ = std::shared_ptr<rcl_clock_t>(clock_unique_ptr.release(), rcl_clock_deleter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivanpauno fyi: I see what you're doing here, but it's not better than new rcl_clock_t() right there on line 50..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the temporary unique_ptr is only useful if some exception may occur between allocating and initializing the clock. Unless I miss something, I agree with hidmic that new rcl_clock_t will work just as well.

auto ret = rcl_clock_init(clock_type, rcl_clock_.get(), &allocator_);
if (ret != RCL_RET_OK) {
RCUTILS_LOG_ERROR("Failed to fini rcl clock.");
exceptions::throw_from_rcl_error(ret, "could not get current time stamp");
}
}

Clock::~Clock() {}

Time
Clock::now()
{
Time now(0, 0, rcl_clock_.type);
Time now(0, 0, rcl_clock_->type);

auto ret = rcl_clock_get_now(&rcl_clock_, &now.rcl_time_.nanoseconds);
auto ret = rcl_clock_get_now(rcl_clock_.get(), &now.rcl_time_.nanoseconds);
if (ret != RCL_RET_OK) {
exceptions::throw_from_rcl_error(ret, "could not get current time stamp");
}
Expand All @@ -63,13 +72,13 @@ Clock::now()
bool
Clock::ros_time_is_active()
{
if (!rcl_clock_valid(&rcl_clock_)) {
if (!rcl_clock_valid(rcl_clock_.get())) {
RCUTILS_LOG_ERROR("ROS time not valid!");
return false;
}

bool is_enabled = false;
auto ret = rcl_is_enabled_ros_time_override(&rcl_clock_, &is_enabled);
auto ret = rcl_is_enabled_ros_time_override(rcl_clock_.get(), &is_enabled);
if (ret != RCL_RET_OK) {
exceptions::throw_from_rcl_error(
ret, "Failed to check ros_time_override_status");
Expand All @@ -80,13 +89,13 @@ Clock::ros_time_is_active()
rcl_clock_t *
Clock::get_clock_handle() noexcept
{
return &rcl_clock_;
return rcl_clock_.get();
}

rcl_clock_type_t
Clock::get_clock_type() const noexcept
{
return rcl_clock_.type;
return rcl_clock_->type;
}

void
Expand Down Expand Up @@ -120,23 +129,25 @@ Clock::create_jump_callback(

// Try to add the jump callback to the clock
rcl_ret_t ret = rcl_clock_add_jump_callback(
&rcl_clock_, threshold, Clock::on_time_jump,
rcl_clock_.get(), threshold, Clock::on_time_jump,
handler.get());
if (RCL_RET_OK != ret) {
exceptions::throw_from_rcl_error(ret, "Failed to add time jump callback");
}

std::weak_ptr<rcl_clock_t> weak_clock = rcl_clock_;
// *INDENT-OFF*
// create shared_ptr that removes the callback automatically when all copies are destructed
// TODO(dorezyuk) UB, if the clock leaves scope before the JumpHandler
return JumpHandler::SharedPtr(handler.release(), [this](JumpHandler * handler) noexcept {
rcl_ret_t ret = rcl_clock_remove_jump_callback(&rcl_clock_, Clock::on_time_jump,
handler);
delete handler;
handler = NULL;
if (RCL_RET_OK != ret) {
RCUTILS_LOG_ERROR("Failed to remove time jump callback");
return JumpHandler::SharedPtr(handler.release(), [weak_clock](JumpHandler * handler) noexcept {
auto shared_clock = weak_clock.lock();
if (shared_clock) {
rcl_ret_t ret = rcl_clock_remove_jump_callback(shared_clock.get(), Clock::on_time_jump,
handler);
if (RCL_RET_OK != ret) {
RCUTILS_LOG_ERROR("Failed to remove time jump callback");
}
}
delete handler;
});
// *INDENT-ON*
}
Expand Down
6 changes: 3 additions & 3 deletions rclcpp/src/rclcpp/time_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void TimeSource::set_clock(
enable_ros_time(clock);
}

auto ret = rcl_set_ros_time_override(&(clock->rcl_clock_), rclcpp::Time(*msg).nanoseconds());
auto ret = rcl_set_ros_time_override(clock->get_clock_handle(), rclcpp::Time(*msg).nanoseconds());
if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(
ret, "Failed to set ros_time_override_status");
Expand Down Expand Up @@ -275,7 +275,7 @@ void TimeSource::on_parameter_event(const rcl_interfaces::msg::ParameterEvent::S

void TimeSource::enable_ros_time(std::shared_ptr<rclcpp::Clock> clock)
{
auto ret = rcl_enable_ros_time_override(&clock->rcl_clock_);
auto ret = rcl_enable_ros_time_override(clock->get_clock_handle());
if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(
ret, "Failed to enable ros_time_override_status");
Expand All @@ -284,7 +284,7 @@ void TimeSource::enable_ros_time(std::shared_ptr<rclcpp::Clock> clock)

void TimeSource::disable_ros_time(std::shared_ptr<rclcpp::Clock> clock)
{
auto ret = rcl_disable_ros_time_override(&clock->rcl_clock_);
auto ret = rcl_disable_ros_time_override(clock->get_clock_handle());
if (ret != RCL_RET_OK) {
rclcpp::exceptions::throw_from_rcl_error(
ret, "Failed to enable ros_time_override_status");
Expand Down