1/*
2 * Copyright (c) 2009, 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 * @test
26 * @bug 6851282
27 * @summary JIT miscompilation results in null entry in array when using CompressedOops
28 *
29 * @run main/othervm/timeout=600 -Xmx256m -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops
30 *    compiler.c2.Test6851282
31 */
32
33package compiler.c2;
34
35import java.util.ArrayList;
36import java.util.List;
37
38public class Test6851282 {
39  void foo(A a, A[] as) {
40    for (A a1 : as) {
41      B[] filtered = a.c(a1);
42      for (B b : filtered) {
43        if (b == null) {
44          System.out.println("bug: b == null");
45          System.exit(97);
46        }
47      }
48    }
49  }
50
51  public static void main(String[] args) {
52    List<A> as = new ArrayList<A>();
53    for (int i = 0; i < 5000; i++) {
54      List<B> bs = new ArrayList<B>();
55      for (int j = i; j < i + 1000; j++)
56        bs.add(new B(j));
57      as.add(new A(bs.toArray(new B[0])));
58    }
59    new Test6851282().foo(as.get(0), as.subList(1, as.size()).toArray(new A[0]));
60  }
61
62  static class A {
63    final B[] bs;
64
65    public A(B[] bs) {
66      this.bs = bs;
67    }
68
69    final B[] c(final A a) {
70      return new BoxedArray<B>(bs).filter(new Function<B, Boolean>() {
71        public Boolean apply(B arg) {
72          for (B b : a.bs) {
73            if (b.d == arg.d)
74              return true;
75          }
76          return false;
77        }
78      });
79    }
80  }
81
82  static class BoxedArray<T> {
83
84    private final T[] array;
85
86    BoxedArray(T[] array) {
87      this.array = array;
88    }
89
90    public T[] filter(Function<T, Boolean> function) {
91      boolean[] include = new boolean[array.length];
92      int len = 0;
93      int i = 0;
94      while (i < array.length) {
95        if (function.apply(array[i])) {
96          include[i] = true;
97          len += 1;
98        }
99        i += 1;
100      }
101      T[] result = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), len);
102      len = 0;
103      i = 0;
104      while (len < result.length) {
105        if (include[i]) {
106          result[len] = array[i];
107          len += 1;
108        }
109        i += 1;
110      }
111      return result;
112    }
113  }
114
115  static interface Function<T, R> {
116    R apply(T arg);
117  }
118
119  static class B {
120    final int d;
121
122    public B(int d) {
123      this.d = d;
124    }
125  }
126}
127
128