https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121317

            Bug ID: 121317
           Summary: Incorrect info about variables possibly modified by
                    longjmp/vfork
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: taylor.hutt at broadcom dot com
  Target Milestone: ---

With gcc 15.1, the code below will print correct diagnostics, but due
to inlining of functions, the information about the location of the
failure, and the variable affected is incorrect.


gcc-defect.c: In function 'test':
gcc-defect.c:11:11: warning: variable 'msr' might be clobbered by 'longjmp' or
'vfork' [-Wclobbered]
   11 |    uint64 msr;
      |           ^~~
gcc-defect.c:11:11: warning: variable 'msr' might be clobbered by 'longjmp' or
'vfork' [-Wclobbered]

The variable that should be reported is 'msrVal'

If 'msrVal' is made volatile, the error is not presented.


/*
 * To compile:
 *
 *  gcc -m32 -Wclobbered -O1 -o gcc-defect.o -c gcc-defect.c
 *
 * I would have to do a lot of digging to determine at which SHA our
 * compiler was built.  If desired, I will provide that information.
 *
 * This will not produce any diagnostics on with 'gcc 15.1' on
 * godbolt.org, but will with 'gcc trunk'
 */

typedef unsigned int uint32;
typedef long long unsigned int uint64;

typedef struct context_t {
} context_t;


static inline uint64
rdmsr(uint32 cx)
{
   uint64 msr;
   __asm__ __volatile__(
      "rdmsr"
      : "=A" (msr)
      : "c" (cx)
   );

   return msr;
}

static inline void
wrmsr(uint32 cx, uint64 value)
{
   __asm__ __volatile__(
      "wrmsr"
      :
      : "A" (value),
        "c" (cx)
    );
}

int rand(void);
void print(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
int save_context(context_t *ctx) __attribute__((returns_twice));

void test(void)
{
   uint64 v      = rand();
   uint64 msrVal = rdmsr(0xfe);
   context_t __ctx;

   if (save_context(&__ctx)) {
      wrmsr(0xfe, v);
   }

   if (save_context(&__ctx)) {
      wrmsr(0xfe, msrVal);
   }
   msrVal = rdmsr(0xfe);

   if (rand()) {
      print("%llx", msrVal);
   }
}
  • [Bug c/121317] New: Incorrect... taylor.hutt at broadcom dot com via Gcc-bugs

Reply via email to