libjava gcc-3_0-branch, klass timing out in testsuite again
Fergus Henderson
fjh@cs.mu.oz.au
Sat May 5 08:38:00 GMT 2001
On 05-May-2001, Jeff Sturm <jsturm@one-point.com> wrote:
>> That is, it behaves exactly as if these lines in Class.h do nothing:
>> // C++ ctors set the vtbl pointer to point at an offset inside the vtable
> // object. That doesn't work for Java, so this hack adjusts it back.
> void *p = ((void **)this)[0];
> ((void **)this)[0] = (void *)((char *)p-2*sizeof (void *));
>> Rebuilding libjava with -fno-strict-aliasing is the quick fix:
...
> Can some C++ person comment on the code fragment above in terms of
> aliasing?
The above code fragment violates the rule on aliasing
(3.10 [basic.lval] paragraph 15), because it accesses the object `*this'
with a different type. Hence it has undefined behaviour.
A possible fix is to use a union:
union Self {
void *vtable_ptr;
typeof(*this) self;
};
Self *self = (Self *)this;
self->vtable_ptr = (void *)((char *)self->vtable_ptr - 2 * sizeof (void *));
This is still not strictly conforming C++, but I think g++ supports it,
even with -fstrict-aliasing.
If you're going to be making assumptions about how vtables are layed out,
then I think it's safe to also assume that `char *' and `void *' have
the same representation, in which case you can write that a little more
readably using type `char *' for the vtable_ptr:
union Self {
char *vtable_ptr;
typeof(*this) self;
};
((Self *)this)->vtable_ptr -= 2 * sizeof (void *);
However, this is a matter of taste. You may prefer the other way.
You may also prefer hard-coding the class name instead of using
`typeof(*this)', especially since typeof is only supported in GNU C,
not GNU C++, I think. As you can see I haven't tested this code ;-)
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
| of excellence is a lethal habit"
WWW: < http://www.cs.mu.oz.au/~fjh > | -- the last words of T. S. Garp.
More information about the Java
mailing list