x86 64 bit function argument bug?,..
Stephen Biggs
xyzzy@hotpop.com
Sun May 4 16:49:00 GMT 2003
On Sun, 2003-05-04 at 14:02, Falk Hueffner wrote:
> Stephen Biggs <xyzzy@hotpop.com> writes:
>
> > On Sun, 2003-05-04 at 11:54, Falk Hueffner wrote:
> > > Stephen Biggs <xyzzy@hotpop.com> writes:
> > > > I want to take an address of an argument and dereference it to
> > > > access the value that is passed to a function on the stack.
> > >
> > > That should work just fine. If it doesn't, it's a bug.
> >
> > Ah, now we understand each other. I am saying that the x86 behavior
> > for 64 bit value arguments is a bug.
>
> Maybe you can provide a test case without undefined behaviour that
> shows this?
>
> #include <stdio.h>
> void f(unsigned long long *p) {
> printf("%llx\n", *p);
> }
> void g(unsigned long long x) {
> f(&x);
> }
> int main(void) {
> g(0x1122334455667788ULL);
> }
>
> works for
Yes, this works because the hidden promotion gives the address of the
slot, NOT the argument list.
if you instead tried:
#include <stdio.h>
void f(unsigned int *p) {
printf("%x\n", *p);
}
void g(unsigned long long x,...) {
f((unsigned int *)(((char *)&x)+sizeof(x)));
}
int main(void) {
g(0x1122334455667788ULL,0x11223344U);
}
... you would get bogus results, if not a crash, because it is taking
the address of the quantity on the stack previous to the call instead of
the current argument list. If the long long was changed to an int or any
other quantity that is NOT 64 bit, the above code would work.
>
> > > > Basically I am trying to do varargs/stdarg without using varargs or
> > > > any built-in functions.
> > >
> > > That is impossible to do portably.
> >
> > Granted. I would be satisfied with it working in GCC, and it does on
> > the x86 except for 64 bit values.
>
> I'm pretty sure that is still impossible, unless you also restrict
> yourself to the x86 architecture.
>
> --
> Falk
>
More information about the Gcc
mailing list