From 6fe3ff90cd86d539371403381f6c580fc097e689 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 19 May 2019 14:33:36 -0700 Subject: fix #946 Use after free in TLS This also introduces a more efficient reference counting usage based on atomics, rather than locks. --- src/platform/windows/win_thread.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/platform/windows/win_thread.c') diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 158b8133..146520e9 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -128,13 +128,13 @@ nni_atomic_flag_reset(nni_atomic_flag *f) } void -nni_atomic_inc64(nni_atomic_u64 *v, uint64_t bump) +nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { InterlockedAddNoFence64(&v->v, (LONGLONG) bump); } void -nni_atomic_dec64(nni_atomic_u64 *v, uint64_t bump) +nni_atomic_sub64(nni_atomic_u64 *v, uint64_t bump) { // Windows lacks a sub, so we add the negative. InterlockedAddNoFence64(&v->v, (0ll - (LONGLONG) bump)); @@ -165,6 +165,18 @@ nni_atomic_init64(nni_atomic_u64 *v) InterlockedExchange64(&v->v, 0); } +void +nni_atomic_inc64(nni_atomic_u64 *v) +{ + (void) InterlockedIncrementAcquire64(&v->v); +} + +uint64_t +nni_atomic_dec64_nv(nni_atomic_u64 *v) +{ + return ((uint64_t)(InterlockedDecrementRelease64(&v->v))); +} + static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; -- cgit v1.2.3-70-g09d2