x86 64 bit function argument bug?

Stephen Biggs xyzzy@hotpop.com
Sun May 4 13:49:00 GMT 2003


On Sun, 2003-05-04 at 11:22, Falk Hueffner wrote:
> Stephen Biggs <xyzzy@hotpop.com> writes:
> 
> > The following weird 2 lines of C code (yes, I know taking the address of
> > a disappearing entity is an evil thing!):
> > int *i ( int arg ) { return &arg; }
> > long long *ll ( long long arg ) { return &arg; }
> 
> Since it is impossible to use the resulting pointer without invoking
> undefined behaviour, I don't think it is particularly interesting what
> code is generated. We should probably warn about that.

I realize that this might be a bad example, since, yes, the demo code
does not do anything, but believe me, I have a use for taking the
address of an argument, and this weirdness is messing me up big-time.

> 
> > Note that the 2.4 Linux kernel depends on this compiling as it expects
> > in at least one place that I know of:
> > 
> > net/core/neighbour.c
> > void neigh_destroy(struct neighbour *neigh)
> > {
> >         struct hh_cache *hh;
> > 
> >         if (!neigh->dead) {
> >                 printk("Destroying alive neighbour %p from %08lx\n",
> >                        neigh,
> >                        *(((unsigned long*)&neigh)-1));
> >                 return;
> >         }
> 
> This is different. The address of the argument is not returned.
> However, this code contains undefined pointer arithmetics plus an
> aliasing violation, and just generally doesn't make a lot of sense.

I am not being understood.  I do not want to take the address of an
argument and return it.  I only wrote the demo code the way I did to try
to make sure that the generated code was not optimized out.  Again, it
looks like this code in the kernel is relying on a hack and undefined
behavior of GCC.

> 
> > If it is not a bug and is expected behavior, can anybody think of
> > any sort of fairly portable work around?
> 
> Maybe if you told us what that code is supposed to do.

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.  Basically I am
trying to do varargs/stdarg without using varargs or any built-in
functions.

> 
> -- 
> 	Falk
> 





More information about the Gcc mailing list