From 82c50cb89287cf4ad99fc4a988d16f39aa2c6359 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 1 Nov 2022 00:28:21 +0300 Subject: Fix data race in GC_heapsize_at_forced_unmap variable (a cherry-pick of commit 856760844 from 'release-8_2') Issue #488 (bdwgc). Write to GC_heapsize_at_forced_unmap should be only performed when holding the allocation lock. * alloc.c (GC_heapsize_at_forced_unmap): Move the definition upper to be before GC_try_to_collect_general(); add comment. * alloc.c (GC_try_to_collect_general): Set GC_heapsize_at_forced_unmap value (after LOCK) if force_unmap; move comment from GC_heapsize_at_forced_unmap(). * alloc.c (GC_try_to_collect_general): Remove GC_ATTR_UNUSED for force_unmap. * alloc.c (GC_gcollect_and_unmap): Do not set GC_heapsize_at_forced_unmap. --- alloc.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/alloc.c b/alloc.c index 4065b4dc..59b887b9 100644 --- a/alloc.c +++ b/alloc.c @@ -1043,9 +1043,12 @@ STATIC void GC_finish_collection(void) # endif } +STATIC word GC_heapsize_at_forced_unmap = 0; + /* accessed with the allocation lock held */ + /* If stop_func == 0 then GC_default_stop_func is used instead. */ STATIC GC_bool GC_try_to_collect_general(GC_stop_func stop_func, - GC_bool force_unmap GC_ATTR_UNUSED) + GC_bool force_unmap) { GC_bool result; IF_USE_MUNMAP(int old_unmap_threshold;) @@ -1056,6 +1059,11 @@ STATIC GC_bool GC_try_to_collect_general(GC_stop_func stop_func, if (GC_debugging_started) GC_print_all_smashed(); GC_INVOKE_FINALIZERS(); LOCK(); + if (force_unmap) { + /* Record current heap size to make heap growth more conservative */ + /* afterwards (as if the heap is growing from zero size again). */ + GC_heapsize_at_forced_unmap = GC_heapsize; + } DISABLE_CANCEL(cancel_state); # ifdef USE_MUNMAP old_unmap_threshold = GC_unmap_threshold; @@ -1094,13 +1102,8 @@ GC_API void GC_CALL GC_gcollect(void) if (GC_have_errors) GC_print_all_errors(); } -STATIC word GC_heapsize_at_forced_unmap = 0; - GC_API void GC_CALL GC_gcollect_and_unmap(void) { - /* Record current heap size to make heap growth more conservative */ - /* afterwards (as if the heap is growing from zero size again). */ - GC_heapsize_at_forced_unmap = GC_heapsize; /* Collect and force memory unmapping to OS. */ (void)GC_try_to_collect_general(GC_never_stop_func, TRUE); } -- cgit v1.2.1