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

            Bug ID: 129251
           Summary: wrong code at -O2: copying from __seg_fs to __seg_gs
                    leaves the destination unchanged
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <asm/prctl.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>

static int source = 1;
static int destination = 2;

static long ap(unsigned long op, unsigned long arg)
{
  return syscall(SYS_arch_prctl, op, arg);
}

__attribute__((noinline))
static void copy(void)
{
  int __seg_fs *src = 0;
  int __seg_gs *dst = 0;
  *dst = *src;
}

int main(void)
{
  unsigned long old_fs = 0, old_gs = 0;
  if (ap(ARCH_GET_FS, (unsigned long)&old_fs)
      || ap(ARCH_GET_GS, (unsigned long)&old_gs)
      || ap(ARCH_SET_FS, (unsigned long)&source)
      || ap(ARCH_SET_GS, (unsigned long)&destination))
    return 2;
  copy();
  if (ap(ARCH_SET_FS, old_fs) || ap(ARCH_SET_GS, old_gs))
    return 3;
  printf("%d\n", destination);
}

godbolt: https://godbolt.org/z/4TjohdE1h

The program sets the FS base to source and the GS base to destination. It
prints 1 at -O0 but 2 at -O2; the copy must store source's value into
destination, so 1 is correct.
  • [Bug rtl-optimization/129251] Ne... kyuwoncho18 at gmail dot com via Gcc-bugs

Reply via email to