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

timer: fix oss-fuzz issue #11852 #6982

Merged
merged 5 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions source/common/event/timer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
namespace Envoy {
namespace Event {

void TimerUtils::millisecondsToTimeval(const std::chrono::milliseconds& d, timeval& tv) {
std::chrono::seconds secs = std::chrono::duration_cast<std::chrono::seconds>(d);
std::chrono::microseconds usecs = std::chrono::duration_cast<std::chrono::microseconds>(d - secs);

tv.tv_sec = secs.count();
tv.tv_usec = usecs.count();
}

TimerImpl::TimerImpl(Libevent::BasePtr& libevent, TimerCb cb) : cb_(cb) {
ASSERT(cb_);
evtimer_assign(
Expand All @@ -22,11 +30,8 @@ void TimerImpl::enableTimer(const std::chrono::milliseconds& d) {
if (d.count() == 0) {
event_active(&raw_event_, EV_TIMEOUT, 0);
} else {
// TODO(#4332): use duration_cast more nicely to clean up this code.
std::chrono::microseconds us = std::chrono::duration_cast<std::chrono::microseconds>(d);
timeval tv;
tv.tv_sec = us.count() / 1000000;
tv.tv_usec = us.count() % 1000000;
TimerUtils::millisecondsToTimeval(d, tv);
event_add(&raw_event_, &tv);
}
}
Expand Down
8 changes: 8 additions & 0 deletions source/common/event/timer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
namespace Envoy {
namespace Event {

/**
* Utility helper functions for Timer implementation.
*/
class TimerUtils {
public:
static void millisecondsToTimeval(const std::chrono::milliseconds& d, timeval& tv);
};

/**
* libevent implementation of Timer.
*/
Expand Down
24 changes: 24 additions & 0 deletions test/common/event/dispatcher_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "common/api/api_impl.h"
#include "common/common/lock_guard.h"
#include "common/event/dispatcher_impl.h"
#include "common/event/timer_impl.h"
#include "common/stats/isolated_store_impl.h"

#include "test/mocks/common.h"
Expand Down Expand Up @@ -197,6 +198,29 @@ TEST(TimerImplTest, TimerEnabledDisabled) {
EXPECT_FALSE(timer->enabled());
}

TEST(TimerImplTest, TimerValueConversion) {
timeval tv;
std::chrono::milliseconds msecs;

// Basic test with zero milliseconds.
msecs = std::chrono::milliseconds(0);
TimerUtils::millisecondsToTimeval(msecs, tv);
EXPECT_EQ(tv.tv_sec, 0);
EXPECT_EQ(tv.tv_usec, 0);

// 2050 milliseconds is 2 seconds and 50000 microseconds.
msecs = std::chrono::milliseconds(2050);
TimerUtils::millisecondsToTimeval(msecs, tv);
EXPECT_EQ(tv.tv_sec, 2);
EXPECT_EQ(tv.tv_usec, 50000);

// Check maximum value conversion.
msecs = std::chrono::milliseconds::duration::max();
TimerUtils::millisecondsToTimeval(msecs, tv);
EXPECT_EQ(tv.tv_sec, msecs.count() / 1000);
EXPECT_EQ(tv.tv_usec, (msecs.count() % tv.tv_sec) * 1000);
}

} // namespace
} // namespace Event
} // namespace Envoy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
static_resources { clusters { name: " " connect_timeout { nanos: 4 } hosts {
Copy link
Member

Choose a reason for hiding this comment

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

Could you also add a unit test for this new clamping or conversion boundary behavior somewhere, e.g. https://github.com/envoyproxy/envoy/blob/master/test/common/event/dispatcher_impl_test.cc#L189? The corpus entries are useful, but they are hard to reason about when trying to understand behavior, and are subject to fuzzer rot.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added some tests. I refactored the timeval conversion to a separate function to make testing easier.

pipe { } } health_checks { timeout { nanos: 4 } interval { nanos: 4 } unhealthy_threshold { } healthy_threshold { } tcp_health_check { } no_traffic_interval { seconds: 2818048 } interval_jitter_percent: 537791091 } } }