Skip to content

Commit

Permalink
Workaround 'uninitialized variable sp passed to APPROX_SP' cppcheck FP
Browse files Browse the repository at this point in the history
(fix of commit 0fb49af)

* include/private/gc_priv.h (APPROX_SP): Rename to STORE_APPROX_SP_TO;
change argument type from ptr_t* to ptr_t& (lvalue); update comment.
* mark_rts.c (GC_approx_sp): Use STORE_APPROX_SP_TO(sp) instead of
APPROX_SP(&sp).
* misc.c (GC_call_with_stack_base): Likewise.
* misc.c [!THREADS] (GC_call_with_gc_active): Likewise.
* pthread_support.c (GC_call_with_gc_active): Likewise.
* tools/setjmp_t.c (nested_sp, main): Likewise.
  • Loading branch information
ivmai committed Oct 22, 2024
1 parent 6bbe4eb commit a7d01bc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions include/private/gc_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1946,23 +1946,23 @@ GC_INNER void GC_push_all_register_sections(
/* Return the current stack pointer, approximately. */
GC_INNER ptr_t GC_approx_sp(void);

/* Same as GC_approx_sp() but a macro. sp_ptr should be a pointer */
/* to a local variable of volatile ptr_t type. */
/* Same as GC_approx_sp() but a macro. sp should be a local */
/* variable of volatile ptr_t type. */
#if (defined(E2K) && defined(__clang__) \
|| (defined(S390) && __clang_major__ < 8)) \
&& !defined(CPPCHECK)
/* Workaround some bugs in clang: */
/* "undefined reference to llvm.frameaddress" error (clang-9/e2k); */
/* a crash in SystemZTargetLowering of libLLVM-3.8 (s390). */
# define APPROX_SP(sp_ptr) (void)(*(sp_ptr) = (ptr_t)(sp_ptr))
# define STORE_APPROX_SP_TO(sp) (void)(sp = (ptr_t)(&sp))
#elif defined(CPPCHECK) \
|| (__GNUC__ >= 4 /* GC_GNUC_PREREQ(4, 0) */ \
&& !defined(STACK_NOT_SCANNED))
/* TODO: Use GC_GNUC_PREREQ after fixing a bug in cppcheck. */
# define APPROX_SP(sp_ptr) \
(void)(*(sp_ptr) = (ptr_t)__builtin_frame_address(0))
/* Note: lvalue is passed instead of pointer to sp (because of cppcheck). */
# define STORE_APPROX_SP_TO(sp) (void)(sp = (ptr_t)__builtin_frame_address(0))
#else
# define APPROX_SP(sp_ptr) (void)(*(sp_ptr) = (ptr_t)(sp_ptr))
# define STORE_APPROX_SP_TO(sp) (void)(sp = (ptr_t)(&sp))
#endif

GC_INNER GC_bool GC_should_collect(void);
Expand Down
2 changes: 1 addition & 1 deletion mark_rts.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ GC_approx_sp(void)
/* This also forces stack to grow if necessary. Otherwise the */
/* later accesses might cause the kernel to think we are doing */
/* something wrong. */
APPROX_SP(&sp);
STORE_APPROX_SP_TO(sp);
return (/* no volatile */ ptr_t)sp;
}

Expand Down
4 changes: 2 additions & 2 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,7 @@ GC_call_with_stack_base(GC_stack_base_func fn, void *arg)
struct GC_stack_base base;
void *result;

APPROX_SP((volatile ptr_t *)&base.mem_base);
STORE_APPROX_SP_TO(*(volatile ptr_t *)&base.mem_base);
#ifdef IA64
base.reg_base = GC_save_regs_in_stack();
/* TODO: Unnecessarily flushes register stack, */
Expand Down Expand Up @@ -2438,7 +2438,7 @@ GC_call_with_gc_active(GC_fn_type fn, void *client_data)
/* Adjust our stack bottom pointer (this could happen if */
/* GC_get_main_stack_base() is unimplemented or broken for */
/* the platform). Note: stacksect variable is reused here. */
APPROX_SP((volatile ptr_t *)&stacksect.saved_stack_ptr);
STORE_APPROX_SP_TO(*(volatile ptr_t *)&stacksect.saved_stack_ptr);
if (HOTTER_THAN(GC_stackbottom, stacksect.saved_stack_ptr))
GC_stackbottom = stacksect.saved_stack_ptr;

Expand Down
2 changes: 1 addition & 1 deletion pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ GC_call_with_gc_active(GC_fn_type fn, void *client_data)
/* GC_get_stack_base() was used which returned GC_SUCCESS). */
stack_end = crtn->stack_end; /* read of a volatile field */
GC_ASSERT(stack_end != NULL);
APPROX_SP((volatile ptr_t *)&stacksect.saved_stack_ptr);
STORE_APPROX_SP_TO(*(volatile ptr_t *)&stacksect.saved_stack_ptr);
if (HOTTER_THAN(stack_end, stacksect.saved_stack_ptr)) {
crtn->stack_end = stacksect.saved_stack_ptr;
# if defined(I386) && defined(GC_WIN32_THREADS)
Expand Down
4 changes: 2 additions & 2 deletions tools/setjmp_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ nested_sp(void)
{
volatile ptr_t sp;

APPROX_SP(&sp);
STORE_APPROX_SP_TO(sp);
return ADDR(sp);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ main(void)
static volatile int y = 0;
#endif

APPROX_SP(&sp);
STORE_APPROX_SP_TO(sp);
printf("This appears to be a %s running %s\n", MACH_TYPE, OS_TYPE);
#if defined(CPPCHECK)
(void)nested_sp(); /* to workaround a bug in cppcheck */
Expand Down

0 comments on commit a7d01bc

Please sign in to comment.