1 /*
   2  * @test
   3  * @summary moving predicate out of loops may cause array accesses to bypass null check and caused crash
   4  * @run main/othervm -Xbatch -XX:-TieredCompilation -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:CompileCommand=compileonly,TestBypassNullCheck::getController  TestBypassNullCheck
   5  */
   6 public class TestBypassNullCheck {
   7    Config initConfig;
   8    public TestBypassNullCheck() {
   9        initConfig = new Config(1234, "test");
  10    }
  11    public TestBypassNullCheck getController() {
  12        String customGroupName = initConfig.userName == null ? "" : initConfig.userName;
  13        customGroupName = customGroupName + ":" + initConfig.dataId;
  14        State.setCustomName(customGroupName);
  15        return this;
  16    }
  17 
  18    public static void main(String [] args) {
  19        System.out.println("test"+":"+args.length+State.getCustomName());
  20        TestBypassNullCheck t = new TestBypassNullCheck();
  21        for (int i=0;i<10000;i++) {
  22            t.getController();
  23        }
  24        t.initConfig = new Config(1234, null);
  25        t.getController();
  26    }
  27 }
  28 
  29 class Config {
  30     String dataId;
  31     String userName;
  32     public Config(long id, String name) { dataId = ""+id; userName = name; }
  33 }
  34 
  35 class State {
  36     static String customerName="test";
  37     public static void setCustomName(String name) {
  38         customerName = name;
  39     }
  40 
  41     public static String getCustomName() { return customerName; }
  42 }