On Fri, 3 Jul 2026 12:38:04 GMT, Christian Hagedorn <[email protected]> 
wrote:

> The `clone()` intrinsic directly returns a value object because it has no 
> identity - the clone is trivially a no-op:
> https://github.com/openjdk/valhalla/blob/2af7c2350b22ed03850b6b6c146b622ca758f441/src/hotspot/share/opto/library_call.cpp#L6041-L6045
> 
> But this is incorrect if the value class does not implement `Cloneable`. In 
> this case, we must throw `CloneNotSupportedException` which is currently 
> missing.
> 
> The fix adds a check whether the value class implements `Cloneable`. If it 
> does, we execute the existing code no-op path. Otherwise, we could either do 
> a call to the runtime `clone()` implementation, as done for the existing slow 
> path of this intrinsic, or we simply emit an uncommon trap. I've decided to 
> use the latter approach since it's more straight forward and rather an 
> edge-case situation. I also included a `too_many_traps()` check to avoid 
> repeated recompilations.
> 
> Thanks,
> Christian
>  
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

This pull request has now been integrated.

Changeset: cea67ed9
Author:    Christian Hagedorn <[email protected]>
URL:       
https://git.openjdk.org/valhalla/commit/cea67ed942e1b56fa20723702e4e0038429cec34
Stats:     140 lines in 4 files changed: 138 ins; 0 del; 2 mod

8387673: [lworld] C2: clone() intrinsic does not throw 
CloneNotSupportedException for value classes not implementing Cloneable

Reviewed-by: thartmann

-------------

PR: https://git.openjdk.org/valhalla/pull/2625

Reply via email to