From 0ca8be59d7e6c4186be587673df98e9861747fb9 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 23 Apr 2024 23:18:53 +0300 Subject: [PATCH] Define NOOP1_PTR() internal macro (refactoring) Issue #627 (bdwgc). * allchblk.c [VALGRIND_TRACKING] (GC_free_profiler_hook): Replace GC_noop1((word)p) to NOOP1_PTR(p). * alloc.c [!GC_DISABLE_INCREMENTAL && !NO_CLOCK && CPPCHECK] (GC_timeout_stop_func): Likewise. * alloc.c [GWW_VDB] (GC_add_to_heap): Likewise. * alloc.c [CPPCHECK] (GC_allocobj): Likewise. * backgraph.c [CPPCHECK] (push_in_progress, ensure_struct, add_edge, backwards_height, update_max_height): Likewise. * darwin_stop_world.c [!POWERPC && !ARM32 && !AARCH64 && CPPCHECK] (GC_FindTopOfStack): Likewise. * dyn_load.c [LINUX && !USE_PROC_FOR_LIBRARIES && CPPCHECK && HOST_ANDROID && !GC_DONT_DEFINE_LINK_MAP && !(__ANDROID_API__>=21)] (GC_register_dynamic_libraries): Likewise. * dyn_load.c [DARWIN && LINT2] (GC_init_dyld): Likewise. * include/private/gc_priv.h [E2K && CPPCHECK] (PS_ALLOCA_BUF): Likewise. * mach_dep.c [!HAVE_PUSH_REGS && !EMSCRIPTEN && UNIX_LIKE && !NO_GETCONTEXT && GETCONTEXT_FPU_EXCMASK_BUG && X86_64 && CPPCHECK] (GC_with_callee_saves_pushed): Likewise. * mark.c [CPPCHECK] (GC_signal_mark_stack_overflow, GC_steal_mark_stack): Likewise. * misc.c [CPPCHECK] (GC_default_warn_proc, GC_call_with_gc_active): Likewise. * misc.c [!THREAD && CPPCHECK] (GC_do_blocking_inner): Likewise. * os_dep.c [NEED_PROC_MAPS && LINT2] (GC_get_maps): Likewise. * os_dep.c [(NEED_FIND_LIMIT || UNIX_LIKE || WRAP_MARK_SOME) && CPPCHECK && ADDRESS_SANITIZER] (GC_set_and_save_fault_handler): Likewise. * os_dep.c [CPPCHECK] (GC_get_register_stack_base, GC_linux_main_stack_base): Likewise. * os_dep.c [!ANY_MSWIN && (SVR4 || AIX || DGUX) && !PCR && !AO_HAVE_fetch_and_add && CPPCHECK] (GC_SysVGetDataStart): Likewise. * os_dep.c [USE_MUNMAP && (CPPCHECK || LINT2)] (block_unmap_inner, GC_remap): Likewise. * os_dep.c [MPROTECT_VDB && !DARWIN && CPPCHECK && ADDRESS_SANITIZER] (GC_dirty_init): Likewise. * pthread_support.c [CPPCHECK] (GC_call_with_gc_active): Likewise. * specific.c [LINT2] (GC_remove_specific_after_fork): Likewise. * tests/gctest.c [CPPCHECK] (reverse_test_inner, test_long_mult, warn_proc): Likewise. * tests/gctest.c [!NO_TYPED_TEST] (typed_test): Likewise. * tests/gctest.c (run_one_test): Likewise. * win32_threads.c [CPPCHECK] (copy_ptr_regs, GC_win32_start_inner): Likewise. * include/private/gc_priv.h (NOOP1_PTR): New macro. --- allchblk.c | 2 +- alloc.c | 8 ++++---- backgraph.c | 10 +++++----- darwin_stop_world.c | 2 +- dyn_load.c | 8 ++++---- include/private/gc_priv.h | 7 ++++++- mach_dep.c | 2 +- mark.c | 4 ++-- misc.c | 7 ++++--- os_dep.c | 20 ++++++++++---------- pthread_support.c | 2 +- specific.c | 2 +- tests/gctest.c | 14 +++++++------- win32_threads.c | 4 ++-- 14 files changed, 49 insertions(+), 43 deletions(-) diff --git a/allchblk.c b/allchblk.c index d7e44b8b1..22e298e0a 100644 --- a/allchblk.c +++ b/allchblk.c @@ -1018,7 +1018,7 @@ STATIC struct hblk *GC_allochblk_nth(size_t lb_adjusted, int k, GC_ASSERT(I_HOLD_LOCK()); # endif /* Prevent treating this function by the compiler as a no-op one. */ - GC_noop1((word)p); + NOOP1_PTR(p); } #endif /* VALGRIND_TRACKING */ diff --git a/alloc.c b/alloc.c index 47f8d6c57..4896fbcd5 100644 --- a/alloc.c +++ b/alloc.c @@ -267,7 +267,7 @@ GC_API GC_stop_func GC_CALL GC_get_stop_func(void) time_diff = MS_TIME_DIFF(current_time, GC_start_time); nsec_diff = NS_FRAC_TIME_DIFF(current_time, GC_start_time); # if defined(CPPCHECK) - GC_noop1((word)(&nsec_diff)); + NOOP1_PTR(&nsec_diff); # endif if (time_diff >= GC_time_limit && (time_diff > GC_time_limit || nsec_diff >= GC_time_lim_nsec)) { @@ -1514,7 +1514,7 @@ STATIC void GC_add_to_heap(struct hblk *h, size_t bytes) old_capacity * sizeof(struct HeapSect)); # else /* TODO: implement GWW-aware recycling as in alloc_mark_stack */ - GC_noop1((word)old_heap_sects); + NOOP1_PTR(old_heap_sects); # endif } } @@ -1833,12 +1833,12 @@ GC_INNER ptr_t GC_allocobj(size_t lg, int k) GC_continue_reclaim(lg, k); EXIT_GC(); # if defined(CPPCHECK) - GC_noop1((word)(&flh)); + NOOP1_PTR(&flh); # endif if (NULL == *flh) { GC_new_hblk(lg, k); # if defined(CPPCHECK) - GC_noop1((word)(&flh)); + NOOP1_PTR(&flh); # endif if (NULL == *flh) { ENTER_GC(); diff --git a/backgraph.c b/backgraph.c index ab10894ad..b10dd5486 100644 --- a/backgraph.c +++ b/backgraph.c @@ -152,7 +152,7 @@ static void push_in_progress(ptr_t p) n_in_progress * sizeof(ptr_t)); # elif defined(LINT2) /* TODO: implement GWW-aware recycling as in alloc_mark_stack */ - GC_noop1((word)in_progress_space); + NOOP1_PTR(in_progress_space); # endif in_progress_space = new_in_progress_space; } @@ -195,7 +195,7 @@ static void ensure_struct(ptr_t p) be -> flags = 0; # if defined(CPPCHECK) - GC_noop1((word)(&old_back_ptr)); + NOOP1_PTR(&old_back_ptr); /* Workaround a false positive that old_back_ptr cannot be null. */ # endif if (NULL == old_back_ptr) { @@ -227,7 +227,7 @@ static void add_edge(ptr_t p, ptr_t q) return; } # if defined(CPPCHECK) - GC_noop1((word)(&pred)); + NOOP1_PTR(&pred); # endif if (NULL == pred) { static unsigned random_number = 13; @@ -392,7 +392,7 @@ static word backwards_height(ptr_t p) GC_ASSERT(I_HOLD_LOCK()); # if defined(CPPCHECK) - GC_noop1((word)(&pred)); + NOOP1_PTR(&pred); # endif if (NULL == pred) return 1; @@ -483,7 +483,7 @@ static void update_max_height(ptr_t p, size_t n_bytes, word gc_descr) /* to p, but it can't have decreased. */ back_ptr = GET_OH_BG_PTR(p); # if defined(CPPCHECK) - GC_noop1((word)(&back_ptr)); + NOOP1_PTR(&back_ptr); # endif if (back_ptr != NULL && (ADDR(back_ptr) & FLAG_MANY) != 0) { be = (back_edges *)((word)back_ptr & ~(word)FLAG_MANY); diff --git a/darwin_stop_world.c b/darwin_stop_world.c index 2dd96b2c7..518165147 100644 --- a/darwin_stop_world.c +++ b/darwin_stop_world.c @@ -78,7 +78,7 @@ GC_INNER ptr_t GC_FindTopOfStack(unsigned long stack_start) frame = (StackFrame *)sp_reg; # else # if defined(CPPCHECK) - GC_noop1((word)&frame); + NOOP1_PTR(&frame); # endif ABORT("GC_FindTopOfStack(0) is not implemented"); # endif diff --git a/dyn_load.c b/dyn_load.c index 44caaabc4..b2696115e 100644 --- a/dyn_load.c +++ b/dyn_load.c @@ -774,9 +774,9 @@ GC_INNER void GC_register_dynamic_libraries(void) } # if defined(CPPCHECK) && defined(HOST_ANDROID) \ && !defined(GC_DONT_DEFINE_LINK_MAP) && !(__ANDROID_API__ >= 21) - GC_noop1((word)lm->l_name); - GC_noop1((word)lm->l_ld); - GC_noop1((word)lm->l_prev); + NOOP1_PTR(lm -> l_name); + GC_noop1((word)(lm -> l_ld)); + NOOP1_PTR(lm -> l_prev); # endif } } @@ -1495,7 +1495,7 @@ GC_INNER void GC_init_dyld(void) ABORT("dlopen failed (to bind fully image)"); /* Note that the handle is never closed. */ # ifdef LINT2 - GC_noop1((word)dl_handle); + NOOP1_PTR(dl_handle); # endif } # else diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index cb097b96b..23a2a68ec 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -292,6 +292,11 @@ typedef struct hblkhdr hdr; #define GC_WORD_MAX (~(word)0) +/* A handy macro to prevent certain compiler false positive */ +/* warnings and misoptimizations by making the compiler to treat */ +/* the specified pointer as the one stored to some global location. */ +#define NOOP1_PTR(p) GC_noop1(ADDR(p)) + /* Convert given pointer to its address. Result is of word type. */ #define ADDR(p) ((word)(p)) @@ -2052,7 +2057,7 @@ GC_INNER void GC_with_callee_saves_pushed(GC_with_callee_saves_func fn, /* Workaround "Uninitialized bs_lo" and "obsolete alloca() called" */ /* false positive warnings. */ # define PS_ALLOCA_BUF(pbuf, sz) \ - (void)(GC_noop1((word)(pbuf)), *(pbuf) = __builtin_alloca(sz)) + (void)(NOOP1_PTR(pbuf), *(pbuf) = __builtin_alloca(sz)) # else # define PS_ALLOCA_BUF(pbuf, sz) (void)(*(pbuf) = alloca(sz)) # endif diff --git a/mach_dep.c b/mach_dep.c index 3aecb2b66..3af415f56 100644 --- a/mach_dep.c +++ b/mach_dep.c @@ -259,7 +259,7 @@ GC_INNER void GC_with_callee_saves_pushed(GC_with_callee_saves_func fn, unsigned short old_fcw; # if defined(CPPCHECK) - GC_noop1((word)&old_fcw); + NOOP1_PTR(&old_fcw); # endif __asm__ __volatile__ ("fstcw %0" : "=m" (*&old_fcw)); # else diff --git a/mark.c b/mark.c index c438e9f17..988c186f2 100644 --- a/mark.c +++ b/mark.c @@ -621,7 +621,7 @@ STATIC mse * GC_signal_mark_stack_overflow(mse *msp) GC_COND_LOG_PRINTF("Mark stack overflow; current size: %lu entries\n", (unsigned long)GC_mark_stack_size); # if defined(CPPCHECK) - GC_noop1((word)msp); + NOOP1_PTR(msp); # endif return msp - GC_MARK_STACK_DISCARDS; } @@ -1003,7 +1003,7 @@ STATIC mse * GC_steal_mark_stack(mse * low, mse * high, mse * local, } *next = p; # if defined(CPPCHECK) - GC_noop1((word)local); + NOOP1_PTR(local); # endif return top; } diff --git a/misc.c b/misc.c index 2422b2ae4..52f6934cc 100644 --- a/misc.c +++ b/misc.c @@ -2000,7 +2000,7 @@ STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg) # if defined(CPPCHECK) /* Workaround "parameter can be declared as pointer to const" */ /* cppcheck warning. */ - GC_noop1((word)msg); + NOOP1_PTR(msg); # endif GC_warn_printf(msg, arg); } @@ -2352,7 +2352,8 @@ GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, void *client_data) GC_ASSERT(GC_traced_stack_sect == &stacksect); # if defined(CPPCHECK) - GC_noop1((word)GC_traced_stack_sect - (word)GC_blocked_sp); + NOOP1_PTR(GC_traced_stack_sect); + NOOP1_PTR(GC_blocked_sp); # endif /* Restore original "stack section". */ GC_traced_stack_sect = stacksect.prev; @@ -2389,7 +2390,7 @@ STATIC void GC_do_blocking_inner(ptr_t data, void *context) GC_ASSERT(GC_blocked_sp == (ptr_t)(&d)); # endif # if defined(CPPCHECK) - GC_noop1((word)GC_blocked_sp); + NOOP1_PTR(GC_blocked_sp); # endif GC_blocked_sp = NULL; } diff --git a/os_dep.c b/os_dep.c index 96ec1ec34..77c0e6548 100644 --- a/os_dep.c +++ b/os_dep.c @@ -197,7 +197,7 @@ GC_INNER const char * GC_get_maps(void) while (maps_size >= maps_buf_sz) { # ifdef LINT2 /* Workaround passing tainted maps_buf to a tainted sink. */ - GC_noop1((word)maps_buf); + NOOP1_PTR(maps_buf); # else GC_scratch_recycle_no_gww(maps_buf, maps_buf_sz); # endif @@ -936,7 +936,7 @@ GC_INNER void GC_setpagesize(void) # endif # endif /* !USE_SEGV_SIGACT */ # if defined(CPPCHECK) && defined(ADDRESS_SANITIZER) - GC_noop1((word)&__asan_default_options); + NOOP1_PTR(&__asan_default_options); # endif } #endif /* NEED_FIND_LIMIT || UNIX_LIKE || WRAP_MARK_SOME */ @@ -1114,7 +1114,7 @@ GC_INNER void GC_setpagesize(void) # if defined(CPPCHECK) /* Workaround a warning that the address of the global */ /* symbol (which is a weak one) cannot be null. */ - GC_noop1((word)(&p_libc_ia64_register_backing_store_base)); + NOOP1_PTR(&p_libc_ia64_register_backing_store_base); # endif if (p_libc_ia64_register_backing_store_base != NULL && __libc_ia64_register_backing_store_base != NULL) { @@ -1155,7 +1155,7 @@ GC_INNER void GC_setpagesize(void) ptr_t *p_libc_stack_end = &__libc_stack_end; # if defined(CPPCHECK) - GC_noop1((word)(&p_libc_stack_end)); + NOOP1_PTR(&p_libc_stack_end); # endif if (p_libc_stack_end != NULL && __libc_stack_end != NULL) { # if defined(IA64) @@ -2003,7 +2003,7 @@ void GC_register_data_segments(void) /* Fallback to non-atomic fetch-and-store. */ char v = *result; # if defined(CPPCHECK) - GC_noop1((word)(&v)); + NOOP1_PTR(&v); # endif *result = v; # endif @@ -2043,7 +2043,7 @@ void GC_register_data_segments(void) GC_reset_fault_handler(); } else { GC_reset_fault_handler(); - /* As above, we go to plan B */ + /* As above, we go to plan B. */ result = (ptr_t)GC_find_limit(DATAEND, FALSE); } return result; @@ -2664,7 +2664,7 @@ static void block_unmap_inner(ptr_t start_addr, size_t len) ABORT("unmap: mmap() result differs from start_addr"); # if defined(CPPCHECK) || defined(LINT2) /* Explicitly store the resource handle to a global variable. */ - GC_noop1((word)result); + NOOP1_PTR(result); # endif # endif GC_unmapped_bytes += len; @@ -2717,7 +2717,7 @@ GC_INNER void GC_remap(ptr_t start, size_t bytes) } } # ifdef LINT2 - GC_noop1((word)result); + NOOP1_PTR(result); # endif GC_ASSERT(GC_unmapped_bytes >= alloc_len); GC_unmapped_bytes -= alloc_len; @@ -2744,7 +2744,7 @@ GC_INNER void GC_remap(ptr_t start, size_t bytes) if (result != start_addr) ABORT("remap: mmap() result differs from start_addr"); # if defined(CPPCHECK) || defined(LINT2) - GC_noop1((word)result); + NOOP1_PTR(result); # endif # undef IGNORE_PAGES_EXECUTABLE # else @@ -3502,7 +3502,7 @@ GC_API GC_push_other_roots_proc GC_CALL GC_get_push_other_roots(void) # endif # endif /* !MSWIN32 && !MSWINCE */ # if defined(CPPCHECK) && defined(ADDRESS_SANITIZER) - GC_noop1((word)&__asan_default_options); + NOOP1_PTR(&__asan_default_options); # endif return TRUE; } diff --git a/pthread_support.c b/pthread_support.c index 3407d6df4..20aeb3979 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -2109,7 +2109,7 @@ GC_API void * GC_CALL GC_call_with_gc_active(GC_fn_type fn, void *client_data) GC_ASSERT(me -> crtn == crtn); GC_ASSERT(crtn -> traced_stack_sect == &stacksect); # ifdef CPPCHECK - GC_noop1((word)(crtn -> traced_stack_sect)); + NOOP1_PTR(crtn -> traced_stack_sect); # endif crtn -> traced_stack_sect = stacksect.prev; # ifdef E2K diff --git a/specific.c b/specific.c index b2dd225fe..c801fa660 100644 --- a/specific.c +++ b/specific.c @@ -134,7 +134,7 @@ GC_INNER void GC_remove_specific_after_fork(tsd * key, pthread_t t) /* This can only happen if the concurrent access is from another */ /* thread, and hence has missed the cache, but still... */ # ifdef LINT2 - GC_noop1((word)entry); + NOOP1_PTR(entry); # endif /* With GC, we're done, since the pointers from the cache will */ diff --git a/tests/gctest.c b/tests/gctest.c index 67e373475..d64f9d55b 100644 --- a/tests/gctest.c +++ b/tests/gctest.c @@ -857,7 +857,7 @@ static void *GC_CALLBACK reverse_test_inner(void *data) } # if defined(CPPCHECK) - GC_noop1((GC_word)data); + NOOP1_PTR(data); # endif # ifndef BIG # if defined(MACOS) \ @@ -1451,7 +1451,7 @@ static void typed_test(void) newP = (GC_word *)old[1]; } GC_gcollect(); - GC_noop1((GC_word)x); + NOOP1_PTR(x); } #endif /* !NO_TYPED_TEST */ @@ -1667,7 +1667,7 @@ static void run_one_test(void) GC_printf("Out of memory in GC_posix_memalign\n"); exit(69); } - GC_noop1((GC_word)p); + NOOP1_PTR(p); AO_fetch_and_add1(&collectable_count); } # ifndef GC_NO_VALLOC @@ -1714,7 +1714,7 @@ static void run_one_test(void) FAIL; } # if defined(CPPCHECK) - GC_noop1((GC_word)x); + NOOP1_PTR(x); # endif # ifdef GC_REQUIRE_WCSDUP { @@ -1983,8 +1983,8 @@ static void test_long_mult(void) LONG_MULT(hp, lp, (unsigned32)0x1234567UL, (unsigned32)0xfedcba98UL); # if defined(CPPCHECK) - GC_noop1((GC_word)(&hp)); - GC_noop1((GC_word)(&lp)); + NOOP1_PTR(&hp); + NOOP1_PTR(&lp); # endif if (hp != (unsigned32)0x121fa00UL || lp != (unsigned32)0x23e20b28UL) { GC_printf("LONG_MULT gives wrong result\n"); @@ -2221,7 +2221,7 @@ void SetMinimumStack(long minSize) static void GC_CALLBACK warn_proc(char *msg, GC_word p) { # if defined(CPPCHECK) - GC_noop1((GC_word)msg); + NOOP1_PTR(msg); # endif GC_printf(msg, (unsigned long)p); /*FAIL;*/ diff --git a/win32_threads.c b/win32_threads.c index f15d79711..5cd23df09 100644 --- a/win32_threads.c +++ b/win32_threads.c @@ -740,7 +740,7 @@ static ptr_t copy_ptr_regs(word *regs, const CONTEXT *pcontext) { PUSH4(IntT10,IntT11,IntT12,IntAt); sp = (ptr_t)context.IntSp; # elif defined(CPPCHECK) - GC_noop1((word)regs); + NOOP1_PTR(regs); sp = (ptr_t)(word)cnt; /* to workaround "cnt not used" false positive */ # else # error Architecture is not supported @@ -1402,7 +1402,7 @@ STATIC void *GC_CALLBACK GC_win32_start_inner(struct GC_stack_base *sb, (long)GetCurrentThreadId()); # endif # if defined(CPPCHECK) - GC_noop1((word)sb); + NOOP1_PTR(sb); # endif return ret; }