Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85591 - trunk/libs/thread/test
From: vicente.botet_at_[hidden]
Date: 2013-09-07 08:11:18


Author: viboes
Date: 2013-09-07 08:11:18 EDT (Sat, 07 Sep 2013)
New Revision: 85591
URL: http://svn.boost.org/trac/boost/changeset/85591

Log:
Thread: protect condition_variable/_any wait_for and wait_until from malicious input.

Added:
   trunk/libs/thread/test/test_9079_a.cpp (contents, props changed)
   trunk/libs/thread/test/test_9079_b.cpp (contents, props changed)
Text files modified:
   trunk/libs/thread/test/Jamfile.v2 | 4 +
   trunk/libs/thread/test/test_9079_a.cpp | 57 ++++++++++++++++++++++++++++
   trunk/libs/thread/test/test_9079_b.cpp | 81 ++++++++++++++++++++++++++++++++++++++++
   3 files changed, 141 insertions(+), 1 deletions(-)

Modified: trunk/libs/thread/test/Jamfile.v2
==============================================================================
--- trunk/libs/thread/test/Jamfile.v2 Sat Sep 7 03:24:47 2013 (r85590)
+++ trunk/libs/thread/test/Jamfile.v2 2013-09-07 08:11:18 EDT (Sat, 07 Sep 2013) (r85591)
@@ -792,7 +792,9 @@
           #[ thread-run test_8600.cpp ]
           #[ thread-run test_8943.cpp ]
           #[ thread-run test_8960.cpp ]
-
+ [ thread-run test_9079_a.cpp ]
+ [ thread-run test_9079_b.cpp ]
+
     ;
 
 }

Added: trunk/libs/thread/test/test_9079_a.cpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/libs/thread/test/test_9079_a.cpp 2013-09-07 08:11:18 EDT (Sat, 07 Sep 2013) (r85591)
@@ -0,0 +1,57 @@
+// Copyright (C) 2013 Vicente Botet
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// A
+
+//#include <boost/log/trivial.hpp>
+#include <boost/chrono.hpp>
+#include <boost/thread.hpp>
+#include <boost/thread/condition_variable.hpp>
+
+//#if !defined(BOOST_NO_CXX11_ALIGNAS)
+//#error
+//# define BOOST_ALIGNMENT2(x) alignas(x)
+//#elif defined(_MSC_VER)
+//#error
+//# define BOOST_ALIGNMENT2(x) __declspec(align(x))
+//#elif defined(__GNUC__)
+//#error
+//# define BOOST_ALIGNMENT(x) __attribute__ ((__aligned__(x)))
+//#else
+//#error
+//# define BOOST_NO_ALIGNMENT2
+//# define BOOST_ALIGNMENT2(x)
+//#endif
+
+typedef boost::chrono::high_resolution_clock Clock;
+typedef Clock::time_point TimePoint;
+
+inline TimePoint real_time_now()
+{
+ return Clock::now();
+}
+
+int main()
+{
+ using namespace boost::chrono;
+
+ boost::condition_variable m_task_spawn_condition;
+
+ boost::mutex main_thread_mutex;
+ boost::unique_lock < boost::mutex > main_thread_lock(main_thread_mutex);
+
+ //BOOST_LOG_TRIVIAL(info) << "[TaskScheduler::run_and_wait] Scheduling loop - BEGIN";
+
+ //while (true)
+ {
+ static const milliseconds TIME_BACK = milliseconds(1);
+ m_task_spawn_condition.wait_until(
+ main_thread_lock,
+ real_time_now() - TIME_BACK); // wait forever
+ m_task_spawn_condition.wait_for( main_thread_lock, - TIME_BACK ); // same problem
+ //BOOST_LOG_TRIVIAL(trace) << "TICK";
+ }
+
+}

Added: trunk/libs/thread/test/test_9079_b.cpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/libs/thread/test/test_9079_b.cpp 2013-09-07 08:11:18 EDT (Sat, 07 Sep 2013) (r85591)
@@ -0,0 +1,81 @@
+// Copyright (C) 2013 Vicente Botet
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// B
+
+#include <boost/atomic.hpp>
+//#include <boost/log/trivial.hpp>
+#include <boost/chrono.hpp>
+#include <boost/thread.hpp>
+#include <boost/thread/condition_variable.hpp>
+
+typedef boost::chrono::high_resolution_clock Clock;
+typedef Clock::time_point TimePoint;
+
+inline TimePoint real_time_now()
+{
+ return Clock::now();
+}
+
+class Foo {
+ boost::atomic<bool> m_is_exiting;
+ TimePoint m_next_tick_time;
+
+public:
+
+ bool is_exiting() const
+ {
+ return m_is_exiting;
+ }
+
+ TimePoint spawn_tasks() // note that in my app, this call takes more time than here
+ {
+ using namespace boost::chrono;
+ const TimePoint now = real_time_now();
+
+ if (m_next_tick_time < now) {
+ m_next_tick_time = now + seconds(1);
+ //BOOST_LOG_TRIVIAL(info) << "TICK!";
+ }
+
+ return m_next_tick_time;
+ }
+
+};
+
+int main()
+{
+ using namespace boost::chrono;
+ static const milliseconds MIN_TIME_TASKS_SPAWN_FREQUENCY = milliseconds(1);
+ //microseconds(1); // THE SHORTER THE QUICKER TO REPRODUCE THE BUG
+
+ boost::condition_variable m_task_spawn_condition;
+ Foo foo;
+
+ boost::mutex main_thread_mutex;
+ boost::unique_lock < boost::mutex > main_thread_lock(main_thread_mutex);
+
+ //BOOST_LOG_TRIVIAL(info) << "[TaskScheduler::run_and_wait] Scheduling loop - BEGIN";
+
+ while (!foo.is_exiting()) {
+ const TimePoint next_task_spawn_time = foo.spawn_tasks();
+
+ const TimePoint now = real_time_now();
+ const TimePoint next_minimum_spawn_time = now + MIN_TIME_TASKS_SPAWN_FREQUENCY;
+ const TimePoint next_spawn_time = next_task_spawn_time > TimePoint()
+ && next_task_spawn_time < next_minimum_spawn_time
+ ? next_task_spawn_time : next_minimum_spawn_time;
+
+ const TimePoint::duration wait_time = next_spawn_time - now;
+ if (wait_time > wait_time.zero()) {
+ // BOOST_LOG_TRIVIAL(trace) << "WAIT TIME: " << wait_time; // UNCOMMENT THIS: MAKES IT WORKS. WAT??????
+ m_task_spawn_condition.wait_until(
+ main_thread_lock,
+ next_spawn_time); // DON'T WORK: WILL WAIT IF next_spawn_time is too close!
+ }
+
+ }
+
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk