C++ delete operator undefined behavior

Kim Barrett kim.barrett at oracle.com
Sat Feb 9 18:16:16 UTC 2019


> 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).

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