Skip to content

Commit

Permalink
Resolve gcc out of bounds warning by using va_arg to acces varargs in…
Browse files Browse the repository at this point in the history
…stead of pointer arithmetic.
  • Loading branch information
insertinterestingnamehere committed Nov 10, 2023
1 parent 69a51f4 commit c2db460
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions src/fastcontext/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# include <config.h>
#endif

#include <stdarg.h>
#include <stdio.h>
#include "fastcontext/taskimpl.h"
#include <string.h> /* for memmove(), per C89 */
Expand All @@ -15,16 +16,6 @@
#include "qt_prefetch.h"
#include "qt_asserts.h"

#if defined(__GNUC__)
#define QT_SUPPRESS_BOUNDS_CHECK_WARNINGS \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Warray-bounds\"")
#define QT_END_SUPPRESS_BOUNDS_CHECK_WARNINGS _Pragma("GCC diagnostic pop")
#else
#define QT_SUPPRESS_BOUNDS_CHECK_WARNINGS
#define QT_END_SUPPRESS_BOUNDS_CHECK_WARNINGS
#endif

#ifdef NEEDPOWERMAKECONTEXT
void INTERNAL qt_makectxt(uctxt_t *ucp,
void (*func)(void),
Expand Down Expand Up @@ -56,25 +47,15 @@ void INTERNAL qt_makectxt(uctxt_t *ucp,
{
uintptr_t *sp;

# ifdef NEEDX86REGISTERARGS
//int i;
va_list argp;

va_start(argp, argc);
# endif

assert((uintptr_t)(ucp->uc_stack.ss_sp) > 1024);
sp = (uintptr_t *)(ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size); /* sp = top of stack */
sp -= argc; /* count down to where 8(%rsp) should be */
sp = (void *)((uintptr_t)sp - (uintptr_t)sp % 16); /* 16-align for OS X */
/* now copy from my arg list to the function's arglist */
#if 0
memcpy(sp, &argc + 1, argc * sizeof(uintptr_t));
#else
QT_SUPPRESS_BOUNDS_CHECK_WARNINGS
*(uintptr_t*)sp = *(uintptr_t*)((&argc)+1);
QT_END_SUPPRESS_BOUNDS_CHECK_WARNINGS
#endif
va_list argp;
va_start(argp, argc);
*(uintptr_t*)sp = va_arg(argp, uintptr_t);
va_end(argp);
/*for (i=0; i<argc; ++i) {
* uintptr_t tmp = va_arg(argp, uintptr_t);
* sp[i] = tmp;
Expand All @@ -84,7 +65,7 @@ QT_END_SUPPRESS_BOUNDS_CHECK_WARNINGS
/* HOWEVER, the function may not be expecting to pull from the stack,
* several 64-bit architectures expect that args will be in the correct
* registers! */
ucp->mc.mc_edi = va_arg(argp, uintptr_t);
ucp->mc.mc_edi = *(uintptr_t*)sp;
# if 0
for (i = 0; i < argc; i++) {
switch (i) {
Expand All @@ -102,9 +83,6 @@ QT_END_SUPPRESS_BOUNDS_CHECK_WARNINGS
*--sp = 0; /* return address */
ucp->mc.mc_eip = (long)func;
ucp->mc.mc_esp = (long)sp;
# ifdef NEEDX86REGISTERARGS
va_end(argp);
# endif
}

#elif defined(NEEDTILEMAKECONTEXT)
Expand Down

0 comments on commit c2db460

Please sign in to comment.