I'm writing here because original gcc seems unaffected (asm code 
https://gcc.godbolt.org/ looks right).
I don't want to post it to bugtracker, because my English skills are strongly 
limited and someone else can write better report, so if you want, feel free to 
do this :)

Function with variable arguments causes compiler (C and C++) to emit bad code.
For ms_abi functions generated with LTO turned on program will crash and for 
sysv_abi program will generate wrong results.

Compiler: g++ (x86_64-posix-seh, Built by MinGW-W64 project) 6.1.0
System: Windows 10 x64
Target: x86_64-w64-mingw32

Example code:
--------------------------------------------------------------------------------
#include <cstdarg>
#include <cstdint>
#include <cstdio>

//__attribute__((sysv_abi))
void f1(void** x, ...) {
  va_list ap;
  va_start(ap, x);

  while (x != nullptr) {
    *x = nullptr;
    x = va_arg (ap, void**);
  }

  va_end(ap);
}

int main() {
  void* ptrs[] = {
    (void*)0x87654321,
    (void*)0x01234567,
    (void*)0xFFFFFFFF,
    (void*)0xBADC0FFEE0DDF00D,
  };

  f1(&ptrs[0],  &ptrs[1],  &ptrs[2],  &ptrs[3],  nullptr);
  printf("%p %p %p %p", ptrs[0],  ptrs[1],  ptrs[2],  ptrs[3]);
  
  return 0;
}
--------------------------------------------------------------------------------
Cmdline:
g++ test.cpp -o test.exe -flto
test
--------------------------------------------------------------------------------
Results:
PASS: ms_abi --> 0000000000000000 0000000000000000 0000000000000000 
0000000000000000
FAIL: ms_abi + LTO --> SIGSEGV
FAIL: sysv_abi --> 0000000000000000 0000000001234567 00000000ffffffff 
badc0ffee0ddf00d
FAIL: sysv_abi + LTO --> 0000000000000000 0000000001234567 0000000000000000 
0000000000000000

------------------------------------------------------------------------------
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to