C++ delete operator undefined behavior

Ioi Lam ioi.lam at oracle.com
Sat Feb 9 18:36:41 UTC 2019


On 2/9/19 10:16 AM, Kim Barrett wrote:
>> On Feb 9, 2019, at 3:42 AM, Ioi Lam <ioi.lam at oracle.com> wrote:
>>
>> I am getting a "ud2" instruction on the Mac for a 'delete' expression. According to this page, it's clang flagging an undefined behavior:
>>
>> https://stackoverflow.com/questions/21529308/why-does-clang-generate-ud2-opcode-on-osx
>>
>> My code looks like this:
>>
>> class MetaspaceClosure {
>>    class Ref : public ResourceObj {
>>    ...
>>    };
> You have changed Ref to derived from ResourceObj; in current mainline it has
> no baseclass.  Ref is abstract, but in mainline doesn’t have a virtual destructor
> (which is a bug; it probably should also be non-copyable).  delete through a
> pointer to a base class (which Ref* certainly is, since Ref is abstract) that
> doesn’t have a virtual destructor is guaranteed slicing, e.g. UB.  So there you
> go.  Add the missing (empty) public virtual destructor for Ref (and for extra
> credit, poison copy and assign).

Hi Kim,

Thanks for the explanation. It starts to make sense now :-)

I am not familiar with "poisoning". Are there any examples in the 
hotspot code?

Also, why would clang stop putting in the 'ud2' instruction after I 
switched to subclassing from CHeapObj?

Thanks

- Ioi


> There are some gcc/clang warning options that we could be using that would
> report mistakes like this, but the last time I tried poking at that it seemed like
> a substantial tar baby.  But maybe somebody should try again and really follow
> through.
>


More information about the hotspot-dev mailing list