8046516: Segmentation fault in JVM (easily reproducible)

Thu, 12 Jun 2014 10:15:43 -0700

author
kvn
date
Thu, 12 Jun 2014 10:15:43 -0700
changeset 6728
4077c61b03a0
parent 6727
22146594cd5a
child 6729
3e1cec358ab9

8046516: Segmentation fault in JVM (easily reproducible)
Summary: Place new nodes on the previous loop exit in reorg_offsets().
Reviewed-by: roland

src/share/vm/opto/loopopts.cpp file | annotate | diff | comparison | revisions
test/compiler/loopopts/TestLogSum.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/vm/opto/loopopts.cpp	Thu Jun 12 15:57:16 2014 -0700
     1.2 +++ b/src/share/vm/opto/loopopts.cpp	Thu Jun 12 10:15:43 2014 -0700
     1.3 @@ -2769,11 +2769,11 @@
     1.4        // Hit!  Refactor use to use the post-incremented tripcounter.
     1.5        // Compute a post-increment tripcounter.
     1.6        Node *opaq = new (C) Opaque2Node( C, cle->incr() );
     1.7 -      register_new_node( opaq, u_ctrl );
     1.8 +      register_new_node(opaq, exit);
     1.9        Node *neg_stride = _igvn.intcon(-cle->stride_con());
    1.10        set_ctrl(neg_stride, C->root());
    1.11        Node *post = new (C) AddINode( opaq, neg_stride);
    1.12 -      register_new_node( post, u_ctrl );
    1.13 +      register_new_node(post, exit);
    1.14        _igvn.rehash_node_delayed(use);
    1.15        for (uint j = 1; j < use->req(); j++) {
    1.16          if (use->in(j) == phi)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/compiler/loopopts/TestLogSum.java	Thu Jun 12 10:15:43 2014 -0700
     2.3 @@ -0,0 +1,111 @@
     2.4 +/*
     2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8046516
    2.30 + * @summary Segmentation fault in JVM (easily reproducible)
    2.31 + * @run main/othervm -XX:-TieredCompilation -Xbatch TestLogSum
    2.32 + * @author jackkamm@gmail.com
    2.33 + */
    2.34 +
    2.35 +import java.util.Arrays;
    2.36 +import java.util.HashMap;
    2.37 +import java.util.List;
    2.38 +import java.util.Map;
    2.39 +public class TestLogSum {
    2.40 +  public static void main(String[] args) {
    2.41 +    double sum;
    2.42 +
    2.43 +    for (int i = 0; i < 6; i++) {
    2.44 +        for (int n = 2; n < 30; n++) {
    2.45 +           for (int j = 1; j <= n; j++) {
    2.46 +              for (int k = 1; k <= j; k++) {
    2.47 +                // System.out.println(computeSum(k, j));
    2.48 +                sum = computeSum(k, j);
    2.49 +              }
    2.50 +           }
    2.51 +        }
    2.52 +      }
    2.53 +   }
    2.54 +
    2.55 +   private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>();
    2.56 +   public static double computeSum(int x, int y) {
    2.57 +      List<Integer> key = Arrays.asList(new Integer[] {x, y});
    2.58 +
    2.59 +      if (!cache.containsKey(key)) {
    2.60 +
    2.61 +        // explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error
    2.62 +        LogSumArray toReturn = new LogSumArray(x);
    2.63 +
    2.64 +        // changing loop indices will prevent the error
    2.65 +        // in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error
    2.66 +        for (int z = 1; z < x+1; z++) {
    2.67 +           double logSummand = Math.log(z + x + y);
    2.68 +           toReturn.addLogSummand(logSummand);
    2.69 +        }
    2.70 +
    2.71 +        // returning the value here without cacheing it will prevent the segfault
    2.72 +        cache.put(key, toReturn.retrieveLogSum());
    2.73 +      }
    2.74 +      return cache.get(key);
    2.75 +   }
    2.76 +
    2.77 +   /*
    2.78 +    * Given a bunch of logarithms log(X),log(Y),log(Z),...
    2.79 +    * This class is used to compute the log of the sum, log(X+Y+Z+...)
    2.80 +    */
    2.81 +   private static class LogSumArray {
    2.82 +      private double[] logSummandArray;
    2.83 +      private int currSize;
    2.84 +
    2.85 +      private double maxLogSummand;
    2.86 +
    2.87 +      public LogSumArray(int maxEntries) {
    2.88 +        this.logSummandArray = new double[maxEntries];
    2.89 +
    2.90 +        this.currSize = 0;
    2.91 +        this.maxLogSummand = Double.NEGATIVE_INFINITY;
    2.92 +      }
    2.93 +
    2.94 +      public void addLogSummand(double logSummand) {
    2.95 +        logSummandArray[currSize] = logSummand;
    2.96 +        currSize++;
    2.97 +        // removing this line will prevent the error
    2.98 +        maxLogSummand = Math.max(maxLogSummand, logSummand);
    2.99 +      }
   2.100 +
   2.101 +      public double retrieveLogSum() {
   2.102 +        if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY;
   2.103 +
   2.104 +        assert currSize <= logSummandArray.length;
   2.105 +
   2.106 +        double factorSum = 0;
   2.107 +        for (int i = 0; i < currSize; i++) {
   2.108 +           factorSum += Math.exp(logSummandArray[i] - maxLogSummand);
   2.109 +        }
   2.110 +
   2.111 +        return Math.log(factorSum) + maxLogSummand;
   2.112 +      }
   2.113 +   }
   2.114 +}

mercurial