From 5826a077f9415cab88f90553fbfdeaabb439a53d Mon Sep 17 00:00:00 2001
From: Mikael Vidstedt <mikael@openjdk.org>
Date: Tue, 10 Jan 2023 04:39:53 +0000
Subject: [PATCH 1/7] 8299693: Change to Xcode12.4+1.1 devkit for building on
 macOS at Oracle

Reviewed-by: erikj
Backport-of: 4b6809b94a09871712df7a1c51b7192adbe2093b
---
 make/conf/jib-profiles.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/make/conf/jib-profiles.js b/make/conf/jib-profiles.js
index ccd1558b80f..51f840a826a 100644
--- a/make/conf/jib-profiles.js
+++ b/make/conf/jib-profiles.js
@@ -1035,7 +1035,7 @@ var getJibProfilesDependencies = function (input, common) {
 
     var devkit_platform_revisions = {
         linux_x64: "gcc11.2.0-OL6.4+1.0",
-        macosx: "Xcode12.4+1.0",
+        macosx: "Xcode12.4+1.1",
         windows_x64: "VS2022-17.1.0+1.0",
         linux_aarch64: "gcc11.2.0-OL7.6+1.0",
         linux_arm: "gcc8.2.0-Fedora27+1.0",

From 21d468e5751b082edc8db919e378fbb1cc6dc9ad Mon Sep 17 00:00:00 2001
From: Nick Gasson <ngasson@openjdk.org>
Date: Tue, 10 Jan 2023 13:29:55 +0000
Subject: [PATCH 2/7] 8299733: AArch64: "unexpected literal addressing mode"
 assertion failure with -XX:+PrintC1Statistics

Co-authored-by: Ningsheng Jian <njian@openjdk.org>
Reviewed-by: chagedorn, fyang, aph
---
 src/hotspot/cpu/aarch64/assembler_aarch64.hpp | 17 +++++++++++++++--
 test/hotspot/jtreg/ProblemList-Xcomp.txt      |  4 +---
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/hotspot/cpu/aarch64/assembler_aarch64.hpp b/src/hotspot/cpu/aarch64/assembler_aarch64.hpp
index 5c0adf45d27..1f7ca7fcb56 100644
--- a/src/hotspot/cpu/aarch64/assembler_aarch64.hpp
+++ b/src/hotspot/cpu/aarch64/assembler_aarch64.hpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2014, 2021, Red Hat Inc. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
@@ -505,7 +505,20 @@ class Address {
   }
 
   bool uses(Register reg) const {
-    return base() == reg || index() == reg;
+    switch (_mode) {
+    case literal:
+    case no_mode:
+      return false;
+    case base_plus_offset:
+    case base_plus_offset_reg:
+    case pre:
+    case post:
+    case post_reg:
+      return base() == reg || index() == reg;
+    default:
+      ShouldNotReachHere();
+      return false;
+    }
   }
 
   address target() const {
diff --git a/test/hotspot/jtreg/ProblemList-Xcomp.txt b/test/hotspot/jtreg/ProblemList-Xcomp.txt
index 3596de61c22..11c236c9f99 100644
--- a/test/hotspot/jtreg/ProblemList-Xcomp.txt
+++ b/test/hotspot/jtreg/ProblemList-Xcomp.txt
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -38,5 +38,3 @@ serviceability/sa/TestJhsdbJstackMixed.java 8248675 linux-aarch64
 serviceability/jvmti/VMObjectAlloc/VMObjectAllocTest.java 8288430 generic-all
 
 gc/cslocker/TestCSLocker.java 8293289 generic-x64
-
-compiler/c1/TestPrintC1Statistics.java 8298053 linux-aarch64

From 151450ea9b78243130eb89a1c8ea9ad7ac13fb4a Mon Sep 17 00:00:00 2001
From: Patricio Chilano Mateo <pchilanomate@openjdk.org>
Date: Tue, 10 Jan 2023 17:16:26 +0000
Subject: [PATCH 3/7] 8294744: AArch64:
 applications/kitchensink/Kitchensink.java crashed:
 assert(oopDesc::is_oop(obj)) failed: not an oop

Co-authored-by: Fei Yang <fyang@openjdk.org>
Reviewed-by: aph, fyang, dcubed
---
 .../cpu/aarch64/templateInterpreterGenerator_aarch64.cpp   | 7 +++++--
 .../cpu/riscv/templateInterpreterGenerator_riscv.cpp       | 6 +++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/hotspot/cpu/aarch64/templateInterpreterGenerator_aarch64.cpp b/src/hotspot/cpu/aarch64/templateInterpreterGenerator_aarch64.cpp
index eeb080d586a..eca8b7f1ca4 100644
--- a/src/hotspot/cpu/aarch64/templateInterpreterGenerator_aarch64.cpp
+++ b/src/hotspot/cpu/aarch64/templateInterpreterGenerator_aarch64.cpp
@@ -836,9 +836,12 @@ void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
     __ stp(r10, rscratch1, Address(sp, 4 * wordSize));
     // Move SP out of the way
     __ mov(sp, rscratch1);
-    } else {
-    __ mov(rscratch1, sp);
+  } else {
+    // Make sure there is room for the exception oop pushed in case method throws
+    // an exception (see TemplateInterpreterGenerator::generate_throw_exception())
+    __ sub(rscratch1, sp, 2 * wordSize);
     __ stp(zr, rscratch1, Address(sp, 4 * wordSize));
+    __ mov(sp, rscratch1);
   }
 }
 
diff --git a/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp b/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp
index 49a2b87c232..213e360c20e 100644
--- a/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp
+++ b/src/hotspot/cpu/riscv/templateInterpreterGenerator_riscv.cpp
@@ -776,8 +776,12 @@ void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call) {
     // Move SP out of the way
     __ mv(sp, t0);
   } else {
-    __ sd(sp, Address(sp, 5 * wordSize));
+    // Make sure there is room for the exception oop pushed in case method throws
+    // an exception (see TemplateInterpreterGenerator::generate_throw_exception())
+    __ sub(t0, sp, 2 * wordSize);
+    __ sd(t0, Address(sp, 5 * wordSize));
     __ sd(zr, Address(sp, 4 * wordSize));
+    __ mv(sp, t0);
   }
 }
 

From de79162fdf122236fd518a51fd47aec75daf2948 Mon Sep 17 00:00:00 2001
From: Xiaohong Gong <xgong@openjdk.org>
Date: Wed, 11 Jan 2023 01:49:56 +0000
Subject: [PATCH 4/7] 8299715: IR test: VectorGatherScatterTest.java fails with
 SVE randomly

Reviewed-by: psandoz, thartmann
---
 .../jtreg/compiler/vectorapi/VectorGatherScatterTest.java | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/test/hotspot/jtreg/compiler/vectorapi/VectorGatherScatterTest.java b/test/hotspot/jtreg/compiler/vectorapi/VectorGatherScatterTest.java
index fe626e5246d..4298f1d7000 100644
--- a/test/hotspot/jtreg/compiler/vectorapi/VectorGatherScatterTest.java
+++ b/test/hotspot/jtreg/compiler/vectorapi/VectorGatherScatterTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022, Arm Limited. All rights reserved.
+ * Copyright (c) 2022, 2023, Arm Limited. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -79,7 +79,7 @@ public class VectorGatherScatterTest {
             ia[i] = i;
             la[i] = RD.nextLong(25);
             da[i] = RD.nextDouble(25.0);
-            m[i] = RD.nextBoolean();
+            m[i] = i % 2 == 0;
         }
     }
 
@@ -102,6 +102,8 @@ public class VectorGatherScatterTest {
     @IR(counts = { IRNode.LOAD_VECTOR_GATHER_MASKED, ">= 1" })
     public static void testLoadGatherMasked() {
         VectorMask<Long> mask = VectorMask.fromArray(L_SPECIES, m, 0);
+        // "mask" is guaranteed to be not alltrue, in case the masked
+        // gather load is optimized to the non-masked version.
         LongVector av = LongVector.fromArray(L_SPECIES, la, 0, ia, 0, mask);
         av.intoArray(lr, 0);
         IntVector bv = IntVector.fromArray(I_SPECIES, ia, 0);
@@ -132,6 +134,8 @@ public class VectorGatherScatterTest {
     public static void testStoreScatterMasked() {
         VectorMask<Double> mask = VectorMask.fromArray(D_SPECIES, m, 0);
         DoubleVector av = DoubleVector.fromArray(D_SPECIES, da, 0);
+        // "mask" is guaranteed to be not alltrue, in case the masked
+        // scatter store is optimized to the non-masked version.
         av.intoArray(dr, 0, ia, 0, mask);
         IntVector bv = IntVector.fromArray(I_SPECIES, ia, 0);
         bv.add(0).intoArray(ir, 0);

From 0abb87a488e99cdbc418e14411a6bbf7a3f28079 Mon Sep 17 00:00:00 2001
From: Abhishek Kumar <abhiscxk@openjdk.org>
Date: Wed, 11 Jan 2023 07:42:42 +0000
Subject: [PATCH 5/7] 8299227: host `exif.org` not found in link in doc comment

Reviewed-by: prr, serb
---
 .../sun/imageio/plugins/tiff/TIFFImageWriteParam.java    | 7 ++++---
 .../javax/imageio/metadata/doc-files/tiff_metadata.html  | 9 +++++----
 src/java.desktop/share/native/libjavajpeg/imageioJPEG.c  | 8 +++++---
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageWriteParam.java b/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageWriteParam.java
index a012ac990fd..5005f096bab 100644
--- a/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageWriteParam.java
+++ b/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageWriteParam.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -78,8 +78,9 @@ import javax.imageio.ImageWriteParam;
  * <tr>
  * <td>Exif JPEG</td>
  * <td>Exif-specific JPEG compression (see note following this table)</td>
- * <td><a href="http://www.exif.org/Exif2-2.PDF">Exif 2.2 Specification</a>
- * (PDF), section 4.5.5, "Basic Structure of Thumbnail Data"</td>
+ * <td><a href="https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf">
+ * Exif 2.3 Specification</a>
+ * (PDF), section 4.5.8, "Basic Structure of Thumbnail Data"</td>
  * </table>
  *
  * <p>
diff --git a/src/java.desktop/share/classes/javax/imageio/metadata/doc-files/tiff_metadata.html b/src/java.desktop/share/classes/javax/imageio/metadata/doc-files/tiff_metadata.html
index 9bc70cd96d4..ee6b4a8fe10 100644
--- a/src/java.desktop/share/classes/javax/imageio/metadata/doc-files/tiff_metadata.html
+++ b/src/java.desktop/share/classes/javax/imageio/metadata/doc-files/tiff_metadata.html
@@ -5,7 +5,7 @@
     <title>TIFF Metadata Format Specification and Usage Notes</title>
 </head>
 <!--
-Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
+Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
 This code is free software; you can redistribute it and/or modify it
@@ -574,8 +574,9 @@ DEFLATE Compressed Data Format Specification</a></td>
 <th scope="row">9</th>
 <td>Exif JPEG</td>
 <td>Exif-specific JPEG compression (see note following this table)</td>
-<td><a href="http://www.exif.org/Exif2-2.PDF">Exif 2.2 Specification</a>
-(PDF), section 4.5.5, "Basic Structure of Thumbnail Data"</td>
+<td><a href="https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf">
+    Exif 2.3 Specification</a>
+(PDF), section 4.5.8, "Basic Structure of Thumbnail Data"</td>
 </tbody>
 </table>
 
@@ -739,7 +740,7 @@ color space is grayscale.</li>
 be written as supplied.</p>
 
 <p>If an Exif image is being written, the set of fields present and their
-values will be modified such that the result is in accord with the Exif 2.2
+values will be modified such that the result is in accord with the Exif 2.3
 specification.</p>
 
 <p>Setting up the image metadata to write to a TIFF stream may be simplified
diff --git a/src/java.desktop/share/native/libjavajpeg/imageioJPEG.c b/src/java.desktop/share/native/libjavajpeg/imageioJPEG.c
index 4ebd565fe35..6b1b9e4f99b 100644
--- a/src/java.desktop/share/native/libjavajpeg/imageioJPEG.c
+++ b/src/java.desktop/share/native/libjavajpeg/imageioJPEG.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1614,7 +1614,8 @@ Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setSource
 /*
  * For EXIF images, the APP1 will appear immediately after the SOI,
  * so it's safe to only look at the first marker in the list.
- * (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 58)
+ * (see https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf,
+ * section 4.7, page 83)
  */
 #define IS_EXIF(c) \
     (((c)->marker_list != NULL) && ((c)->marker_list->marker == JPEG_APP1))
@@ -1715,7 +1716,8 @@ Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImageHeader
              *  - we got JFIF image
              *     Must be YCbCr (see http://www.w3.org/Graphics/JPEG/jfif3.pdf, page 2)
              *  - we got EXIF image
-             *     Must be YCbCr (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 63)
+             *     Must be YCbCr (see https://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf,
+             *     section 4.7, page 88)
              *  - something else
              *     Apply heuristical rules to identify actual colorspace.
              */

From 636976ada8773474a5540234a38667668349b30b Mon Sep 17 00:00:00 2001
From: Jan Lahoda <jlahoda@openjdk.org>
Date: Wed, 11 Jan 2023 07:52:18 +0000
Subject: [PATCH 6/7] 8299849: Revert JDK-8294461: wrong effectively final
 determination by javac

Reviewed-by: vromero
---
 .../classes/com/sun/tools/javac/comp/Flow.java     | 11 ++++++-----
 .../8294461/EffectivelyFinalLoopIncrement.java     | 14 --------------
 .../8294461/EffectivelyFinalLoopIncrement.out      |  2 --
 3 files changed, 6 insertions(+), 21 deletions(-)
 delete mode 100644 test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.java
 delete mode 100644 test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.out

diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
index 7269f0a7ee3..0c151058b18 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
@@ -2027,13 +2027,14 @@ public class Flow {
         void letInit(DiagnosticPosition pos, VarSymbol sym) {
             if (sym.adr >= firstadr && trackable(sym)) {
                 if ((sym.flags() & EFFECTIVELY_FINAL) != 0) {
-                    if (inits.isMember(sym.adr) || !uninits.isMember(sym.adr)) {
-                        //assignment targeting an effectively final variable makes the
-                        //variable lose its status of effectively final if the variable
-                        //is definitely assigned or _not_ definitively unassigned
+                    if (!uninits.isMember(sym.adr)) {
+                        //assignment targeting an effectively final variable
+                        //makes the variable lose its status of effectively final
+                        //if the variable is _not_ definitively unassigned
                         sym.flags_field &= ~EFFECTIVELY_FINAL;
+                    } else {
+                        uninit(sym);
                     }
-                    uninit(sym);
                 }
                 else if ((sym.flags() & FINAL) != 0) {
                     if ((sym.flags() & PARAMETER) != 0) {
diff --git a/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.java b/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.java
deleted file mode 100644
index b920368bea8..00000000000
--- a/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.java
+++ /dev/null
@@ -1,14 +0,0 @@
-/*
- * @test /nodynamiccopyright/
- * @summary Verify for() loop variable not effectively final even if loop never increments
- * @bug 8294461
- * @compile/fail/ref=EffectivelyFinalLoopIncrement.out -XDrawDiagnostics EffectivelyFinalLoopIncrement.java
- */
-class EffectivelyFinalLoopIncrement {
-    EffectivelyFinalLoopIncrement() {
-        for (int i = 0; i < 10; i++) {
-            Runnable r = () -> System.out.println(i);   // variable i is NOT effectively final
-            break;                                      // even though "i++" is never reached
-        }
-    }
-}
diff --git a/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.out b/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.out
deleted file mode 100644
index 8fbf3ab69b1..00000000000
--- a/test/langtools/tools/javac/lambda/8294461/EffectivelyFinalLoopIncrement.out
+++ /dev/null
@@ -1,2 +0,0 @@
-EffectivelyFinalLoopIncrement.java:10:51: compiler.err.cant.ref.non.effectively.final.var: i, (compiler.misc.lambda)
-1 error

From 945ef07564bb2e7db9743d07d7d9ac7faa3f3d4d Mon Sep 17 00:00:00 2001
From: Maurizio Cimadamore <mcimadamore@openjdk.org>
Date: Wed, 11 Jan 2023 10:31:25 +0000
Subject: [PATCH 7/7] 8299862: OfAddress setter should disallow heap segments

Reviewed-by: jvernee
---
 .../java/lang/foreign/MemorySegment.java        |  2 ++
 .../classes/java/lang/invoke/MethodHandles.java |  5 ++++-
 .../classes/jdk/internal/foreign/Utils.java     |  5 +++--
 .../jdk/internal/foreign/abi/SharedUtils.java   |  2 +-
 .../java/foreign/TestMemoryAccessInstance.java  | 17 +++++++++++++++++
 5 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/java.base/share/classes/java/lang/foreign/MemorySegment.java b/src/java.base/share/classes/java/lang/foreign/MemorySegment.java
index a1da55b8c0e..11633df2ce7 100644
--- a/src/java.base/share/classes/java/lang/foreign/MemorySegment.java
+++ b/src/java.base/share/classes/java/lang/foreign/MemorySegment.java
@@ -1734,6 +1734,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
      * @throws IndexOutOfBoundsException when the access operation falls outside the <em>spatial bounds</em> of the
      * memory segment.
      * @throws UnsupportedOperationException if this segment is {@linkplain #isReadOnly() read-only}.
+     * @throws UnsupportedOperationException if {@code value} is not a {@linkplain #isNative() native} segment.
      */
     @ForceInline
     default void set(ValueLayout.OfAddress layout, long offset, MemorySegment value) {
@@ -2079,6 +2080,7 @@ public sealed interface MemorySegment permits AbstractMemorySegmentImpl {
      * @throws IndexOutOfBoundsException when the access operation falls outside the <em>spatial bounds</em> of the
      * memory segment.
      * @throws UnsupportedOperationException if this segment is {@linkplain #isReadOnly() read-only}.
+     * @throws UnsupportedOperationException if {@code value} is not a {@linkplain #isNative() native} segment.
      */
     @ForceInline
     default void setAtIndex(ValueLayout.OfAddress layout, long index, MemorySegment value) {
diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
index a63f53d3ca1..4c901afefb1 100644
--- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
+++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
@@ -7931,8 +7931,11 @@ assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
      * {@code get} and {@code set} access modes will result in an {@code IllegalStateException}. If access is partially aligned,
      * atomic access is only guaranteed with respect to the largest power of two that divides the GCD of {@code A} and {@code S}.
      * <p>
-     * Finally, in all other cases, we say that a memory access operation is <em>misaligned</em>; in such cases an
+     * In all other cases, we say that a memory access operation is <em>misaligned</em>; in such cases an
      * {@code IllegalStateException} is thrown, irrespective of the access mode being used.
+     * <p>
+     * Finally, if {@code T} is {@code MemorySegment} all write access modes throw {@link IllegalArgumentException}
+     * unless the value to be written is a {@linkplain MemorySegment#isNative() native} memory segment.
      *
      * @param layout the value layout for which a memory access handle is to be obtained.
      * @return the new memory segment view var handle.
diff --git a/src/java.base/share/classes/jdk/internal/foreign/Utils.java b/src/java.base/share/classes/jdk/internal/foreign/Utils.java
index f00c49bb442..facde705729 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/Utils.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/Utils.java
@@ -38,6 +38,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Supplier;
 import jdk.internal.access.SharedSecrets;
+import jdk.internal.foreign.abi.SharedUtils;
 import jdk.internal.vm.annotation.ForceInline;
 import static java.lang.foreign.ValueLayout.JAVA_BYTE;
 
@@ -62,8 +63,8 @@ public final class Utils {
                     MethodType.methodType(boolean.class, byte.class));
             BOOL_TO_BYTE = lookup.findStatic(Utils.class, "booleanToByte",
                     MethodType.methodType(byte.class, boolean.class));
-            ADDRESS_TO_LONG = lookup.findVirtual(MemorySegment.class, "address",
-                    MethodType.methodType(long.class));
+            ADDRESS_TO_LONG = lookup.findStatic(SharedUtils.class, "unboxSegment",
+                    MethodType.methodType(long.class, MemorySegment.class));
             LONG_TO_ADDRESS_SAFE = lookup.findStatic(Utils.class, "longToAddressSafe",
                     MethodType.methodType(MemorySegment.class, long.class));
             LONG_TO_ADDRESS_UNSAFE = lookup.findStatic(Utils.class, "longToAddressUnsafe",
diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java b/src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java
index 77e4e05659d..fb25a18a92d 100644
--- a/src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java
+++ b/src/java.base/share/classes/jdk/internal/foreign/abi/SharedUtils.java
@@ -259,7 +259,7 @@ public final class SharedUtils {
         }
     }
 
-    static long unboxSegment(MemorySegment segment) {
+    public static long unboxSegment(MemorySegment segment) {
         if (!segment.isNative()) {
             throw new IllegalArgumentException("Heap segment not allowed: " + segment);
         }
diff --git a/test/jdk/java/foreign/TestMemoryAccessInstance.java b/test/jdk/java/foreign/TestMemoryAccessInstance.java
index d2a8ba14f9c..e80c170ca65 100644
--- a/test/jdk/java/foreign/TestMemoryAccessInstance.java
+++ b/test/jdk/java/foreign/TestMemoryAccessInstance.java
@@ -29,6 +29,7 @@
 
 import java.lang.foreign.Arena;
 import java.lang.foreign.MemorySegment;
+import java.lang.foreign.SegmentScope;
 import java.lang.foreign.ValueLayout;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
@@ -132,6 +133,22 @@ public class TestMemoryAccessInstance {
         }
     }
 
+    @Test(expectedExceptions = IllegalArgumentException.class,
+            expectedExceptionsMessageRegExp = ".*Heap segment not allowed.*")
+    public void badHeapSegmentSet() {
+        MemorySegment targetSegment = MemorySegment.allocateNative(ValueLayout.ADDRESS.byteSize(), SegmentScope.auto());
+        MemorySegment segment = MemorySegment.ofArray(new byte[]{ 0, 1, 2 });
+        targetSegment.set(ValueLayout.ADDRESS, 0, segment); // should throw
+    }
+
+    @Test(expectedExceptions = IllegalArgumentException.class,
+            expectedExceptionsMessageRegExp = ".*Heap segment not allowed.*")
+    public void badHeapSegmentSetAtIndex() {
+        MemorySegment targetSegment = MemorySegment.allocateNative(ValueLayout.ADDRESS.byteSize(), SegmentScope.auto());
+        MemorySegment segment = MemorySegment.ofArray(new byte[]{ 0, 1, 2 });
+        targetSegment.setAtIndex(ValueLayout.ADDRESS, 0, segment); // should throw
+    }
+
     static final ByteOrder NE = ByteOrder.nativeOrder();
 
     @DataProvider(name = "segmentAccessors")