1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test TestUnsafeOffheapSwap
  26  * @summary Miscompilation in Unsafe off-heap swap routines
  27  * @key gc
  28  *
  29  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation
  30  *                   -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
  31  *                   TestUnsafeOffheapSwap
  32  */
  33 
  34 import java.util.*;
  35 import java.lang.reflect.*;
  36 import sun.misc.Unsafe;
  37 
  38 public class TestUnsafeOffheapSwap {
  39 
  40     static final int SIZE = 10000;
  41     static final long SEED = 1;
  42 
  43     static final sun.misc.Unsafe UNSAFE;
  44     static final int SCALE;
  45 
  46     static {
  47         try {
  48             Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
  49             f.setAccessible(true);
  50             UNSAFE = (sun.misc.Unsafe) f.get(null);
  51             SCALE = UNSAFE.ARRAY_INT_INDEX_SCALE;
  52         } catch (Exception e) {
  53             throw new RuntimeException("Unable to get Unsafe instance.", e);
  54         }
  55     }
  56 
  57     static Memory mem;
  58     static int[] arr;
  59 
  60     public static void main(String[] args) throws Exception {
  61         // Bug is exposed when memory.addr is not known statically
  62         mem = new Memory(SIZE*SCALE);
  63         arr = new int[SIZE];
  64 
  65         for (int i = 0; i < 10; i++) {
  66             test();
  67         }
  68     }
  69 
  70     static void test() {
  71         Random rnd = new Random(SEED);
  72         for (int i = 0; i < SIZE; i++) {
  73             int value = rnd.nextInt();
  74             mem.setInt(i, value);
  75             arr[i] = value;
  76         }
  77 
  78         for (int i = 0; i < SIZE; i++) {
  79             if (arr[i] != mem.getInt(i)) {
  80                 throw new IllegalStateException("TESTBUG: Values mismatch before swaps");
  81             }
  82         }
  83 
  84         for (int i = 1; i < SIZE; i++) {
  85             mem.swap(i - 1, i);
  86             int tmp = arr[i - 1];
  87             arr[i - 1] = arr[i];
  88             arr[i] = tmp;
  89         }
  90 
  91         for (int i = 0; i < SIZE; i++) {
  92             if (arr[i] != mem.getInt(i)) {
  93                 throw new IllegalStateException("Values mismatch after swaps");
  94             }
  95         }
  96     }
  97 
  98     static class Memory {
  99         private final long addr;
 100 
 101         Memory(int size) {
 102             addr = UNSAFE.allocateMemory(size);
 103         }
 104 
 105         public int getInt(int idx) {
 106             return UNSAFE.getInt(addr + idx*SCALE);
 107         }
 108 
 109         public void setInt(int idx, int val) {
 110             UNSAFE.putInt(addr + idx*SCALE, val);
 111         }
 112 
 113         public void swap(int a, int b) {
 114             int tmp = getInt(a);
 115             setInt(a, getInt(b));
 116             setInt(b, tmp);
 117         }
 118     }
 119 }