1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. 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 /*
  26  * @test
  27  * @bug 8143930
  28  * @summary C1 LinearScan asserts when compiling two back-to-back CompareAndSwapLongs
  29  * @modules java.base/jdk.internal.misc
  30  *
  31  * @run testng/othervm -Diters=200000 -XX:TieredStopAtLevel=1
  32  *      compiler.intrinsics.unsafe.UnsafeTwoCASLong
  33  */
  34 
  35 package compiler.intrinsics.unsafe;
  36 
  37 import org.testng.annotations.Test;
  38 
  39 import java.lang.reflect.Field;
  40 
  41 import static org.testng.Assert.*;
  42 
  43 public class UnsafeTwoCASLong {
  44     static final int ITERS = Integer.getInteger("iters", 1);
  45     static final jdk.internal.misc.Unsafe UNSAFE;
  46     static final long V_OFFSET;
  47 
  48     static {
  49         try {
  50             Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  51             f.setAccessible(true);
  52             UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
  53         } catch (Exception e) {
  54             throw new RuntimeException("Unable to get Unsafe instance.", e);
  55         }
  56 
  57         try {
  58             Field vField = UnsafeTwoCASLong.class.getDeclaredField("v");
  59             V_OFFSET = UNSAFE.objectFieldOffset(vField);
  60         } catch (Exception e) {
  61             throw new RuntimeException(e);
  62         }
  63     }
  64 
  65     long v;
  66 
  67     @Test
  68     public void testFieldInstance() {
  69         UnsafeTwoCASLong t = new UnsafeTwoCASLong();
  70         for (int c = 0; c < ITERS; c++) {
  71             testAccess(t, V_OFFSET);
  72         }
  73     }
  74 
  75     static void testAccess(Object base, long offset) {
  76         UNSAFE.compareAndSwapLong(base, offset, 1L, 2L);
  77         UNSAFE.compareAndSwapLong(base, offset, 2L, 1L);
  78     }
  79 
  80 }
  81