Path: blob/master/test/hotspot/jtreg/gc/shenandoah/compiler/TestUnsafeOffheapSwap.java
41153 views
/*1* Copyright (c) 2019, Red Hat, Inc. 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/**24* @test TestUnsafeOffheapSwap25* @summary Miscompilation in Unsafe off-heap swap routines26* @requires vm.gc.Shenandoah27* @modules java.base/jdk.internal.misc:+open28*29* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation30* -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC31* TestUnsafeOffheapSwap32*/3334import java.util.*;35import jdk.internal.misc.Unsafe;3637public class TestUnsafeOffheapSwap {3839static final int SIZE = 10000;40static final long SEED = 1;4142static final jdk.internal.misc.Unsafe UNSAFE = Unsafe.getUnsafe();43static final int SCALE = UNSAFE.ARRAY_INT_INDEX_SCALE;4445static Memory mem;46static int[] arr;4748public static void main(String[] args) throws Exception {49// Bug is exposed when memory.addr is not known statically50mem = new Memory(SIZE*SCALE);51arr = new int[SIZE];5253for (int i = 0; i < 10; i++) {54test();55}56}5758static void test() {59Random rnd = new Random(SEED);60for (int i = 0; i < SIZE; i++) {61int value = rnd.nextInt();62mem.setInt(i, value);63arr[i] = value;64}6566for (int i = 0; i < SIZE; i++) {67if (arr[i] != mem.getInt(i)) {68throw new IllegalStateException("TESTBUG: Values mismatch before swaps");69}70}7172for (int i = 1; i < SIZE; i++) {73mem.swap(i - 1, i);74int tmp = arr[i - 1];75arr[i - 1] = arr[i];76arr[i] = tmp;77}7879for (int i = 0; i < SIZE; i++) {80if (arr[i] != mem.getInt(i)) {81throw new IllegalStateException("Values mismatch after swaps");82}83}84}8586static class Memory {87private final long addr;8889Memory(int size) {90addr = UNSAFE.allocateMemory(size);91}9293public int getInt(int idx) {94return UNSAFE.getInt(addr + idx*SCALE);95}9697public void setInt(int idx, int val) {98UNSAFE.putInt(addr + idx*SCALE, val);99}100101public void swap(int a, int b) {102int tmp = getInt(a);103setInt(a, getInt(b));104setInt(b, tmp);105}106}107}108109110