1 /*
   2  * Copyright (c) 2019 SAP SE. 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
  26  * @bug 8221083
  27  * @requires vm.gc.Serial
  28  * @requires vm.bits == 64 & vm.opt.final.UseCompressedOops == true
  29  * @summary On ppc64, C1 erroneously emits a 32-bit compare instruction for oop compares.
  30  * @modules java.base/jdk.internal.misc:+open
  31  * @library /test/lib /
  32  * @build sun.hotspot.WhiteBox
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  * @run main/othervm -Xbatch -XX:-UseTLAB -Xmx4m -XX:+UseSerialGC -XX:HeapBaseMinAddress=0x700000000
  35  *      -XX:CompileCommand=compileonly,compiler.codegen.TestOopCmp::nullTest
  36  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  37  *      compiler.codegen.TestOopCmp
  38  * @author volker.simonis@gmail.com
  39  */
  40 
  41 package compiler.codegen;
  42 
  43 import sun.hotspot.WhiteBox;
  44 
  45 public class TestOopCmp {
  46 
  47     private static Object nullObj = null;
  48 
  49     public static boolean nullTest(Object o) {
  50         if (o == nullObj) {
  51             return true;
  52         } else {
  53             return false;
  54         }
  55     }
  56 
  57     public static void main(String args[]) {
  58 
  59         WhiteBox WB = WhiteBox.getWhiteBox();
  60 
  61         // The test is started with -XX:HeapBaseMinAddress=0x700000000 and a
  62         // small heap of only 4mb. This works pretty reliable and at least on
  63         // Linux/Windows we will get a heap starting at 0x700000000.
  64         // The test also runs with -XX:+UseSerialGC which means that we'll get
  65         // eden starting at 0x700000000.
  66         // Calling 'System.gc()' will clean up all the objects from eden, so if
  67         // eden starts at 0x700000000 the first allocation right after the
  68         // system GC will be allcoated right at address 0x700000000.
  69         System.gc();
  70         String s = new String("I'm not null!!!");
  71         if (WB.getObjectAddress(s) == 0x700000000L) {
  72             System.out.println("Got object at address 0x700000000");
  73         }
  74 
  75         // We call 'nullTest()' with the newly allocated String object. If it was
  76         // allocated at 0x700000000, its 32 least-significant bits will be 0 and a
  77         // 32-bit comparison with 'nullObj' (which is 'null') will yield true and
  78         // result in a test failure.
  79         // If the code generated for 'nullTest()' correctly performs a 64-bit
  80         // comparison or if we didn't manage to allcoate 's' at 0x700000000 the
  81         // test will always succeed.
  82         for (int i = 0; i < 30_000; i++) {
  83             if (nullTest(s)) {
  84                 throw new RuntimeException("Comparing non-null object with null returned 'true'");
  85             }
  86         }
  87     }
  88 }