Path: blob/master/test/jdk/sun/misc/UnsafeFieldOffsets.java
41145 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8238358 824744425* @summary Ensure that sun.misc.Unsafe::objectFieldOffset and staticFieldOffset26* throw UnsupportedOperationException on Field of a hidden or record class27* @modules jdk.unsupported28* @compile --enable-preview -source ${jdk.version} UnsafeFieldOffsets.java29* @run testng/othervm --enable-preview UnsafeFieldOffsets30*/3132import sun.misc.Unsafe;3334import java.io.IOException;35import java.io.UncheckedIOException;36import java.lang.invoke.MethodHandles;37import java.lang.reflect.Field;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;4142import org.testng.annotations.Test;43import static org.testng.Assert.*;4445public class UnsafeFieldOffsets {46static class Fields {47static final Object STATIC_FINAL = new Object();48static Object STATIC_NON_FINAL = new Object();49final Object FINAL = new Object();50Object NON_FINAL = new Object();51}52record TestRecord(int i) {53static final Object STATIC_FINAL = new Object();54static Object STATIC_NON_FINAL = new Object();55}5657private static Unsafe UNSAFE = getUnsafe();58private static final Class<?> HIDDEN_CLASS = defineHiddenClass();59private static final Class<?> RECORD_CLASS = TestRecord.class;6061private static Unsafe getUnsafe() {62try {63Field f = Unsafe.class.getDeclaredField("theUnsafe");64f.setAccessible(true);65return (Unsafe) f.get(null);66} catch (ReflectiveOperationException e) {67throw new RuntimeException(e);68}69}7071private static Class<?> defineHiddenClass() {72String classes = System.getProperty("test.classes");73Path cf = Paths.get(classes, "UnsafeFieldOffsets$Fields.class");74try {75byte[] bytes = Files.readAllBytes(cf);76Class<?> c = MethodHandles.lookup().defineHiddenClass(bytes, true).lookupClass();77assertTrue(c.isHidden());78return c;79} catch (IOException e) {80throw new UncheckedIOException(e);81} catch (IllegalAccessException e) {82throw new RuntimeException(e);83}84}8586@Test87public void testNormalClass() throws Throwable {88// hidden class89testStaticField(HIDDEN_CLASS, "STATIC_FINAL");90testStaticField(HIDDEN_CLASS, "STATIC_NON_FINAL");91testInstanceField(HIDDEN_CLASS, "FINAL");92testInstanceField(HIDDEN_CLASS, "NON_FINAL");93}9495@Test96public void testHiddenClass() throws Throwable {97// hidden class98testStaticField(HIDDEN_CLASS, "STATIC_FINAL");99testStaticField(HIDDEN_CLASS, "STATIC_NON_FINAL");100testInstanceField(HIDDEN_CLASS, "FINAL");101testInstanceField(HIDDEN_CLASS, "NON_FINAL");102}103104@Test105public void testRecordClass() throws Throwable {106// record class107testRecordStaticField(RECORD_CLASS, "STATIC_FINAL");108testRecordStaticField(RECORD_CLASS, "STATIC_NON_FINAL");109testRecordInstanceField(RECORD_CLASS, "i");110}111112private static void testStaticField(Class<?> c, String name) throws Exception {113Field f = c.getDeclaredField(name);114try {115UNSAFE.staticFieldOffset(f);116assertFalse(c.isHidden(), "Expected UOE thrown: " + c);117} catch (UnsupportedOperationException e) {118assertTrue(c.isHidden(), "Expected hidden class: " + c);119}120}121122private static void testInstanceField(Class<?> c, String name) throws Exception {123Field f = c.getDeclaredField(name);124try {125UNSAFE.objectFieldOffset(f);126assertFalse(c.isHidden(), "Expected UOE thrown: " + c);127} catch (UnsupportedOperationException e) {128assertTrue(c.isHidden(), "Expected hidden class: " + c);129}130}131132@SuppressWarnings("preview")133private static void testRecordStaticField(Class<?> c, String name) throws Exception {134Field f = c.getDeclaredField(name);135try {136UNSAFE.staticFieldOffset(f);137assertFalse(c.isRecord(), "Expected UOE thrown: " + c);138} catch (UnsupportedOperationException e) {139assertTrue(c.isRecord(), "Expected record class: " + c);140}141}142143@SuppressWarnings("preview")144private static void testRecordInstanceField(Class<?> c, String name) throws Exception {145Field f = c.getDeclaredField(name);146try {147UNSAFE.objectFieldOffset(f);148assertFalse(c.isRecord(), "Expected UOE thrown: " + c);149} catch (UnsupportedOperationException e) {150assertTrue(c.isRecord(), "Expected record class: " + c);151}152}153}154155156