[cfe-dev] Is it a va_arg bug in clang?

Shi, Steven via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 7 07:06:59 PST 2016


Hello,
I'm trying to use the clang3.7 to compile  64bits x86 code with Microsoft ABI in Ubuntu 64bit, but I find the va_arg get the wrong variable argument from the va_list. Below is my test code.

#include <stdio.h>
#include <stdarg.h>

void
__attribute__((ms_abi))
Foo_va_list (
    int                       VaNum,
    const char  *Format,
  ...
  )
{
                va_list  Marker;
                long long    Value;

                va_start (Marker, Format);
                for (int i = 0; i < VaNum; i++ ) {
                                Value = va_arg (Marker, int);
                                printf("Value = 0x%llx\n", Value);
                }
                va_end (Marker);
}


int main()
{
  Foo_va_list (16, "0123456789abcdef= %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x \n", 0,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf);
  return 0;
}

The expected output are:
Value = 0x0
Value = 0x1
Value = 0x2
Value = 0x3
Value = 0x4
Value = 0x5
Value = 0x6
Value = 0x7
Value = 0x8
Value = 0x9
Value = 0xa
Value = 0xb
Value = 0xc
Value = 0xd
Value = 0xe
Value = 0xf

But the real output are:
Value = 0x0
Value = 0x1
Value = 0x2
Value = 0x3
Value = 0x4
Value = 0x5
Value = 0x0
Value = 0x1
Value = 0x2
Value = 0x3
Value = 0x4
Value = 0x5
Value = 0x6
Value = 0x7
Value = 0x8
Value = 0x9

After I remove the Microsoft ABI definition __attribute__((ms_abi)) above  Foo_va_list () in the code, the output will become correct. I find GCC also have similar wrong behavior with __attribute__((ms_abi)). I don't know how to make the __attribute__((ms_abi)) work correctly with va_arg.  I appreciate if any suggestion.






Steven Shi
Intel\SSG\STO\UEFI Firmware

Tel: +86 021-61166522
iNet: 821-6522

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160107/f2491347/attachment.html>


More information about the cfe-dev mailing list