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

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

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

Changes: https://git.openjdk.org/valhalla/pull/2625/files
  Webrev: https://webrevs.openjdk.org/?repo=valhalla&pr=2625&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8387673
  Stats: 47 lines in 4 files changed: 45 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/valhalla/pull/2625.diff
  Fetch: git fetch https://git.openjdk.org/valhalla.git pull/2625/head:pull/2625

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

Reply via email to