From 33ee8becf224f3e818804f84b4c774a3dbb0149b Mon Sep 17 00:00:00 2001 From: Ludovic Henry Date: Tue, 13 Dec 2016 16:58:15 -0500 Subject: [PATCH] [threadpool] Assert that we do not overflow ThreadPoolCounter starting and working fields (#4138) --- mono/metadata/threadpool.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/mono/metadata/threadpool.c b/mono/metadata/threadpool.c index 9b404869ea4..cfe2892b375 100644 --- a/mono/metadata/threadpool.c +++ b/mono/metadata/threadpool.c @@ -87,22 +87,17 @@ static mono_lazy_init_t status = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED; static ThreadPool* threadpool; -#define COUNTER_CHECK(counter) \ - do { \ - g_assert (sizeof (ThreadPoolCounter) == sizeof (gint32)); \ - g_assert (counter._.starting >= 0); \ - g_assert (counter._.working >= 0); \ - } while (0) - #define COUNTER_ATOMIC(threadpool,var,block) \ do { \ ThreadPoolCounter __old; \ do { \ g_assert (threadpool); \ - __old = COUNTER_READ (threadpool); \ - (var) = __old; \ + (var) = __old = COUNTER_READ (threadpool); \ { block; } \ - COUNTER_CHECK (var); \ + if (!(counter._.starting >= 0)) \ + g_error ("%s: counter._.starting = %d, but should be >= 0", __func__, counter._.starting); \ + if (!(counter._.working >= 0)) \ + g_error ("%s: counter._.working = %d, but should be >= 0", __func__, counter._.working); \ } while (InterlockedCompareExchange (&threadpool->counters.as_gint32, (var).as_gint32, __old.as_gint32) != __old.as_gint32); \ } while (0) @@ -146,6 +141,8 @@ initialize (void) threadpool = g_new0 (ThreadPool, 1); g_assert (threadpool); + g_assert (sizeof (ThreadPoolCounter) == sizeof (gint32)); + mono_refcount_init (threadpool, destroy); threadpool->domains = g_ptr_array_new (); @@ -359,6 +356,9 @@ worker_callback (gpointer unused) thread = mono_thread_internal_current (); COUNTER_ATOMIC (threadpool, counter, { + if (!(counter._.working < 32767 /* G_MAXINT16 */)) + g_error ("%s: counter._.working = %d, but should be < 32767", __func__, counter._.working); + counter._.starting --; counter._.working ++; }); @@ -809,6 +809,9 @@ ves_icall_System_Threading_ThreadPool_RequestWorkerThread (void) mono_refcount_inc (threadpool); COUNTER_ATOMIC (threadpool, counter, { + if (!(counter._.starting < 32767 /* G_MAXINT16 */)) + g_error ("%s: counter._.starting = %d, but should be < 32767", __func__, counter._.starting); + counter._.starting ++; }); -- 2.11.4.GIT