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